fine_monitor_pds.py 23.2 KB
Newer Older
wang.wenrong's avatar
wang.wenrong committed
1 2 3 4
from pot_libs.logger import log
from pot_libs.mysql_util.mysql_util import MysqlUtil
from pot_libs.es_util.es_utils import EsUtil
from unify_api import constants
wang.wenrong's avatar
wang.wenrong committed
5
import pandas as pd
wang.wenrong's avatar
wang.wenrong committed
6 7 8 9 10 11 12


async def get_location_by_ids(location_ids):
    '''
    根据location_id获取各项温度信息
    '''
    async with MysqlUtil() as conn:
wang.wenrong's avatar
wang.wenrong committed
13
        sql = "select lid,item,ad_type from location where lid in %s"
wang.wenrong's avatar
wang.wenrong committed
14
        result = await conn.fetchall(sql, args=(tuple(location_ids),))
wang.wenrong's avatar
wang.wenrong committed
15
        location_info = {res.get('lid'): res for res in result}
wang.wenrong's avatar
wang.wenrong committed
16 17 18
    return location_info


wang.wenrong's avatar
wang.wenrong committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
async def get_mtid_by_location_ids(location_ids):
    """
    根据location——id获取mtid
    """
    async with MysqlUtil() as conn:
        sql = f"""
            SELECT
                    m.mtid,
                    m.`name`,
                    m.m_type,
                    l.lid,
                    l.item,
                    l.ad_type 
            FROM
                    monitor m
                    INNER JOIN location l ON m.mtid = l.mtid 
            WHERE
                    l.lid in {tuple(location_ids)}
                    AND m.demolished = 0
            """
        result = await conn.fetchall(sql, )
    return result


wang.wenrong's avatar
wang.wenrong committed
43 44 45 46 47 48
async def get_threshold_by_location(location_ids, type='overResidualCurrent',
                                    default_threshold=30):
    '''
    根据location_id获取阈值
    '''
    async with MysqlUtil() as conn:
wang.wenrong's avatar
wang.wenrong committed
49 50
        sql = "select threshold from soe_config_record where lid in %s " \
              "and etype = %s limit 1 "
wang.wenrong's avatar
wang.wenrong committed
51 52 53 54 55 56 57 58 59 60 61 62 63
        settings = await conn.fetchall(sql, args=(tuple(location_ids), type))
        if settings:
            return settings[0]["threshold"]
    return default_threshold


async def get_es_aiao_15min_data(query_body):
    '''
    从es中获取环境相关数据(15min)
    '''
    async with EsUtil() as es:
        es_results = await es.search_origin(body=query_body,
                                            index=constants.LOCATION_15MIN_AIAO)
lcn's avatar
lcn committed
64
    
wang.wenrong's avatar
wang.wenrong committed
65 66 67 68 69 70 71 72 73 74
    return es_results.get("aggregations", {})


async def get_es_point_15min_data(query_body):
    '''
    从es中获取电力相关数据(15min)
    '''
    async with EsUtil() as es:
        es_results = await es.search_origin(body=query_body,
                                            index=constants.POINT_15MIN_INDEX)
lcn's avatar
lcn committed
75
    
wang.wenrong's avatar
wang.wenrong committed
76
    return es_results.get("aggregations", {})
wang.wenrong's avatar
wang.wenrong committed
77 78 79 80 81 82 83 84 85 86 87 88


async def get_es_point_1min_data(query_body, start):
    '''
    从es中获取电气相关数据(1min)
    '''
    async with EsUtil() as es:
        # 电气相关数据1min的需要分表查询
        p_database = "poweriot_point_1min_index_" + start[:4] + "_" + \
                     str(int(start[5:7]))
        es_results = await es.search_origin(body=query_body,
                                            index=p_database)
lcn's avatar
lcn committed
89
    
wang.wenrong's avatar
wang.wenrong committed
90
    return es_results.get("hits", {}).get("hits", {})
wang.wenrong's avatar
wang.wenrong committed
91 92 93 94 95


async def get_aiao_1min_pds(slots, temp_res_data, res_curr_th):
    temp, res, s_value, a_value, b_value, c_value, n_value = [], [], {
        "item": "漏电流", "threhold": res_curr_th}, {"item": "A相"}, {
lcn's avatar
lcn committed
96 97 98 99
        "item": "B相"}, {
        "item": "C相"}, {
        "item": "N相"}
    
wang.wenrong's avatar
wang.wenrong committed
100 101 102 103 104 105 106
    temp1, temp2, temp3, temp4, res_curr = {}, {}, {}, {}, {}
    [temp1.update({i.get("ts"): i.get("temp1")}) for i in temp_res_data]
    [temp2.update({i.get("ts"): i.get("temp2")}) for i in temp_res_data]
    [temp3.update({i.get("ts"): i.get("temp3")}) for i in temp_res_data]
    [temp4.update({i.get("ts"): i.get("temp4")}) for i in temp_res_data]
    [res_curr.update({i.get("ts"): i.get("residual_current")}) for i in
     temp_res_data]
lcn's avatar
lcn committed
107
    
wang.wenrong's avatar
wang.wenrong committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    a_slot, b_slot, c_slot, n_slot, res_slot = [], [], [], [], []
    [a_slot.append(temp1.get(i, "")) for i in slots]
    [b_slot.append(temp2.get(i, "")) for i in slots]
    [c_slot.append(temp3.get(i, "")) for i in slots]
    [n_slot.append(temp4.get(i, "")) for i in slots]
    [res_slot.append(res_curr.get(i, "")) for i in slots]
    a_value.update({"value_slots": a_slot})
    b_value.update({"value_slots": b_slot})
    c_value.update({"value_slots": c_slot})
    n_value.update({"value_slots": n_slot})
    s_value.update({"value_slots": res_slot})
    temp.append(a_value)
    temp.append(b_value)
    temp.append(c_value)
    temp.append(n_value)
    res.append(s_value)
lcn's avatar
lcn committed
124
    
wang.wenrong's avatar
wang.wenrong committed
125 126 127 128 129 130
    return temp, res


async def get_aiao_data_pds(slots, temp_res_data, res_curr_th):
    temp, res, s_value, a_value, b_value, c_value, n_value = [], [], {
        "item": "漏电流", "threhold": res_curr_th}, {"item": "A相"}, {
lcn's avatar
lcn committed
131 132
        "item": "B相"}, {"item": "C相"}, {"item": "N线"}
    
wang.wenrong's avatar
wang.wenrong committed
133 134 135 136 137 138 139 140 141 142 143 144
    temp1, temp2, temp3, temp4, res_curr = {}, {}, {}, {}, {}
    for i in temp_res_data:
        if i.get('ad_field') == 'temp1':
            temp1.update({i.get("create_time"): i.get("value_avg")})
        elif i.get('ad_field') == "temp2":
            temp2.update({i.get("create_time"): i.get("value_avg")})
        elif i.get('ad_field') == "temp3":
            temp3.update({i.get("create_time"): i.get("value_avg")})
        elif i.get('ad_field') == "temp4":
            temp4.update({i.get("create_time"): i.get("value_avg")})
        else:
            res_curr.update({i.get("create_time"): i.get("value_avg")})
lcn's avatar
lcn committed
145
    
wang.wenrong's avatar
wang.wenrong committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    a_slot, b_slot, c_slot, n_slot, res_slot = [], [], [], [], []
    [a_slot.append(temp1.get(i, "")) for i in slots]
    [b_slot.append(temp2.get(i, "")) for i in slots]
    [c_slot.append(temp3.get(i, "")) for i in slots]
    [n_slot.append(temp4.get(i, "")) for i in slots]
    [res_slot.append(res_curr.get(i, "")) for i in slots]
    a_value.update({"value_slots": a_slot})
    b_value.update({"value_slots": b_slot})
    c_value.update({"value_slots": c_slot})
    n_value.update({"value_slots": n_slot})
    s_value.update({"value_slots": res_slot})
    temp.append(a_value)
    temp.append(b_value)
    temp.append(c_value)
    temp.append(n_value)
    res.append(s_value)
    return temp, res


async def get_point_1min_chart_pds(ctnum, slots, data):
    if ctnum == 3:
        i, v, power, ia_value, ib_value, ic_value, ua_value, ub_value, uc_value, pttl_value, qttl_value \
            = [], [], [], {"item": "ia"}, {"item": "ib"}, {"item": "ic"}, {
            "item": "ua"}, {"item": "ub"}, {"item": "uc"}, {"item": "pttl"}, {
lcn's avatar
lcn committed
170 171
            "item": "qttl"}
        
wang.wenrong's avatar
wang.wenrong committed
172 173 174 175 176 177 178 179 180 181
        ia_dict, ib_dict, ic_dict, pttl_dict, qttl_dict, ua_dict, ub_dict, uc_dict \
            = {}, {}, {}, {}, {}, {}, {}, {}
        [ia_dict.update({i.get("ts"): i.get("ia")}) for i in data]
        [ib_dict.update({i.get("ts"): i.get("ib")}) for i in data]
        [ic_dict.update({i.get("ts"): i.get("ic")}) for i in data]
        [pttl_dict.update({i.get("ts"): i.get("pttl")}) for i in data]
        [qttl_dict.update({i.get("ts"): i.get("qttl")}) for i in data]
        [ua_dict.update({i.get("ts"): i.get("ua")}) for i in data]
        [ub_dict.update({i.get("ts"): i.get("ub")}) for i in data]
        [uc_dict.update({i.get("ts"): i.get("uc")}) for i in data]
lcn's avatar
lcn committed
182
        
wang.wenrong's avatar
wang.wenrong committed
183 184
        ia_list, ib_list, ic_list, pttl_list, qttl_list, ua_list, ub_list, uc_list \
            = [], [], [], [], [], [], [], []
lcn's avatar
lcn committed
185
        
wang.wenrong's avatar
wang.wenrong committed
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
        [ia_list.append(ia_dict.get(i, "")) for i in slots]
        [ib_list.append(ia_dict.get(i, "")) for i in slots]
        [ic_list.append(ia_dict.get(i, "")) for i in slots]
        [pttl_list.append(ia_dict.get(i, "")) for i in slots]
        [qttl_list.append(ia_dict.get(i, "")) for i in slots]
        [ua_list.append(ia_dict.get(i, "")) for i in slots]
        [ub_list.append(ia_dict.get(i, "")) for i in slots]
        [uc_list.append(ia_dict.get(i, "")) for i in slots]
        ia_value.update({"value_slots": ia_list})
        ib_value.update({"value_slots": ib_list})
        ic_value.update({"value_slots": ic_list})
        pttl_value.update({"value_slots": pttl_list})
        qttl_value.update({"value_slots": qttl_list})
        ua_value.update({"value_slots": ua_list})
        ub_value.update({"value_slots": ub_list})
        uc_value.update({"value_slots": uc_list})
        i.append(ia_value)
        i.append(ib_value)
        i.append(ic_value)
        v.append(ua_value)
        v.append(ub_value)
        v.append(uc_value)
        power.append(pttl_value)
        power.append(qttl_value)
    else:
        i, v, power, ia_value, ic_value, uab_value, ucb_value, pttl_value, qttl_value \
            = [], [], [], {"item": "ia"}, {"item": "ic"}, {
            "item": "uab"}, {"item": "ucb"}, {"item": "pttl"}, {"item": "qttl"}
lcn's avatar
lcn committed
214
        
wang.wenrong's avatar
wang.wenrong committed
215 216 217 218 219 220 221 222
        ia_dict, ic_dict, pttl_dict, qttl_dict, uab_dict, ucb_dict, \
            = {}, {}, {}, {}, {}, {}
        [ia_dict.update({i.get("ts"): i.get("ia")}) for i in data]
        [ic_dict.update({i.get("ts"): i.get("ic")}) for i in data]
        [pttl_dict.update({i.get("ts"): i.get("pttl")}) for i in data]
        [qttl_dict.update({i.get("ts"): i.get("qttl")}) for i in data]
        [uab_dict.update({i.get("ts"): i.get("ua")}) for i in data]
        [ucb_dict.update({i.get("ts"): i.get("ub")}) for i in data]
lcn's avatar
lcn committed
223
        
wang.wenrong's avatar
wang.wenrong committed
224 225
        ia_list, ic_list, pttl_list, qttl_list, uab_list, ucb_list \
            = [], [], [], [], [], []
lcn's avatar
lcn committed
226
        
wang.wenrong's avatar
wang.wenrong committed
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
        [ia_list.append(ia_dict.get(i, "")) for i in slots]
        [ic_list.append(ia_dict.get(i, "")) for i in slots]
        [pttl_list.append(ia_dict.get(i, "")) for i in slots]
        [qttl_list.append(ia_dict.get(i, "")) for i in slots]
        [uab_list.append(ia_dict.get(i, "")) for i in slots]
        [ucb_list.append(ia_dict.get(i, "")) for i in slots]
        ia_value.update({"value_slots": ia_list})
        ic_value.update({"value_slots": ic_list})
        pttl_value.update({"value_slots": pttl_list})
        qttl_value.update({"value_slots": qttl_list})
        uab_value.update({"value_slots": uab_list})
        ucb_value.update({"value_slots": ucb_list})
        i.append(ia_value)
        i.append(ic_value)
        v.append(uab_value)
        v.append(ucb_value)
        power.append(pttl_value)
        power.append(qttl_value)
    return i, v, power


async def get_point_data_chart_pds(ctnum, slots, data):
    if ctnum == 3:
        i, v, power, ia_value, ib_value, ic_value, ua_value, ub_value, uc_value, pttl_value, qttl_value \
            = [], [], [], {"item": "ia"}, {"item": "ib"}, {"item": "ic"}, {
            "item": "ua"}, {"item": "ub"}, {"item": "uc"}, {"item": "pttl"}, {
lcn's avatar
lcn committed
253 254
            "item": "qttl"}
        
wang.wenrong's avatar
wang.wenrong committed
255 256
        ia_dict, ib_dict, ic_dict, pttl_dict, qttl_dict, ua_dict, ub_dict, uc_dict \
            = {}, {}, {}, {}, {}, {}, {}, {}
wang.wenrong's avatar
wang.wenrong committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
        [ia_dict.update({i.get("create_time"): i.get("ia_mean")}) for i in
         data]
        [ib_dict.update({i.get("create_time"): i.get("ib_mean")}) for i in
         data]
        [ic_dict.update({i.get("create_time"): i.get("ic_mean")}) for i in
         data]
        [pttl_dict.update({i.get("create_time"): i.get("pttl_mean")}) for i in
         data]
        [qttl_dict.update({i.get("create_time"): i.get("qttl_mean")}) for i in
         data]
        [ua_dict.update({i.get("create_time"): i.get("ua_mean")}) for i in
         data]
        [ub_dict.update({i.get("create_time"): i.get("ub_mean")}) for i in
         data]
        [uc_dict.update({i.get("create_time"): i.get("uc_mean")}) for i in
         data]
lcn's avatar
lcn committed
273
        
wang.wenrong's avatar
wang.wenrong committed
274 275
        ia_list, ib_list, ic_list, pttl_list, qttl_list, ua_list, ub_list, uc_list \
            = [], [], [], [], [], [], [], []
lcn's avatar
lcn committed
276
        
wang.wenrong's avatar
wang.wenrong committed
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
        [ia_list.append(ia_dict.get(i, "")) for i in slots]
        [ib_list.append(ia_dict.get(i, "")) for i in slots]
        [ic_list.append(ia_dict.get(i, "")) for i in slots]
        [pttl_list.append(ia_dict.get(i, "")) for i in slots]
        [qttl_list.append(ia_dict.get(i, "")) for i in slots]
        [ua_list.append(ia_dict.get(i, "")) for i in slots]
        [ub_list.append(ia_dict.get(i, "")) for i in slots]
        [uc_list.append(ia_dict.get(i, "")) for i in slots]
        ia_value.update({"value_slots": ia_list})
        ib_value.update({"value_slots": ib_list})
        ic_value.update({"value_slots": ic_list})
        pttl_value.update({"value_slots": pttl_list})
        qttl_value.update({"value_slots": qttl_list})
        ua_value.update({"value_slots": ua_list})
        ub_value.update({"value_slots": ub_list})
        uc_value.update({"value_slots": uc_list})
        i.append(ia_value)
        i.append(ib_value)
        i.append(ic_value)
        v.append(ua_value)
        v.append(ub_value)
        v.append(uc_value)
        power.append(pttl_value)
        power.append(qttl_value)
    else:
        i, v, power, ia_value, ic_value, uab_value, ucb_value, pttl_value, qttl_value \
            = [], [], [], {"item": "ia"}, {"item": "ic"}, {
            "item": "uab"}, {"item": "ucb"}, {"item": "pttl"}, {"item": "qttl"}
lcn's avatar
lcn committed
305
        
wang.wenrong's avatar
wang.wenrong committed
306 307
        ia_dict, ic_dict, pttl_dict, qttl_dict, uab_dict, ucb_dict, \
            = {}, {}, {}, {}, {}, {}
wang.wenrong's avatar
wang.wenrong committed
308 309 310 311 312 313 314 315 316 317 318 319
        [ia_dict.update({i.get("create_time"): i.get("ia_mean")}) for i in
         data]
        [ic_dict.update({i.get("create_time"): i.get("ic_mean")}) for i in
         data]
        [pttl_dict.update({i.get("create_time"): i.get("pttl_mean")}) for i in
         data]
        [qttl_dict.update({i.get("create_time"): i.get("qttl_mean")}) for i in
         data]
        [uab_dict.update({i.get("create_time"): i.get("ua_mean")}) for i in
         data]
        [ucb_dict.update({i.get("create_time"): i.get("ub_mean")}) for i in
         data]
lcn's avatar
lcn committed
320
        
wang.wenrong's avatar
wang.wenrong committed
321 322
        ia_list, ic_list, pttl_list, qttl_list, uab_list, ucb_list \
            = [], [], [], [], [], []
lcn's avatar
lcn committed
323
        
wang.wenrong's avatar
wang.wenrong committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
        [ia_list.append(ia_dict.get(i, "")) for i in slots]
        [ic_list.append(ia_dict.get(i, "")) for i in slots]
        [pttl_list.append(ia_dict.get(i, "")) for i in slots]
        [qttl_list.append(ia_dict.get(i, "")) for i in slots]
        [uab_list.append(ia_dict.get(i, "")) for i in slots]
        [ucb_list.append(ia_dict.get(i, "")) for i in slots]
        ia_value.update({"value_slots": ia_list})
        ic_value.update({"value_slots": ic_list})
        pttl_value.update({"value_slots": pttl_list})
        qttl_value.update({"value_slots": qttl_list})
        uab_value.update({"value_slots": uab_list})
        ucb_value.update({"value_slots": ucb_list})
        i.append(ia_value)
        i.append(ic_value)
        v.append(uab_value)
        v.append(ucb_value)
        power.append(pttl_value)
        power.append(qttl_value)
    return i, v, power
wang.wenrong's avatar
wang.wenrong committed
343 344 345 346 347 348 349 350 351 352 353


GENERAL_PARAM_FIELD_2 = [
    "lf_mean", "lf_min", "lf_max", "pttl_mean", "pttl_min",
    "pttl_max", "qttl_mean", "qttl_min", "qttl_max",
    "costtl_mean", "costtl_min", "costtl_max", "uab_mean",
    "uab_min", "uab_max", "ucb_mean", "ucb_min", "ucb_max",
    "ia_mean", "ia_min", "ia_max", "ic_mean", "ic_min",
    "ic_max", "freq_mean", "freq_min", "freq_max"
]
GENERAL_PARAM_FIELD_3 = [
wang.wenrong's avatar
wang.wenrong committed
354 355 356
    # "lf_mean", "lf_min", "lf_max", "freq_mean", "freq_min",
    #     "freq_max", "costtl_mean", "costtl_min", "costtl_max",
    "pttl_mean", "pttl_min",
wang.wenrong's avatar
wang.wenrong committed
357
    "pttl_max", "qttl_mean", "qttl_min", "qttl_max",
wang.wenrong's avatar
wang.wenrong committed
358
    "ua_mean",
wang.wenrong's avatar
wang.wenrong committed
359 360 361
    "ua_min", "ua_max", "ub_mean", "ub_min", "ub_max",
    "uc_mean", "uc_min", "uc_max", "ia_mean", "ia_min",
    "ia_max", "ib_mean", "ib_min", "ib_max", "ic_mean",
wang.wenrong's avatar
wang.wenrong committed
362
    "ic_min", "ic_max",
wang.wenrong's avatar
wang.wenrong committed
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
]
ELECTRIC_QUALITY_FIELD_2 = [
    "ubl_mean", "ubl_min", "ubl_max", "ibl_mean",
    "ibl_min", "ibl_max", "thduab_mean", "thduab_min",
    "thduab_max", "thducb_mean", "thducb_min",
    "thducb_max", "thdia_mean", "thdia_min",
    "thdia_max", "thdic_mean", "thdic_min", "thdic_max",
    "uab_dev_mean", "uab_dev_min", "uab_dev_max",
    "freq_dev_mean", "freq_dev_min", "freq_dev_max",
]

ELECTRIC_QUALITY_FIELD_3 = [
    "ubl_mean", "ubl_min", "ubl_max", "ibl_mean",
    "ibl_min", "ibl_max", "thdua_mean", "thdua_min",
    "thdua_max", "thdub_mean", "thdub_min", "thdub_max",
    "thduc_mean", "thduc_min", "thduc_max",
    "thdia_mean", "thdia_min", "thdia_max",
    "thdib_mean", "thdib_min", "thdib_max",
    "thdic_mean", "thdic_min", "thdic_max",
    "ua_dev_mean", "ua_dev_min", "ua_dev_max",
    "freq_dev_mean", "freq_dev_min", "freq_dev_max"
]


def cal_electic_value(datas, index_fields, mtid=None):
    """
    用电指数数据封装
    :param datas: 数据
    :param index_fields: 字段
    :param mtid: 字段是否需要合并mtid
    :return:
    """
    df = pd.DataFrame(list(datas))
    indexes_list = []
    _index_fields = {field.rsplit("_", 1)[0] for field in index_fields}
    for item in _index_fields:
        if datas:
            # item = item.rsplit("_", 1)[0]
            max_item_name = f"{item}_max"
            max_value = df[max_item_name].max()
            if not pd.isna(max_value):
                max_datas = df.loc[df[max_item_name].idxmax()].to_dict()
                max_time = max_datas.get(f"{max_item_name}_time")
                max_time = "" if pd.isnull(max_time) else str(max_time)
            else:
                max_value, max_time = "", ""
            min_item_name = f"{item}_min"
            min_value = df[min_item_name].min()
            if not pd.isna(min_value):
                min_datas = df.loc[df[min_item_name].idxmin()].to_dict()
                min_time = min_datas.get(f"{min_item_name}_time")
                min_time = "" if pd.isnull(min_time) else str(min_time)
            else:
                min_value, min_time = "", ""
            mean_item_name = f"{item}_mean"
            avg_value = df[mean_item_name].mean()
            if not pd.isna(avg_value):
                avg_value = round(avg_value, 2)
            else:
                avg_value = ""
            # if not max_value and not min_value and not avg_value:
            #     continue
            if mtid:
                mtid = str(mtid)
                electric_index = dict(
                    index=item,
                )
                electric_index["max_" + mtid] = max_value
                electric_index["max_time_" + mtid] = max_time or ""
                electric_index["min_" + mtid] = min_value
                electric_index["min_time_" + mtid] = min_time or ""
                electric_index["avg_" + mtid] = avg_value
            else:
                electric_index = dict(
                    item=item,
                    max=max_value,
                    max_time=max_time or "",
                    min=min_value,
                    min_time=min_time or "",
                    avg=avg_value,
                )
        else:
            electric_index = dict(item=item, max="", max_time="", min="",
                                  min_time="", avg="")
        indexes_list.append(electric_index)
    return indexes_list


def cal_aiao_value(location_datas, datas, mtid=None):
    """
    安全指数数据封装
    :param location_datas: 安全设置
    :param datas:  数据
    :param mtid: 字段是否需要合并mtid
    :return:
    """
    location_map = {loca["lid"]: loca for loca in location_datas}
    df = pd.DataFrame(list(datas))
    indexes_list = []
    for lid, item in location_map.items():
        if item["ad_type"] == "residual_current":
            index = "漏电流"
        else:
            index = f"{item.get('item')}"
        if datas:
            current_df = df.loc[df["lid"] == lid]
            if current_df.empty:
                continue
            max_value = current_df.value_max.max()
            if not pd.isna(max_value):
                max_datas = df.loc[
                    current_df.value_max.idxmax()].to_dict()
                max_value_time = max_datas.get("value_max_time")
                max_value_time = "" if pd.isnull(max_value_time) else str(
                    max_value_time)
                max_value = round(max_value, 2)
            else:
                max_value, max_value_time = "", ""
            min_value = current_df.value_min.min()
            if not pd.isna(min_value):
                min_datas = df.loc[
                    current_df.value_min.idxmin()].to_dict()
                min_value_time = min_datas.get("value_min_time")
                min_value_time = "" if pd.isnull(min_value_time) else str(
                    min_value_time)
                min_value = round(min_value, 2)
            else:
                min_value, min_value_time = "", ""
            mean_value = current_df.value_avg.mean()
            if not pd.isna(mean_value):
                mean_value = round(mean_value, 2)
            else:
                mean_value = ""
lcn's avatar
lcn committed
496
            
wang.wenrong's avatar
wang.wenrong committed
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 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
            if mtid:
                mtid = str(mtid)
                electric_index = dict(
                    item=index,
                )
                electric_index["max_" + mtid] = max_value
                electric_index["max_time_" + mtid] = max_value_time
                electric_index["min_" + mtid] = min_value
                electric_index["min_time_" + mtid] = min_value_time
                electric_index["avg_" + mtid] = mean_value
            else:
                electric_index = dict(
                    type=item["ad_type"],
                    item=index,
                    max=max_value,
                    max_time=max_value_time,
                    min=min_value,
                    min_time=min_value_time,
                    avg=mean_value,
                )
        else:
            electric_index = dict(type=item["ad_type"], item=index, max="",
                                  max_time="", min="",
                                  min_time="", avg="")
        indexes_list.append(electric_index)
    return indexes_list


def cal_pt_value(datas, mtid=None):
    """
    用电指数数据封装
    :param datas: 数据
    :param mtid: 字段是否需要合并mtid
    :return:
    """
    df = pd.DataFrame(list(datas))
    indexes_list = []
    if datas:
        # item = item.rsplit("_", 1)[0]
        max_item_name = f"temp_max"
        max_value = df[max_item_name].max()
        if not pd.isna(max_value):
            max_datas = df.loc[df[max_item_name].idxmax()].to_dict()
            max_time = max_datas.get(f"{max_item_name}_time")
            max_time = "" if pd.isnull(max_time) else str(max_time)
        else:
            max_value, max_time = "", ""
        min_item_name = f"temp_min"
        min_value = df[min_item_name].min()
        if not pd.isna(min_value):
            min_datas = df.loc[df[min_item_name].idxmin()].to_dict()
            min_time = min_datas.get(f"{min_item_name}_time")
            min_time = "" if pd.isnull(min_time) else str(min_time)
        else:
            min_value, min_time = "", ""
        mean_item_name = f"temp_mean"
        avg_value = df[mean_item_name].mean()
        if not pd.isna(avg_value):
            avg_value = round(avg_value, 2)
        else:
            avg_value = ""
        # if not max_value and not min_value and not avg_value:
        #     continue
        if mtid:
            mtid = str(mtid)
            electric_index = dict(
                index="温度(℃)",
            )
            electric_index["max_" + mtid] = max_value
            electric_index["max_time_" + mtid] = max_time or ""
            electric_index["min_" + mtid] = min_value
            electric_index["min_time_" + mtid] = min_time or ""
            electric_index["avg_" + mtid] = avg_value
        else:
            electric_index = dict(
                index="温度(℃)",
                max=max_value,
                max_time=max_time or "",
                min=min_value,
                min_time=min_time or "",
                avg=avg_value,
            )
    else:
        electric_index = dict(index="温度(℃)", max="", max_time="", min="",
                              min_time="", avg="")
    indexes_list.append(electric_index)
    return indexes_list