Code Duplication    Length = 114-114 lines in 2 locations

myems-api/core/energyflowdiagram.py 2 locations

@@ 1374-1487 (lines=114) @@
1371
        resp.status = falcon.HTTP_200
1372
        _ = id_
1373
1374
    @staticmethod
1375
    def on_get(req, resp, id_):
1376
        if 'API-KEY' not in req.headers or \
1377
                not isinstance(req.headers['API-KEY'], str) or \
1378
                len(str.strip(req.headers['API-KEY'])) == 0:
1379
            access_control(req)
1380
        else:
1381
            api_key_control(req)
1382
        if not id_.isdigit() or int(id_) <= 0:
1383
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1384
                                   description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID')
1385
1386
        cnx = mysql.connector.connect(**config.myems_system_db)
1387
        cursor = cnx.cursor()
1388
1389
        query = (" SELECT id, name, uuid "
1390
                 " FROM tbl_meters ")
1391
        cursor.execute(query)
1392
        rows_meters = cursor.fetchall()
1393
1394
        meter_dict = dict()
1395
        if rows_meters is not None and len(rows_meters) > 0:
1396
            for row in rows_meters:
1397
                meter_dict[row[2]] = {"type": 'meter',
1398
                                      "id": row[0],
1399
                                      "name": row[1],
1400
                                      "uuid": row[2]}
1401
1402
        query = (" SELECT id, name, uuid "
1403
                 " FROM tbl_offline_meters ")
1404
        cursor.execute(query)
1405
        rows_offline_meters = cursor.fetchall()
1406
1407
        offline_meter_dict = dict()
1408
        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
1409
            for row in rows_offline_meters:
1410
                offline_meter_dict[row[2]] = {"type": 'offline_meter',
1411
                                              "id": row[0],
1412
                                              "name": row[1],
1413
                                              "uuid": row[2]}
1414
1415
        query = (" SELECT id, name, uuid "
1416
                 " FROM tbl_virtual_meters ")
1417
        cursor.execute(query)
1418
        rows_virtual_meters = cursor.fetchall()
1419
1420
        virtual_meter_dict = dict()
1421
        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
1422
            for row in rows_virtual_meters:
1423
                virtual_meter_dict[row[2]] = {"type": 'virtual_meter',
1424
                                              "id": row[0],
1425
                                              "name": row[1],
1426
                                              "uuid": row[2]}
1427
1428
        query = (" SELECT id, energy_flow_diagram_id, name "
1429
                 " FROM tbl_energy_flow_diagrams_nodes")
1430
        cursor.execute(query)
1431
        rows_nodes = cursor.fetchall()
1432
1433
        node_dict = dict()
1434
        node_list_dict = dict()
1435
        if rows_nodes is not None and len(rows_nodes) > 0:
1436
            for row in rows_nodes:
1437
                node_dict[row[0]] = row[2]
1438
                if node_list_dict.get(row[1]) is None:
1439
                    node_list_dict[row[1]] = list()
1440
                node_list_dict[row[1]].append({"id": row[0], "name": row[2]})
1441
1442
        query = (" SELECT id, energy_flow_diagram_id, source_node_id, target_node_id, meter_uuid "
1443
                 " FROM tbl_energy_flow_diagrams_links")
1444
        cursor.execute(query)
1445
        rows_links = cursor.fetchall()
1446
1447
        link_list_dict = dict()
1448
        if rows_links is not None and len(rows_links) > 0:
1449
            for row in rows_links:
1450
                # find meter by uuid
1451
                meter = meter_dict.get(row[4], None)
1452
                if meter is None:
1453
                    meter = virtual_meter_dict.get(row[4], None)
1454
                if meter is None:
1455
                    meter = offline_meter_dict.get(row[4], None)
1456
1457
                if link_list_dict.get(row[1]) is None:
1458
                    link_list_dict[row[1]] = list()
1459
                link_list_dict[row[1]].append({"id": row[0],
1460
                                               "source_node": {
1461
                                                   "id": row[2],
1462
                                                   "name": node_dict.get(row[2])},
1463
                                               "target_node": {
1464
                                                   "id": row[3],
1465
                                                   "name": node_dict.get(row[3])},
1466
                                               "meter": meter})
1467
1468
        query = (" SELECT id, name, uuid "
1469
                 " FROM tbl_energy_flow_diagrams "
1470
                 " WHERE id = %s ")
1471
        cursor.execute(query, (id_,))
1472
        row = cursor.fetchone()
1473
        cursor.close()
1474
        cnx.close()
1475
1476
        if row is None:
1477
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1478
                                   description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND')
1479
        else:
1480
            meta_result = {"id": row[0],
1481
                           "name": row[1],
1482
                           "uuid": row[2],
1483
                           "nodes": node_list_dict.get(row[0], None),
1484
                           "links": link_list_dict.get(row[0], None),
1485
                           }
1486
1487
        resp.text = json.dumps(meta_result)
1488
1489
1490
class EnergyFlowDiagramImport:
@@ 258-371 (lines=114) @@
255
        resp.status = falcon.HTTP_200
256
        _ = id_
257
258
    @staticmethod
259
    def on_get(req, resp, id_):
260
        if 'API-KEY' not in req.headers or \
261
                not isinstance(req.headers['API-KEY'], str) or \
262
                len(str.strip(req.headers['API-KEY'])) == 0:
263
            access_control(req)
264
        else:
265
            api_key_control(req)
266
        if not id_.isdigit() or int(id_) <= 0:
267
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
268
                                   description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID')
269
270
        cnx = mysql.connector.connect(**config.myems_system_db)
271
        cursor = cnx.cursor()
272
273
        query = (" SELECT id, name, uuid "
274
                 " FROM tbl_meters ")
275
        cursor.execute(query)
276
        rows_meters = cursor.fetchall()
277
278
        meter_dict = dict()
279
        if rows_meters is not None and len(rows_meters) > 0:
280
            for row in rows_meters:
281
                meter_dict[row[2]] = {"type": 'meter',
282
                                      "id": row[0],
283
                                      "name": row[1],
284
                                      "uuid": row[2]}
285
286
        query = (" SELECT id, name, uuid "
287
                 " FROM tbl_offline_meters ")
288
        cursor.execute(query)
289
        rows_offline_meters = cursor.fetchall()
290
291
        offline_meter_dict = dict()
292
        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
293
            for row in rows_offline_meters:
294
                offline_meter_dict[row[2]] = {"type": 'offline_meter',
295
                                              "id": row[0],
296
                                              "name": row[1],
297
                                              "uuid": row[2]}
298
299
        query = (" SELECT id, name, uuid "
300
                 " FROM tbl_virtual_meters ")
301
        cursor.execute(query)
302
        rows_virtual_meters = cursor.fetchall()
303
304
        virtual_meter_dict = dict()
305
        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
306
            for row in rows_virtual_meters:
307
                virtual_meter_dict[row[2]] = {"type": 'virtual_meter',
308
                                              "id": row[0],
309
                                              "name": row[1],
310
                                              "uuid": row[2]}
311
312
        query = (" SELECT id, energy_flow_diagram_id, name "
313
                 " FROM tbl_energy_flow_diagrams_nodes")
314
        cursor.execute(query)
315
        rows_nodes = cursor.fetchall()
316
317
        node_dict = dict()
318
        node_list_dict = dict()
319
        if rows_nodes is not None and len(rows_nodes) > 0:
320
            for row in rows_nodes:
321
                node_dict[row[0]] = row[2]
322
                if node_list_dict.get(row[1]) is None:
323
                    node_list_dict[row[1]] = list()
324
                node_list_dict[row[1]].append({"id": row[0], "name": row[2]})
325
326
        query = (" SELECT id, energy_flow_diagram_id, source_node_id, target_node_id, meter_uuid "
327
                 " FROM tbl_energy_flow_diagrams_links")
328
        cursor.execute(query)
329
        rows_links = cursor.fetchall()
330
331
        link_list_dict = dict()
332
        if rows_links is not None and len(rows_links) > 0:
333
            for row in rows_links:
334
                # find meter by uuid
335
                meter = meter_dict.get(row[4], None)
336
                if meter is None:
337
                    meter = virtual_meter_dict.get(row[4], None)
338
                if meter is None:
339
                    meter = offline_meter_dict.get(row[4], None)
340
341
                if link_list_dict.get(row[1]) is None:
342
                    link_list_dict[row[1]] = list()
343
                link_list_dict[row[1]].append({"id": row[0],
344
                                               "source_node": {
345
                                                   "id": row[2],
346
                                                   "name": node_dict.get(row[2])},
347
                                               "target_node": {
348
                                                   "id": row[3],
349
                                                   "name": node_dict.get(row[3])},
350
                                               "meter": meter})
351
352
        query = (" SELECT id, name, uuid "
353
                 " FROM tbl_energy_flow_diagrams "
354
                 " WHERE id = %s ")
355
        cursor.execute(query, (id_,))
356
        row = cursor.fetchone()
357
        cursor.close()
358
        cnx.close()
359
360
        if row is None:
361
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
362
                                   description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND')
363
        else:
364
            meta_result = {"id": row[0],
365
                           "name": row[1],
366
                           "uuid": row[2],
367
                           "nodes": node_list_dict.get(row[0], None),
368
                           "links": link_list_dict.get(row[0], None),
369
                           }
370
371
        resp.text = json.dumps(meta_result)
372
373
    @staticmethod
374
    @user_logger