myems/myems-api/reports/equipmenttracking.py

115 lines
5.0 KiB
Python

import falcon
import simplejson as json
import mysql.connector
import config
from anytree import AnyNode, LevelOrderIter
import excelexporters.equipmenttracking
class Reporting:
@staticmethod
def __init__():
""" Initializes Reporting"""
pass
@staticmethod
def on_options(req, resp):
resp.status = falcon.HTTP_200
####################################################################################################################
# PROCEDURES
# Step 1: valid parameters
# Step 2: build a space tree
# Step 3: query all equipments in the space tree
# Step 4: construct the report
####################################################################################################################
@staticmethod
def on_get(req, resp):
print(req.params)
space_id = req.params.get('spaceid')
################################################################################################################
# Step 1: valid parameters
################################################################################################################
if space_id is None:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SPACE_ID')
else:
space_id = str.strip(space_id)
if not space_id.isdigit() or int(space_id) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SPACE_ID')
else:
space_id = int(space_id)
cnx = mysql.connector.connect(**config.myems_system_db)
cursor = cnx.cursor()
cursor.execute(" SELECT name "
" FROM tbl_spaces "
" WHERE id = %s ", (space_id,))
row = cursor.fetchone()
if row is None:
if cursor:
cursor.close()
if cnx:
cnx.close()
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
description='API.SPACE_NOT_FOUND')
else:
space_name = row[0]
################################################################################################################
# Step 2: build a space tree
################################################################################################################
query = (" SELECT id, name, parent_space_id "
" FROM tbl_spaces "
" ORDER BY id ")
cursor.execute(query)
rows_spaces = cursor.fetchall()
node_dict = dict()
if rows_spaces is not None and len(rows_spaces) > 0:
for row in rows_spaces:
parent_node = node_dict[row[2]] if row[2] is not None else None
node_dict[row[0]] = AnyNode(id=row[0], parent=parent_node, name=row[1])
################################################################################################################
# Step 3: query all equipments in the space tree
################################################################################################################
equipment_list = list()
space_dict = dict()
for node in LevelOrderIter(node_dict[space_id]):
space_dict[node.id] = node.name
cursor.execute(" SELECT e.id, e.name AS equipment_name, s.name AS space_name, "
" cc.name AS cost_center_name, e.description "
" FROM tbl_spaces s, tbl_spaces_equipments se, tbl_equipments e, tbl_cost_centers cc "
" WHERE s.id IN ( " + ', '.join(map(str, space_dict.keys())) + ") "
" AND se.space_id = s.id "
" AND se.equipment_id = e.id "
" AND e.cost_center_id = cc.id ", )
rows_equipments = cursor.fetchall()
if rows_equipments is not None and len(rows_equipments) > 0:
for row in rows_equipments:
equipment_list.append({"id": row[0],
"equipment_name": row[1],
"space_name": row[2],
"cost_center_name": row[3],
"description": row[4]})
if cursor:
cursor.close()
if cnx:
cnx.close()
################################################################################################################
# Step 4: construct the report
################################################################################################################
result = {'equipments': equipment_list}
# export result to Excel file and then encode the file to base64 string
result['excel_bytes_base64'] = \
excelexporters.equipmenttracking.export(result,
space_name)
resp.text = json.dumps(result)