Passed
Push — master ( adda50...a6dd98 )
by Guangyu
07:38 queued 15s
created

clean_energy_value   D

Complexity

Total Complexity 58

Size/Duplication

Total Lines 576
Duplicated Lines 5.21 %

Importance

Changes 0
Metric Value
wmc 58
eloc 183
dl 30
loc 576
rs 4.5599
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
F process() 30 560 58

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like clean_energy_value 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 mysql.connector
2
import config
3
import time
4
from datetime import datetime, timedelta
5
6
7
########################################################################################################################
8
# This procedure will find and tag the bad energy values.
9
#
10
# Step 1: get the time slot to clean.
11
# Step 2: check bad case class 1 with high limits and low limits.
12
# Step 3: check bad case class 2 which is in concave shape model.
13
# Step 4: tag the is_bad property of energy values.
14
########################################################################################################################
15
16
def process(logger):
17
18
    while True:
19
        # the outermost loop to reconnect server if there is a connection error
20
        cnx_historical = None
21
        cursor_historical = None
22
        try:
23
            cnx_historical = mysql.connector.connect(**config.myems_historical_db)
24
            cursor_historical = cnx_historical.cursor()
25
        except Exception as e:
26
            logger.error("Error at the begin of clean_energy_value.process " + str(e))
27
            if cursor_historical:
28
                cursor_historical.close()
29
            if cnx_historical:
30
                cnx_historical.disconnect()
31
            time.sleep(60)
32
            continue
33
34
        # Note:
35
        # the default value of unchecked values' is_bad property is NULL
36
        # if a value is checked and the result is bad then is_bad would be set to TRUE
37
        # else if a value is checked and the result is good then is_bad would be set to FALSE
38
39
        ################################################################################################################
40
        # Step 1: get the time slot to clean.
41
        ################################################################################################################
42
43
        min_datetime = None
44
        max_datetime = None
45
        try:
46
            query = (" SELECT MIN(utc_date_time), MAX(utc_date_time) "
47
                     " FROM tbl_energy_value "
48
                     " WHERE is_bad IS NULL ")
49
            cursor_historical.execute(query, ())
50
            row_datetime = cursor_historical.fetchone()
51
            if row_datetime is not None and len(row_datetime) == 2 and \
52
                    isinstance(row_datetime[0], datetime) and isinstance(row_datetime[1], datetime):
53
                # NOTE: To avoid omission mistakes, we start one hour early
54
                min_datetime = row_datetime[0] - timedelta(hours=1)
55
                max_datetime = row_datetime[1]
56
57
        except Exception as e:
58
            print("Error in Step 1 of clean_energy_value.process " + str(e))
59
            logger.error("Error in Step 1 of clean_energy_value.process " + str(e))
60
            if cursor_historical:
61
                cursor_historical.close()
62
            if cnx_historical:
63
                cnx_historical.disconnect()
64
            time.sleep(60)
65
            continue
66
67
        if min_datetime is None or max_datetime is None:
68
            print("min_datetime or max_datetime is None")
69
            if cursor_historical:
70
                cursor_historical.close()
71
            if cnx_historical:
72
                cnx_historical.disconnect()
73
            time.sleep(60)
74
            continue
75
        else:
76
            print("min_datetime: " + min_datetime.isoformat()[0:19])
77
            print("max_datetime: " + max_datetime.isoformat()[0:19])
78
79
        ################################################################################################################
80
        # Step 2: check bad case class 1 with high limits and low limits.
81
        ################################################################################################################
82
83
        ################################################################################################################
84
        # bad case 1.1
85
        # id          point_id utc_date_time        actual_value          is_bad (expected)
86
        # 104814811	  3333     2018-01-31 16:45:04	115603.0078125        good
87
        # 104814588	  3333     2018-01-31 16:44:00	115603.0078125        good
88
        # 104815007	  3333     2018-01-31 16:46:09	1.832278249396618e21  bad
89
        # 104815226	  3333     2018-01-31 16:47:13	1.832278249396618e21  bad
90
        # 104815423	  3333     2018-01-31 16:48:17	1.832278249396618e21  bad
91
        # 104815643	  3333     2018-01-31 16:49:22	1.832278249396618e21  bad
92
        # 104815820	  3333     2018-01-31 16:50:26	1.832278249396618e21  bad
93
        # 104816012	  3333     2018-01-31 16:51:30	1.832278249396618e21  bad
94
        # 104816252	  3333     2018-01-31 16:52:34	1.832278249396618e21  bad
95
        # 104816446	  3333     2018-01-31 16:53:38	1.832278249396618e21  bad
96
        # 104816667	  3333     2018-01-31 16:54:43	1.832278249396618e21  bad
97
        # 104816860	  3333     2018-01-31 16:55:47	1.832278249396618e21  bad
98
        # 104817065	  3333     2018-01-31 16:56:51	1.832278249396618e21  bad
99
        # 104817284	  3333     2018-01-31 16:57:55	1.832278249396618e21  bad
100
        # 104817482	  3333     2018-01-31 16:58:59	1.832278249396618e21  bad
101
        # 104817723	  3333     2018-01-31 17:00:04	1.832278249396618e21  bad
102
        # 104817940	  3333     2018-01-31 17:01:08	115749.0078125        good
103
        # 104818142	  3333     2018-01-31 17:02:11	115749.0078125        good
104
        # 104818380	  3333     2018-01-31 17:03:16	115749.0078125        good
105
        # 104818596	  3333     2018-01-31 17:04:20	115749.0078125        good
106
        ################################################################################################################
107
108
        ################################################################################################################
109
        # bad case 1.2:
110
        # id    point_id  utc_date_time          actual_value           is_bad (expected)
111
        #       3333      2018-01-31 17:27:53    115823.0078125         good
112
        #       3333      2018-01-31 17:28:57    115823.0078125         good
113
        #       3333      2018-01-31 17:30:02    115823.0078125         good
114
        #       3333      2018-01-31 17:31:06    115823.0078125         good
115
        #       3333      2018-01-31 17:32:11    0                      bad
116
        #       3333      2018-01-31 17:33:15    0                      bad
117
        #       3333      2018-01-31 17:34:19    0                      bad
118
        #       3333      2018-01-31 17:35:24    0                      bad
119
        #       3333      2018-01-31 17:36:28    0                      bad
120
        #       3333      2018-01-31 17:37:32    0                      bad
121
        #       3333      2018-01-31 17:38:36    0                      bad
122
        #       3333      2018-01-31 17:39:41    0                      bad
123
        #       3333      2018-01-31 17:40:44    0                      bad
124
        #       3333      2018-01-31 17:41:49    0                      bad
125
        #       3333      2018-01-31 17:43:57    0                      bad
126
        #       3333      2018-01-31 17:42:53    0                      bad
127
        #       3333      2018-01-31 17:45:01    0                      bad
128
        #       3333      2018-01-31 17:46:06    0                      bad
129
        #       3333      2018-01-31 17:47:10    0                      bad
130
        #       3333      2018-01-31 17:48:14    115969.0078125         good
131
        #       3333      2018-01-31 17:49:18    115969.0078125         good
132
        #       3333      2018-01-31 17:50:22    115969.0078125         good
133
        ################################################################################################################
134
135
        ################################################################################################################
136
        # bad case 1.3:
137
        # id    point_id  utc_date_time          actual_value           is_bad (expected)
138
        #       3333      2018-02-04 07:00:38    139968                  good
139
        #       3333      2018-02-04 07:01:42    139968                  good
140
        #       3333      2018-02-04 07:03:54    -7.068193740872921e-3   bad
141
        #       3333      2018-02-04 07:04:58    -7.068193740872921e-3   bad
142
        #       3333      2018-02-04 07:06:03    -7.068193740872921e-3   bad
143
        #       3333      2018-02-04 07:07:06    -7.068193740872921e-3   bad
144
        #       3333      2018-02-04 07:08:10    -7.068193740872921e-3   bad
145
        #       3333      2018-02-04 07:09:13    -7.068193740872921e-3   bad
146
        #       3333      2018-02-04 07:10:17    -7.068193740872921e-3   bad
147
        #       3333      2018-02-04 07:11:21    -7.068193740872921e-3   bad
148
        #       3333      2018-02-04 07:12:25    -7.068193740872921e-3   bad
149
        #       3333      2018-02-04 07:13:29    -7.068193740872921e-3   bad
150
        #       3333      2018-02-04 07:14:33    -7.068193740872921e-3   bad
151
        #       3333      2018-02-04 07:15:37    -7.068193740872921e-3   bad
152
        #       3333      2018-02-04 07:16:41    -7.068193740872921e-3   bad
153
        #       3333      2018-02-04 07:17:45    140114                  good
154
        #       3333      2018-02-04 07:18:49    140114                  good
155
        #       3333      2018-02-04 07:19:53    140114                  good
156
        ################################################################################################################
157
158
        ################################################################################################################
159
        # bad case 1.4:
160
        # id    point_id  utc_date_time          actual_value           is_bad (expected)
161
        #       3333      2018-02-08 01:16:38    165746.015625          good
162
        #       3333      2018-02-08 01:15:34    165746.015625          good
163
        #       3333      2018-02-08 01:14:30    165746.015625          good
164
        #       3333      2018-02-08 01:13:27    0.00303281145170331    bad
165
        #       3333      2018-02-08 01:12:22    0.00303281145170331    bad
166
        #       3333      2018-02-08 01:11:19    0.00303281145170331    bad
167
        #       3333      2018-02-08 01:10:15    0.00303281145170331    bad
168
        #       3333      2018-02-08 01:09:11    0.00303281145170331    bad
169
        #       3333      2018-02-08 01:08:06    0.00303281145170331    bad
170
        #       3333      2018-02-08 01:07:02    0.00303281145170331    bad
171
        #       3333      2018-02-08 01:05:58    0.00303281145170331    bad
172
        #       3333      2018-02-08 01:04:54    0.00303281145170331    bad
173
        #       3333      2018-02-08 01:03:50    0.00303281145170331    bad
174
        #       3333      2018-02-08 01:02:46    0.00303281145170331    bad
175
        #       3333      2018-02-08 01:01:42    0.00303281145170331    bad
176
        #       3333      2018-02-08 01:00:39    0.00303281145170331    bad
177
        #       3333      2018-02-08 00:59:34    0.00303281145170331    bad
178
        #       3333      2018-02-08 00:58:31    0.00303281145170331    bad
179
        #       3333      2018-02-08 00:57:27    165599.015625          good
180
        #       3333      2018-02-08 00:56:23    165599.015625          good
181
        #       3333      2018-02-08 00:55:20    165599.015625          good
182
        #       3333      2018-02-08 00:54:16    165599.015625          good
183
        ################################################################################################################
184
        print("Step 2: Processing bad case 1.x")
185
        cnx_system = None
186
        cursor_system = None
187
        try:
188
            cnx_system = mysql.connector.connect(**config.myems_system_db)
189
            cursor_system = cnx_system.cursor(dictionary=True)
190
191
            query = (" SELECT id, high_limit, low_limit "
192
                     " FROM tbl_points "
193
                     " WHERE object_type='ENERGY_VALUE'")
194
            cursor_system.execute(query)
195
            rows_points = cursor_system.fetchall()
196
197
            point_dict = dict()
198
            if rows_points is not None and len(rows_points) > 0:
199
                for row in rows_points:
200
                    point_dict[row['id']] = {"high_limit": row['high_limit'],
201
                                             "low_limit": row['low_limit']}
202
        except Exception as e:
203
            logger.error("Error in step 2.1 of clean_energy_value.process " + str(e))
204
            time.sleep(60)
205
            continue
206
        finally:
207
            if cursor_system:
208
                cursor_system.close()
209
            if cnx_system:
210
                cnx_system.disconnect()
211
212
        try:
213
            query = (" SELECT id, point_id, actual_value "
214
                     " FROM tbl_energy_value "
215
                     " WHERE utc_date_time >= %s AND utc_date_time <= %s AND is_bad IS NOT TRUE ")
216
            cursor_historical.execute(query, (min_datetime, max_datetime,))
217
            rows_energy_values = cursor_historical.fetchall()
218
        except Exception as e:
219
            logger.error("Error in step 2.2 of clean_energy_value.process " + str(e))
220
            if cursor_historical:
221
                cursor_historical.close()
222
            if cnx_historical:
223
                cnx_historical.disconnect()
224
            time.sleep(60)
225
            continue
226
227
        # initialize bad list
228
        bad_list = list()
229
230
        if rows_energy_values is not None and len(rows_energy_values) > 0:
231
            for row_energy_value in rows_energy_values:
232
                point_id = row_energy_value[1]
233
                actual_value = row_energy_value[2]
234
                point = point_dict.get(point_id, None)
0 ignored issues
show
introduced by
The variable point_dict does not seem to be defined for all execution paths.
Loading history...
235
                if point is None or actual_value > point['high_limit'] or actual_value < point['low_limit']:
236
                    bad_list.append(row_energy_value[0])
237
238
        print('bad list: ' + str(bad_list))
239 View Code Duplication
        if len(bad_list) > 0:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
240
            try:
241
                update = (" UPDATE tbl_energy_value "
242
                          " SET is_bad = TRUE "
243
                          " WHERE id IN (" + ', '.join(map(str, bad_list)) + ")")
244
                cursor_historical.execute(update, )
245
                cnx_historical.commit()
246
            except Exception as e:
247
                logger.error("Error in step 2.3 of clean_energy_value.process " + str(e))
248
                if cursor_historical:
249
                    cursor_historical.close()
250
                if cnx_historical:
251
                    cnx_historical.disconnect()
252
                time.sleep(60)
253
                continue
254
255
        ################################################################################################################
256
        # Step 3: check bad case class 2 which is in concave shape model.
257
        ################################################################################################################
258
        print("Step 3: Processing bad case 2.x")
259
        ################################################################################################################
260
        # bad case 2.1
261
        # id    point_id  utc_date_time          actual_value       is_bad (expected)
262
        #       3333      2018-02-05 04:55:45    146129.015         good
263
        #       3333      2018-02-05 04:56:49    146129.015         good
264
        #       3333      2018-02-05 04:57:54    146129.015         good
265
        #       3333      2018-02-05 05:22:52    145693.015         bad
266
        #       3333      2018-02-05 05:25:01    146274             good
267
        #       3333      2018-02-05 05:26:03    146274             good
268
        #       3333      2018-02-05 05:27:05    146274             good
269
        #       3333      2018-02-05 05:29:30    146274             good
270
        ################################################################################################################
271
272
        ################################################################################################################
273
        # bad case 2.2
274
        # id    point_id	utc_date_time	    actual_value	is_bad (expected)
275
        #       3321	    2018-05-15 15:09:54	33934040         good
276
        #       3321	    2018-05-15 15:08:51	33934040         good
277
        #       3321	    2018-05-15 15:07:47	33934040         good
278
        #       3321	    2018-05-15 15:06:44	33934040         good
279
        #       3321	    2018-05-15 15:05:40	33934040         good
280
        #       3321	    2018-05-15 15:04:36	33934040         good
281
        #       3321	    2018-05-15 09:09:00	33928880	     bad
282
        #       3321	    2018-05-15 09:05:23	33933568         good
283
        #       3321	    2018-05-15 09:04:20	33933568         good
284
        #       3321	    2018-05-15 09:03:16	33933568         good
285
        #       3321	    2018-05-15 09:02:13	33933560         good
286
        #       3321	    2018-05-15 09:01:09	33933560         good
287
        #       3321	    2018-05-15 09:00:04	33933560         good
288
        ################################################################################################################
289
290
        ################################################################################################################
291
        # bad case 2.3
292
        # id    point_id	utc_date_time	    actual_value	is_bad (expected)
293
        #       554	        2018-05-19 15:32:52	24001            good
294
        #       554	        2018-05-19 15:30:45	24001            good
295
        #       554	        2018-05-19 15:28:39	24001            good
296
        #       554	        2018-05-19 15:26:32	24001            good
297
        #       554	        2018-05-19 15:24:25	24001            good
298
        #       554	        2018-05-19 15:22:18	24001            good
299
        #       554	        2018-05-19 15:20:10	24001            good
300
        #       554	        2018-05-19 15:18:04	24001            good
301
        #       554	        2018-05-19 15:15:58	24001            good
302
        #       554	        2018-05-19 15:13:51	24001            good
303
        #       554	        2018-05-19 15:11:43	24001            good
304
        #       554	        2018-05-19 15:09:37	24001            good
305
        #       554	        2018-05-19 15:07:29	24000            good
306
        #       554	        2018-05-19 15:05:22	23000	         bad
307
        #       554	        2018-05-19 15:03:14	23999            good
308
        #       554	        2018-05-19 15:01:06	23999            good
309
        #       554	        2018-05-19 14:58:59	23999            good
310
        #       554	        2018-05-19 14:56:52	23998            good
311
        #       554	        2018-05-19 14:54:45	23998            good
312
        #       554	        2018-05-19 14:52:39	23998            good
313
        ################################################################################################################
314
        # todo bad case 2.3.1
315
        # "id", "point_id", "utc_date_time", "actual_value", "is_bad" (actual)
316
        # 68504700, 2, "2021-01-09 03:40:12.0", 40454414.063, 0
317
        # 68507243, 2, "2021-01-09 03:43:12.0", 40454476.563, 0
318
        # 68510030, 2, "2021-01-09 03:47:17.0", 40428074.219, 0 ?
319
        # 68512573, 2, "2021-01-09 03:50:18.0", 40454621.094, 0
320
        # 68515421, 2, "2021-01-09 03:54:23.0", 40454703.125, 0
321
        # 68517964, 2, "2021-01-09 03:57:23.0", 40454761.719, 0
322
323
        ################################################################################################################
324
        # bad case 2.4
325
        # id       point_id utc_date_time          actual_value    is_bad (expected)
326
        # 104373141 3336    2018-01-30 03:04:12    216463.015625   good
327
        # 104373337 3336    2018-01-30 03:05:15    216463.015625   good
328
        # 104373555 3336    2018-01-30 03:06:20    216463.015625   good
329
        # 104373750 3336    2018-01-30 03:07:25    192368.015625   bad
330
        # 104373957 3336    2018-01-30 03:08:29    192368.015625   bad
331
        # 104374175 3336    2018-01-30 03:09:33    192368.015625   bad
332
        # 104374382 3336    2018-01-30 03:10:38    192368.015625   bad
333
        # 104374604 3336    2018-01-30 03:11:42    192368.015625   bad
334
        # 104374792 3336    2018-01-30 03:12:47    192368.015625   bad
335
        # 104375010 3336    2018-01-30 03:13:51    192368.015625   bad
336
        # 104375200 3336    2018-01-30 03:14:55    192368.015625   bad
337
        # 104375418 3336    2018-01-30 03:16:00    192368.015625   bad
338
        # 104375617 3336    2018-01-30 03:17:04    192368.015625   bad
339
        # 104375837 3336    2018-01-30 03:18:08    192368.015625   bad
340
        # 104376023 3336    2018-01-30 03:19:12    192368.015625   bad
341
        # 104376216 3336    2018-01-30 03:20:16    192368.015625   bad
342
        # 104376435 3336    2018-01-30 03:21:21    192368.015625   bad
343
        # 104376634 3336    2018-01-30 03:22:25    192368.015625   bad
344
        # 104376853 3336    2018-01-30 03:23:30    192368.015625   bad
345
        # 104377071 3336    2018-01-30 03:24:34    192368.015625   bad
346
        # 104377274 3336    2018-01-30 03:25:38    192368.015625   bad
347
        # 104377501 3336    2018-01-30 03:26:42    216574.015625   good
348
        # 104377714 3336    2018-01-30 03:27:47    216574.015625   good
349
        ################################################################################################################
350
351
        ################################################################################################################
352
        # bad case 2.5
353
        # id       point_id utc_date_time          actual_value  is_bad (expected)
354
        # 104370839 3334    2018-01-30 02:52:23    844966.0625   good
355
        # 104371064 3334    2018-01-30 02:53:27    844966.0625   good
356
        # 104371261 3334    2018-01-30 02:54:32    844966.0625   good
357
        # 104371479 3334    2018-01-30 02:55:36    826142.0625   bad
358
        # 104371672 3334    2018-01-30 02:56:41    826142.0625   bad
359
        # 104371884 3334    2018-01-30 02:57:45    826142.0625   bad
360
        # 104372110 3334    2018-01-30 02:58:49    826142.0625   bad
361
        # 104372278 3334    2018-01-30 02:59:54    845019.0625   good
362
        # 104372512 3334    2018-01-30 03:00:58    845019.0625   good
363
        # 104372704 3334    2018-01-30 03:02:03    845019.0625   good
364
        ################################################################################################################
365
366
        ################################################################################################################
367
        # bad case 2.6
368
        # 394084273	1001444	2019-08-22 03:39:44	   38969028      good
369
        # 394083709	1001444	2019-08-22 03:38:43    38968876	     good
370
        # 394083145	1001444	2019-08-22 03:37:43    28371884      bad
371
        # 394082019	1001444	2019-08-22 03:35:42    28371884      bad
372
        # 394081456	1001444	2019-08-22 03:34:42    28371884      bad
373
        # 394080892	1001444	2019-08-22 03:33:42    28371884      bad
374
        # 394079200	1001444	2019-08-22 03:30:38    28371884      bad
375
        # 394077511	1001444	2019-08-22 03:27:37    38968408	     good
376
        # 394076947	1001444	2019-08-22 03:26:37    38968236	     good
377
        # 394076384	1001444	2019-08-22 03:25:37    38968060	     good
378
        ################################################################################################################
379
380
        ################################################################################################################
381
        # bad case 2.7
382
        # id       point_id utc_date_time          actual_value   is_bad (expected)
383
        # 17303260 11       2020-3-15 05:43:52     33600          good
384
        # 17303399 11       2020-3-15 05:44:58     33600          good
385
        # 17303538 11       2020-3-15 05:46:04     33600          good
386
        # 17303677 11       2020-3-15 05:47:10     33500          bad
387
        # 17303816 11       2020-3-15 05:48:15     33500          bad
388
        # 17303955 11       2020-3-15 05:49:21     33600          good
389
        # 17304094 11       2020-3-15 05:50:27     33600          good
390
        # 17304233 11       2020-3-15 05:51:33     33600          good
391
        ################################################################################################################
392
393
        try:
394
            query = (" SELECT point_id, id, utc_date_time, actual_value "
395
                     " FROM tbl_energy_value "
396
                     " WHERE utc_date_time >= %s AND utc_date_time <= %s AND is_bad IS NOT TRUE "
397
                     " ORDER BY point_id, utc_date_time ")
398
            cursor_historical.execute(query, (min_datetime, max_datetime,))
399
            rows_energy_values = cursor_historical.fetchall()
400
        except Exception as e:
401
            logger.error("Error in step 3.1 of clean_energy_value.process " + str(e))
402
            if cursor_historical:
403
                cursor_historical.close()
404
            if cnx_historical:
405
                cnx_historical.disconnect()
406
            time.sleep(60)
407
            continue
408
409
        point_value_dict = dict()
410
        current_point_value_list = list()
411
        current_point_id = 0
412
413
        if rows_energy_values is not None and len(rows_energy_values) > 0:
414
            for row_energy_value in rows_energy_values:
415
                previous_point_id = current_point_id
416
                current_point_id = row_energy_value[0]
417
                if current_point_id not in point_value_dict.keys():
418
                    # new point id found
419
                    # save previous point values
420
                    if len(current_point_value_list) > 0:
421
                        point_value_dict[previous_point_id] = current_point_value_list
422
                        current_point_value_list = list()
423
424
                    current_point_value_list.append({'id': row_energy_value[1],
425
                                                     'actual_value': row_energy_value[3]})
426
                else:
427
                    current_point_value_list.append({'id': row_energy_value[1],
428
                                                     'actual_value': row_energy_value[3]})
429
            # end of for loop
430
            # save rest point values
431
            if len(current_point_value_list) > 0:
432
                point_value_dict[current_point_id] = current_point_value_list
433
434
        # reinitialize bad list
435
        bad_list = list()
436
437
        for point_id, point_value_list in point_value_dict.items():
438
            if len(point_value_list) <= 1:
439
                continue
440
            elif len(point_value_list) == 2:
441
                if point_value_list[1]['actual_value'] < point_value_list[0]['actual_value']:
442
                    bad_list.append(point_value_list[1]['id'])
443
                continue
444
            else:
445
                base_point_value = point_value_list[0]['actual_value']
446
                concave_point_value_list = list()
447
                for i in range(len(point_value_list)):
448
                    if point_value_list[i]['actual_value'] < base_point_value:
449
                        # candidate concave value found
450
                        concave_point_value_list.append(point_value_list[i]['id'])
451
                    else:
452
                        # normal value found
453
                        if len(concave_point_value_list) > 0:
454
                            # save confirmed concave value(s) to bad value(s)
455
                            bad_list.extend(concave_point_value_list)
456
457
                        # prepare for next candidate concave value list
458
                        base_point_value = point_value_list[i]['actual_value']
459
                        concave_point_value_list.clear()
460
                continue
461
462
        print('bad list: ' + str(bad_list))
463 View Code Duplication
        if len(bad_list) > 0:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
464
            try:
465
                update = (" UPDATE tbl_energy_value "
466
                          " SET is_bad = TRUE "
467
                          " WHERE id IN (" + ', '.join(map(str, bad_list)) + ")")
468
                cursor_historical.execute(update, )
469
                cnx_historical.commit()
470
            except Exception as e:
471
                logger.error("Error in step 3.2 of clean_energy_value.process " + str(e))
472
                if cursor_historical:
473
                    cursor_historical.close()
474
                if cnx_historical:
475
                    cnx_historical.disconnect()
476
                time.sleep(60)
477
                continue
478
479
        ################################################################################################################
480
        # TODO: bad case 2.8
481
        # id          point_id utc_date_time          actual_value is_bad (expected)
482
        # 105752070    3333    2018-02-04 00:27:15    138144       good
483
        # 105752305    3333    2018-02-04 00:28:19    138144       good
484
        # 105752523    3333    2018-02-04 00:29:22    138144       good
485
        # 105752704    3333    2018-02-04 00:30:26    138144       good
486
        # 105752924    3333    2018-02-04 00:31:30    138144       good
487
        # 105753138    3333    2018-02-04 00:32:34    138144       good
488
        # 105753351    3333    2018-02-04 00:33:38    138144       good
489
        # 105753577    3333    2018-02-04 00:34:42    52776558592  bad?
490
        # 105753794    3333    2018-02-04 00:35:46    52776558592  bad?
491
        # 105753999    3333    2018-02-04 00:36:50    52776558592  bad?
492
        # 105754231    3333    2018-02-04 00:37:54    52776558592  bad?
493
        # 105754443    3333    2018-02-04 00:38:58    52776558592  bad?
494
        # 105754655    3333    2018-02-04 00:40:01    52776558592  bad?
495
        # 105754878    3333    2018-02-04 00:41:06    52776558592  bad?
496
        # 105755092    3333    2018-02-04 00:42:09    52776558592  bad?
497
        # 105755273    3333    2018-02-04 00:43:14    52776558592  bad?
498
        # 105755495    3333    2018-02-04 00:44:17    52776558592  bad?
499
        # 105755655    3333    2018-02-04 00:45:21    52776558592  bad?
500
        # 105755854    3333    2018-02-04 00:46:25    52776558592  bad?
501
        # 105756073    3333    2018-02-04 00:47:29    52776558592  bad?
502
        # 105756272    3333    2018-02-04 00:48:34    52776558592  bad?
503
        # 105756489    3333    2018-02-04 00:49:38    52776558592  bad?
504
        ################################################################################################################
505
506
        ################################################################################################################
507
        # TODO: bad case 2.10
508
        # id       point_id utc_date_time          actual_value   is_bad (expected)
509
        # 106363135 3336    2018-02-06 04:45:57    253079.015625  good
510
        # 106363776 3336    2018-02-06 04:49:09    253079.015625  good
511
        # 106364381 3336    2018-02-06 04:52:21    253079.015625  good
512
        # 106364603 3336    2018-02-06 04:53:25    253079.015625  good
513
        # 106365213 3336    2018-02-06 04:56:37    253079.015625  good
514
        # 106365634 3336    2018-02-06 04:58:45    253079.015625  good
515
        # 106366055 3336    2018-02-06 05:00:53    253079.015625  good
516
        # 106367097 3336    2018-02-06 05:06:12    259783.015625  bad?
517
        # 106367507 3336    2018-02-06 05:08:21    259783.015625  bad?
518
        # 106368318 3336    2018-02-06 05:12:37    259783.015625  bad?
519
        # 106368732 3336    2018-02-06 05:14:44    259783.015625  bad?
520
        # 106368952 3336    2018-02-06 05:15:48    259783.015625  bad?
521
        # 106369145 3336    2018-02-06 05:16:52    259783.015625  bad?
522
        # 106369353 3336    2018-02-06 05:17:56    259783.015625  bad?
523
        ################################################################################################################
524
525
        ################################################################################################################
526
        # TODO: bad case 2.11
527
        # id       point_id utc_date_time          actual_value   is_bad (expected)
528
        # 14784589 21	    2020-03-05 07:22:22    17990           good
529
        # 14784450 21	    2020-03-05 07:21:17    17990           good
530
        # 14784311 21	    2020-03-05 07:20:10    17990           good
531
        # 14784172 21	    2020-03-05 07:19:04    17990           good
532
        # 14784033 21	    2020-03-05 07:17:58    18990           bad
533
        # 14783894 21	    2020-03-05 07:16:52    17990           good
534
        # 14783755 21	    2020-03-05 07:15:46    17990           good
535
        # 14783616 21	    2020-03-05 07:14:40    17990           good
536
        # 14783477 21	    2020-03-05 07:13:34    17990           good
537
        # 14783338 21	    2020-03-05 07:12:28    17990           good
538
        # 14783199 21	    2020-03-05 07:11:22    17990           good
539
        ################################################################################################################
540
541
        ################################################################################################################
542
        # TODO: bad case 2.12
543
        # id       point_id utc_date_time          actual_value   is_bad (expected)
544
        # 3337308  21       2020-01-07 09:02:18    7990           good
545
        # 3337174  21       2020-01-07 09:01:13    7990	          good
546
        # 3337040  21       2020-01-07 09:00:08    7990	          good
547
        # 3336906  21       2020-01-07 08:59:04    7990	          good
548
        # 3336772  21       2020-01-07 08:57:59    7990	          good
549
        # 3336638  21       2020-01-07 08:56:54    8990	          bad
550
        # 3336504  21       2020-01-07 08:55:49    7990	          good
551
        # 3336370  21       2020-01-07 08:54:44    7990	          good
552
        # 3336236  21       2020-01-07 08:53:39    7990	          good
553
        # 3336102  21       2020-01-07 08:52:34    7990	          good
554
        # 3335968  21       2020-01-07 08:51:30    7990	          good
555
        ################################################################################################################
556
        # Step 4: tag the is_bad property of energy values.
557
        ################################################################################################################
558
        try:
559
            update = (" UPDATE tbl_energy_value "
560
                      " SET is_bad = FALSE "
561
                      " WHERE utc_date_time >= %s AND utc_date_time < %s AND is_bad IS NULL ")
562
            # NOTE: use '<' instead of '<=' in WHERE statement because there may be some new inserted values
563
            cursor_historical.execute(update, (min_datetime, max_datetime,))
564
            cnx_historical.commit()
565
        except Exception as e:
566
            logger.error("Error in step 4 of clean_energy_value.process " + str(e))
567
            time.sleep(60)
568
            continue
569
        finally:
570
            if cursor_historical:
571
                cursor_historical.close()
572
            if cnx_historical:
573
                cnx_historical.disconnect()
574
575
        time.sleep(900)
576