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