-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.py
79 lines (66 loc) · 2.51 KB
/
App.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
import subprocess
# Returns an json blob from a json output of vnStat
def vnStatGetJSON():
result = subprocess.run(['vnstat', '--json'], stdout=subprocess.PIPE)
json_blob = result.stdout.decode('utf-8')
return json_blob
#================================================
# time handling stuff
#================================================
def numberStringSize(integer, minsize, align_right=True):
num_text = str(integer)
empty_zeros = '0' * (minsize - len(num_text))
if align_right:
num_text = empty_zeros + num_text
else:
num_text = num_text + empty_zeros
return num_text
# Gets a timestamp obj from a single point in time from vnstat
# This works for JSON V2
def getTimestamp(time_point):
year = time_point['date']['year']
month = time_point['date'].get('month', 0)
day = time_point['date'].get('day', 0)
time = time_point.get('time', { })
hour = time.get('hour', 0)
minute = time.get('minute', 0)
timestamp = {'year':year, 'month':month, 'day':day, 'hour':hour, 'minute':minute}
return timestamp
# Returns a timestamp string from a timestamp obj
def timeStr(timeObj):
year = str(timeObj['year'])
month = numberStringSize(timeObj['month'], 2)
day = numberStringSize(timeObj['day'], 2)
hour = numberStringSize(timeObj['hour'], 2)
minute = numberStringSize(timeObj['minute'], 2)
timestamp = year + "-" + month + "-" + day + " " + hour + ":" + minute
return timestamp
#================================================
# graph handling functions
#================================================
# Puts the rx,tx,date in an object
def histogramFormat(timespan, data, json_version):
data_points = []
for i in data:
if json_version == "1" and timespan == "hours":
i['time'] = {}
i['time']['hour'] = i['id']
datetimeObj = getTimestamp(i)
datetimeStr = timeStr(datetimeObj)
data_points.append({'rx':i['rx'], 'tx':i['tx'], 'datetime':datetimeStr})
return data_points
# Modifies a json obj from json_filtered
def vnstat_graph(obj):
obj['simple_data'] = {}
# ignore certain types of 'simple' data
ignore_traffic = ['total', 'tops', 'top']
traffic = obj['traffic']
histograms = {}
for t in traffic:
data = traffic[t]
if not t in ignore_traffic:
histograms[t] = histogramFormat(t, data, obj['jsonversion'])
else:
obj['simple_data'][t] = data
obj.pop('traffic')
obj['graph_data'] = histograms