combined_equipment_billing_output_category.main()   F
last analyzed

Complexity

Conditions 57

Size

Total Lines 249
Code Lines 172

Duplication

Lines 249
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 172
dl 249
loc 249
rs 0
c 0
b 0
f 0
cc 57
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like combined_equipment_billing_output_category.main() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import time
2
from datetime import datetime, timedelta
3
from decimal import Decimal
4
import mysql.connector
5
import tariff
6
import config
7
8
9
########################################################################################################################
10
# PROCEDURES
11
# Step 1: get all combined equipments
12
# for each combined equipment in list:
13
#   Step 2: get the latest start_datetime_utc
14
#   Step 3: get all energy input data since the latest start_datetime_utc
15
#   Step 4: get tariffs
16
#   Step 5: calculate billing by multiplying energy with tariff
17
#   Step 6: save billing data to database
18
########################################################################################################################
19
20
21 View Code Duplication
def main(logger):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
22
23
    while True:
24
        # the outermost while loop
25
        ################################################################################################################
26
        # Step 1: get all combined equipments
27
        ################################################################################################################
28
        cnx_system_db = None
29
        cursor_system_db = None
30
        try:
31
            cnx_system_db = mysql.connector.connect(**config.myems_system_db)
32
            cursor_system_db = cnx_system_db.cursor()
33
        except Exception as e:
34
            logger.error("Error in step 1.1 of combined_equipment_billing_input_category " + str(e))
35
            if cursor_system_db:
36
                cursor_system_db.close()
37
            if cnx_system_db:
38
                cnx_system_db.close()
39
            # sleep and continue the outermost while loop
40
            time.sleep(60)
41
            continue
42
43
        print("Connected to MyEMS System Database")
44
45
        try:
46
            cursor_system_db.execute(" SELECT id, name, cost_center_id "
47
                                     " FROM tbl_combined_equipments "
48
                                     " ORDER BY id ")
49
            rows_combined_equipments = cursor_system_db.fetchall()
50
51
            if rows_combined_equipments is None or len(rows_combined_equipments) == 0:
52
                print("Step 1.2: There isn't any combined equipments. ")
53
                if cursor_system_db:
54
                    cursor_system_db.close()
55
                if cnx_system_db:
56
                    cnx_system_db.close()
57
                # sleep and continue the outermost while loop
58
                time.sleep(60)
59
                continue
60
61
            combined_equipment_list = list()
62
            for row in rows_combined_equipments:
63
                combined_equipment_list.append({"id": row[0], "name": row[1], "cost_center_id": row[2]})
64
65
        except Exception as e:
66
            logger.error("Error in step 1.2 of combined_equipment_billing_output_category " + str(e))
67
            if cursor_system_db:
68
                cursor_system_db.close()
69
            if cnx_system_db:
70
                cnx_system_db.close()
71
            # sleep and continue the outermost while loop
72
            time.sleep(60)
73
            continue
74
75
        print("Step 1.2: Got all combined equipments from MyEMS System Database")
76
77
        cnx_energy_db = None
78
        cursor_energy_db = None
79
        try:
80
            cnx_energy_db = mysql.connector.connect(**config.myems_energy_db)
81
            cursor_energy_db = cnx_energy_db.cursor()
82
        except Exception as e:
83
            logger.error("Error in step 1.3 of combined_equipment_billing_output_category " + str(e))
84
            if cursor_energy_db:
85
                cursor_energy_db.close()
86
            if cnx_energy_db:
87
                cnx_energy_db.close()
88
89
            if cursor_system_db:
90
                cursor_system_db.close()
91
            if cnx_system_db:
92
                cnx_system_db.close()
93
            # sleep and continue the outermost while loop
94
            time.sleep(60)
95
            continue
96
97
        print("Connected to MyEMS Energy Database")
98
99
        cnx_billing_db = None
100
        cursor_billing_db = None
101
        try:
102
            cnx_billing_db = mysql.connector.connect(**config.myems_billing_db)
103
            cursor_billing_db = cnx_billing_db.cursor()
104
        except Exception as e:
105
            logger.error("Error in step 1.4 of combined_equipment_billing_output_category " + str(e))
106
            if cursor_billing_db:
107
                cursor_billing_db.close()
108
            if cnx_billing_db:
109
                cnx_billing_db.close()
110
111
            if cursor_energy_db:
112
                cursor_energy_db.close()
113
            if cnx_energy_db:
114
                cnx_energy_db.close()
115
116
            if cursor_system_db:
117
                cursor_system_db.close()
118
            if cnx_system_db:
119
                cnx_system_db.close()
120
            # sleep and continue the outermost while loop
121
            time.sleep(60)
122
            continue
123
124
        print("Connected to MyEMS Billing Database")
125
126
        for combined_equipment in combined_equipment_list:
127
128
            ############################################################################################################
129
            # Step 2: get the latest start_datetime_utc
130
            ############################################################################################################
131
            print("Step 2: get the latest start_datetime_utc from billing database for " + combined_equipment['name'])
132
            try:
133
                cursor_billing_db.execute(" SELECT MAX(start_datetime_utc) "
134
                                          " FROM tbl_combined_equipment_output_category_hourly "
135
                                          " WHERE combined_equipment_id = %s ",
136
                                          (combined_equipment['id'], ))
137
                row_datetime = cursor_billing_db.fetchone()
138
                start_datetime_utc = datetime.strptime(config.start_datetime_utc, '%Y-%m-%d %H:%M:%S')
139
                start_datetime_utc = start_datetime_utc.replace(minute=0, second=0, microsecond=0, tzinfo=None)
140
141
                if row_datetime is not None and len(row_datetime) > 0 and isinstance(row_datetime[0], datetime):
142
                    # replace second and microsecond with 0
143
                    # note: do not replace minute in case of calculating in half hourly
144
                    start_datetime_utc = row_datetime[0].replace(second=0, microsecond=0, tzinfo=None)
145
                    # start from the next time slot
146
                    start_datetime_utc += timedelta(minutes=config.minutes_to_count)
147
148
                print("start_datetime_utc: " + start_datetime_utc.isoformat()[0:19])
149
            except Exception as e:
150
                logger.error("Error in step 2 of combined_equipment_billing_output_category " + str(e))
151
                # break the for combined equipment loop
152
                break
153
154
            ############################################################################################################
155
            # Step 3: get all energy output data since the latest start_datetime_utc
156
            ############################################################################################################
157
            print("Step 3: get all energy output data since the latest start_datetime_utc")
158
159
            query = (" SELECT start_datetime_utc, energy_category_id, actual_value "
160
                     " FROM tbl_combined_equipment_output_category_hourly "
161
                     " WHERE combined_equipment_id = %s AND start_datetime_utc >= %s "
162
                     " ORDER BY id ")
163
            cursor_energy_db.execute(query, (combined_equipment['id'], start_datetime_utc, ))
164
            rows_hourly = cursor_energy_db.fetchall()
165
166
            if rows_hourly is None or len(rows_hourly) == 0:
167
                print("Step 3: There isn't any energy output data to calculate. ")
168
                # continue the for combined equipment loop
169
                continue
170
171
            energy_dict = dict()
172
            energy_category_list = list()
173
            end_datetime_utc = start_datetime_utc
174
            for row_hourly in rows_hourly:
175
                current_datetime_utc = row_hourly[0]
176
                energy_category_id = row_hourly[1]
177
178
                if energy_category_id not in energy_category_list:
179
                    energy_category_list.append(energy_category_id)
180
181
                actual_value = row_hourly[2]
182
                if energy_dict.get(current_datetime_utc) is None:
183
                    energy_dict[current_datetime_utc] = dict()
184
                energy_dict[current_datetime_utc][energy_category_id] = actual_value
185
                if current_datetime_utc > end_datetime_utc:
186
                    end_datetime_utc = current_datetime_utc
187
188
            ############################################################################################################
189
            # Step 4: get tariffs
190
            ############################################################################################################
191
            print("Step 4: get tariffs")
192
            tariff_dict = dict()
193
            for energy_category_id in energy_category_list:
194
                tariff_dict[energy_category_id] = \
195
                    tariff.get_energy_category_tariffs(combined_equipment['cost_center_id'],
196
                                                       energy_category_id,
197
                                                       start_datetime_utc,
198
                                                       end_datetime_utc)
199
            ############################################################################################################
200
            # Step 5: calculate billing by multiplying energy with tariff
201
            ############################################################################################################
202
            print("Step 5: calculate billing by multiplying energy with tariff")
203
            billing_dict = dict()
204
205
            if len(energy_dict) > 0:
206
                for current_datetime_utc in energy_dict.keys():
207
                    billing_dict[current_datetime_utc] = dict()
208
                    for energy_category_id in energy_category_list:
209
                        current_tariff = tariff_dict[energy_category_id].get(current_datetime_utc)
210
                        current_energy = energy_dict[current_datetime_utc].get(energy_category_id)
211
                        if current_tariff is not None \
212
                                and isinstance(current_tariff, Decimal) \
213
                                and current_energy is not None \
214
                                and isinstance(current_energy, Decimal):
215
                            billing_dict[current_datetime_utc][energy_category_id] = \
216
                                current_energy * current_tariff
217
218
                    if len(billing_dict[current_datetime_utc]) == 0:
219
                        del billing_dict[current_datetime_utc]
220
221
            ############################################################################################################
222
            # Step 6: save billing data to billing database
223
            ############################################################################################################
224
            print("Step 6: save billing data to billing database")
225
226
            if len(billing_dict) > 0:
227
                try:
228
                    add_values = (" INSERT INTO tbl_combined_equipment_output_category_hourly "
229
                                  "             (combined_equipment_id, "
230
                                  "              energy_category_id, "
231
                                  "              start_datetime_utc, "
232
                                  "              actual_value) "
233
                                  " VALUES  ")
234
235
                    for current_datetime_utc in billing_dict:
236
                        for energy_category_id in energy_category_list:
237
                            current_billing = billing_dict[current_datetime_utc].get(energy_category_id)
238
                            if current_billing is not None and isinstance(current_billing, Decimal):
239
                                add_values += " (" + str(combined_equipment['id']) + ","
240
                                add_values += " " + str(energy_category_id) + ","
241
                                add_values += "'" + current_datetime_utc.isoformat()[0:19] + "',"
242
                                add_values += str(billing_dict[current_datetime_utc][energy_category_id]) + "), "
243
                    print("add_values:" + add_values)
244
                    # trim ", " at the end of string and then execute
245
                    cursor_billing_db.execute(add_values[:-2])
246
                    cnx_billing_db.commit()
247
                except Exception as e:
248
                    logger.error("Error in step 6 of combined_equipment_billing_output_category " + str(e))
249
                    # break the for combined_equipment loop
250
                    break
251
252
        # end of for combined equipment loop
253
        if cnx_system_db:
254
            cnx_system_db.close()
255
        if cursor_system_db:
256
            cursor_system_db.close()
257
258
        if cnx_energy_db:
259
            cnx_energy_db.close()
260
        if cursor_energy_db:
261
            cursor_energy_db.close()
262
263
        if cnx_billing_db:
264
            cnx_billing_db.close()
265
        if cursor_billing_db:
266
            cursor_billing_db.close()
267
        print("go to sleep 300 seconds...")
268
        time.sleep(300)
269
        print("wake from sleep, and continue to work...")
270
    # end of the outermost while loop
271