Statistics
| Branch: | Revision:

peerstreamer-logs-analyzer / lib / experiment.py @ a60088f6

History | View | Annotate | Download (2.36 KB)

1
class Experiment:
2
        TIME_SENSIBILITY = 30
3

    
4
        def __init__(self):
5
                self.sessions = []
6

    
7
        def removeSessionByTime(self,session_time):
8
                for session in self.sessions:
9
                        if session.time < (session_time + 5) and session.time > (session_time-5):
10
                                self.sessions.remove(session)
11

    
12
        def getSession(self,time,creation=False):
13
                for session in self.sessions:
14
                        if ((session.time < (int(time)+Experiment.TIME_SENSIBILITY)) 
15
                        and (session.time > (int(time)-Experiment.TIME_SENSIBILITY))):
16
                                return session
17
                if creation:
18
                        session = Session(time)
19
                        self.sessions.append(session)
20
                        return session
21
                else:
22
                        return None
23

    
24
        def addr2hostname(self,addr,without_port=False):
25
                hostname = None        
26
                for session in self.sessions:
27
                        hostname = session.addr2hostname(addr.strip(),without_port)
28
                        if hostname:
29
                                break # I know it is horrible... stupid python!
30
                #print "hostname for addr "+addr+": "+str(hostname)
31
                return hostname
32

    
33
class Session:
34
        def __init__(self,time):
35
                self.time=int(time)
36
                self.peers = []
37

    
38
        def addPeer(self,peer):
39
                self.peers.append(peer)
40

    
41
        def getSource(self):
42
                for peer in self.peers:
43
                        if peer.is_source:
44
                                return peer
45
                return None
46

    
47
        def getPeer(self,hostname):
48
                for peer in self.peers:
49
                        if peer.hostname == hostname:
50
                                return peer
51
                return None
52

    
53
        def addr2hostname(self,addr,without_port=False):
54
                hostname = None
55
                for peer in self.peers :
56
                        if without_port and peer.address.split(':')[0] == addr:
57
                                hostname = peer.hostname
58
                        if peer.address == addr:
59
                                hostname = peer.hostname
60
                        if hostname:
61
                                break # I know it is horrible... stupid python!
62
                return hostname
63

    
64
class Peer:
65
        def __init__(self,hostname,source,dataframe):
66
                self.hostname = hostname
67
                self.is_source = source
68
                self.published = dataframe
69
                received = dataframe[dataframe['action'] == 'RECEIVED']
70
                sent = dataframe[dataframe['action'] == 'SENT']
71
                if len(received) > 0:
72
                        self.address = ''.join(received['receiver'][0])
73
                else:
74
                        if len(sent) > 0:
75
                                self.address = ''.join(sent['sender'][0])
76
                        else:
77
                                self.address = 'unkwnon'
78

    
79
        def published_interval(self,begin_time,end_time):
80
                return self.published[(self.published['logTime'] > begin_time) & \
81
                                                                                       (self.published['logTime'] < end_time)]
82

    
83
        def published_interval_sec(self,begin_time,end_time):
84
                return self.published[(self.published['logTime'] > (begin_time*1000000)) & \
85
                                                                                       (self.published['logTime'] < (end_time*1000000))]
86