fine_monitor_pds.py 18 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)
ZZH's avatar
ZZH 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)
ZZH's avatar
ZZH 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)
ZZH's avatar
ZZH 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相"}, {
ZZH's avatar
ZZH 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]
ZZH's avatar
ZZH 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)
ZZH's avatar
ZZH 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相"}, {
ZZH's avatar
ZZH committed
131 132 133 134
                                                                 "item": "B相"}, {
                                                                 "item": "C相"}, {
                                                                 "item": "N线"}

wang.wenrong's avatar
wang.wenrong committed
135 136 137 138 139 140 141 142 143 144 145 146
    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")})
ZZH's avatar
ZZH committed
147

wang.wenrong's avatar
wang.wenrong committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    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:
ZZH's avatar
ZZH committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        fields = ["ia", "ib", "ic", "ua", "ub", "uc", "pttl", "qttl"]
    else:
        fields = ["ia", "ic", "uab", "ucb", "pttl", "qttl"]

    d_parse = {field: {} for field in fields}
    for d_item in data:
        for field in fields:
            d_parse[field][d_item.get("ts")] = d_item.get(field)

    d_stats = {field: [] for field in fields}
    for field in fields:
        d_stats[field] = [d_parse[field].get(s, "") for s in slots]

    if ctnum == 3:
        d_ia = dict(item="ia", value_slots=d_stats["ia"])
        d_ib = dict(item="ib", value_slots=d_stats["ib"])
        d_ic = dict(item="ic", value_slots=d_stats["ic"])
        d_ua = dict(item="ua", value_slots=d_stats["ua"])
        d_ub = dict(item="ub", value_slots=d_stats["ub"])
        d_uc = dict(item="uc", value_slots=d_stats["uc"])

        i = [d_ia, d_ib, d_ic]
        v = [d_ua, d_ub, d_uc]
wang.wenrong's avatar
wang.wenrong committed
192
    else:
ZZH's avatar
ZZH committed
193 194 195 196 197 198 199 200 201
        d_ia = dict(item="ia", value_slots=d_stats["ia"])
        d_ic = dict(item="ic", value_slots=d_stats["ic"])
        d_uab = dict(item="uab", value_slots=d_stats["uab"])
        d_ucb = dict(item="ucb", value_slots=d_stats["ucb"])
        i = [d_ia, d_ic]
        v = [d_uab, d_ucb]
    d_pttl = dict(item="pttl", value_slots=d_stats["pttl"])
    d_qttl = dict(item="qttl", value_slots=d_stats["qttl"])
    power = [d_pttl, d_qttl]
wang.wenrong's avatar
wang.wenrong committed
202 203 204 205 206
    return i, v, power


async def get_point_data_chart_pds(ctnum, slots, data):
    if ctnum == 3:
ZZH's avatar
ZZH committed
207
        fields = ["ia", "ib", "ic", "ua", "ub", "uc", "pttl", "qttl"]
wang.wenrong's avatar
wang.wenrong committed
208
    else:
ZZH's avatar
ZZH committed
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
        fields = ["ia", "ic", "uab", "ucb", "pttl", "qttl"]

    d_parse = {field: {} for field in fields}
    for d_item in data:
        for field in fields:
            ts = d_item.get("create_time")
            d_parse[field][ts] = d_item.get(f"{field}_mean")

    d_stats = {field: [] for field in fields}
    for field in fields:
        d_stats[field] = [d_parse[field].get(s, "") for s in slots]

    if ctnum == 3:
        d_ia = dict(item="ia", value_slots=d_stats["ia"])
        d_ib = dict(item="ib", value_slots=d_stats["ib"])
        d_ic = dict(item="ic", value_slots=d_stats["ic"])
        d_ua = dict(item="ua", value_slots=d_stats["ua"])
        d_ub = dict(item="ub", value_slots=d_stats["ub"])
        d_uc = dict(item="uc", value_slots=d_stats["uc"])

        i = [d_ia, d_ib, d_ic]
        v = [d_ua, d_ub, d_uc]
    else:
        d_ia = dict(item="ia", value_slots=d_stats["ia"])
        d_ic = dict(item="ic", value_slots=d_stats["ic"])
        d_uab = dict(item="uab", value_slots=d_stats["uab"])
        d_ucb = dict(item="ucb", value_slots=d_stats["ucb"])

        i = [d_ia, d_ic]
        v = [d_uab, d_ucb]
    d_pttl = dict(item="pttl", value_slots=d_stats["pttl"])
    d_qttl = dict(item="qttl", value_slots=d_stats["qttl"])
    power = [d_pttl, d_qttl]
wang.wenrong's avatar
wang.wenrong committed
242
    return i, v, power
wang.wenrong's avatar
wang.wenrong committed
243 244 245 246 247 248 249 250 251 252 253


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
254 255 256
    # "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
257
    "pttl_max", "qttl_mean", "qttl_min", "qttl_max",
wang.wenrong's avatar
wang.wenrong committed
258
    "ua_mean",
wang.wenrong's avatar
wang.wenrong committed
259 260 261
    "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
262
    "ic_min", "ic_max",
wang.wenrong's avatar
wang.wenrong committed
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
]
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 = ""
ZZH's avatar
ZZH committed
396

wang.wenrong's avatar
wang.wenrong committed
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
            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