Code Duplication    Length = 74-75 lines in 2 locations

myems-api/core/microgrid.py 1 location

@@ 20-94 (lines=75) @@
17
        _ = req
18
        resp.status = falcon.HTTP_200
19
20
    @staticmethod
21
    def on_get(req, resp):
22
        access_control(req)
23
        cnx = mysql.connector.connect(**config.myems_system_db)
24
        cursor = cnx.cursor()
25
26
        query = (" SELECT id, name, uuid "
27
                 " FROM tbl_contacts ")
28
        cursor.execute(query)
29
        rows_contacts = cursor.fetchall()
30
31
        contact_dict = dict()
32
        if rows_contacts is not None and len(rows_contacts) > 0:
33
            for row in rows_contacts:
34
                contact_dict[row[0]] = {"id": row[0],
35
                                        "name": row[1],
36
                                        "uuid": row[2]}
37
38
        query = (" SELECT id, name, uuid "
39
                 " FROM tbl_cost_centers ")
40
        cursor.execute(query)
41
        rows_cost_centers = cursor.fetchall()
42
43
        cost_center_dict = dict()
44
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
45
            for row in rows_cost_centers:
46
                cost_center_dict[row[0]] = {"id": row[0],
47
                                            "name": row[1],
48
                                            "uuid": row[2]}
49
        svg_dict = dict()
50
51
        query = (" SELECT id, name, uuid "
52
                 " FROM tbl_svgs ")
53
        cursor.execute(query)
54
        rows_svgs = cursor.fetchall()
55
        if rows_svgs is not None and len(rows_svgs) > 0:
56
            for row in rows_svgs:
57
                svg_dict[row[0]] = {"id": row[0],
58
                                    "name": row[1],
59
                                    "uuid": row[2]}
60
61
        query = (" SELECT id, name, uuid, "
62
                 "        address, postal_code, latitude, longitude, rated_capacity, rated_power, "
63
                 "        contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, "
64
                 "        phase_of_lifecycle, description "
65
                 " FROM tbl_microgrids "
66
                 " ORDER BY id ")
67
        cursor.execute(query)
68
        rows_microgrids = cursor.fetchall()
69
70
        result = list()
71
        if rows_microgrids is not None and len(rows_microgrids) > 0:
72
            for row in rows_microgrids:
73
                meta_result = {"id": row[0],
74
                               "name": row[1],
75
                               "uuid": row[2],
76
                               "address": row[3],
77
                               "postal_code": row[4],
78
                               "latitude": row[5],
79
                               "longitude": row[6],
80
                               "rated_capacity": row[7],
81
                               "rated_power": row[8],
82
                               "contact": contact_dict.get(row[9], None),
83
                               "cost_center": cost_center_dict.get(row[10], None),
84
                               "serial_number": row[11],
85
                               "svg": svg_dict.get(row[12], None),
86
                               "is_cost_data_displayed": bool(row[13]),
87
                               "phase_of_lifecycle": row[14],
88
                               "description": row[15],
89
                               "qrcode": 'microgrid:' + row[2]}
90
                result.append(meta_result)
91
92
        cursor.close()
93
        cnx.close()
94
        resp.text = json.dumps(result)
95
96
    @staticmethod
97
    @user_logger

myems-api/core/photovoltaicpowerstation.py 1 location

@@ 21-94 (lines=74) @@
18
        _ = req
19
        resp.status = falcon.HTTP_200
20
21
    @staticmethod
22
    def on_get(req, resp):
23
        access_control(req)
24
        cnx = mysql.connector.connect(**config.myems_system_db)
25
        cursor = cnx.cursor()
26
27
        query = (" SELECT id, name, uuid "
28
                 " FROM tbl_contacts ")
29
        cursor.execute(query)
30
        rows_contacts = cursor.fetchall()
31
32
        contact_dict = dict()
33
        if rows_contacts is not None and len(rows_contacts) > 0:
34
            for row in rows_contacts:
35
                contact_dict[row[0]] = {"id": row[0],
36
                                        "name": row[1],
37
                                        "uuid": row[2]}
38
39
        query = (" SELECT id, name, uuid "
40
                 " FROM tbl_cost_centers ")
41
        cursor.execute(query)
42
        rows_cost_centers = cursor.fetchall()
43
44
        cost_center_dict = dict()
45
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
46
            for row in rows_cost_centers:
47
                cost_center_dict[row[0]] = {"id": row[0],
48
                                            "name": row[1],
49
                                            "uuid": row[2]}
50
51
        svg_dict = dict()
52
53
        query = (" SELECT id, name, uuid "
54
                 " FROM tbl_svgs ")
55
        cursor.execute(query)
56
        rows_svgs = cursor.fetchall()
57
        if rows_svgs is not None and len(rows_svgs) > 0:
58
            for row in rows_svgs:
59
                svg_dict[row[0]] = {"id": row[0],
60
                                    "name": row[1],
61
                                    "uuid": row[2]}
62
63
        query = (" SELECT id, name, uuid, "
64
                 "        station_code, address, latitude, longitude, rated_capacity, rated_power, "
65
                 "        contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle, description "
66
                 " FROM tbl_photovoltaic_power_stations "
67
                 " ORDER BY id ")
68
        cursor.execute(query)
69
        rows_photovoltaic_power_stations = cursor.fetchall()
70
71
        result = list()
72
        if rows_photovoltaic_power_stations is not None and len(rows_photovoltaic_power_stations) > 0:
73
            for row in rows_photovoltaic_power_stations:
74
                meta_result = {"id": row[0],
75
                               "name": row[1],
76
                               "uuid": row[2],
77
                               "station_code": row[3],
78
                               "address": row[4],
79
                               "latitude": row[5],
80
                               "longitude": row[6],
81
                               "rated_capacity": row[7],
82
                               "rated_power": row[8],
83
                               "contact": contact_dict.get(row[9], None),
84
                               "cost_center": cost_center_dict.get(row[10], None),
85
                               "svg": svg_dict.get(row[11], None),
86
                               "is_cost_data_displayed": bool(row[12]),
87
                               "phase_of_lifecycle": row[13],
88
                               "description": row[14],
89
                               "qrcode": 'photovoltaicpowerstation:' + row[2]}
90
                result.append(meta_result)
91
92
        cursor.close()
93
        cnx.close()
94
        resp.text = json.dumps(result)
95
96
    @staticmethod
97
    @user_logger