| @@ 9-115 (lines=107) @@ | ||
| 6 | import excelexporters.metertracking |
|
| 7 | ||
| 8 | ||
| 9 | 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 | ||
| @@ 9-113 (lines=105) @@ | ||
| 6 | import excelexporters.equipmenttracking |
|
| 7 | ||
| 8 | ||
| 9 | 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 | ||