-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.py
200 lines (185 loc) · 8.31 KB
/
report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""
Copyright Russell Ferriday 2010
First release May 2010
This file is part of ATTUsageAnalysis.
ATTUsageAnalysis is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ATTUsageAnalysis is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ATTUsageAnalysis. If not, see <http://www.gnu.org/licenses/>.
Doctests
--------
Now make a usage object, and pass it to a new Report. Handing in a directory
helps to interpret the traffic report.
>>> from attusage import AttUsage
>>> usage = AttUsage()
>>> usage.process('testreport.csv')
>>> directory = Directory('testdir.dir')
>>> report = Report(usage, directory=directory)
>>> print report
<Report>
>>> for line in report.text():
... print line
Directory:
508-235-6915 Roger Smith
508-235-7829 Jane Smith
<BLANKLINE>
Summary By User:
Starting 2010-03-17 10:07:00, Ending 2010-04-03 17:48:00
<BLANKLINE>
Name: JANE SMITH, Number: 508-235-7829
Calls In: 0, Out:10
Texts In: 7, Out:3
: Calls | Texts | Text Sessions
: From To | From To | In Out
508-235-6915 Roger Smith : 6 | 4 2 | 4 1
508-704-1151 - : | 3 1 | 1 1
508-748-9880 - : 2 | |
508-235-7829 Jane Smith : 1 | |
508-534-0399 - : 1 | |
--------------------------------------------------------------------------------
Name: ROGER SMITH, Number: 508-235-6915
Calls In: 6, Out:4
Texts In: 4, Out:10
: Calls | Texts | Text Sessions
: From To | From To | In Out
508-704-0185 - : | 1 3 | 1
Data Transfer - : | 3 | 2
508-704-1151 - : | 3 | 3
508-235-7829 Jane Smith : 2 | 1 1 | 1 1
48368 - : | 2 | 2
508-544-3223 - : 1 | |
508-546-3100 - : 1 | |
480-786-7200 - : 1 | |
508-927-5208 - : 2 | |
508-550-6500 - : 1 | |
508-235-6915 Roger Smith : 1 | |
949-218-9820 - : 1 | |
--------------------------------------------------------------------------------
"""
import datetime
EXCHANGE_DURATION = datetime.timedelta(minutes=10)
def fmt(value):
return value or ''
class Directory(dict):
def __init__(self, filename):
super(Directory, self).__init__()
self.filename = filename
if not filename:
return
with open(self.filename) as table:
for line in iter(table):
line = line.split()
if len(line) > 1:
number, name = line[0], ' '.join(line[1:])
self[number] = name
def __repr__(self):
return '<Directory> entries: %s' % len(self)
class Report(object):
def __init__(self,usage, directory=None):
self.directory = directory or {}
self.usage=usage
self.lines = []
def __repr__(self):
return '<Report>'
def p(self,line):
"Print a line to the print buffer"
self.lines.append(line)
def put_directory(self):
"Puts the directory into the print buffer"
self.p('Directory:')
for number, id in sorted(self.directory.items(), key=lambda i: i[0]):
self.p('%12s %s' % (number, id))
def do_summary(self):
"Puts a summary to print buffer"
self.p('Summary By User:')
self.put_timespan()
self.p('')
for user in self.usage.users.values():
incalls, outcalls, intexts, outtexts = 0,0,0,0
parties = {}
self.p('Name: %s, Number: %s' % (user.name, user.number))
for call in user.calls:
party = parties.setdefault(call.number, \
{'fromcalls':0, 'tocalls':0, \
'fromtexts':0, 'totexts':0,\
'lastexchangestarted':datetime.datetime(2000,1,1),
'textexchangesrc': 0,
'textexchangedst': 0
})
if call.is_incoming():
incalls+=1
party['fromcalls'] += 1
else:
outcalls+=1
party['tocalls'] += 1
for text in user.texts:
party = parties.setdefault(text.number, \
{'fromcalls':0, 'tocalls':0, \
'fromtexts':0, 'totexts':0,\
'lastexchangestarted':datetime.datetime(2000,1,1),
'textexchangesrc': 0,
'textexchangedst': 0
})
if text.is_incoming():
intexts+=1
party['fromtexts'] += 1
else:
outtexts+=1
party['totexts'] += 1
if text.time > party['lastexchangestarted'] + EXCHANGE_DURATION:
party['lastexchangestarted'] = text.time
if text.incoming :
party['textexchangesrc'] += 1
else:
party['textexchangedst'] += 1
self.p('Calls In: %s, Out:%s' % (incalls, outcalls))
self.p('Texts In: %s, Out:%s' % (intexts, outtexts))
self.p(' : Calls |'\
' Texts |'\
' Text Sessions')
self.p(' : From To |'\
' From To |'\
' In Out')
for number, party in sorted(parties.items(), key=lambda i: -(i[1]['fromtexts'] + i[1]['totexts'])):
id = self.directory.get(number, '-')
self.p('%13s %11s : %4s %4s |'\
' %4s %4s |'\
' %4s %4s' % \
(number, id, \
fmt(party['fromcalls']), fmt(party['tocalls']),\
fmt(party['fromtexts']), fmt(party['totexts']),\
fmt(party['textexchangesrc']), fmt(party['textexchangedst'])\
))
self.p('-' * 80)
def put_timespan(self):
"Finds earliest and latest calls in the report, puts to print buffer"
earliest = datetime.datetime.now()
latest = datetime.datetime(2000,1,1)
for user in self.usage.users.values():
for call in user.calls:
if call.time < earliest:
earliest = call.time
elif call.time > latest:
latest = call.time
for text in user.texts:
if text.time < earliest:
earliest = text.time
elif text.time > latest:
latest = text.time
self.p('Starting %s, Ending %s' % (earliest, latest))
def text(self):
"generate a text report and return as a sequence of lines"
self.put_directory();
self.p('')
self.do_summary()
return self.lines
if __name__ == "__main__":
import doctest
doctest.testmod()