annotate graph.py @ 7:40a3b403a096

Merge
author Daniel O'Connor <darius@dons.net.au>
date Fri, 22 Dec 2017 13:05:05 +0100
parents a7e9775b33f6
children 30e7adf283ca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 import argparse
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 import datetime
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 import dateutil
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 import exceptions
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 import matplotlib
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 import matplotlib.dates
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 import numpy
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 import os
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 import requests
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 import sqlite3
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 import tzlocal
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 class Column(object):
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 def __init__(self, rowname, title, table, units, limits = (None, None), conv = None):
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 self.rowname = rowname
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 self.title = title
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 self.table = table
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 self.units = units
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 self.limits = limits
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 self.conv = None
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 columns = [
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 Column('main_voltage', 'Battery Voltage', 'eprolog', 'Vdc'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 Column('aux_voltage', 'Aux Voltage', 'eprolog', 'Vdc'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 Column('battery_curr', 'Battery Current', 'eprolog', 'A'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 Column('amp_hours', 'Battery Amp Hours', 'eprolog', 'Ah'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 Column('state_of_charge', 'State of Charge', 'eprolog', '%', (0, 100)),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 Column('time_remaining', 'Time Remaining', 'eprolog', 'min'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 Column('battery_temp', 'Battery Temperature', 'eprolog', 'C'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 Column('ac_act_power', 'Active Power', 'giantlog', 'W'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 Column('ac_app_power', 'Apparent Power', 'giantlog', 'W'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 Column('ac_frequency', 'AC Frequency', 'giantlog', 'Hz'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 Column('ac_volts', 'AC Voltage', 'giantlog', 'Vac'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 Column('batt_chr_curr', 'Discharge Current', 'giantlog', 'A'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 Column('batt_dis_curr', 'Charge Current', 'giantlog', 'A'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 Column('battery_cap', 'Battery Capacity', 'giantlog', '%', (0, 100)),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 Column('battery_volts', 'Battery Voltage', 'giantlog', 'Vdc'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 Column('grid_frequency', 'Grid Frequency', 'giantlog', 'Hz'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 Column('grid_volts', 'Grid Voltage', 'giantlog', 'Vac'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 Column('hs_temperature', 'HS Temperature', 'giantlog', 'C'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 Column('load_pct', 'Load', 'giantlog', '%', (0, 100)),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 ]
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 def valid_date(s):
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 try:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 return datetime.datetime.strptime(s, "%Y-%m-%d")
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 except ValueError:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 raise argparse.ArgumentTypeError("Not a valid date: '{0}'.".format(s))
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 def main():
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 parser = argparse.ArgumentParser()
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 parser.add_argument('-f', '--filename', help = 'Path to database', type = str, required = True)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 parser.add_argument('-g', '--graphfn', help = 'File to write graph to', type = str)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 parser.add_argument('-d', '--days', help = 'Days ago to graph', type = int)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 parser.add_argument('-s', '--start', help = 'Start date for graph (YYYY-MM-DD)', type = valid_date)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 parser.add_argument('-e', '--end', help = 'End date for graph (YYYY-MM-DD)', type = valid_date)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 parser.add_argument('-c', '--column', help = 'Column to plot (can be specified multiple times)', type = str, action = 'append')
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 args = parser.parse_args()
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 if args.days is not None and args.days < 0:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 parser.error('days must be non-negative')
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 # Can specify..
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 # Start and end
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 # Start and days
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 # End and days
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 # Nothing
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 # Want to end up with a start & end
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 if args.start is not None and args.end is not None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 pass
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 elif args.start is not None and args.days is not None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 args.end = args.start + datetime.timedelta(days = args.days)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 elif args.end is not None and args.days is not None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 args.start = args.end - datetime.timedelta(days = args.days)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 elif args.start is None and args.end is None and args.days is None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 end = datetime.date.today()
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 end = datetime.datetime(start.year, start.month, start.day)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 args.start = args.end - datetime.timedelta(days = args.days)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 else:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 parser.error('can\'t specify days, start and end simultaneously')
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 if args.start >= args.end:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 parser.error('Start must be before end')
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 cols = args.column
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 if cols == None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 cols = ['main_voltage', 'aux_voltage', 'ac_app_power']
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 dbh = sqlite3.connect(args.filename, detect_types = sqlite3.PARSE_DECLTYPES)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 cur = dbh.cursor()
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 # Get local timezone name and convert start/end to it
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 # Why is this so hard...
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 ltname = tzlocal.get_localzone().zone
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 ltname = 'Australia/Adelaide'
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 lt = dateutil.tz.gettz(ltname)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 utc = dateutil.tz.gettz('UTC')
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 matplotlib.rcParams['timezone'] = ltname
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 if args.start.tzinfo == None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 args.start = args.start.replace(tzinfo = lt)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 if args.end.tzinfo == None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 args.end = args.end.replace(tzinfo = lt)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 startlt = args.start
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 endlt = args.end
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 args.start = args.start.astimezone(utc)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 args.end = args.end.astimezone(utc)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 graph(args.graphfn, cur, cols, int(args.start.strftime('%s')), int(args.end.strftime('%s')), lt, utc)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 def graph(fname, cur, _cols, start, end, lt, utc):
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 import numpy
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 import matplotlib
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 import matplotlib.dates
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 startdt = datetime.datetime.fromtimestamp(start).replace(tzinfo = utc).astimezone(lt)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 enddt = datetime.datetime.fromtimestamp(end).replace(tzinfo = utc).astimezone(lt)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 colourlist = ['b','g','r','c','m','y','k']
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 cols = []
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126 yaxisunits1 = None
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 yaxisunits2 = None
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 ax1lines = []
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 ax2lines = []
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 colouridx = 0
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 for col in _cols:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 # Check the column exists
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 for c in columns:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134 if col == c.rowname:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 cols.append(c)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136 break
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137 else:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 raise exceptions.Exception('Unknown column name ' + c)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
140 # Work out what axes we are using
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
141 if yaxisunits1 == None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142 yaxisunits1 = c.units
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 if yaxisunits2 == None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 if c.units != yaxisunits1:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 yaxisunits2 = c.units
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 else:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 if c.units != yaxisunits1 and c.units != yaxisunits2:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 raise exceptions.Exception('Asked to graph >2 different units')
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 for c in cols:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 # Get the data
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 cur.execute('SELECT tstamp, ' + c.rowname + ' FROM ' + c.table + ' WHERE tstamp > ? AND tstamp < ? ORDER BY tstamp',
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153 (start, end))
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 ary = numpy.array(cur.fetchall())
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155 if ary.shape[0] == 0:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 print('No data for ' + c.rowname)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 return
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
159 # Create TZ naive from POSIX stamp, then convert to TZ aware UTC then adjust to local time
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 c.xdata = map(lambda f: datetime.datetime.fromtimestamp(f).replace(tzinfo = utc).astimezone(lt), ary[:,0])
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 c.ydata = ary[:,1]
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162 if c.conv != None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 c.ydata = map(c.conv, c.ydata)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
165 scale_min, scale_max = c.limits
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167 # DoD?
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168 c.annotation = None
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
169
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
170 # Work out which axis to plot on
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
171 if c.units == yaxisunits1:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172 ax = ax1lines
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173 else:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 ax = ax2lines
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175 c.colour = colourlist[colouridx]
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
176 colouridx += 1
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177 ax.append(c)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179 # Load the right backend for display or save
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
180 if fname == None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181 import matplotlib.pylab
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182 fig = matplotlib.pylab.figure()
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183 else:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
184 import matplotlib.backends.backend_agg
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185 fig = matplotlib.figure.Figure(figsize = (12, 6), dpi = 75)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187 # Do the plot
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188 ax1 = fig.add_subplot(111)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
189 ax1.set_ylabel(yaxisunits1)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191 annotations = []
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192 for line in ax1lines:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 ax1.plot(line.xdata, line.ydata, label = line.title, color = line.colour)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 if line.limits[0] != None or line.limits[1] != None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
195 ax1.set_ylim(line.limits[0], line.limits[1])
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196 if line.annotation != None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197 annotations.append(line.annotation)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198 ax1.legend(loc = 'upper left')
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
199
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200 if len(ax2lines) > 0:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201 ax2 = ax1.twinx()
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202 ax2.set_ylabel(yaxisunits2)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
204 for line in ax2lines:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
205 ax2.plot(line.xdata, line.ydata, label = line.title, color = line.colour)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
206 if line.limits[0] != None or line.limits[1] != None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
207 ax2.set_ylim(line.limits[0], line.limits[1])
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
208 if line.annotation != None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
209 annotations.append(line.annotation)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211 ax2.legend(loc = 'upper right')
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
212
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
213 if len(annotations) > 0:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
214 ax1.text(0.02, 0.9, reduce(lambda a, b: a + '\n' + b, annotations),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215 transform = ax1.transAxes, bbox = dict(facecolor = 'red', alpha = 0.5),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
216 ha = 'left', va = 'top')
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
217 ndays = int(max(1, round((end - start) / 86400)))
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218 for ax in fig.get_axes():
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
219 if (enddt - startdt).total_seconds() > 86400:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220 ax.set_title('%s to %s' % (startdt.strftime('%Y-%m-%d'), enddt.strftime('%Y-%m-%d')))
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
221 else:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
222 ax.set_title('%s' % (startdt.strftime('%Y-%m-%d')))
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
223 ax.set_xlim([startdt, enddt])
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
224 ax.format_xdata = lambda d: matplotlib.dates.num2date(d).strftime('%d %b %H:%M')
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
225 ax.xaxis.grid(True)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226 ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%d %b\n%H:%M'))
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227 ax.xaxis.set_major_locator(matplotlib.dates.HourLocator(interval = 2 * ndays))
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
228 ax.xaxis.set_minor_locator(matplotlib.dates.MinuteLocator(interval = 5 * ndays))
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
229 for label in ax.get_xticklabels():
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230 label.set_ha('center')
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
231 label.set_rotation(90)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
232
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
233 # Fudge margins to give more graph and less space
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
234 fig.subplots_adjust(left = 0.10, right = 0.88, top = 0.95, bottom = 0.15)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
235 if fname == None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
236 matplotlib.pyplot.show()
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
237 else:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
238 canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig) # Sets canvas in fig too
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
239 fig.savefig(startlt.strftime(fname))
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
240
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
241 def updatedb(cur, data):
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
242 mkdb(cur)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
243 for d in data['reads']['data']:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
244 ts = datetime.datetime.strptime(d['t_stamp'], '%Y-%m-%dT%H:%M:%SZ')
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
245 # Note we rename *energy* to *power* here to match what it actually means
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
246 vals = [ts, d['battery_charge'], d['battery_energy'], d['energy_consumed'], d['energy_expected'], d['energy_exported'], d['energy_generated'],
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
247 d['energy_imported'], d['estimated_savings'], d['pv_forecast'], d['pv_generation']['battery_energy'],
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
248 d['pv_generation']['grid_energy'], d['pv_generation']['site_energy'], d['site_consumption']['battery_energy'],
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
249 d['site_consumption']['grid_energy'], d['site_consumption']['pv_energy']]
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
250 skip = True
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
251 for v in vals[1:]:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
252 if v != None:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
253 skip = False
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
254 break
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
255 if skip:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
256 print('Skipping empty record at ' + str(ts))
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
257 continue
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
258 cur.execute('INSERT OR IGNORE INTO agl(t_stamp, battery_charge, battery_power, power_consumed, power_expected, power_exported, power_generated, power_imported, estimated_savings, pv_forecast, pv_gen_battery, pv_gen_grid, pv_gen_site, site_cons_battery, site_cons_grid, site_cons_pv) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', vals)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
259
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
260 def gettoken(username, password):
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
261 authblob = json.encoder.JSONEncoder().encode({'email' : username, 'password' : password})
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
262 reply = requests.request('POST', loginurl, data = authblob, headers = {'Content-Type' : 'application/json'})
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
263 if reply.status_code != 200:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
264 return None
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
265 return json.decoder.JSONDecoder().decode(reply.content)['access_token']
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
266
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
267 def getdata(token, startdate, enddate):
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
268 #print('getting ' + startdate.strftime('%Y-%m-%d'))
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
269 reply = requests.request('GET', dataurl, params = {
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
270 'startDate' : startdate.strftime('%Y-%m-%d'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
271 'endDate' : enddate.strftime('%Y-%m-%d'),
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
272 'granularity' : 'Minute',
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
273 'metrics' : 'read',
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
274 'units' : 'W',
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
275 }, headers = { 'Authorization' : 'Bearer ' + token})
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
276
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
277 if reply.status_code != 200:
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
278 return None
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
279
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
280 return json.decoder.JSONDecoder().decode(reply.content)
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
281
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
282 def logout(token):
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
283 reply = requests.request('GET', logouturl, headers = { 'Authorization' : 'Bearer ' + token})
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
284 return reply.status_code == 200
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
285
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
286 if __name__ == '__main__':
a7e9775b33f6 Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
287 main()