Statistics
| Branch: | Revision:

peerstreamer-logs-analyzer / py_logs_splitter.py @ a60088f6

History | View | Annotate | Download (3 KB)

1
#!/usr/bin/python
2
import getopt,os,sys
3
from py_logs_analizer import * 
4
from py_logs_visualizer import * 
5
sys.path.insert(0,'lib')
6
from utilities import *
7
from peerstreamer_logs import *
8
import shutil as sh
9

    
10
SESSION_TOLERANCE=20
11
SESSION_INDEPENDENT_KEYWORDS=['rtt','timespread']
12

    
13
def name_splitter(folder):
14
# returns a dictionary of type: [timestamp] -> group_name
15
        session_groups = {}
16
        for logfile in os.listdir(folder):
17
                if is_source_from_filename(logfile):
18
                        source_info = logfile.split('.')[0].split('_')
19
                        if len(source_info) > 4:
20
                                session_groups[time_from_filename(logfile)] = source_info[3]
21
                        else:
22
                                session_groups[time_from_filename(logfile)] = 'standard'
23
        return session_groups
24

    
25
def get_group(logfile,session_groups,time):
26
        # returns the string group name for logfile
27
        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

    
34
        if (logfile.split('.')[0].split('_')[3]) in session_groups.values():
35
                group = logfile.split('.')[0].split('_')[3]
36
        else:
37
                group = None
38
        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
                if not os.path.exists(dst+'/'+logfile):
49
                        os.symlink(folder+'/'+logfile,dst+'/'+logfile)
50

    
51
def splitter(folder):
52
        # get the dictionary of time and groups names
53
        session_groups = name_splitter(folder)
54

    
55
        # prepare the folders
56
        for name in set(session_groups.values()):
57
                if not os.path.exists(folder+'/'+name):
58
                        os.makedirs(folder+'/'+name)
59

    
60
        # symlinks logfiles in the correct folders
61
        for logfile in os.listdir(folder):
62
                if os.path.isfile(folder+"/"+logfile):
63
                        logtime = time_from_filename(logfile) 
64

    
65

    
66
                        if session_independent(logfile):
67
                                multiple_dst_copy(folder,logfile,[folder +'/'+ v for v in set(session_groups.values())])
68
                        else:
69
                                group = get_group(logfile,session_groups,logtime)
70
                                if group is not None:
71
                                        newfolder = folder+'/'+group+'/'
72
                                        if not os.path.exists(newfolder+logfile):
73
                                                os.symlink(folder+'/'+logfile,newfolder+logfile)
74
                                else:
75
                                        print "[ERROR] group not found for "+logfile
76

    
77
        return set(session_groups.values())
78

    
79
def splitter_main(argv):
80
        images = False
81
        try:
82
                opts,args = getopt.getopt(argv,"hif:",["help","images","folder"])
83
        except getopt.GetoptError:
84
                sys.exit(2)
85
        for opt,arg in opts:
86
                if opt in ("-h","--help"):
87
                        sys.exit()
88
                elif opt in ("-f","--folder"):
89
                        folder = os.path.abspath(arg)
90
                elif opt in ("-i","--images"):
91
                        images = True
92

    
93
        try:
94
                folder
95
        except NameError:
96
                print "please provide a folder path"
97
                sys.exit()
98
        print "folder is " + folder
99
        groups = splitter(folder)
100
        print "groups found:" + str(groups) 
101
        for group in groups:
102
                analyze_main(['-f',folder+'/'+group])
103
                if images:
104
                        visual_main(['-s','-f',folder+'/'+group])
105

    
106

    
107
if __name__ == "__main__":
108
        splitter_main(sys.argv[1:])