Code Duplication    Length = 127-140 lines in 3 locations

myems-api/core/combinedequipment.py 2 locations

@@ 2848-2987 (lines=140) @@
2845
                           " (combined_equipment_id, virtual_meter_id, is_output ) "
2846
                           " VALUES (%s, %s, %s) ")
2847
                cursor.execute(add_row, (new_id, virtual_meter['id'], virtual_meter['is_output']))
2848
        if new_values['parameters'] is not None and len(new_values['parameters']) > 0:
2849
            for parameters in new_values['parameters']:
2850
                cursor.execute(" SELECT name "
2851
                               " FROM tbl_combined_equipments_parameters "
2852
                               " WHERE name = %s AND combined_equipment_id = %s ", (parameters['name'], new_id))
2853
                if cursor.fetchone() is not None:
2854
                    cursor.close()
2855
                    cnx.close()
2856
                    raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2857
                                           description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE')
2858
                if 'point' in parameters:
2859
                    if parameters['point'] is None:
2860
                        point_id = None
2861
                    elif parameters['point']['id'] is not None and \
2862
                            parameters['point']['id'] <= 0:
2863
                        raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2864
                                               description='API.INVALID_POINT_ID')
2865
                    else:
2866
                        point_id = parameters['point']['id']
2867
                else:
2868
                    point_id = None
2869
                numerator_meter_uuid = None
2870
                if 'numerator_meter' in parameters:
2871
                    if parameters['numerator_meter'] is not None and \
2872
                            isinstance(parameters['numerator_meter']['uuid'], str) and \
2873
                            len(str.strip(parameters['numerator_meter']['uuid'])) > 0:
2874
                        numerator_meter_uuid = str.strip(parameters['numerator_meter']['uuid'])
2875
2876
                denominator_meter_uuid = None
2877
                if 'denominator_meter' in parameters:
2878
                    if parameters['denominator_meter'] is not None and \
2879
                            isinstance(parameters['denominator_meter']['uuid'], str) and \
2880
                            len(str.strip(parameters['denominator_meter']['uuid'])) > 0:
2881
                        denominator_meter_uuid = str.strip(parameters['denominator_meter']['uuid'])
2882
2883
                # validate by parameter type
2884
                if parameters['parameter_type'] == 'point':
2885
                    if point_id is None:
2886
                        cursor.close()
2887
                        cnx.close()
2888
                        raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2889
                                               description='API.INVALID_POINT_ID')
2890
2891
                    query = (" SELECT id, name "
2892
                             " FROM tbl_points "
2893
                             " WHERE id = %s ")
2894
                    cursor.execute(query, (point_id,))
2895
                    if cursor.fetchone() is None:
2896
                        cursor.close()
2897
                        cnx.close()
2898
                        raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2899
                                               description='API.POINT_NOT_FOUND')
2900
2901
                elif parameters['parameter_type'] == 'constant':
2902
                    if parameters['constant'] is None:
2903
                        cursor.close()
2904
                        cnx.close()
2905
                        raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2906
                                               description='API.INVALID_CONSTANT_VALUE')
2907
2908
                elif parameters['parameter_type'] == 'fraction':
2909
2910
                    query = (" SELECT id, name, uuid "
2911
                             " FROM tbl_meters ")
2912
                    cursor.execute(query)
2913
                    rows_meters = cursor.fetchall()
2914
2915
                    meter_dict = dict()
2916
                    if rows_meters is not None and len(rows_meters) > 0:
2917
                        for row in rows_meters:
2918
                            meter_dict[row[2]] = {"type": 'meter',
2919
                                                  "id": row[0],
2920
                                                  "name": row[1],
2921
                                                  "uuid": row[2]}
2922
2923
                    query = (" SELECT id, name, uuid "
2924
                             " FROM tbl_offline_meters ")
2925
                    cursor.execute(query)
2926
                    rows_offline_meters = cursor.fetchall()
2927
2928
                    offline_meter_dict = dict()
2929
                    if rows_offline_meters is not None and len(rows_offline_meters) > 0:
2930
                        for row in rows_offline_meters:
2931
                            offline_meter_dict[row[2]] = {"type": 'offline_meter',
2932
                                                          "id": row[0],
2933
                                                          "name": row[1],
2934
                                                          "uuid": row[2]}
2935
2936
                    query = (" SELECT id, name, uuid "
2937
                             " FROM tbl_virtual_meters ")
2938
                    cursor.execute(query)
2939
                    rows_virtual_meters = cursor.fetchall()
2940
2941
                    virtual_meter_dict = dict()
2942
                    if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
2943
                        for row in rows_virtual_meters:
2944
                            virtual_meter_dict[row[2]] = {"type": 'virtual_meter',
2945
                                                          "id": row[0],
2946
                                                          "name": row[1],
2947
                                                          "uuid": row[2]}
2948
2949
                    # validate numerator meter uuid
2950
                    if meter_dict.get(numerator_meter_uuid) is None and \
2951
                            virtual_meter_dict.get(numerator_meter_uuid) is None and \
2952
                            offline_meter_dict.get(numerator_meter_uuid) is None:
2953
                        cursor.close()
2954
                        cnx.close()
2955
                        raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2956
                                               description='API.INVALID_NUMERATOR_METER_UUID')
2957
2958
                    # validate denominator meter uuid
2959
                    if denominator_meter_uuid is None:
2960
                        raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2961
                                               description='API.INVALID_DENOMINATOR_METER_UUID')
2962
2963
                    if denominator_meter_uuid == numerator_meter_uuid:
2964
                        cursor.close()
2965
                        cnx.close()
2966
                        raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2967
                                               description='API.INVALID_DENOMINATOR_METER_UUID')
2968
2969
                    if denominator_meter_uuid not in meter_dict and \
2970
                            denominator_meter_uuid not in virtual_meter_dict and \
2971
                            denominator_meter_uuid not in offline_meter_dict:
2972
                        cursor.close()
2973
                        cnx.close()
2974
                        raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2975
                                               description='API.INVALID_DENOMINATOR_METER_UUID')
2976
2977
                add_values = (" INSERT INTO tbl_combined_equipments_parameters "
2978
                              "    (combined_equipment_id, name, parameter_type, constant, "
2979
                              "     point_id, numerator_meter_uuid, denominator_meter_uuid) "
2980
                              " VALUES (%s, %s, %s, %s, %s, %s, %s) ")
2981
                cursor.execute(add_values, (new_id,
2982
                                            parameters['name'],
2983
                                            parameters['parameter_type'],
2984
                                            parameters['constant'],
2985
                                            point_id,
2986
                                            numerator_meter_uuid,
2987
                                            denominator_meter_uuid))
2988
        cnx.commit()
2989
        cursor.close()
2990
        cnx.close()
@@ 3412-3551 (lines=140) @@
3409
                               " (combined_equipment_id, virtual_meter_id, is_output ) "
3410
                               " VALUES (%s, %s, %s) ")
3411
                    cursor.execute(add_row, (new_id, virtual_meter['id'], virtual_meter['is_output']))
3412
            if meta_result['parameters'] is not None and len(meta_result['parameters']) > 0:
3413
                for parameters in meta_result['parameters']:
3414
                    cursor.execute(" SELECT name "
3415
                                   " FROM tbl_combined_equipments_parameters "
3416
                                   " WHERE name = %s AND combined_equipment_id = %s ", (parameters['name'], new_id))
3417
                    if cursor.fetchone() is not None:
3418
                        cursor.close()
3419
                        cnx.close()
3420
                        raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3421
                                               description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE')
3422
                    if 'point' in parameters:
3423
                        if parameters['point'] is None:
3424
                            point_id = None
3425
                        elif parameters['point']['id'] is not None and \
3426
                                parameters['point']['id'] <= 0:
3427
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3428
                                                   description='API.INVALID_POINT_ID')
3429
                        else:
3430
                            point_id = parameters['point']['id']
3431
                    else:
3432
                        point_id = None
3433
                    numerator_meter_uuid = None
3434
                    if 'numerator_meter' in parameters:
3435
                        if parameters['numerator_meter'] is not None and \
3436
                                isinstance(parameters['numerator_meter']['uuid'], str) and \
3437
                                len(str.strip(parameters['numerator_meter']['uuid'])) > 0:
3438
                            numerator_meter_uuid = str.strip(parameters['numerator_meter']['uuid'])
3439
3440
                    denominator_meter_uuid = None
3441
                    if 'denominator_meter' in parameters:
3442
                        if parameters['denominator_meter'] is not None and \
3443
                                isinstance(parameters['denominator_meter']['uuid'], str) and \
3444
                                len(str.strip(parameters['denominator_meter']['uuid'])) > 0:
3445
                            denominator_meter_uuid = str.strip(parameters['denominator_meter']['uuid'])
3446
3447
                    # validate by parameter type
3448
                    if parameters['parameter_type'] == 'point':
3449
                        if point_id is None:
3450
                            cursor.close()
3451
                            cnx.close()
3452
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3453
                                                   description='API.INVALID_POINT_ID')
3454
3455
                        query = (" SELECT id, name "
3456
                                 " FROM tbl_points "
3457
                                 " WHERE id = %s ")
3458
                        cursor.execute(query, (point_id,))
3459
                        if cursor.fetchone() is None:
3460
                            cursor.close()
3461
                            cnx.close()
3462
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3463
                                                   description='API.POINT_NOT_FOUND')
3464
3465
                    elif parameters['parameter_type'] == 'constant':
3466
                        if parameters['constant'] is None:
3467
                            cursor.close()
3468
                            cnx.close()
3469
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3470
                                                   description='API.INVALID_CONSTANT_VALUE')
3471
3472
                    elif parameters['parameter_type'] == 'fraction':
3473
3474
                        query = (" SELECT id, name, uuid "
3475
                                 " FROM tbl_meters ")
3476
                        cursor.execute(query)
3477
                        rows_meters = cursor.fetchall()
3478
3479
                        meter_dict = dict()
3480
                        if rows_meters is not None and len(rows_meters) > 0:
3481
                            for row in rows_meters:
3482
                                meter_dict[row[2]] = {"type": 'meter',
3483
                                                      "id": row[0],
3484
                                                      "name": row[1],
3485
                                                      "uuid": row[2]}
3486
3487
                        query = (" SELECT id, name, uuid "
3488
                                 " FROM tbl_offline_meters ")
3489
                        cursor.execute(query)
3490
                        rows_offline_meters = cursor.fetchall()
3491
3492
                        offline_meter_dict = dict()
3493
                        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
3494
                            for row in rows_offline_meters:
3495
                                offline_meter_dict[row[2]] = {"type": 'offline_meter',
3496
                                                              "id": row[0],
3497
                                                              "name": row[1],
3498
                                                              "uuid": row[2]}
3499
3500
                        query = (" SELECT id, name, uuid "
3501
                                 " FROM tbl_virtual_meters ")
3502
                        cursor.execute(query)
3503
                        rows_virtual_meters = cursor.fetchall()
3504
3505
                        virtual_meter_dict = dict()
3506
                        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
3507
                            for row in rows_virtual_meters:
3508
                                virtual_meter_dict[row[2]] = {"type": 'virtual_meter',
3509
                                                              "id": row[0],
3510
                                                              "name": row[1],
3511
                                                              "uuid": row[2]}
3512
3513
                        # validate numerator meter uuid
3514
                        if meter_dict.get(numerator_meter_uuid) is None and \
3515
                                virtual_meter_dict.get(numerator_meter_uuid) is None and \
3516
                                offline_meter_dict.get(numerator_meter_uuid) is None:
3517
                            cursor.close()
3518
                            cnx.close()
3519
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3520
                                                   description='API.INVALID_NUMERATOR_METER_UUID')
3521
3522
                        # validate denominator meter uuid
3523
                        if denominator_meter_uuid is None:
3524
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3525
                                                   description='API.INVALID_DENOMINATOR_METER_UUID')
3526
3527
                        if denominator_meter_uuid == numerator_meter_uuid:
3528
                            cursor.close()
3529
                            cnx.close()
3530
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3531
                                                   description='API.INVALID_DENOMINATOR_METER_UUID')
3532
3533
                        if denominator_meter_uuid not in meter_dict and \
3534
                                denominator_meter_uuid not in virtual_meter_dict and \
3535
                                denominator_meter_uuid not in offline_meter_dict:
3536
                            cursor.close()
3537
                            cnx.close()
3538
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3539
                                                   description='API.INVALID_DENOMINATOR_METER_UUID')
3540
3541
                    add_values = (" INSERT INTO tbl_combined_equipments_parameters "
3542
                                  "    (combined_equipment_id, name, parameter_type, constant, "
3543
                                  "     point_id, numerator_meter_uuid, denominator_meter_uuid) "
3544
                                  " VALUES (%s, %s, %s, %s, %s, %s, %s) ")
3545
                    cursor.execute(add_values, (new_id,
3546
                                                parameters['name'],
3547
                                                parameters['parameter_type'],
3548
                                                parameters['constant'],
3549
                                                point_id,
3550
                                                numerator_meter_uuid,
3551
                                                denominator_meter_uuid))
3552
            cnx.commit()
3553
            cursor.close()
3554
            cnx.close()

myems-api/core/equipment.py 1 location

@@ 3099-3225 (lines=127) @@
3096
                        " INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output ) "
3097
                        " VALUES (%s, %s, %s) ")
3098
                    cursor.execute(add_row, (new_id, virtual_meter['id'], virtual_meter['is_output']))
3099
            if meta_result['parameters'] is not None and len(meta_result['parameters']) > 0:
3100
                for parameters in meta_result['parameters']:
3101
                    cursor.execute(" SELECT name "
3102
                                   " FROM tbl_equipments_parameters "
3103
                                   " WHERE name = %s AND equipment_id = %s ", (parameters['name'], new_id))
3104
                    if cursor.fetchone() is not None:
3105
                        cursor.close()
3106
                        cnx.close()
3107
                        raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3108
                                               description='API.EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE')
3109
                    if 'point' in parameters:
3110
                        if parameters['point'] is None:
3111
                            point_id = None
3112
                        elif parameters['point']['id'] is not None and \
3113
                                parameters['point']['id'] <= 0:
3114
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3115
                                                   description='API.INVALID_POINT_ID')
3116
                        else:
3117
                            point_id = parameters['point']['id']
3118
                    else:
3119
                        point_id = None
3120
                    numerator_meter_uuid = None
3121
                    if 'numerator_meter' in parameters:
3122
                        if parameters['numerator_meter'] is not None and \
3123
                                isinstance(parameters['numerator_meter']['uuid'], str) and \
3124
                                len(str.strip(parameters['numerator_meter']['uuid'])) > 0:
3125
                            numerator_meter_uuid = str.strip(parameters['numerator_meter']['uuid'])
3126
3127
                    denominator_meter_uuid = None
3128
                    if 'denominator_meter' in parameters:
3129
                        if parameters['denominator_meter'] is not None and \
3130
                                isinstance(parameters['denominator_meter']['uuid'], str) and \
3131
                                len(str.strip(parameters['denominator_meter']['uuid'])) > 0:
3132
                            denominator_meter_uuid = str.strip(parameters['denominator_meter']['uuid'])
3133
3134
                    # validate by parameter type
3135
                    if parameters['parameter_type'] == 'point':
3136
                        if point_id is None:
3137
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3138
                                                   description='API.INVALID_POINT_ID')
3139
                        query = (" SELECT id, name "
3140
                                 " FROM tbl_points "
3141
                                 " WHERE id = %s ")
3142
                        cursor.execute(query, (point_id,))
3143
                        if cursor.fetchone() is None:
3144
                            cursor.close()
3145
                            cnx.close()
3146
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3147
                                                   description='API.POINT_NOT_FOUND')
3148
3149
                    elif parameters['parameter_type'] == 'constant':
3150
                        if parameters['constant'] is None:
3151
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3152
                                                   description='API.INVALID_CONSTANT_VALUE')
3153
3154
                    elif parameters['parameter_type'] == 'fraction':
3155
                        query = (" SELECT id, name, uuid "
3156
                                 " FROM tbl_meters ")
3157
                        cursor.execute(query)
3158
                        rows_meters = cursor.fetchall()
3159
                        meter_dict = dict()
3160
                        if rows_meters is not None and len(rows_meters) > 0:
3161
                            for row in rows_meters:
3162
                                meter_dict[row[2]] = {"type": 'meter',
3163
                                                      "id": row[0],
3164
                                                      "name": row[1],
3165
                                                      "uuid": row[2]}
3166
3167
                        query = (" SELECT id, name, uuid "
3168
                                 " FROM tbl_offline_meters ")
3169
                        cursor.execute(query)
3170
                        rows_offline_meters = cursor.fetchall()
3171
3172
                        offline_meter_dict = dict()
3173
                        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
3174
                            for row in rows_offline_meters:
3175
                                offline_meter_dict[row[2]] = {"type": 'offline_meter',
3176
                                                              "id": row[0],
3177
                                                              "name": row[1],
3178
                                                              "uuid": row[2]}
3179
3180
                        query = (" SELECT id, name, uuid "
3181
                                 " FROM tbl_virtual_meters ")
3182
                        cursor.execute(query)
3183
                        rows_virtual_meters = cursor.fetchall()
3184
3185
                        virtual_meter_dict = dict()
3186
                        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
3187
                            for row in rows_virtual_meters:
3188
                                virtual_meter_dict[row[2]] = {"type": 'virtual_meter',
3189
                                                              "id": row[0],
3190
                                                              "name": row[1],
3191
                                                              "uuid": row[2]}
3192
3193
                        # validate numerator meter uuid
3194
                        if numerator_meter_uuid is None:
3195
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3196
                                                   description='API.INVALID_NUMERATOR_METER_UUID')
3197
3198
                        if meter_dict.get(numerator_meter_uuid) is None and \
3199
                                virtual_meter_dict.get(numerator_meter_uuid) is None and \
3200
                                offline_meter_dict.get(numerator_meter_uuid) is None:
3201
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3202
                                                   description='API.INVALID_NUMERATOR_METER_UUID')
3203
3204
                        # validate denominator meter uuid
3205
                        if denominator_meter_uuid == numerator_meter_uuid:
3206
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3207
                                                   description='API.INVALID_DENOMINATOR_METER_UUID')
3208
3209
                        if denominator_meter_uuid not in meter_dict and \
3210
                                denominator_meter_uuid not in virtual_meter_dict and \
3211
                                denominator_meter_uuid not in offline_meter_dict:
3212
                            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3213
                                                   description='API.INVALID_DENOMINATOR_METER_UUID')
3214
3215
                    add_values = (" INSERT INTO tbl_equipments_parameters "
3216
                                  "    (equipment_id, name, parameter_type, constant, "
3217
                                  "     point_id, numerator_meter_uuid, denominator_meter_uuid) "
3218
                                  " VALUES (%s, %s, %s, %s, %s, %s, %s) ")
3219
                    cursor.execute(add_values, (new_id,
3220
                                                parameters['name'],
3221
                                                parameters['parameter_type'],
3222
                                                parameters['constant'],
3223
                                                point_id,
3224
                                                numerator_meter_uuid,
3225
                                                denominator_meter_uuid))
3226
            cnx.commit()
3227
            cursor.close()
3228
            cnx.close()