-
Notifications
You must be signed in to change notification settings - Fork 2
/
energydash_app.py
executable file
·252 lines (224 loc) · 8.68 KB
/
energydash_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
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
#!/usr/bin/env python
################################################################################
# file: energydash_app.py
# description: energydash web application
################################################################################
# Copyright 2013 Chris Linstid
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################
from flask import Flask, render_template
from datetime import datetime, timedelta
import pytz
import json
import pymongo
import logging
import urllib
import sys
from utc_conversion import *
from settings import *
app = Flask(__name__)
app.logger.addHandler(logging.StreamHandler(stream=sys.stderr))
mongo_uri = 'mongodb://{user}:{password}@{host}/{database}'.format(user=urllib.quote(MONGO_USER),
password=urllib.quote(MONGO_PASSWORD),
host=MONGO_HOST,
database=MONGO_DATABASE_NAME)
client = pymongo.MongoClient(MONGO_HOST, replicaset=MONGO_REPLICA_SET)
db = client[MONGO_DATABASE_NAME]
readings = db.envir_reading
hours = db.hours
bookmarks = db.bookmarks
day_map = {
'Mon': 1,
'Tue': 2,
'Wed': 3,
'Thu': 4,
'Fri': 5,
'Sat': 6,
'Sun': 7
}
def get_min(entries, key):
return min(map(lambda entry: entry[key], entries))
def get_max(entries, key):
return max(map(lambda entry: entry[key], entries))
def get_avg(entries, key):
key_entries = map(lambda entry: entry[key], entries)
return sum(key_entries) / len(key_entries)
def get_last_entry():
return bookmarks.find_one({'_id': 'seconds'})
def get_last_hour():
now = datetime.now(tz=pytz.utc)
one_hour_ago = now - timedelta(hours=1)
readings.ensure_index('reading_timestamp', pymongo.ASCENDING)
cursor = readings.find({'reading_timestamp': {'$gte': one_hour_ago}})
cursor = cursor.sort('reading_timestamp', pymongo.ASCENDING)
return map(lambda reading: [int(dt_to_seconds(reading['reading_timestamp']))*1000,
reading['total_watts']],
cursor)
@app.route('/')
def start_app():
last_entry = get_last_entry()
return render_template('index.html',
local_timezone=LOCAL_TIMEZONE,
date=local_str_from_naive_utc_dt(last_entry['timestamp'],
LOCAL_TIMEZONE),
usage=last_entry['usage'],
temp_f=last_entry['tempf'])
@app.route('/last_hour')
def fetch_last_hour():
last_hour = get_last_hour()
logger = logging.getLogger('last_hour')
logger.info('{} entries from the last hour: {} --> {}.'.format(len(last_hour),
last_hour[0][0],
last_hour[-1][0]))
logger.debug(last_hour)
return json.dumps(last_hour)
@app.route('/last_7_days')
def get_last_7_days():
now = datetime.now(tz=pytz.utc)
seven_days_ago = now - timedelta(days=7)
cursor = hours.find({'_id': {'$gte': seven_days_ago}},
{'_id': 1, 'average_usage': 1, 'average_tempf': 1})
cursor = cursor.sort('_id', pymongo.ASCENDING)
# TODO This should really be done with an aggregation!
usage_list = []
tempf_list = []
for hour in cursor:
usage_list.append([int(dt_to_seconds(hour['_id']))*1000,
hour['average_usage']])
tempf_list.append([int(dt_to_seconds(hour['_id']))*1000,
hour['average_tempf']])
hours_list = [
{
'label': 'Usage (watts)',
'data': sorted(usage_list, key=lambda hour: hour[0]),
'yaxis': 1
},
{
'label': 'Temperature (° F)',
'data': sorted(tempf_list, key=lambda hour: hour[0]),
'yaxis': 2
}
]
return json.dumps(hours_list)
@app.route('/current_state')
def fetch_current_state():
last_entry = get_last_entry()
return json.dumps({
'date': local_str_from_naive_utc_dt(last_entry['timestamp'],
LOCAL_TIMEZONE),
'usage': last_entry['usage'],
'temp_f': last_entry['tempf']
})
@app.route('/hod')
def hours_of_day():
hours_in_day = db.hours_in_day
cursor = hours_in_day.find({}, {'_id': 1, 'average_usage': 1, 'average_tempf': 1})
usage_list = []
tempf_list = []
for hour in cursor:
usage_list.append([int(hour['_id']), hour['average_usage']])
tempf_list.append([int(hour['_id']), hour['average_tempf']])
hours_list = [
{
'label': 'Usage (watts)',
'data': sorted(usage_list, key=lambda hour: hour[0]),
'yaxis': 1
},
{
'label': 'Temperature (° F)',
'data': sorted(tempf_list, key=lambda hour: hour[0]),
'yaxis': 2
}
]
return json.dumps(hours_list)
@app.route('/last_24_hours')
def last_24_hours():
now = datetime.now(tz=pytz.utc)
start = now - timedelta(hours=24)
usage_list = []
tempf_list = []
cursor = hours.find({'_id': {'$gte': start}},
{
'_id': 1,
'average_usage': 1,
'average_tempf': 1
})
cursor = cursor.sort('_id', pymongo.ASCENDING)
# TODO This should really be done with an aggregation!
for hour in cursor:
usage_list.append([int(dt_to_seconds(hour['_id']))*1000,
hour['average_usage']])
tempf_list.append([int(dt_to_seconds(hour['_id']))*1000,
hour['average_tempf']])
hours_list = [
{
'label': 'Usage (watts)',
'data': sorted(usage_list, key=lambda hour: hour[0]),
'yaxis': 1
},
{
'label': 'Temperature (° F)',
'data': sorted(tempf_list, key=lambda hour: hour[0]),
'yaxis': 2
}
]
obj = {
'chart_data': hours_list,
'min_usage': round(get_min(usage_list, 1), 2),
'max_usage': round(get_max(usage_list, 1), 2),
'avg_usage': round(get_avg(usage_list, 1), 2),
'min_tempf': round(get_min(tempf_list, 1), 2),
'max_tempf': round(get_max(tempf_list, 1), 2),
'avg_tempf': round(get_avg(tempf_list, 1), 2)
}
return json.dumps(obj)
def get_dow():
day_map = {
'Mon': 1,
'Tue': 2,
'Wed': 3,
'Thu': 4,
'Fri': 5,
'Sat': 6,
'Sun': 7
}
hours_per_dow = db.hours_per_dow
cursor = hours_per_dow.find({},
{
'_id': 1,
'hours': 1
})
days = []
for day in cursor:
hour_list = []
for hour, data in day['hours'].iteritems():
hour_list.append([int(hour), data['average_usage']])
hour_list = sorted(hour_list, key=lambda hour: hour[0])
days.append({
'label': day['_id'],
'data': hour_list
})
days = sorted(days, key=lambda day: day_map[day['label']])
return days
@app.route('/dow')
def days_of_week():
return json.dumps(get_dow())
@app.teardown_request
def shutdown_session(exception=None):
pass
if __name__ == '__main__':
logging.basicConfig(format='[%(asctime)s/%(name)s]: %(message)s',
level=logging.INFO)
logger = logging.getLogger('main')
app.run(host='0.0.0.0', debug=True)