Code Duplication    Length = 127-127 lines in 2 locations

myems-api/core/virtualmeter.py 2 locations

@@ 884-1010 (lines=127) @@
881
    def on_options(req, resp, id_):
882
        resp.status = falcon.HTTP_200
883
884
    @staticmethod
885
    def on_get(req, resp, id_):
886
        if 'API-KEY' not in req.headers or \
887
                not isinstance(req.headers['API-KEY'], str) or \
888
                len(str.strip(req.headers['API-KEY'])) == 0:
889
            access_control(req)
890
        else:
891
            api_key_control(req)
892
        if not id_.isdigit() or int(id_) <= 0:
893
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
894
                                   description='API.INVALID_VIRTUAL_METER_ID')
895
896
        cnx = mysql.connector.connect(**config.myems_system_db)
897
        cursor = cnx.cursor()
898
899
        query = (" SELECT id, name, uuid "
900
                 " FROM tbl_energy_categories ")
901
        cursor.execute(query)
902
        rows_energy_categories = cursor.fetchall()
903
904
        energy_category_dict = dict()
905
        if rows_energy_categories is not None and len(rows_energy_categories) > 0:
906
            for row in rows_energy_categories:
907
                energy_category_dict[row[0]] = {"id": row[0],
908
                                                "name": row[1],
909
                                                "uuid": row[2]}
910
911
        query = (" SELECT id, name, uuid, energy_category_id "
912
                 " FROM tbl_energy_items ")
913
        cursor.execute(query)
914
        rows_energy_items = cursor.fetchall()
915
916
        energy_item_dict = dict()
917
        if rows_energy_items is not None and len(rows_energy_items) > 0:
918
            for row in rows_energy_items:
919
                energy_item_dict[row[0]] = {"id": row[0],
920
                                            "name": row[1],
921
                                            "uuid": row[2]}
922
923
        query = (" SELECT id, name, uuid "
924
                 " FROM tbl_cost_centers ")
925
        cursor.execute(query)
926
        rows_cost_centers = cursor.fetchall()
927
928
        cost_center_dict = dict()
929
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
930
            for row in rows_cost_centers:
931
                cost_center_dict[row[0]] = {"id": row[0],
932
                                            "name": row[1],
933
                                            "uuid": row[2]}
934
935
        query = (" SELECT id, name, uuid, equation, energy_category_id, is_counted, cost_center_id, "
936
                 "        energy_item_id, description "
937
                 " FROM tbl_virtual_meters "
938
                 " WHERE id = %s ")
939
        cursor.execute(query, (id_,))
940
        row = cursor.fetchone()
941
        if row is None:
942
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
943
                                   description='API.VIRTUAL_METER_NOT_FOUND')
944
        else:
945
            meta_result = {"id": row[0],
946
                           "name": row[1],
947
                           "uuid": row[2],
948
                           "equation": row[3],
949
                           "energy_category": energy_category_dict.get(row[4], None),
950
                           "is_counted": True if row[5] else False,
951
                           "cost_center": cost_center_dict.get(row[6], None),
952
                           "energy_item": energy_item_dict.get(row[7], None),
953
                           "description": row[8],
954
                           "expression": {}}
955
956
        expression = dict()
957
958
        if meta_result["equation"] is not None:
959
            expression = {'equation': meta_result["equation"], 'variables': []}
960
961
            query_variables = (" SELECT v.id, v.name, v.meter_type, v.meter_id "
962
                               " FROM tbl_virtual_meters  vm, tbl_variables v "
963
                               " WHERE vm.id = %s AND v.virtual_meter_id = vm.id "
964
                               " ORDER BY v.name ")
965
            cursor.execute(query_variables, (meta_result["id"],))
966
            rows_variables = cursor.fetchall()
967
            if rows_variables is not None:
968
                for row_variable in rows_variables:
969
                    if row_variable[2].lower() == 'meter':
970
                        query_meter = (" SELECT m.name "
971
                                       " FROM tbl_meters m "
972
                                       " WHERE m.id = %s ")
973
                        cursor.execute(query_meter, (row_variable[3],))
974
                        row_meter = cursor.fetchone()
975
                        if row_meter is not None:
976
                            expression['variables'].append({'id': row_variable[0],
977
                                                            'name': row_variable[1],
978
                                                            'meter_type': row_variable[2],
979
                                                            'meter_id': row_variable[3],
980
                                                            'meter_name': row_meter[0]})
981
                    elif row_variable[2].lower() == 'offline_meter':
982
                        query_meter = (" SELECT m.name "
983
                                       " FROM tbl_offline_meters m "
984
                                       " WHERE m.id = %s ")
985
                        cursor.execute(query_meter, (row_variable[3],))
986
                        row_meter = cursor.fetchone()
987
                        if row_meter is not None:
988
                            expression['variables'].append({'id': row_variable[0],
989
                                                            'name': row_variable[1],
990
                                                            'meter_type': row_variable[2],
991
                                                            'meter_id': row_variable[3],
992
                                                            'meter_name': row_meter[0]})
993
                    elif row_variable[2].lower() == 'virtual_meter':
994
                        query_meter = (" SELECT m.name "
995
                                       " FROM tbl_virtual_meters m "
996
                                       " WHERE m.id = %s ")
997
                        cursor.execute(query_meter, (row_variable[3],))
998
                        row_meter = cursor.fetchone()
999
                        if row_meter is not None:
1000
                            expression['variables'].append({'id': row_variable[0],
1001
                                                            'name': row_variable[1],
1002
                                                            'meter_type': row_variable[2],
1003
                                                            'meter_id': row_variable[3],
1004
                                                            'meter_name': row_meter[0]})
1005
1006
        meta_result['expression'] = expression
1007
1008
        cursor.close()
1009
        cnx.close()
1010
        resp.text = json.dumps(meta_result)
1011
1012
1013
class VirtualMeterImport:
@@ 354-480 (lines=127) @@
351
    def on_options(req, resp, id_):
352
        resp.status = falcon.HTTP_200
353
354
    @staticmethod
355
    def on_get(req, resp, id_):
356
        if 'API-KEY' not in req.headers or \
357
                not isinstance(req.headers['API-KEY'], str) or \
358
                len(str.strip(req.headers['API-KEY'])) == 0:
359
            access_control(req)
360
        else:
361
            api_key_control(req)
362
        if not id_.isdigit() or int(id_) <= 0:
363
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
364
                                   description='API.INVALID_VIRTUAL_METER_ID')
365
366
        cnx = mysql.connector.connect(**config.myems_system_db)
367
        cursor = cnx.cursor()
368
369
        query = (" SELECT id, name, uuid "
370
                 " FROM tbl_energy_categories ")
371
        cursor.execute(query)
372
        rows_energy_categories = cursor.fetchall()
373
374
        energy_category_dict = dict()
375
        if rows_energy_categories is not None and len(rows_energy_categories) > 0:
376
            for row in rows_energy_categories:
377
                energy_category_dict[row[0]] = {"id": row[0],
378
                                                "name": row[1],
379
                                                "uuid": row[2]}
380
381
        query = (" SELECT id, name, uuid, energy_category_id "
382
                 " FROM tbl_energy_items ")
383
        cursor.execute(query)
384
        rows_energy_items = cursor.fetchall()
385
386
        energy_item_dict = dict()
387
        if rows_energy_items is not None and len(rows_energy_items) > 0:
388
            for row in rows_energy_items:
389
                energy_item_dict[row[0]] = {"id": row[0],
390
                                            "name": row[1],
391
                                            "uuid": row[2]}
392
393
        query = (" SELECT id, name, uuid "
394
                 " FROM tbl_cost_centers ")
395
        cursor.execute(query)
396
        rows_cost_centers = cursor.fetchall()
397
398
        cost_center_dict = dict()
399
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
400
            for row in rows_cost_centers:
401
                cost_center_dict[row[0]] = {"id": row[0],
402
                                            "name": row[1],
403
                                            "uuid": row[2]}
404
405
        query = (" SELECT id, name, uuid, equation, energy_category_id, is_counted, cost_center_id, "
406
                 "        energy_item_id, description "
407
                 " FROM tbl_virtual_meters "
408
                 " WHERE id = %s ")
409
        cursor.execute(query, (id_,))
410
        row = cursor.fetchone()
411
        if row is None:
412
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
413
                                   description='API.VIRTUAL_METER_NOT_FOUND')
414
        else:
415
            meta_result = {"id": row[0],
416
                           "name": row[1],
417
                           "uuid": row[2],
418
                           "equation": row[3],
419
                           "energy_category": energy_category_dict.get(row[4], None),
420
                           "is_counted": True if row[5] else False,
421
                           "cost_center": cost_center_dict.get(row[6], None),
422
                           "energy_item": energy_item_dict.get(row[7], None),
423
                           "description": row[8],
424
                           "expression": {}}
425
426
        expression = dict()
427
428
        if meta_result["equation"] is not None:
429
            expression = {'equation': meta_result["equation"], 'variables': []}
430
431
            query_variables = (" SELECT v.id, v.name, v.meter_type, v.meter_id "
432
                               " FROM tbl_virtual_meters  vm, tbl_variables v "
433
                               " WHERE vm.id = %s AND v.virtual_meter_id = vm.id "
434
                               " ORDER BY v.name ")
435
            cursor.execute(query_variables, (meta_result["id"],))
436
            rows_variables = cursor.fetchall()
437
            if rows_variables is not None:
438
                for row_variable in rows_variables:
439
                    if row_variable[2].lower() == 'meter':
440
                        query_meter = (" SELECT m.name "
441
                                       " FROM tbl_meters m "
442
                                       " WHERE m.id = %s ")
443
                        cursor.execute(query_meter, (row_variable[3],))
444
                        row_meter = cursor.fetchone()
445
                        if row_meter is not None:
446
                            expression['variables'].append({'id': row_variable[0],
447
                                                            'name': row_variable[1],
448
                                                            'meter_type': row_variable[2],
449
                                                            'meter_id': row_variable[3],
450
                                                            'meter_name': row_meter[0]})
451
                    elif row_variable[2].lower() == 'offline_meter':
452
                        query_meter = (" SELECT m.name "
453
                                       " FROM tbl_offline_meters m "
454
                                       " WHERE m.id = %s ")
455
                        cursor.execute(query_meter, (row_variable[3],))
456
                        row_meter = cursor.fetchone()
457
                        if row_meter is not None:
458
                            expression['variables'].append({'id': row_variable[0],
459
                                                            'name': row_variable[1],
460
                                                            'meter_type': row_variable[2],
461
                                                            'meter_id': row_variable[3],
462
                                                            'meter_name': row_meter[0]})
463
                    elif row_variable[2].lower() == 'virtual_meter':
464
                        query_meter = (" SELECT m.name "
465
                                       " FROM tbl_virtual_meters m "
466
                                       " WHERE m.id = %s ")
467
                        cursor.execute(query_meter, (row_variable[3],))
468
                        row_meter = cursor.fetchone()
469
                        if row_meter is not None:
470
                            expression['variables'].append({'id': row_variable[0],
471
                                                            'name': row_variable[1],
472
                                                            'meter_type': row_variable[2],
473
                                                            'meter_id': row_variable[3],
474
                                                            'meter_name': row_meter[0]})
475
476
        meta_result['expression'] = expression
477
478
        cursor.close()
479
        cnx.close()
480
        resp.text = json.dumps(meta_result)
481
482
    @staticmethod
483
    @user_logger