Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.14 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
        def getPeer(self,hostname):
34
                for session in self.sessions:
35
                                peer = session.getPeer(hostname)
36
                                if peer:
37
                                                return peer
38
                return None
39

    
40
class Session:
41
        def __init__(self,time):
42
                self.time=int(time)
43
                self.peers = []
44

    
45
        def addPeer(self,peer):
46
                self.peers.append(peer)
47

    
48
        def getSource(self):
49
                for peer in self.peers:
50
                        if peer.is_source:
51
                                return peer
52
                return None
53

    
54
        def getPeer(self,hostname):
55
                for peer in self.peers:
56
                        if peer.hostname == hostname:
57
                                return peer
58
                return None
59

    
60
        def addr2hostname(self,addr,without_port=False):
61
                hostname = None
62
                for peer in self.peers :
63
                        if without_port and peer.address.split(':')[0] == addr:
64
                                hostname = peer.hostname
65
                        if peer.address == addr:
66
                                hostname = peer.hostname
67
                        if hostname:
68
                                break # I know it is horrible... stupid python!
69
                return hostname
70

    
71
class Peer:
72
        def __init__(self,hostname,source,dataframe):
73
                self.hostname = hostname
74
                self.is_source = source
75
                self.published = dataframe
76
                self.neighbours = None
77
                if len(dataframe) >0:
78
                        received = dataframe[dataframe['action'] == 'RECEIVED']
79
                        sent = dataframe[dataframe['action'] == 'SENT']
80
                        if len(received) > 0:
81
                                i = received.first_valid_index()
82
                                self.address = ''.join(received['receiver'][i])
83
                        else:
84
                                if len(sent) > 0:
85
                                        i = sent.first_valid_index()
86
                                        self.address = ''.join(sent['sender'][i])
87
                                else:
88
                                        self.address = 'unknown'
89
                self.address = 'unknown'
90

    
91
        def setNeighbours(self,neighs):
92
                self.neighbours = neighs
93
                if self.address == 'unknown':
94
                        i = self.neighbours.first_valid_index()
95
                        self.address = ''.join(self.neighbours['logger'][i])
96

    
97
        def published_interval(self,begin_time,end_time):
98
                return self.published[(self.published['logTime'] > begin_time) & \
99
                                                                                       (self.published['logTime'] < end_time)]
100

    
101
        def published_interval_sec(self,begin_time,end_time):
102
                return self.published[(self.published['logTime'] > (begin_time*1000000)) & \
103
                                                                                       (self.published['logTime'] < (end_time*1000000))]
104

    
105
        def neighbours_interval_sec(self,begin_time,end_time):
106
                if self.neighbours:
107
                        return self.neighbours[(self.neighbours['logTime'] > (begin_time*1000000)) & \
108
                                (self.neighbours['logTime'] < (end_time*1000000))]
109
                else:
110
                        print "[WARNING] peer "+str(self.hostname)+" has not neigh data"
111
                        return None