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