forked from dufferzafar/netuse
/
netuse.py
executable file
·283 lines (194 loc) · 6.93 KB
/
netuse.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
#!/usr/bin/python3
"""
Calculate internet usage from log files created by a custom script.
Can also display stats like suggested usage, weekly report etc.
"""
import sys
import os
# Used for calculating suggested and hourly internet usage
from datetime import date, timedelta, datetime
# Print bar charts to terminal
# Not a module on PyPI
import termgraph
# A tiny wrapper over notify-send
# Not a module on PyPI
import notify
# Load settings from config file
# See file 'config.py.example' for what the settings mean.
from config import (
START_DATE,
DAYS_IN_MONTH,
TOTAL_DATA,
EPOCH_DIFF,
CORRECTION_FACTOR,
LOGFILES_PATH,
)
# How many bytes in an MB?
MB = 1024 * 1024
# Ideally, these values shouldn't be globals but passed around to functions!
s_day, s_month, s_year = map(int, START_DATE.split("/"))
# not (Explicit is better than implicit)
join = os.path.join
def gen_file_list():
"""Generate a list of 'monthly' files to read in."""
down_filelist = []
up_filelist = []
start_date = date(s_year, s_month, s_day)
end_date = date.today()
for din in daterange(start_date, end_date):
down_path = join(LOGFILES_PATH, str(din.year), din.strftime('%b'), "down")
up_path = join(LOGFILES_PATH, str(din.year), din.strftime('%b'), "up")
day = "%02d" % din.day
# Skip when day doesn't exist
if not os.path.isfile(join(down_path, day)):
continue
down_filelist.append(join(down_path, day))
up_filelist.append(join(up_path, day))
return down_filelist, up_filelist
def read_files(files):
"""Read files and generate tuples of (data, epoch)."""
tuples = []
for _file in files:
with open(_file) as f:
tuples.extend([tuple(map(to_int, s.split(";")))
for s in f.readlines()])
return tuples
def calculate(tuples):
"""Calculate actual data usage from the list of tuples."""
total = 0
previous = tuples[0]
for current in tuples[1:]:
# Some of the data has no epoch entries
# only byte usage for that day
if len(current) == 1:
total += current[0]
continue
usage = current[0] - previous[0]
duration = current[1] - previous[1]
if duration <= EPOCH_DIFF and usage >= 0:
total += usage
else:
total += current[0]
previous = current
return total
def calculate_monthly_stats():
"""Calculate stats for the current month."""
down_filelist, up_filelist = gen_file_list()
total_down = calculate(read_files(down_filelist)) // MB
total_down += correction(total_down)
total_up = calculate(read_files(up_filelist)) // MB
data_left = TOTAL_DATA - total_down - total_up
start_date = date(s_year, s_month, s_day)
end_date = start_date + timedelta(days=DAYS_IN_MONTH)
days_left = (end_date - date.today()).days
suggested = data_left // days_left
return total_down, total_up, data_left, days_left, end_date, suggested
def monthly():
"""Print the monthly stats month."""
output = [
"Downloaded:\t%4d MB",
"Uploaded:\t%4d MB\n",
"Data Left:\t%4d MB",
"Days Left:\t%4d Days",
"End Date:\t%s (11:59 PM)\n",
"Suggested:\t%4d MB (Per Day)",
]
print("\n".join(output) % calculate_monthly_stats())
def daily(t=date.today()):
"""Print stats for a single day, default today."""
# Path of day's file
path = join(LOGFILES_PATH, t.strftime('%G'), t.strftime('%b'), "%s", t.strftime('%d'))
down = calculate(read_files([path % "down"])) // MB
up = calculate(read_files([path % "up"])) // MB
output = (
"Downloaded:\t%4d MB \n"
"Uploaded:\t%4d MB \n"
) % (down, up)
print(output)
def weekly():
"""
Use termgraph to plot usage of this week.
Data is aggregated according to days.
"""
# Get this month's file list
down_filelist, _ = gen_file_list()
# Iterate over every file in this week
week = {}
for file in down_filelist[-7:]:
week[file.split('/')[-1]] = calculate(read_files([file])) // MB
data = sorted(week.items())
print("Data downloaded this week:\n")
termgraph.chart(
labels=["%s%s" % (d[0], ordinal_suffix(int(d[0]))) for d in data],
data=[d[1] for d in data],
args=dict(
width=30,
suffix=" MB",
format="{:>5.0f}",
)
)
print("Total: %d MB" % sum([d[1] for d in data]))
def hourly():
"""Calculate usage of last hour."""
# Read today's file
t = date.today()
path = join(LOGFILES_PATH, t.strftime('%G'), t.strftime('%b'), "%s", t.strftime('%d'))
tuples = read_files([path % "down"])
# Calculate timestamp of an hour ago
hour_ago = datetime.now() - timedelta(hours=1)
hour_ago_ts = int(hour_ago.strftime("%s"))
# Only keep tuples of the last hour
# whose timestamp is greater than last hour stamp
tuples = [t for t in tuples if t[1] > hour_ago_ts]
hourly_usage = calculate(tuples) // MB
return hourly_usage
def noti():
"""Send notification about usage and the data remaining."""
_, _, data_left, _, _, suggested = calculate_monthly_stats()
# TODO: If end date is today, add a line about that too
# or maybe a custom alert?
title = "Remaining Data: %d MB" % data_left
body = "\n".join([
"You've downloaded %d MB in the last hour.",
"Suggested usage is %d MB per day."
]) % (hourly(), suggested)
notify.send(title, body)
# ================================================================ Helper functions
def ordinal_suffix(d):
"""
Return ordnial suffixes for an integer.
Taken from: http://stackoverflow.com/a/5891598/2043048
"""
return 'th' if 11 <= d <= 13 else {1: 'st', 2: 'nd', 3: 'rd'}.get(d % 10, 'th')
def to_int(s):
"""Convert string to integer, empty string is zero."""
if s.strip() and s.strip('\0'):
return int(s)
else:
return 0
def daterange(start_date, end_date):
"""Iterate over a range of dates. Both ends inclusive."""
for n in range(int((end_date - start_date).days) + 1):
yield start_date + timedelta(n)
def correction(n):
"""
For reasons I can't comprehend, the results are wrong.
My results differ significantly from what MTS shows me.
The correction factor, if it exists, will depend on, the
total data that has been downloaded till now.
This is a really crude hack - I have no idea what I am doing.
"""
return n * CORRECTION_FACTOR
# If I am being called directly (rather than being imported)
if __name__ == '__main__':
# FIXME: Replace this with something 'real' like docopt/click
if '-t' in sys.argv:
daily()
elif '-w' in sys.argv:
weekly()
elif '-h' in sys.argv:
print("Data downloaded in the last hour: %d MB" % hourly())
elif '-n' in sys.argv:
noti()
else:
monthly()