1
|
|
|
import falcon |
2
|
|
|
import simplejson as json |
3
|
|
|
import mysql.connector |
4
|
|
|
import config |
5
|
|
|
from anytree import Node, AnyNode, LevelOrderIter |
6
|
|
|
import excelexporters.metertracking |
7
|
|
|
|
8
|
|
|
|
9
|
|
View Code Duplication |
class Reporting: |
|
|
|
|
10
|
|
|
@staticmethod |
11
|
|
|
def __init__(): |
12
|
|
|
pass |
13
|
|
|
|
14
|
|
|
@staticmethod |
15
|
|
|
def on_options(req, resp): |
16
|
|
|
resp.status = falcon.HTTP_200 |
17
|
|
|
|
18
|
|
|
#################################################################################################################### |
19
|
|
|
# PROCEDURES |
20
|
|
|
# Step 1: valid parameters |
21
|
|
|
# Step 2: build a space tree |
22
|
|
|
# Step 3: query all meters in the space tree |
23
|
|
|
# Step 4: construct the report |
24
|
|
|
#################################################################################################################### |
25
|
|
|
@staticmethod |
26
|
|
|
def on_get(req, resp): |
27
|
|
|
print(req.params) |
28
|
|
|
space_id = req.params.get('spaceid') |
29
|
|
|
|
30
|
|
|
################################################################################################################ |
31
|
|
|
# Step 1: valid parameters |
32
|
|
|
################################################################################################################ |
33
|
|
|
if space_id is None: |
34
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SPACE_ID') |
35
|
|
|
else: |
36
|
|
|
space_id = str.strip(space_id) |
37
|
|
|
if not space_id.isdigit() or int(space_id) <= 0: |
38
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SPACE_ID') |
39
|
|
|
else: |
40
|
|
|
space_id = int(space_id) |
41
|
|
|
|
42
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
43
|
|
|
cursor = cnx.cursor(dictionary=True) |
44
|
|
|
|
45
|
|
|
cursor.execute(" SELECT name " |
46
|
|
|
" FROM tbl_spaces " |
47
|
|
|
" WHERE id = %s ", (space_id,)) |
48
|
|
|
row = cursor.fetchone() |
49
|
|
|
|
50
|
|
|
if row is None: |
51
|
|
|
if cursor: |
52
|
|
|
cursor.close() |
53
|
|
|
if cnx: |
54
|
|
|
cnx.disconnect() |
55
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
56
|
|
|
description='API.SPACE_NOT_FOUND') |
57
|
|
|
else: |
58
|
|
|
space_name = row['name'] |
59
|
|
|
|
60
|
|
|
################################################################################################################ |
61
|
|
|
# Step 2: build a space tree |
62
|
|
|
################################################################################################################ |
63
|
|
|
|
64
|
|
|
query = (" SELECT id, name, parent_space_id " |
65
|
|
|
" FROM tbl_spaces " |
66
|
|
|
" ORDER BY id ") |
67
|
|
|
cursor.execute(query) |
68
|
|
|
rows_spaces = cursor.fetchall() |
69
|
|
|
node_dict = dict() |
70
|
|
|
if rows_spaces is not None and len(rows_spaces) > 0: |
71
|
|
|
for row in rows_spaces: |
72
|
|
|
parent_node = node_dict[row['parent_space_id']] if row['parent_space_id'] is not None else None |
73
|
|
|
node_dict[row['id']] = AnyNode(id=row['id'], parent=parent_node, name=row['name']) |
74
|
|
|
|
75
|
|
|
################################################################################################################ |
76
|
|
|
# Step 3: query all meters in the space tree |
77
|
|
|
################################################################################################################ |
78
|
|
|
meter_list = list() |
79
|
|
|
space_dict = dict() |
80
|
|
|
|
81
|
|
|
for node in LevelOrderIter(node_dict[space_id]): |
82
|
|
|
space_dict[node.id] = node.name |
83
|
|
|
|
84
|
|
|
cursor.execute(" SELECT m.id, m.name AS meter_name, s.name AS space_name, " |
85
|
|
|
" cc.name AS cost_center_name, ec.name AS energy_category_name, " |
86
|
|
|
" m.description " |
87
|
|
|
" FROM tbl_spaces s, tbl_spaces_meters sm, tbl_meters m, tbl_cost_centers cc, " |
88
|
|
|
" tbl_energy_categories ec " |
89
|
|
|
" WHERE s.id IN ( " + ', '.join(map(str, space_dict.keys())) + ") " |
90
|
|
|
" AND sm.space_id = s.id AND sm.meter_id = m.id " |
91
|
|
|
" AND m.cost_center_id = cc.id AND m.energy_category_id = ec.id ", ) |
92
|
|
|
rows_meters = cursor.fetchall() |
93
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
94
|
|
|
for row in rows_meters: |
95
|
|
|
meter_list.append({"id": row['id'], |
96
|
|
|
"meter_name": row['meter_name'], |
97
|
|
|
"space_name": row['space_name'], |
98
|
|
|
"cost_center_name": row['cost_center_name'], |
99
|
|
|
"energy_category_name": row['energy_category_name'], |
100
|
|
|
"description": row['description']}) |
101
|
|
|
|
102
|
|
|
if cursor: |
103
|
|
|
cursor.close() |
104
|
|
|
if cnx: |
105
|
|
|
cnx.disconnect() |
106
|
|
|
|
107
|
|
|
################################################################################################################ |
108
|
|
|
# Step 4: construct the report |
109
|
|
|
################################################################################################################ |
110
|
|
|
result = {'meters': meter_list} |
111
|
|
|
# export result to Excel file and then encode the file to base64 string |
112
|
|
|
result['excel_bytes_base64'] = \ |
113
|
|
|
excelexporters.metertracking.export(result, |
114
|
|
|
space_name) |
115
|
|
|
resp.body = json.dumps(result) |
116
|
|
|
|