1
|
|
|
from datetime import datetime, timedelta, timezone |
2
|
|
|
from decimal import Decimal |
3
|
|
|
import falcon |
4
|
|
|
import mysql.connector |
5
|
|
|
import simplejson as json |
6
|
|
|
from core import utilities |
7
|
|
|
import config |
8
|
|
|
from core.useractivity import access_control, api_key_control |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class Reporting: |
12
|
|
|
def __init__(self): |
13
|
|
|
"""Initializes Class""" |
14
|
|
|
pass |
15
|
|
|
|
16
|
|
|
@staticmethod |
17
|
|
|
def on_options(req, resp): |
18
|
|
|
_ = req |
19
|
|
|
resp.status = falcon.HTTP_200 |
20
|
|
|
|
21
|
|
|
#################################################################################################################### |
22
|
|
|
# PROCEDURES |
23
|
|
|
# Step 1: valid parameters |
24
|
|
|
# Step 2: query the energy storage power station list |
25
|
|
|
# Step 3: query generation energy data in 7 days |
26
|
|
|
# Step 4: query generation energy data in this month |
27
|
|
|
# Step 5: query generation energy data in this year |
28
|
|
|
# Step 6: construct the report |
29
|
|
|
#################################################################################################################### |
30
|
|
|
@staticmethod |
31
|
|
|
def on_get(req, resp): |
32
|
|
|
if 'API-KEY' not in req.headers or \ |
33
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
34
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
35
|
|
|
access_control(req) |
36
|
|
|
else: |
37
|
|
|
api_key_control(req) |
38
|
|
|
user_uuid = req.params.get('useruuid') |
39
|
|
|
|
40
|
|
|
################################################################################################################ |
41
|
|
|
# Step 1: valid parameters |
42
|
|
|
################################################################################################################ |
43
|
|
|
if user_uuid is None: |
44
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_USER_UUID') |
45
|
|
|
else: |
46
|
|
|
user_uuid = str.strip(user_uuid) |
47
|
|
|
if len(user_uuid) != 36: |
48
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
49
|
|
|
description='API.INVALID_USER_UUID') |
50
|
|
|
|
51
|
|
|
################################################################################################################ |
52
|
|
|
# Step 2: query the energy storage power station list |
53
|
|
|
################################################################################################################ |
54
|
|
|
cnx_user = mysql.connector.connect(**config.myems_user_db) |
55
|
|
|
cursor_user = cnx_user.cursor() |
56
|
|
|
cursor_user.execute(" SELECT id, is_admin, privilege_id " |
57
|
|
|
" FROM tbl_users " |
58
|
|
|
" WHERE uuid = %s ", (user_uuid,)) |
59
|
|
|
row_user = cursor_user.fetchone() |
60
|
|
|
if row_user is None: |
61
|
|
|
if cursor_user: |
62
|
|
|
cursor_user.close() |
63
|
|
|
if cnx_user: |
64
|
|
|
cnx_user.close() |
65
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
66
|
|
|
description='API.USER_NOT_FOUND') |
67
|
|
|
|
68
|
|
|
user = {'id': row_user[0], 'is_admin': row_user[1], 'privilege_id': row_user[2]} |
69
|
|
|
|
70
|
|
|
# Get energy storage power stations |
71
|
|
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
72
|
|
|
cursor_system_db = cnx_system_db.cursor() |
73
|
|
|
query = (" SELECT m.id, m.name, m.uuid " |
74
|
|
|
" FROM tbl_photovoltaic_power_stations m, tbl_photovoltaic_power_stations_users mu " |
75
|
|
|
" WHERE m.phase_of_lifecycle != '3installation' " |
76
|
|
|
" AND m.id = mu.photovoltaic_power_station_id " |
77
|
|
|
" AND mu.user_id = %s " |
78
|
|
|
" ORDER BY id ") |
79
|
|
|
cursor_system_db.execute(query, (user['id'],)) |
80
|
|
|
rows_photovoltaic_power_stations = cursor_system_db.fetchall() |
81
|
|
|
|
82
|
|
|
photovoltaic_power_station_list = list() |
83
|
|
|
photovoltaic_power_station_names = list() |
84
|
|
|
if rows_photovoltaic_power_stations is not None and len(rows_photovoltaic_power_stations) > 0: |
85
|
|
|
for row in rows_photovoltaic_power_stations: |
86
|
|
|
meta_result = {"id": row[0], |
87
|
|
|
"name": row[1], |
88
|
|
|
"uuid": row[2]} |
89
|
|
|
photovoltaic_power_station_list.append(meta_result) |
90
|
|
|
photovoltaic_power_station_names.append(row[1]) |
91
|
|
|
################################################################################################################ |
92
|
|
|
# Step 3: query generation energy data in 7 days |
93
|
|
|
################################################################################################################ |
94
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
95
|
|
|
if config.utc_offset[0] == '-': |
96
|
|
|
timezone_offset = -timezone_offset |
97
|
|
|
reporting = dict() |
98
|
|
|
reporting['generation_7_days'] = dict() |
99
|
|
|
reporting['generation_this_month'] = dict() |
100
|
|
|
reporting['generation_this_year'] = dict() |
101
|
|
|
|
102
|
|
|
end_datetime_utc = datetime.utcnow() |
103
|
|
|
end_datetime_local = datetime.utcnow() + timedelta(minutes=timezone_offset) |
104
|
|
|
period_type = 'daily' |
105
|
|
|
start_datetime_local = end_datetime_local.replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=6) |
106
|
|
|
start_datetime_utc = start_datetime_local - timedelta(minutes=timezone_offset) |
107
|
|
|
print('start_datetime_local:' + start_datetime_local.isoformat()) |
108
|
|
|
print('end_datetime_local:' + end_datetime_local.isoformat()) |
109
|
|
|
print('start_datetime_utc:' + start_datetime_utc.isoformat()) |
110
|
|
|
print('end_datetime_utc:' + end_datetime_utc.isoformat()) |
111
|
|
|
|
112
|
|
|
cnx_energy_db = mysql.connector.connect(**config.myems_energy_db) |
113
|
|
|
cursor_energy_db = cnx_energy_db.cursor() |
114
|
|
|
|
115
|
|
|
reporting['generation_7_days'] = dict() |
116
|
|
|
reporting['generation_7_days']['timestamps_array'] = list() |
117
|
|
|
reporting['generation_7_days']['values_array'] = list() |
118
|
|
|
reporting['generation_7_days']['total_values'] = list() |
119
|
|
|
|
120
|
|
|
for photovoltaic_power_station in photovoltaic_power_station_list: |
121
|
|
|
timestamps = list() |
122
|
|
|
values = list() |
123
|
|
|
query = (" SELECT start_datetime_utc, actual_value " |
124
|
|
|
" FROM tbl_photovoltaic_power_station_generation_hourly " |
125
|
|
|
" WHERE photovoltaic_power_station_id = %s " |
126
|
|
|
" AND start_datetime_utc >= %s " |
127
|
|
|
" AND start_datetime_utc < %s " |
128
|
|
|
" ORDER BY start_datetime_utc ") |
129
|
|
|
cursor_energy_db.execute(query, (photovoltaic_power_station['id'], start_datetime_utc, end_datetime_utc)) |
130
|
|
|
rows_generation_hourly = cursor_energy_db.fetchall() |
131
|
|
|
|
132
|
|
|
rows_generation_periodically = utilities.aggregate_hourly_data_by_period(rows_generation_hourly, |
133
|
|
|
start_datetime_utc, |
134
|
|
|
end_datetime_utc, |
135
|
|
|
period_type) |
136
|
|
|
for row_generation_periodically in rows_generation_periodically: |
137
|
|
|
current_datetime_local = row_generation_periodically[0].replace(tzinfo=timezone.utc) + \ |
138
|
|
|
timedelta(minutes=timezone_offset) |
139
|
|
|
if period_type == 'hourly': |
140
|
|
|
current_datetime = current_datetime_local.isoformat()[0:19] |
141
|
|
|
elif period_type == 'daily': |
142
|
|
|
current_datetime = current_datetime_local.isoformat()[0:10] |
143
|
|
|
elif period_type == 'weekly': |
144
|
|
|
current_datetime = current_datetime_local.isoformat()[0:10] |
145
|
|
|
elif period_type == 'monthly': |
146
|
|
|
current_datetime = current_datetime_local.isoformat()[0:7] |
147
|
|
|
elif period_type == 'yearly': |
148
|
|
|
current_datetime = current_datetime_local.isoformat()[0:4] |
149
|
|
|
|
150
|
|
|
actual_value = Decimal(0.0) if row_generation_periodically[1] is None else \ |
151
|
|
|
row_generation_periodically[1] |
152
|
|
|
timestamps.append(current_datetime) |
|
|
|
|
153
|
|
|
values.append(actual_value) |
154
|
|
|
reporting['generation_7_days']['timestamps_array'].append(timestamps) |
155
|
|
|
reporting['generation_7_days']['values_array'].append(values) |
156
|
|
|
for i in range(len(values)): |
157
|
|
|
if len(reporting['generation_7_days']['total_values']) <= i: |
158
|
|
|
reporting['generation_7_days']['total_values'].append(Decimal(0.0)) |
159
|
|
|
else: |
160
|
|
|
reporting['generation_7_days']['total_values'][i] += values[i] |
161
|
|
|
|
162
|
|
|
################################################################################################################ |
163
|
|
|
# Step 5: query generation energy data in this month |
164
|
|
|
################################################################################################################ |
165
|
|
|
end_datetime_utc = datetime.utcnow() |
166
|
|
|
end_datetime_local = datetime.utcnow() + timedelta(minutes=timezone_offset) |
167
|
|
|
period_type = 'daily' |
168
|
|
|
start_datetime_local = end_datetime_local.replace(day=1, hour=0, minute=0, second=0, microsecond=0) |
169
|
|
|
start_datetime_utc = start_datetime_local - timedelta(minutes=timezone_offset) |
170
|
|
|
print('start_datetime_local:' + start_datetime_local.isoformat()) |
171
|
|
|
print('end_datetime_local:' + end_datetime_local.isoformat()) |
172
|
|
|
print('start_datetime_utc:' + start_datetime_utc.isoformat()) |
173
|
|
|
print('end_datetime_utc:' + end_datetime_utc.isoformat()) |
174
|
|
|
|
175
|
|
|
reporting['generation_this_month'] = dict() |
176
|
|
|
reporting['generation_this_month']['timestamps_array'] = list() |
177
|
|
|
reporting['generation_this_month']['values_array'] = list() |
178
|
|
|
reporting['generation_this_month']['total_values'] = list() |
179
|
|
|
|
180
|
|
|
for photovoltaic_power_station in photovoltaic_power_station_list: |
181
|
|
|
timestamps = list() |
182
|
|
|
values = list() |
183
|
|
|
query = (" SELECT start_datetime_utc, actual_value " |
184
|
|
|
" FROM tbl_photovoltaic_power_station_generation_hourly " |
185
|
|
|
" WHERE photovoltaic_power_station_id = %s " |
186
|
|
|
" AND start_datetime_utc >= %s " |
187
|
|
|
" AND start_datetime_utc < %s " |
188
|
|
|
" ORDER BY start_datetime_utc ") |
189
|
|
|
cursor_energy_db.execute(query, (photovoltaic_power_station['id'], start_datetime_utc, end_datetime_utc)) |
190
|
|
|
rows_generation_hourly = cursor_energy_db.fetchall() |
191
|
|
|
|
192
|
|
|
rows_generation_periodically = utilities.aggregate_hourly_data_by_period(rows_generation_hourly, |
193
|
|
|
start_datetime_utc, |
194
|
|
|
end_datetime_utc, |
195
|
|
|
period_type) |
196
|
|
|
|
197
|
|
|
for row_generation_periodically in rows_generation_periodically: |
198
|
|
|
current_datetime_local = row_generation_periodically[0].replace(tzinfo=timezone.utc) + \ |
199
|
|
|
timedelta(minutes=timezone_offset) |
200
|
|
|
if period_type == 'hourly': |
201
|
|
|
current_datetime = current_datetime_local.isoformat()[0:19] |
202
|
|
|
elif period_type == 'daily': |
203
|
|
|
current_datetime = current_datetime_local.isoformat()[0:10] |
204
|
|
|
elif period_type == 'weekly': |
205
|
|
|
current_datetime = current_datetime_local.isoformat()[0:10] |
206
|
|
|
elif period_type == 'monthly': |
207
|
|
|
current_datetime = current_datetime_local.isoformat()[0:7] |
208
|
|
|
elif period_type == 'yearly': |
209
|
|
|
current_datetime = current_datetime_local.isoformat()[0:4] |
210
|
|
|
|
211
|
|
|
actual_value = Decimal(0.0) if row_generation_periodically[1] is None else \ |
212
|
|
|
row_generation_periodically[1] |
213
|
|
|
timestamps.append(current_datetime) |
214
|
|
|
values.append(actual_value) |
215
|
|
|
reporting['generation_this_month']['timestamps_array'].append(timestamps) |
216
|
|
|
reporting['generation_this_month']['values_array'].append(values) |
217
|
|
|
for i in range(len(values)): |
218
|
|
|
if len(reporting['generation_this_month']['total_values']) <= i: |
219
|
|
|
reporting['generation_this_month']['total_values'].append(Decimal(0.0)) |
220
|
|
|
else: |
221
|
|
|
reporting['generation_this_month']['total_values'][i] += values[i] |
222
|
|
|
|
223
|
|
|
################################################################################################################ |
224
|
|
|
# Step 7: query generation energy data in this year |
225
|
|
|
################################################################################################################ |
226
|
|
|
end_datetime_utc = datetime.utcnow() |
227
|
|
|
end_datetime_local = datetime.utcnow() + timedelta(minutes=timezone_offset) |
228
|
|
|
period_type = 'monthly' |
229
|
|
|
start_datetime_local = end_datetime_local.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0) |
230
|
|
|
start_datetime_utc = start_datetime_local - timedelta(minutes=timezone_offset) |
231
|
|
|
print('start_datetime_local:' + start_datetime_local.isoformat()) |
232
|
|
|
print('end_datetime_local:' + end_datetime_local.isoformat()) |
233
|
|
|
print('start_datetime_utc:' + start_datetime_utc.isoformat()) |
234
|
|
|
print('end_datetime_utc:' + end_datetime_utc.isoformat()) |
235
|
|
|
|
236
|
|
|
reporting['generation_this_year'] = dict() |
237
|
|
|
reporting['generation_this_year']['timestamps_array'] = list() |
238
|
|
|
reporting['generation_this_year']['values_array'] = list() |
239
|
|
|
reporting['generation_this_year']['total_values'] = list() |
240
|
|
|
|
241
|
|
|
for photovoltaic_power_station in photovoltaic_power_station_list: |
242
|
|
|
timestamps = list() |
243
|
|
|
values = list() |
244
|
|
|
query = (" SELECT start_datetime_utc, actual_value " |
245
|
|
|
" FROM tbl_photovoltaic_power_station_generation_hourly " |
246
|
|
|
" WHERE photovoltaic_power_station_id = %s " |
247
|
|
|
" AND start_datetime_utc >= %s " |
248
|
|
|
" AND start_datetime_utc < %s " |
249
|
|
|
" ORDER BY start_datetime_utc ") |
250
|
|
|
cursor_energy_db.execute(query, (photovoltaic_power_station['id'], start_datetime_utc, end_datetime_utc)) |
251
|
|
|
rows_generation_hourly = cursor_energy_db.fetchall() |
252
|
|
|
|
253
|
|
|
rows_generation_periodically = utilities.aggregate_hourly_data_by_period(rows_generation_hourly, |
254
|
|
|
start_datetime_utc, |
255
|
|
|
end_datetime_utc, |
256
|
|
|
period_type) |
257
|
|
|
for row_generation_periodically in rows_generation_periodically: |
258
|
|
|
current_datetime_local = row_generation_periodically[0].replace(tzinfo=timezone.utc) + \ |
259
|
|
|
timedelta(minutes=timezone_offset) |
260
|
|
|
if period_type == 'hourly': |
261
|
|
|
current_datetime = current_datetime_local.isoformat()[0:19] |
262
|
|
|
elif period_type == 'daily': |
263
|
|
|
current_datetime = current_datetime_local.isoformat()[0:10] |
264
|
|
|
elif period_type == 'weekly': |
265
|
|
|
current_datetime = current_datetime_local.isoformat()[0:10] |
266
|
|
|
elif period_type == 'monthly': |
267
|
|
|
current_datetime = current_datetime_local.isoformat()[0:7] |
268
|
|
|
elif period_type == 'yearly': |
269
|
|
|
current_datetime = current_datetime_local.isoformat()[0:4] |
270
|
|
|
|
271
|
|
|
actual_value = Decimal(0.0) if row_generation_periodically[1] is None else \ |
272
|
|
|
row_generation_periodically[1] |
273
|
|
|
timestamps.append(current_datetime) |
274
|
|
|
values.append(actual_value) |
275
|
|
|
reporting['generation_this_year']['timestamps_array'].append(timestamps) |
276
|
|
|
reporting['generation_this_year']['values_array'].append(values) |
277
|
|
|
for i in range(len(values)): |
278
|
|
|
if len(reporting['generation_this_year']['total_values']) <= i: |
279
|
|
|
reporting['generation_this_year']['total_values'].append(Decimal(0.0)) |
280
|
|
|
else: |
281
|
|
|
reporting['generation_this_year']['total_values'][i] += values[i] |
282
|
|
|
|
283
|
|
|
################################################################################################################ |
284
|
|
|
# Step 9: construct the report |
285
|
|
|
################################################################################################################ |
286
|
|
|
if cursor_system_db: |
287
|
|
|
cursor_system_db.close() |
288
|
|
|
if cnx_system_db: |
289
|
|
|
cnx_system_db.close() |
290
|
|
|
|
291
|
|
|
if cursor_energy_db: |
292
|
|
|
cursor_energy_db.close() |
293
|
|
|
if cnx_energy_db: |
294
|
|
|
cnx_energy_db.close() |
295
|
|
|
|
296
|
|
|
result = dict() |
297
|
|
|
result['photovoltaic_power_station_names'] = photovoltaic_power_station_names |
298
|
|
|
result['reporting'] = reporting |
299
|
|
|
resp.text = json.dumps(result) |
300
|
|
|
|