Statistics
| Branch: | Revision:

peerstreamer-logs-analyzer / lib / experiment.py @ 1a481d7b

History | View | Annotate | Download (2.13 KB)

1
class Experiment:
2
        TIME_SENSIBILITY = 300
3

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

    
7
        def getSession(self,time):
8
                for session in self.sessions:
9
                        if ((session.time < (int(time)+Experiment.TIME_SENSIBILITY)) 
10
                        and (session.time > (int(time)-Experiment.TIME_SENSIBILITY))):
11
                                return session
12
                session = Session(time)
13
                self.sessions.append(session)
14
                return session
15

    
16
        def addr2hostname(self,addr,without_port=False):
17
                hostname = None        
18
                for session in self.sessions:
19
                        hostname = session.addr2hostname(addr.strip(),without_port)
20
                        if hostname:
21
                                break # I know it is horrible... stupid python!
22
                #print "hostname for addr "+addr+": "+str(hostname)
23
                return hostname
24

    
25
class Session:
26
        def __init__(self,time):
27
                self.time=int(time)
28
                self.peers = []
29

    
30
        def addPeer(self,peer):
31
                self.peers.append(peer)
32

    
33
        def getSource(self):
34
                for peer in self.peers:
35
                        if peer.is_source:
36
                                return peer
37
                return None
38

    
39
        def getPeer(self,hostname):
40
                for peer in self.peers:
41
                        if peer.hostname == hostname:
42
                                return peer
43
                return None
44

    
45
        def addr2hostname(self,addr,without_port=False):
46
                hostname = None
47
                for peer in self.peers :
48
                        if without_port and peer.address.split(':')[0] == addr:
49
                                hostname = peer.hostname
50
                        if peer.address == addr:
51
                                hostname = peer.hostname
52
                        if hostname:
53
                                break # I know it is horrible... stupid python!
54
                return hostname
55

    
56
class Peer:
57
        def __init__(self,hostname,source,dataframe):
58
                self.hostname = hostname
59
                self.is_source = source
60
                self.published = dataframe
61
                received = dataframe[dataframe['action'] == 'RECEIVED']
62
                sent = dataframe[dataframe['action'] == 'SENT']
63
                if len(received) > 0:
64
                        self.address = ''.join(received['receiver'][0])
65
                else:
66
                        if len(sent) > 0:
67
                                self.address = ''.join(sent['sender'][0])
68
                        else:
69
                                self.address = 'unkwnon'
70

    
71
        def published_interval(self,begin_time,end_time):
72
                return self.published[(self.published['logTime'] > begin_time) & \
73
                                                                                       (self.published['logTime'] < end_time)]
74

    
75
        def published_interval_sec(self,begin_time,end_time):
76
                return self.published[(self.published['logTime'] > (begin_time*1000000)) & \
77
                                                                                       (self.published['logTime'] < (end_time*1000000))]
78