| Conditions | 15 |
| Total Lines | 81 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like metertracking.Reporting.on_get() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import falcon |
||
| 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 | cursor.close() |
||
| 49 | cnx.disconnect() |
||
| 50 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 51 | description='API.SPACE_NOT_FOUND') |
||
| 52 | |||
| 53 | ################################################################################################################ |
||
| 54 | # Step 2: build a space tree |
||
| 55 | ################################################################################################################ |
||
| 56 | |||
| 57 | query = (" SELECT id, name, parent_space_id " |
||
| 58 | " FROM tbl_spaces " |
||
| 59 | " ORDER BY id ") |
||
| 60 | cursor.execute(query) |
||
| 61 | rows_spaces = cursor.fetchall() |
||
| 62 | node_dict = dict() |
||
| 63 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 64 | for row in rows_spaces: |
||
| 65 | parent_node = node_dict[row['parent_space_id']] if row['parent_space_id'] is not None else None |
||
| 66 | node_dict[row['id']] = AnyNode(id=row['id'], parent=parent_node, name=row['name']) |
||
| 67 | print(node_dict) |
||
| 68 | ################################################################################################################ |
||
| 69 | # Step 3: query all meters in the space tree |
||
| 70 | ################################################################################################################ |
||
| 71 | meter_list = list() |
||
| 72 | sub_space_dict = dict() |
||
| 73 | |||
| 74 | for node in LevelOrderIter(node_dict[space_id]): |
||
| 75 | sub_space_dict[node.id] = node.name |
||
| 76 | |||
| 77 | cursor.execute(" SELECT m.id, m.name AS meter_name, s.name AS space_name, " |
||
| 78 | " cc.name AS cost_center_name, ec.name AS energy_category_name, " |
||
| 79 | " m.description " |
||
| 80 | " FROM tbl_spaces s, tbl_spaces_meters sm, tbl_meters m, tbl_cost_centers cc, " |
||
| 81 | " tbl_energy_categories ec " |
||
| 82 | " WHERE s.id IN ( " + ', '.join(map(str, sub_space_dict.keys())) + ") " |
||
| 83 | " AND sm.space_id = s.id AND sm.meter_id = m.id " |
||
| 84 | " AND m.cost_center_id = cc.id AND m.energy_category_id = ec.id ", ) |
||
| 85 | rows_meters = cursor.fetchall() |
||
| 86 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 87 | for row in rows_meters: |
||
| 88 | meter_list.append({"id": row['id'], |
||
| 89 | "meter_name": row['meter_name'], |
||
| 90 | "space_name": row['space_name'], |
||
| 91 | "cost_center_name": row['cost_center_name'], |
||
| 92 | "energy_category_name": row['energy_category_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(meter_list) |
||
| 105 |