Mercurial > ~darius > hgwebdir.cgi > agl
comparison agl.py @ 13:988e511a5f29
Run strftime on filename.
Use localtime for display / filename.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Mon, 11 Sep 2017 20:57:54 +0930 |
parents | b610ce4ec081 |
children | db4951d2d303 |
comparison
equal
deleted
inserted
replaced
12:b610ce4ec081 | 13:988e511a5f29 |
---|---|
25 | 25 |
26 def main(): | 26 def main(): |
27 parser = argparse.ArgumentParser() | 27 parser = argparse.ArgumentParser() |
28 parser.add_argument('-u', '--update', help = 'Update data', action="store_true") | 28 parser.add_argument('-u', '--update', help = 'Update data', action="store_true") |
29 parser.add_argument('-g', '--graph', help = 'Produce graph', action="store_true") | 29 parser.add_argument('-g', '--graph', help = 'Produce graph', action="store_true") |
30 parser.add_argument('-f', '--filename', help = 'Filename to save graph as', type = str) | 30 parser.add_argument('-f', '--filename', help = 'Filename to save graph as (uses strftime on start)', type = str) |
31 parser.add_argument('-s', '--start', help = 'Start date for graph (YYYY-MM-DD)', type = valid_date) | 31 parser.add_argument('-s', '--start', help = 'Start date for graph (YYYY-MM-DD)', type = valid_date) |
32 parser.add_argument('-e', '--end', help = 'End date for graph (YYYY-MM-DD)', type = valid_date) | 32 parser.add_argument('-e', '--end', help = 'End date for graph (YYYY-MM-DD)', type = valid_date) |
33 | 33 |
34 args = parser.parse_args() | 34 args = parser.parse_args() |
35 | 35 |
53 | 53 |
54 end = args.end | 54 end = args.end |
55 if end is None: | 55 if end is None: |
56 end = start + datetime.timedelta(days = 1) | 56 end = start + datetime.timedelta(days = 1) |
57 end = datetime.datetime(end.year, end.month, end.day) | 57 end = datetime.datetime(end.year, end.month, end.day) |
58 | |
59 dbh = sqlite3.connect(dbfn, detect_types = sqlite3.PARSE_DECLTYPES) | 58 dbh = sqlite3.connect(dbfn, detect_types = sqlite3.PARSE_DECLTYPES) |
60 cur = dbh.cursor() | 59 cur = dbh.cursor() |
61 if args.update: | 60 if args.update: |
62 date = start | 61 date = start |
63 while date < end: | 62 while date < end: |
78 updatedb(cur, data) | 77 updatedb(cur, data) |
79 dbh.commit() | 78 dbh.commit() |
80 date += datetime.timedelta(days = 1) | 79 date += datetime.timedelta(days = 1) |
81 | 80 |
82 if args.graph: | 81 if args.graph: |
83 graph(args.filename, cur, ['battery_charge', 'battery_power', 'power_imported', 'power_exported', 'power_consumed', 'power_generated'], start, end) | 82 graph(args.filename, cur, |
83 ['battery_charge', 'battery_power', 'power_imported', 'power_exported', 'power_consumed', 'power_generated'], | |
84 start, end) | |
84 | 85 |
85 def mkdb(cur): | 86 def mkdb(cur): |
86 cur.execute(''' | 87 cur.execute(''' |
87 CREATE TABLE IF NOT EXISTS agl ( | 88 CREATE TABLE IF NOT EXISTS agl ( |
88 t_stamp TIMESTAMP PRIMARY KEY, | 89 t_stamp TIMESTAMP PRIMARY KEY, |
185 | 186 |
186 if start.tzinfo == None: | 187 if start.tzinfo == None: |
187 start = start.replace(tzinfo = lt) | 188 start = start.replace(tzinfo = lt) |
188 if end.tzinfo == None: | 189 if end.tzinfo == None: |
189 end = end.replace(tzinfo = lt) | 190 end = end.replace(tzinfo = lt) |
190 | 191 startlt = start |
192 endlt = end | |
191 start = start.astimezone(utc) | 193 start = start.astimezone(utc) |
192 end = end.astimezone(utc) | 194 end = end.astimezone(utc) |
193 | 195 |
194 # Actually get the data | 196 # Actually get the data |
195 colstr = reduce(lambda a, b: a + ', ' + b, cols) | 197 colstr = reduce(lambda a, b: a + ', ' + b, cols) |
262 ax1.text(0.02, 0.9, reduce(lambda a, b: a + '\n' + b, annotations), | 264 ax1.text(0.02, 0.9, reduce(lambda a, b: a + '\n' + b, annotations), |
263 transform = ax1.transAxes, bbox = dict(facecolor = 'red', alpha = 0.5), | 265 transform = ax1.transAxes, bbox = dict(facecolor = 'red', alpha = 0.5), |
264 ha = 'left', va = 'top') | 266 ha = 'left', va = 'top') |
265 ndays = int(max(1, round(((end - start).total_seconds()) / 86400))) | 267 ndays = int(max(1, round(((end - start).total_seconds()) / 86400))) |
266 for ax in fig.get_axes(): | 268 for ax in fig.get_axes(): |
267 ax.set_title('%s to %s' % (start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d'))) | 269 ax.set_title('%s to %s' % (startlt.strftime('%Y-%m-%d'), endlt.strftime('%Y-%m-%d'))) |
268 ax.set_xlim([start, end]) | 270 ax.set_xlim([start, end]) |
269 ax.format_xdata = lambda d: matplotlib.dates.num2date(d).strftime('%d %b %H:%M') | 271 ax.format_xdata = lambda d: matplotlib.dates.num2date(d).strftime('%d %b %H:%M') |
270 ax.xaxis.grid(True) | 272 ax.xaxis.grid(True) |
271 ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%d %b\n%H:%M')) | 273 ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%d %b\n%H:%M')) |
272 ax.xaxis.set_major_locator(matplotlib.dates.HourLocator(interval = 2 * ndays)) | 274 ax.xaxis.set_major_locator(matplotlib.dates.HourLocator(interval = 2 * ndays)) |
279 fig.subplots_adjust(left = 0.10, right = 0.88, top = 0.95, bottom = 0.15) | 281 fig.subplots_adjust(left = 0.10, right = 0.88, top = 0.95, bottom = 0.15) |
280 if fname == None: | 282 if fname == None: |
281 matplotlib.pyplot.show() | 283 matplotlib.pyplot.show() |
282 else: | 284 else: |
283 canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig) # Sets canvas in fig too | 285 canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig) # Sets canvas in fig too |
284 fig.savefig(fname) | 286 fig.savefig(startlt.strftime(fname)) |
285 | 287 |
286 def updatedb(cur, data): | 288 def updatedb(cur, data): |
287 mkdb(cur) | 289 mkdb(cur) |
288 for d in data['reads']['data']: | 290 for d in data['reads']['data']: |
289 ts = datetime.datetime.strptime(d['t_stamp'], '%Y-%m-%dT%H:%M:%SZ') | 291 ts = datetime.datetime.strptime(d['t_stamp'], '%Y-%m-%dT%H:%M:%SZ') |