Issues (1647)

myems-cleaning/clean_energy_value.py (2 issues)

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