|
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.equipmenttracking |
|
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 equipments 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
|
|
|
# Step 2: build a space tree |
|
61
|
|
|
################################################################################################################ |
|
62
|
|
|
|
|
63
|
|
|
query = (" SELECT id, name, parent_space_id " |
|
64
|
|
|
" FROM tbl_spaces " |
|
65
|
|
|
" ORDER BY id ") |
|
66
|
|
|
cursor.execute(query) |
|
67
|
|
|
rows_spaces = cursor.fetchall() |
|
68
|
|
|
node_dict = dict() |
|
69
|
|
|
if rows_spaces is not None and len(rows_spaces) > 0: |
|
70
|
|
|
for row in rows_spaces: |
|
71
|
|
|
parent_node = node_dict[row['parent_space_id']] if row['parent_space_id'] is not None else None |
|
72
|
|
|
node_dict[row['id']] = AnyNode(id=row['id'], parent=parent_node, name=row['name']) |
|
73
|
|
|
|
|
74
|
|
|
################################################################################################################ |
|
75
|
|
|
# Step 3: query all equipments in the space tree |
|
76
|
|
|
################################################################################################################ |
|
77
|
|
|
equipment_list = list() |
|
78
|
|
|
space_dict = dict() |
|
79
|
|
|
|
|
80
|
|
|
for node in LevelOrderIter(node_dict[space_id]): |
|
81
|
|
|
space_dict[node.id] = node.name |
|
82
|
|
|
|
|
83
|
|
|
cursor.execute(" SELECT e.id, e.name AS equipment_name, s.name AS space_name, " |
|
84
|
|
|
" cc.name AS cost_center_name, e.description " |
|
85
|
|
|
" FROM tbl_spaces s, tbl_spaces_equipments se, tbl_equipments e, tbl_cost_centers cc " |
|
86
|
|
|
" WHERE s.id IN ( " + ', '.join(map(str, space_dict.keys())) + ") " |
|
87
|
|
|
" AND se.space_id = s.id " |
|
88
|
|
|
" AND se.equipment_id = e.id " |
|
89
|
|
|
" AND e.cost_center_id = cc.id ", ) |
|
90
|
|
|
rows_equipments = cursor.fetchall() |
|
91
|
|
|
if rows_equipments is not None and len(rows_equipments) > 0: |
|
92
|
|
|
for row in rows_equipments: |
|
93
|
|
|
equipment_list.append({"id": row['id'], |
|
94
|
|
|
"equipment_name": row['equipment_name'], |
|
95
|
|
|
"space_name": row['space_name'], |
|
96
|
|
|
"cost_center_name": row['cost_center_name'], |
|
97
|
|
|
"description": row['description']}) |
|
98
|
|
|
|
|
99
|
|
|
if cursor: |
|
100
|
|
|
cursor.close() |
|
101
|
|
|
if cnx: |
|
102
|
|
|
cnx.disconnect() |
|
103
|
|
|
|
|
104
|
|
|
################################################################################################################ |
|
105
|
|
|
# Step 4: construct the report |
|
106
|
|
|
################################################################################################################ |
|
107
|
|
|
result = {'equipments': equipment_list} |
|
108
|
|
|
|
|
109
|
|
|
# export result to Excel file and then encode the file to base64 string |
|
110
|
|
|
result['excel_bytes_base64'] = \ |
|
111
|
|
|
excelexporters.equipmenttracking.export(result, |
|
112
|
|
|
space_name) |
|
113
|
|
|
resp.body = json.dumps(result) |
|
114
|
|
|
|