-
Notifications
You must be signed in to change notification settings - Fork 6
/
graph_presence.py
executable file
·333 lines (269 loc) · 10.8 KB
/
graph_presence.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/python
from os.path import expanduser
from utilities import *
import logging
from datetime import datetime, timedelta
from absl import flags
from absl import app
import matplotlib
import matplotlib.pyplot as plt
plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
from operator import itemgetter
from matplotlib.dates import DateFormatter
from matplotlib.dates import MinuteLocator
FLAGS = flags.FLAGS
flags.DEFINE_string('usertype', "id", 'Type of name on y axis.')
flags.DEFINE_string('timeafter', None, 'Starttime for the graph.', short_name='a')
flags.DEFINE_string('timebefore', None, 'Endtime for the graph.', short_name='b')
flags.DEFINE_boolean('skip_graph', False, 'Whether to avoid graph pop up.',
short_name='s')
flags.DEFINE_integer('ignore_difference_sec', -1, 'Time difference to keep '
'online.', short_name='i')
flags.DEFINE_boolean('sum', False, 'Print total time online.')
flags.DEFINE_integer('graph_type', 1, 'Graph type to show.', short_name='g')
flags.DEFINE_integer('duration', 60, 'Duration for graph type 3.', short_name='D')
flags.DEFINE_boolean('offline_interval', False, 'Interval for which user was offline.', short_name='o')
flags.DEFINE_boolean('skip_printing_sessions', False, 'Skip printing sessions.')
flags.DEFINE_boolean('skip_last_online', False, 'Skip last online.')
# Default offline delay below is manually observed value
flags.DEFINE_integer('offline_delay', 14, 'Delay in seconds after offline status is received from actual offline',
short_name='d')
home = expanduser("~")
settingsDir = home + "/.wweb"
loggingDir = "./logs"
presenceFile = settingsDir + '/presence.json'
FMT = '%Y-%m-%d %H:%M:%S'
class OnlineInfo:
def __init__(self):
self.number = None
self.id = None
self.firstOnlineTime = None
self.lastOfflineTime = None
self.totalOnline = 0
self.currentOnlineTime = None
self.onlineCount = 0
self.durationFrequencyCount = 0
class Graph:
timeAfter = None
timeBefore = None
offlineDelay = None
printSum = False
numberData = {}
duration = None
offlineInterval = False
def __init__(self, timeAfter, timeBefore, offlineDelay, printSum, offlineInterval):
self.timeAfter = timeAfter
self.timeBefore = timeBefore
self.offlineDelay = offlineDelay
self.printSum = printSum
self.offlineInterval = offlineInterval
def onlineSessionComplete(self, numberObj):
numberObj.lastOfflineTime = numberObj.lastOfflineTime - timedelta(seconds=self.offlineDelay)
numberObj.onlineCount +=1
tdelta = self.getTimeDifference(numberObj.lastOfflineTime, numberObj.firstOnlineTime)
if self.duration is not None and tdelta.seconds >= self.duration:
numberObj.durationFrequencyCount += 1
outputString = "Number: {:>12s}, {} to {}, Difference: {}".format(
numberObj.number,
numberObj.firstOnlineTime,
numberObj.lastOfflineTime,
tdelta)
if self.offlineInterval:
offlineInterval = None
if numberObj.currentOnlineTime is not None:
offlineInterval = numberObj.currentOnlineTime - numberObj.lastOfflineTime
else:
offlineInterval = datetime.now() - numberObj.lastOfflineTime
outputString += ", then offline for: {}".format(str(offlineInterval).split(".")[0])
if not FLAGS.skip_printing_sessions:
print(outputString)
self.add_time_difference(numberObj.number, tdelta)
def ongoingOnlineSession(self, numberObj):
numberObj.onlineCount +=1
if not FLAGS.skip_printing_sessions:
outputString = "Number: {:>12s}, Currently online from: {}, Difference: {}".format(
numberObj.number,
numberObj.currentOnlineTime,
str(self.getTimeDifference(datetime.now(),numberObj.currentOnlineTime)).split(
".")[0])
print(outputString)
def add_time_difference(self, number, tdelta):
self.numberData[number].totalOnline = self.numberData[number].totalOnline + tdelta
def getTimeDifference(self, newTime, oldTime):
tdelta = newTime - oldTime
if tdelta < timedelta(0):
return timedelta(0)
return tdelta
def loadPresenceData(self):
with open(presenceFile) as f:
lineList = f.readlines()
for line in lineList:
line = line.strip()
info = line.split(",")
number = info[0]
pType = info[1]
vTime = datetime.strptime(info[2], FMT)
if (self.timeAfter is not None) and (vTime < self.timeAfter):
continue
if (self.timeBefore is not None) and (vTime > self.timeBefore):
continue
if pType == 'composing':
continue
if number not in self.numberData:
onlineInfo = OnlineInfo()
onlineInfo.number = number
onlineInfo.id = info[3]
onlineInfo.totalOnline = datetime.strptime("0:00:00", "%H:%M:%S")
self.numberData[number] = onlineInfo
numberObj = self.numberData[number]
if pType == 'available':
if numberObj.currentOnlineTime is None:
numberObj.currentOnlineTime = vTime
else:
continue
if numberObj.firstOnlineTime is None:
numberObj.firstOnlineTime = vTime
else:
if numberObj.lastOfflineTime is not None:
difference_last_offline = self.getTimeDifference(vTime, numberObj.lastOfflineTime)
if difference_last_offline.seconds <= FLAGS.ignore_difference_sec:
continue
else:
self.onlineSessionComplete(numberObj)
numberObj.lastOfflineTime = None
numberObj.firstOnlineTime = vTime
elif pType == 'unavailable':
if numberObj.currentOnlineTime is None:
continue
numberObj.lastOfflineTime = vTime
numberObj.currentOnlineTime = None
else:
continue
for k, v in self.numberData.iteritems():
if v.lastOfflineTime is not None and v.firstOnlineTime is not None:
self.onlineSessionComplete(v)
for k, v in self.numberData.iteritems():
if v.currentOnlineTime is not None:
self.ongoingOnlineSession(v)
if self.printSum:
for k, v in iter(self.sort_dict(self.numberData, self.cmp_lastoffline_info)):
output = "Number: {:>12s}, Time: {}".format(k, v.totalOnline.strftime("%H:%M:%S"))
if not FLAGS.skip_last_online:
if v.currentOnlineTime is None:
output += ", Last online: {}".format(v.lastOfflineTime)
else:
output += ", Last online: now"
print(output)
def sort_dict(self, dict, comparison_func):
return sorted(dict.iteritems(), key=itemgetter(1),
cmp=comparison_func)
def cmp_lastoffline_info(self, v1, v2):
if v1.currentOnlineTime is None and v2.currentOnlineTime is None:
if v1.lastOfflineTime is None:
return 1
if v2.lastOfflineTime is None:
return -1
if v1.lastOfflineTime < v2.lastOfflineTime:
return -1
return 1
if v1.currentOnlineTime is not None:
if v2.lastOfflineTime is None:
return -1
else:
return 1
if v2.currentOnlineTime is not None:
if v1.lastOfflineTime is None:
return 1
else:
return -1
def sortData(self, key=None, labels=None):
ar = []
for k, v in self.numberData.iteritems():
if FLAGS.usertype == "number":
ar.append((k, key(v), labels(v)))
else:
ar.append((v.id, key(v), labels(v)))
ar = sorted(ar, key=lambda x: x[1])
y_pos = []
x_pos = []
timestring = []
for l in ar:
x_pos.append(l[0])
y_pos.append(l[1])
timestring.append(l[2])
return x_pos, y_pos, timestring
def generateGraph(self):
people, times, timestring = self.sortData(key=lambda x: convertToSeconds(x.totalOnline),
labels= lambda x: x.totalOnline.strftime("%H:%M:%S"))
fig, ax = plt.subplots()
# Example data
# people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
ax.barh(y_pos, times, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.set_xlabel('Time spent')
for i, v in enumerate(times):
ax.text(v + 10, i - 0.1, timestring[i], color='blue')
# ax.xaxis.set_major_locator(MinuteLocator())
# ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))
plt.show()
def generateCountGraph(self):
people, times, timestring = self.sortData(key=lambda x: x.onlineCount,
labels= lambda x: x.onlineCount)
fig, ax = plt.subplots()
# Example data
# people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
ax.barh(y_pos, times, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.set_xlabel('Time whatsapp opened')
for i, v in enumerate(times):
ax.text(v + 0.5, i, timestring[i], color='blue')
# ax.xaxis.set_major_locator(MinuteLocator())
# ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))
plt.show()
def generateDurationFrequencyGraph(self,margin):
people, times, timestring = self.sortData(key=lambda x: x.durationFrequencyCount,
labels= lambda x: x.durationFrequencyCount)
fig, ax = plt.subplots()
# Example data
# people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
ax.barh(y_pos, times, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.set_xlabel('Sessions more than {} seconds'.format(self.duration))
for i, v in enumerate(times):
ax.text(v + 0.5, i, timestring[i], color='blue')
# ax.xaxis.set_major_locator(MinuteLocator())
# ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))
plt.show()
def main(argv):
FLAGS(sys.argv)
logging.basicConfig(filename=loggingDir + "/graph.log",
format='%(asctime)s - %(message).300s',
level=logging.INFO, filemode='w')
logging.Formatter.converter = customTime
timeAfter = None
timeBefore = None
if FLAGS.timeafter is not None:
timeAfter = datetime.strptime(FLAGS.timeafter, FMT)
if FLAGS.timebefore is not None:
timeBefore = datetime.strptime(FLAGS.timebefore, FMT)
g = Graph(timeAfter, timeBefore, FLAGS.offline_delay, FLAGS.sum, FLAGS.offline_interval)
if not FLAGS.skip_graph and FLAGS.graph_type is 3:
g.duration = FLAGS.duration
g.loadPresenceData()
if not FLAGS.skip_graph:
if FLAGS.graph_type is 1:
g.generateGraph()
if FLAGS.graph_type is 2:
g.generateCountGraph()
if FLAGS.graph_type is 3:
g.generateDurationFrequencyGraph(1)
if __name__ == "__main__":
app.run(main)