Statistics
| Branch: | Revision:

peerstreamer-logs-analyzer / py_logs_splitter.py @ master

History | View | Annotate | Download (3.04 KB)

1 1a481d7b Luca Baldesi
#!/usr/bin/python
2
import getopt,os,sys
3 2874fe64 Luca Baldesi
from py_logs_analizer import *
4
from py_logs_visualizer import *
5 1a481d7b Luca Baldesi
sys.path.insert(0,'lib')
6
from utilities import *
7
from peerstreamer_logs import *
8
import shutil as sh
9
10 a60088f6 Luca Baldesi
SESSION_TOLERANCE=20
11 2874fe64 Luca Baldesi
SESSION_INDEPENDENT_KEYWORDS=['usage','TestResults','shortest','rtt','timespread','NetLoad','PSTestNET']
12 1a481d7b Luca Baldesi
13
def name_splitter(folder):
14 a60088f6 Luca Baldesi
# returns a dictionary of type: [timestamp] -> group_name
15 1a481d7b Luca Baldesi
        session_groups = {}
16
        for logfile in os.listdir(folder):
17
                if is_source_from_filename(logfile):
18
                        source_info = logfile.split('.')[0].split('_')
19 c0eac366 Luca Baldesi
                        if len(source_info) > 2:
20
                                session_groups[time_from_filename(logfile)] = source_info[2]
21 1a481d7b Luca Baldesi
                        else:
22
                                session_groups[time_from_filename(logfile)] = 'standard'
23
        return session_groups
24
25 a60088f6 Luca Baldesi
def get_group(logfile,session_groups,time):
26
        # returns the string group name for logfile
27 1a481d7b Luca Baldesi
        group = None
28
        for keytime in session_groups.keys():
29
#                print "comparing  " + str(time) + " with " + str(int(keytime))
30
                if (int(time) < (int(keytime) + SESSION_TOLERANCE)) and \
31
                                (int(time) > (int(keytime) - SESSION_TOLERANCE)):
32
                        group = session_groups[keytime]
33 a60088f6 Luca Baldesi
34 c0eac366 Luca Baldesi
        if (logfile.split('.')[0].split('_')[2]) in session_groups.values():
35
                group = logfile.split('.')[0].split('_')[2]
36 a60088f6 Luca Baldesi
        else:
37
                group = None
38 1a481d7b Luca Baldesi
        return group
39
40
def session_independent(logfile):
41
        for elm in SESSION_INDEPENDENT_KEYWORDS:
42
                if elm in logfile:
43
                        return True
44
        return False
45
46
def multiple_dst_copy(folder,logfile,dsts):
47
        for dst in dsts:
48 a60088f6 Luca Baldesi
                if not os.path.exists(dst+'/'+logfile):
49
                        os.symlink(folder+'/'+logfile,dst+'/'+logfile)
50 1a481d7b Luca Baldesi
51
def splitter(folder):
52 a60088f6 Luca Baldesi
        # get the dictionary of time and groups names
53 1a481d7b Luca Baldesi
        session_groups = name_splitter(folder)
54 a60088f6 Luca Baldesi
55
        # prepare the folders
56 1a481d7b Luca Baldesi
        for name in set(session_groups.values()):
57
                if not os.path.exists(folder+'/'+name):
58
                        os.makedirs(folder+'/'+name)
59
60 a60088f6 Luca Baldesi
        # symlinks logfiles in the correct folders
61 1a481d7b Luca Baldesi
        for logfile in os.listdir(folder):
62
                if os.path.isfile(folder+"/"+logfile):
63 2874fe64 Luca Baldesi
                        logtime = time_from_filename(logfile)
64 a60088f6 Luca Baldesi
65 1a481d7b Luca Baldesi
                        if session_independent(logfile):
66
                                multiple_dst_copy(folder,logfile,[folder +'/'+ v for v in set(session_groups.values())])
67
                        else:
68 a60088f6 Luca Baldesi
                                group = get_group(logfile,session_groups,logtime)
69
                                if group is not None:
70 1a481d7b Luca Baldesi
                                        newfolder = folder+'/'+group+'/'
71 a60088f6 Luca Baldesi
                                        if not os.path.exists(newfolder+logfile):
72
                                                os.symlink(folder+'/'+logfile,newfolder+logfile)
73
                                else:
74
                                        print "[ERROR] group not found for "+logfile
75 1a481d7b Luca Baldesi
76 a60088f6 Luca Baldesi
        return set(session_groups.values())
77
78
def splitter_main(argv):
79
        images = False
80 1a481d7b Luca Baldesi
        try:
81 a60088f6 Luca Baldesi
                opts,args = getopt.getopt(argv,"hif:",["help","images","folder"])
82 1a481d7b Luca Baldesi
        except getopt.GetoptError:
83
                sys.exit(2)
84
        for opt,arg in opts:
85
                if opt in ("-h","--help"):
86
                        sys.exit()
87
                elif opt in ("-f","--folder"):
88 a60088f6 Luca Baldesi
                        folder = os.path.abspath(arg)
89
                elif opt in ("-i","--images"):
90
                        images = True
91
92 1a481d7b Luca Baldesi
        try:
93
                folder
94
        except NameError:
95
                print "please provide a folder path"
96
                sys.exit()
97
        print "folder is " + folder
98 a60088f6 Luca Baldesi
        groups = splitter(folder)
99 2874fe64 Luca Baldesi
        print "groups found:" + str(groups)
100 a60088f6 Luca Baldesi
        for group in groups:
101
                analyze_main(['-f',folder+'/'+group])
102
                if images:
103
                        visual_main(['-s','-f',folder+'/'+group])
104 1a481d7b Luca Baldesi
105
106
if __name__ == "__main__":
107 2874fe64 Luca Baldesi
        splitter_main(sys.argv[1:])