Code Duplication    Length = 45-55 lines in 2 locations

datasource.py 1 location

@@ 319-373 (lines=55) @@
316
        resp.status = falcon.HTTP_200
317
318
319
class DataSourcePointCollection:
320
    @staticmethod
321
    def __init__():
322
        pass
323
324
    @staticmethod
325
    def on_options(req, resp):
326
        resp.status = falcon.HTTP_200
327
328
    @staticmethod
329
    def on_get(req, resp, id_):
330
        if not id_.isdigit() or int(id_) <= 0:
331
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
332
                                   description='API.INVALID_DATA_SOURCE_ID')
333
334
        cnx = mysql.connector.connect(**config.myems_system_db)
335
        cursor = cnx.cursor()
336
337
        cursor.execute(" SELECT name "
338
                       " FROM tbl_data_sources "
339
                       " WHERE id = %s ", (id_,))
340
        if cursor.fetchone() is None:
341
            cursor.close()
342
            cnx.disconnect()
343
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
344
                                   description='API.DATA_SOURCE_NOT_FOUND')
345
346
        result = list()
347
        # Get points of the data source
348
        # NOTE: there is no uuid in tbl_points
349
        query_point = (" SELECT id, name, object_type, "
350
                       "        units, high_limit, low_limit, ratio, is_trend, address, description "
351
                       " FROM tbl_points "
352
                       " WHERE data_source_id = %s "
353
                       " ORDER BY name ")
354
        cursor.execute(query_point, (id_,))
355
        rows_point = cursor.fetchall()
356
357
        if rows_point is not None and len(rows_point) > 0:
358
            for row in rows_point:
359
                meta_result = {"id": row[0],
360
                               "name": row[1],
361
                               "object_type": row[2],
362
                               "units": row[3],
363
                               "high_limit": row[4],
364
                               "low_limit": row[5],
365
                               "ratio": float(row[6]),
366
                               "is_trend": True if row[7] else False,
367
                               "address": row[8],
368
                               "description": row[9]}
369
                result.append(meta_result)
370
371
        cursor.close()
372
        cnx.disconnect()
373
        resp.body = json.dumps(result)
374

distributionsystem.py 1 location

@@ 242-286 (lines=45) @@
239
        resp.status = falcon.HTTP_200
240
241
242
class DistributionSystemDistributionCircuitCollection:
243
    @staticmethod
244
    def __init__():
245
        pass
246
247
    @staticmethod
248
    def on_options(req, resp, id_):
249
        resp.status = falcon.HTTP_200
250
251
    @staticmethod
252
    def on_get(req, resp, id_):
253
        if not id_.isdigit() or int(id_) <= 0:
254
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
255
                                   description='API.INVALID_DISTRIBUTION_SYSTEM_ID')
256
257
        cnx = mysql.connector.connect(**config.myems_system_db)
258
        cursor = cnx.cursor(dictionary=True)
259
260
        cursor.execute(" SELECT name "
261
                       " FROM tbl_distribution_systems "
262
                       " WHERE id = %s ", (id_,))
263
        if cursor.fetchone() is None:
264
            cursor.close()
265
            cnx.disconnect()
266
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
267
                                   description='API.DISTRIBUTION_SYSTEM_NOT_FOUND')
268
269
        query = (" SELECT id, name, uuid, "
270
                 "        distribution_room, switchgear, peak_load, peak_current, customers, meters "
271
                 " FROM tbl_distribution_circuits "
272
                 " WHERE distribution_system_id = %s "
273
                 " ORDER BY name ")
274
        cursor.execute(query, (id_,))
275
        rows = cursor.fetchall()
276
277
        result = list()
278
        if rows is not None and len(rows) > 0:
279
            for row in rows:
280
                meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'],
281
                               "distribution_room": row['distribution_room'], "switchgear": row['switchgear'],
282
                               "peak_load": row['peak_load'], "peak_current": row['peak_current'],
283
                               "customers": row['customers'], "meters": row['meters']}
284
                result.append(meta_result)
285
286
        resp.body = json.dumps(result)
287