health_index_service.py 4.71 KB
Newer Older
lcn's avatar
lcn committed
1
import time
wang.wenrong's avatar
wang.wenrong committed
2 3 4 5
import pendulum
from unify_api.modules.electric.dao.electric_dao import \
    get_elec_mtid_sid_by_cid

lcn's avatar
lcn committed
6 7 8 9 10
from unify_api.utils.common_utils import round_2
from pot_libs.logger import log
from pot_libs.settings import SETTING
from unify_api.modules.home_page.procedures.dev_grade import get_dev_grade
from unify_api.utils import time_format
ZZH's avatar
ZZH committed
11 12
from unify_api.constants import REAL_EXP_TIME
from pot_libs.utils.exc_util import ParamException, BusinessException
lcn's avatar
lcn committed
13 14
from unify_api.modules.home_page.components.health_index import \
    HealthCtlRateRes
ZZH's avatar
ZZH committed
15
from unify_api.modules.zhiwei_u.dao.warning_operations_dao import \
lcn's avatar
lcn committed
16 17 18
    select_point_dao
from unify_api.modules.common.service.td_engine_service import \
    get_td_engine_data
wang.wenrong's avatar
wang.wenrong committed
19 20
from unify_api.utils.taos_new import parse_td_columns, td3_tbl_compate, \
    get_td_table_name
lcn's avatar
lcn committed
21 22


ZZH's avatar
ZZH committed
23
async def health_ctl_rate_srv(cid):
lcn's avatar
lcn committed
24 25 26 27 28 29 30 31 32 33 34 35
    if cid <= 0:
        log.error("param error")
        raise ParamException(message="参数错误, cid参数必须是一个正整数!")
    point_infos = await select_point_dao(cid)
    if not point_infos:
        log.error("cid:%s no point da;ta" % cid)
        raise BusinessException(message="工厂没有任何监测点!")
    stats = {"lf": 0, "costtl": 0, "freq_dev": 0, "thdu": 0, "v_dev": 0,
             "ubl": 0}
    now_ts = int(time.time())
    real_tt = now_ts
    total = 0
wang.wenrong's avatar
wang.wenrong committed
36 37 38 39 40 41 42 43 44 45 46

    datas = await get_elec_mtid_sid_by_cid(cid)
    td_mt_tables = tuple(
        (get_td_table_name("electric", data["mtid"]) for data in datas if
         data["mtid"]))

    td_mt_tables = td3_tbl_compate(td_mt_tables)
    sql = f"select last_row(*) from electric_stb " \
          f"where TBNAME IN {td_mt_tables} group by tbname"

    url = f"{SETTING.stb_url}db_electric?tz=Asia/Shanghai"
lcn's avatar
lcn committed
47 48
    is_succ, results = await get_td_engine_data(url, sql)
    time_str = time_format.get_datetime_str(real_tt)
wang.wenrong's avatar
wang.wenrong committed
49 50

    if not is_succ:
lcn's avatar
lcn committed
51
        log.warn(f"cid={cid} 无任何有效mid")
ZZH's avatar
ZZH committed
52 53
        return HealthCtlRateRes(real_time=time_str, lf=1, costtl=1, thdu=1,
                                v_dev=1, freq_dev=1, ubl=1)
wang.wenrong's avatar
wang.wenrong committed
54 55 56 57 58 59 60 61 62 63 64

    if not results["data"]:  # 兼容:mt表(2.0架构)里面拿不到数据再从sid表(1.0架构)里面拿
        td_s_tables = tuple(
            (f"s{data['sid'].lower()}_e" for data in datas if data["sid"]))

        td_s_tables = td3_tbl_compate(td_s_tables)
        sql = f"select last_row(*) from electric_stb " \
              f"where TBNAME IN {td_s_tables} group by tbname"
        is_succ, results = await get_td_engine_data(url, sql)
        if not is_succ:
            log.warn(f"cid={cid} 无任何有效mid")
ZZH's avatar
ZZH committed
65 66
            return HealthCtlRateRes(real_time=time_str, lf=1, costtl=1, thdu=1,
                                    v_dev=1, freq_dev=1, ubl=1)
wang.wenrong's avatar
wang.wenrong committed
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

    head = parse_td_columns(results)
    datas = []
    for res in results["data"]:
        datas.append(dict(zip(head, res)))
    for data in datas:
        real_tt = pendulum.parse(data["ts"]).int_timestamp
        if now_ts - real_tt > REAL_EXP_TIME:
            continue
        total += 1
        ctnum = data.get("ctnum")
        # 电压偏差
        v_dev = data.get("ua_dev") if ctnum == 3 else data.get("uab_dev")
        grade = get_dev_grade(dev_type="v", cur=v_dev)
        if grade and grade >= 60:
            stats["v_dev"] += 1
        # 频率偏差
        freq_dev = data.get("freq_dev")
        grade = get_dev_grade(dev_type="freq", cur=freq_dev)
        if grade and grade >= 60:
            stats["freq_dev"] += 1
        # 三相电压不平衡度
        ubl = data.get("ubl")
        grade = get_dev_grade(dev_type="ubl", cur=ubl)
        if grade and grade >= 60:
            stats["ubl"] += 1
        # 功率因数
        costtl = data.get("costtl")
        grade = get_dev_grade(dev_type="costtl", cur=costtl)
        if grade and grade >= 60:
            stats["costtl"] += 1
        # (电压)谐波畸变率
        thdu = data.get("thdua") if ctnum == 3 else data.get("thduab")
        grade = get_dev_grade(dev_type="thdu", cur=thdu)
        if grade and grade >= 60:
            stats["thdu"] += 1
        # 负载率
        lf = data.get("lf")
        if lf is None:
            stats["lf"] += 1
        else:
            grade = get_dev_grade(dev_type="lf", cur=lf)
            if grade and grade >= 60:
                stats["lf"] += 1
lcn's avatar
lcn committed
111
    if total == 0:
ZZH's avatar
ZZH committed
112 113
        return HealthCtlRateRes(real_time=time_str, lf=1, costtl=1, thdu=1,
                                v_dev=1, freq_dev=1, ubl=1)
lcn's avatar
lcn committed
114 115 116 117 118 119 120 121 122
    return HealthCtlRateRes(
        real_time=time_str,
        lf=round_2(stats["lf"] / total),
        costtl=round_2(stats["costtl"] / total),
        thdu=round_2(stats["thdu"] / total),
        v_dev=round_2(stats["v_dev"] / total),
        freq_dev=round_2(stats["freq_dev"] / total),
        ubl=round_2(stats["ubl"] / total),
    )