1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
from pot_libs.logger import log
from pot_libs.mysql_util.mysql_util import MysqlUtil
from unify_api.modules.common.procedures.cids import get_cid_info
from unify_api.modules.common.procedures.power_cps import inline_power_use_info
async def get_inline_datas_dao(cids):
inline_sql = f"select `inlid`, `name`, cid cid, " \
f"`inline_tc`, `tc_runtime` from `inline` " \
f"where cid in %s order by cid"
async with MysqlUtil() as conn:
inlines = await conn.fetchall(inline_sql, args=(cids,)) if cids else []
inline_map = {i["inlid"]: i for i in inlines}
inline_cid_map = {i["inlid"]: i["cid"] for i in inlines}
return inline_map, inline_cid_map
async def get_power_factor_results(inline_ids, month_str):
async with MysqlUtil() as conn:
pf_sql = f"select inlid,cos,save_charge pf_cost,save_charge, kpi_x " \
f" from algo_power_factor_result where inlid in %s " \
f"and month = %s"
power_factors = (
await conn.fetchall(pf_sql, args=(
inline_ids, month_str)) if inline_ids else []
)
return power_factors
async def proxy_power_factor_summary(cids, month_str, inline_map=None):
if not inline_map:
inline_map, inline_cid_map = await get_inline_datas_dao(cids)
inline_ids = list(inline_map.keys())
power_factors = await get_power_factor_results(inline_ids, month_str)
unqualified_cnt = 0
first_level_cnt = 0
second_level_cnt = 0
third_level_cnt = 0
fourth_level_cnt = 0
fifth_level_cnt = 0
total_save_charge = round(
sum(
[
round(i["save_charge"], 2)
for i in power_factors
if type(i["save_charge"]) in [int, float] and i[
"save_charge"] >= 0
]
),
2,
)
for pf_res in power_factors:
power_factor = pf_res["kpi_x"]
if power_factor < 0.9:
unqualified_cnt += 1
if power_factor < 0.8:
fifth_level_cnt += 1
elif 0.8 <= power_factor < 0.85:
fourth_level_cnt += 1
elif 0.85 <= power_factor < 0.9:
third_level_cnt += 1
elif 0.9 <= power_factor < 0.95:
second_level_cnt += 1
else:
first_level_cnt += 1
return {
"total_cnt": len(power_factors),
"unqualified_cnt": unqualified_cnt,
"first_level_cnt": first_level_cnt,
"second_level_cnt": second_level_cnt,
"third_level_cnt": third_level_cnt,
"fourth_level_cnt": fourth_level_cnt,
"fifth_level_cnt": fifth_level_cnt,
"total_save_charge": total_save_charge,
}
async def proxy_power_factor_list(
cids,
month_str,
page_size=10,
page_num=1,
sort_field="company_name",
sort_direction="asc",
status_list=None,
):
inline_map, inline_cid_map = await get_inline_datas_dao(cids)
company_map = await get_cid_info(cids)
inline_ids = list(inline_map.keys())
power_factors = await get_power_factor_results(inline_ids, month_str)
all_list = []
for pf_res in power_factors:
desc = "奖励" if pf_res["kpi_x"] >= 0.9 else "罚款"
if status_list and desc not in status_list:
continue
inline_id = pf_res["inlid"]
cid = inline_cid_map[inline_id]
all_list.append(
{
"cid": cid,
"company_name": company_map[cid]["shortname"],
"inline_id": inline_id,
"inline_name": inline_map[inline_id]["name"],
"power_factor": pf_res["kpi_x"],
"status": "奖励" if pf_res["kpi_x"] >= 0.9 else "罚款",
"save_charge": pf_res["save_charge"] if pf_res[
"save_charge"] > 0 else 0,
}
)
if sort_field in ["company_name", "power_factor", "save_charge"]:
all_list.sort(
key=lambda x: x[sort_field],
reverse=True if sort_direction == "desc" else False,
)
return {
"rows": all_list[page_size * (page_num - 1): page_size * page_num],
"total": len(all_list),
}
async def get_md_space_result(inline_ids, month_str):
async with MysqlUtil() as conn:
sql = (
"select `month`, `inline_md_charge`, `inline_tc_charge`, "
"`inline_md_predict`, `save_charge`, `kpi_x`, "
"inlid `related_inlids` "
"from `algo_md_space_analysis_result` "
"inner join`algo_md_space_analysis_unit`"
" on `algo_md_space_analysis_result`.space_analysis_id=`algo_md_space_analysis_unit`.id "
"where inlid in %s and `month` = %s and valid=1;"
)
md_spaces = await conn.fetchall(sql, args=(inline_ids, month_str))
return md_spaces
async def proxy_md_space_list(
cids,
month_str,
page_size=10,
page_num=1,
sort_field="company_name",
sort_direction="asc",
status_list=None,
):
inline_map, inline_cid_map = await get_inline_datas_dao(cids)
company_map = await get_cid_info(cids)
inline_ids = list(inline_map.keys())
md_spaces = await get_md_space_result(inline_ids, month_str)
all_list = []
for mdp_res in md_spaces:
md_space_kpi_x = mdp_res.get("kpi_x")
if type(md_space_kpi_x) in [int, float]:
desc = "有空间" if md_space_kpi_x > 0 else "无空间"
else:
desc = "-"
if status_list and desc not in status_list:
continue
inline_id = mdp_res["related_inlids"]
cid = inline_cid_map[inline_id]
all_list.append(
{
"cid": cid,
"company_name": company_map[cid]["shortname"],
"inline_id": inline_id,
"inline_name": inline_map[inline_id]["name"],
"inline_tc": inline_map[inline_id]["inline_tc"],
"tc_runtime": inline_map[inline_id]["tc_runtime"],
"status": desc,
"save_charge": mdp_res["save_charge"] if mdp_res[
"save_charge"] > 0 else 0,
}
)
if sort_field in ["company_name", "save_charge"]:
all_list.sort(
key=lambda x: x[sort_field],
reverse=True if sort_direction == "desc" else False,
)
return {
"rows": all_list[page_size * (page_num - 1): page_size * page_num],
"total": len(all_list),
}
async def proxy_md_space_summary(cids, month_str, inline_map=None):
if not inline_map:
inline_map, inline_cid_map = await get_inline_datas_dao(cids)
inline_ids = list(inline_map.keys())
md_spaces = await get_md_space_result(inline_ids, month_str)
big_space_cnt = 0
proper_space_cnt = 0
small_space_cnt = 0
no_space_cnt = 0
exits_space_cnt = 0
total_save_charge = round(
sum(
[
round(i["save_charge"], 2)
for i in md_spaces
if type(i["save_charge"]) in [int, float] and i[
"save_charge"] >= 0
]
),
2,
)
for mdp_res in md_spaces:
md_space_kpi_x = mdp_res.get("kpi_x")
if type(md_space_kpi_x) in [int, float]:
if md_space_kpi_x <= 0:
no_space_cnt += 1
elif 0 < md_space_kpi_x <= 0.1:
small_space_cnt += 1
exits_space_cnt += 1
elif 0.1 < md_space_kpi_x <= 0.2:
proper_space_cnt += 1
exits_space_cnt += 1
else:
big_space_cnt += 1
exits_space_cnt += 1
else:
log.error(f"mdp_res={mdp_res} 无法判断需量空间")
return {
"total_cnt": len(md_spaces),
"exits_space_cnt": exits_space_cnt,
"big_space_cnt": big_space_cnt,
"proper_space_cnt": proper_space_cnt,
"small_space_cnt": small_space_cnt,
"no_space_cnt": no_space_cnt,
"total_save_charge": total_save_charge,
}
async def get_power_save_result(inline_ids, month_str):
async with MysqlUtil() as conn:
sql = "select `inlid`, `cost_loss_percent`, `mean_load_factor`, " \
"`kpi_x`, `save_charge` from `algo_economic_operation_result` " \
"where `inlid` in %s and `month` = %s"
power_save_res_list = await conn.fetchall(sql,
args=(inline_ids, month_str))
return power_save_res_list
async def proxy_power_save_list(
cids, month_str, page_size=10, page_num=1, sort_field="company_name",
sort_direction="asc",
):
async with MysqlUtil() as conn:
inline_sql = f"select `inlid`, `name`,cid cid, " \
f"`inline_tc`, `tc_runtime` from `inline` " \
f"where cid in %s order by cid"
inlines = await conn.fetchall(inline_sql, args=(cids,)) if cids else []
inline_map = {i["inlid"]: i for i in inlines}
inline_cid_map = {i["inlid"]: i["cid"] for i in inlines}
company_map = await get_cid_info(cids)
inline_ids = list(inline_map.keys())
power_save_res_list = await get_power_save_result(inline_ids, month_str)
all_list = []
for power_save_res in power_save_res_list:
inline_id = power_save_res["inlid"]
cid = inline_cid_map[inline_id]
all_list.append(
{
"cid": cid,
"company_name": company_map[cid]["shortname"],
"inline_id": inline_id,
"inline_name": inline_map[inline_id]["name"],
"inline_tc": inline_map[inline_id]["inline_tc"],
"tc_runtime": inline_map[inline_id]["tc_runtime"],
"avg_load_rate": power_save_res["mean_load_factor"],
"cos_loss_rate": power_save_res["cost_loss_percent"],
"save_charge": power_save_res["save_charge"]
if power_save_res["save_charge"] > 0
else 0,
}
)
if sort_field in ["company_name", "avg_load_rate", "save_charge"]:
all_list.sort(
key=lambda x: x[sort_field],
reverse=True if sort_direction == "desc" else False,
)
return {
"rows": all_list[page_size * (page_num - 1): page_size * page_num],
"total": len(all_list),
}
async def proxy_power_save_summary(
cids, month_str, inline_map=None,
):
if not inline_map:
# 为了代码复用,减少数据库查询,如果外部传了进线信息,那么没必要再查询了
async with MysqlUtil() as conn:
inline_sql = f"select `inlid`, `name`, cid `cid`, " \
f"`inline_tc`, `tc_runtime` from `inline` " \
f"where cid in %s order by cid"
inlines = await conn.fetchall(inline_sql,
args=(cids,)) if cids else []
inline_map = {i["inlid"]: i for i in inlines}
inline_ids = list(inline_map.keys())
power_save_res_list = await get_power_save_result(inline_ids, month_str)
big_space_cnt = 0
proper_space_cnt = 0
small_space_cnt = 0
no_space_cnt = 0
exits_space_cnt = 0
total_save_charge = round(
sum(
[
round(i["save_charge"], 2)
for i in power_save_res_list
if type(i["save_charge"]) in [int, float] and i[
"save_charge"] >= 0
]
),
2,
)
for power_save_res in power_save_res_list:
kpi_x = power_save_res.get("kpi_x")
if type(kpi_x) in [int, float]:
if kpi_x <= 0.6:
no_space_cnt += 1
elif kpi_x > 0.6 and kpi_x <= 0.7:
small_space_cnt += 1
exits_space_cnt += 1
elif kpi_x > 0.7 and kpi_x <= 0.8:
proper_space_cnt += 1
exits_space_cnt += 1
else:
big_space_cnt += 1
exits_space_cnt += 1
else:
log.error(
f"power_save_res = {power_save_res} kpi_x={kpi_x}无法评价经济运行空间")
return {
"total_cnt": len(power_save_res_list),
"exits_space_cnt": exits_space_cnt,
"big_space_cnt": big_space_cnt,
"proper_space_cnt": proper_space_cnt,
"small_space_cnt": small_space_cnt,
"no_space_cnt": no_space_cnt,
"total_save_charge": total_save_charge,
}
async def get_pcvf_result(inline_ids, month_str):
async with MysqlUtil() as conn:
sql = "select `inlid`, `score`, `cost_save` from `algo_plsi_result` " \
"where `inlid` in %s and `month` = %s"
power_pcvfs = await conn.fetchall(sql, args=(inline_ids, month_str))
return power_pcvfs
async def proxy_pcvf_list(
cids, month_str, page_size=10, page_num=1, sort_field="company_name",
sort_direction="asc",
):
"""
移峰填谷指数
:param cids:
:param month_str:
:param page_size:
:param page_num:
:param sort_field:
:param sort_direction:
:return:
"""
inline_map, inline_cid_map = await get_inline_datas_dao(cids)
company_map = await get_cid_info(cids)
inline_ids = list(inline_map.keys())
power_pcvfs = await get_pcvf_result(inline_ids, month_str)
all_list = []
for power_pcvf in power_pcvfs:
inline_id = power_pcvf["inlid"]
cid = inline_cid_map[inline_id]
all_list.append(
{
"cid": cid,
"company_name": company_map[cid]["shortname"],
"inline_id": inline_id,
"inline_name": inline_map[inline_id]["name"],
"pcvf_index": power_pcvf["score"],
"save_charge": power_pcvf["cost_save"] if power_pcvf[
"cost_save"] > 0 else 0,
}
)
all_inline_ids = [i["inline_id"] for i in all_list]
if not all_inline_ids:
return {"rows": [], "total": 0}
# power_info_map = await inline_power_use_info(all_inline_ids, month_str)
power_info_map = await inline_power_use_info(all_inline_ids, month_str)
for i in all_list:
power = 0
charge = 0
avg_price = 0
inline_id = i["inline_id"]
if inline_id in power_info_map:
charge = round(power_info_map[inline_id]["charge"], 2)
power = round(power_info_map[inline_id]["kwh"], 2)
avg_price = round(charge / power, 2) if power else 0
i["avg_price"] = avg_price
i["power"] = power
i["charge"] = charge
if sort_field in ["company_name", "avg_price", "pcvf_index",
"save_charge"]:
all_list.sort(
key=lambda x: x[sort_field],
reverse=True if sort_direction == "desc" else False,
)
page_rows = all_list[page_size * (page_num - 1): page_size * page_num]
return {
"rows": page_rows,
"total": len(all_list),
}
async def proxy_pcvf_summary(
cids, month_str, inline_map=None,
):
if not inline_map:
inline_map, inline_cid_map = await get_inline_datas_dao(cids)
inline_ids = list(inline_map.keys())
power_pcvf_res_list = await get_pcvf_result(inline_ids, month_str)
total_save_charge = round(
sum(
[
round(i["cost_save"], 2)
for i in power_pcvf_res_list
if type(i["cost_save"]) in [int, float] and i["cost_save"] >= 0
]
),
2,
)
big_space_cnt = 0
proper_space_cnt = 0
small_space_cnt = 0
no_space_cnt = 0
exits_space_cnt = 0
for power_pcvf_res in power_pcvf_res_list:
score = power_pcvf_res.get("score")
if type(score) in [int, float]:
if score >= 90:
no_space_cnt += 1
elif 80 < score < 90:
small_space_cnt += 1
exits_space_cnt += 1
elif 70 < score <= 80:
proper_space_cnt += 1
exits_space_cnt += 1
else:
big_space_cnt += 1
exits_space_cnt += 1
else:
log.error(
f"power_save_res = {power_pcvf_res} score={score}无法评价移峰填谷空间")
return {
"total_cnt": len(power_pcvf_res_list),
"exits_space_cnt": exits_space_cnt,
"big_space_cnt": big_space_cnt,
"proper_space_cnt": proper_space_cnt,
"small_space_cnt": small_space_cnt,
"no_space_cnt": no_space_cnt,
"total_save_charge": total_save_charge,
}
async def proxy_electric_optimization_summary(cids, month_str):
inline_sql = f"select inlid,name,cid cid,inline_tc,tc_runtime" \
f" from inline where `cid` in %s order by cid"
async with MysqlUtil() as conn:
inlines = await conn.fetchall(inline_sql, args=(cids,)) if cids else []
inline_map = {i["inlid"]: i for i in inlines}
inline_ids = list(inline_map.keys())
power_factor_summary_map = await proxy_power_factor_summary(
inline_ids, month_str, inline_map=inline_map
)
md_space_summary_map = await proxy_md_space_summary(
inline_ids, month_str, inline_map=inline_map
)
power_save_summary_map = await proxy_power_save_summary(
inline_ids, month_str, inline_map=inline_map
)
power_pcvf_summary_map = await proxy_pcvf_summary(inline_ids, month_str,
inline_map=inline_map)
return {
"power_factor": {
"can_optimiz_cnt": power_factor_summary_map["unqualified_cnt"],
"save_charge": power_factor_summary_map["total_save_charge"],
},
"md_space": {
"can_optimiz_cnt": md_space_summary_map["exits_space_cnt"],
"save_charge": md_space_summary_map["total_save_charge"],
},
"pcvf": {
"can_optimiz_cnt": power_pcvf_summary_map["exits_space_cnt"],
"save_charge": power_pcvf_summary_map["total_save_charge"],
},
"power_save": {
"can_optimiz_cnt": power_save_summary_map["exits_space_cnt"],
"save_charge": power_save_summary_map["total_save_charge"],
},
}