Skip to content

Instantly share code, notes, and snippets.

@aoirint
Created September 15, 2018 11:48
Show Gist options
  • Save aoirint/14e0a1f4d97e3fa59d2f7bd81ec63557 to your computer and use it in GitHub Desktop.
Save aoirint/14e0a1f4d97e3fa59d2f7bd81ec63557 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as ticker
import time
from datetime import datetime as dt
from datetime import timedelta
import sqlite3
db = sqlite3.connect('./db.sqlite3')
cur = db.cursor()
def dump():
figpath = '%s.png' % pdate.strftime('%Y-%m-%d')
fig, ax = plt.subplots()
start = dt.combine(pdate, dt.min.time())
end = dt.combine(pdate, dt.max.time())
ax.set_xlim(start, end)
ax.set_ylim(0, 20 * 10**9)
ax.plot(log_x, log_y)
ax.plot([ start, end ], [ 10 * 10**9, ] * 2)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: '%.1f GB' % (x / (10**9), )))
fig.suptitle(pdate.isoformat())
fig.savefig(figpath)
pdate = None
log_x, log_y = [], []
results = [ row for row in cur.execute('SELECT * FROM stat ORDER BY time') ]
for idx in range(len(results)):
id, download, upload, time = results[idx]
time_dt = dt.fromtimestamp(time)
time_d = time_dt.date()
if pdate is not None:
if pdate != time_d:
print(pdate)
dump()
log_x.append(time_dt)
log_y.append(download)
pdate = time_d
print(pdate)
dump()
db.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment