count_info.py 11.7 KB
Newer Older
lcn's avatar
lcn committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import time
from datetime import datetime, timedelta

from aioredis import RedisError
from elasticsearch import ElasticsearchException
from pymysql import MySQLError

from pot_libs.logger import log
from pot_libs.sanic_api import summary
from unify_api.modules.common.components.common_cps import CidReq

from unify_api.modules.home_page.components.count_info_cps import (
    CountInfoResp,
    CountInfoReq,
    EconomicPowerCountResp, CountItemNew,
    CountItem, RiskCostReq, RiskCostResp, EconomicPowerCountNewResp
)
from unify_api.modules.home_page.components.count_info_proxy_cps import \
    CountInfoProxyResp, IycResp, IycmResp, RtrResp, CmResp, ApcResp, AsiResp, \
    HsiResp, AiiResp
from unify_api.modules.home_page.procedures.count_info_pds import (
ZZH's avatar
ZZH committed
22
    get_max_aiao_of_filed, normal_rate_of_location,
ZZH's avatar
ZZH committed
23
    other_info,
ZZH's avatar
ZZH committed
24
    power_count_info,
ZZH's avatar
ZZH committed
25
    electric_use_info,
ZZH's avatar
ZZH committed
26
    power_charge_price,
ZZH's avatar
ZZH committed
27
    cal_power_factor,
lcn's avatar
lcn committed
28 29 30 31 32 33
    optimization_count_info, optimization_count_info_new
)
from unify_api.modules.home_page.service.count_info_service import \
    post_zd_info_factory_service, risk_cost_service, info_yang_chen_service, \
    info_yang_chen_map_service, rank_type_ranking_service, \
    condition_monitor_service, alarm_price_costtl_service, \
ZZH's avatar
ZZH committed
34
    alarm_safe_index_service, all_index_info_service
lcn's avatar
lcn committed
35 36 37 38 39 40 41 42 43 44 45 46 47


@summary("获取首页统计信息")
async def post_count_info(request, body: CountInfoReq) -> CountInfoResp:
    # 1. 获取company_id
    company_id = body.cid
    # now_tt = time.time()
    # start_dt = datetime.now() - timedelta(30)
    # start_tt = datetime_to_timestamp(
    #     datetime(start_dt.year, start_dt.month, start_dt.day))
    now_tt = str(datetime.now())
    start_tt = str(datetime.now() - timedelta(30))
    try:
ZZH's avatar
ZZH committed
48 49 50 51 52 53

        max_residual_current = await get_max_aiao_of_filed(company_id,
                                                           start_tt, now_tt,
                                                           "residual_current")
        max_temperature = await get_max_aiao_of_filed(company_id, start_tt,
                                                      now_tt, "temperature")
lcn's avatar
lcn committed
54 55 56 57 58

        # 温度和漏电流实时达标率
        # temperature_qr, residual_current_qr = await normal_rate_of_location(
        #     company_id)
        temperature_qr, residual_current_qr = await \
ZZH's avatar
ZZH committed
59
            normal_rate_of_location(company_id)
lcn's avatar
lcn committed
60 61 62 63

        # 今日报警数和累计安全运行天数
        # today_alarm_count, safe_run_days, alarm_count = await other_info(
        #     company_id)
ZZH's avatar
ZZH committed
64
        today_alarm_count, safe_run_days, alarm_count = await other_info(
lcn's avatar
lcn committed
65 66 67
            company_id)

        # 实时负荷和近30日最高负荷
ZZH's avatar
ZZH committed
68
        current_load, max_30d_load = await power_count_info(company_id)
lcn's avatar
lcn committed
69 70 71

        # 用电安全指数, 报警分, 近30天报警1,2,3级数目
        # electric_info = await electric_use_info(company_id)
ZZH's avatar
ZZH committed
72
        electric_info = await electric_use_info(company_id)
lcn's avatar
lcn committed
73 74

        # 昨日平均电价, 上月平均电价
ZZH's avatar
ZZH committed
75
        yestoday_price, last_month_price = await power_charge_price(
lcn's avatar
lcn committed
76 77 78 79
            company_id)

        # 实时功率因数, 上月功率因数
        # cos_ttl, last_month_cos = await power_factor(company_id)
ZZH's avatar
ZZH committed
80
        cos_ttl, last_month_cos = await cal_power_factor(company_id)
lcn's avatar
lcn committed
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

    # 其实异常捕获这个东西最好是在框架内部做一次就够了
    except (ElasticsearchException, MySQLError, RedisError) as e:
        log.exception(e)
        return CountInfoResp().db_error()
    except Exception as e:
        log.exception(e)
        return CountInfoResp().server_error()

    return CountInfoResp(
        max_residual_current=max_residual_current,
        max_temperature=max_temperature,
        temperature_qr=temperature_qr,
        residual_current_qr=residual_current_qr,
        today_alarm_count=today_alarm_count,
        safe_run_days=safe_run_days,
        current_load=current_load,
        max_30d_load=max_30d_load,
        electric_info=electric_info,
        yestoday_price=yestoday_price,
        last_month_price=last_month_price,
        cos_ttl=cos_ttl,
        last_month_cos=last_month_cos,
    )


@summary("获取首页用电经济指数")
async def post_electric_economic_index(request,
                                       body: CountInfoReq) -> EconomicPowerCountResp:
    cid = int(body.cid)

    count_info_map = await optimization_count_info(cid)

    power_factor_info = count_info_map["power_factor"]
    pcvf_info = count_info_map["pcvf"]
    power_save_info = count_info_map["power_save"]
    md_space_info = count_info_map["md_space"]
    save_percent = count_info_map["save_percent"]
    avg_price = count_info_map["avg_price"]
    md_space_p = count_info_map["md_space_p"]
    mean_load_factor = count_info_map["mean_load_factor"]
    max_save_charge = sum(
        [
            i["save_charge"] if i["save_charge"] else 0
            for i in
            [power_factor_info, pcvf_info, power_save_info, md_space_info]
        ]
    )
    if save_percent == "":
        economic_power_index = 100
    elif save_percent >= 0 and save_percent < 0.005:
        economic_power_index = -2000 * save_percent + 100
    elif save_percent >= 0.005 and save_percent < 0.02:
        economic_power_index = -1000 * save_percent + 95
    elif save_percent >= 0.02 and save_percent < 0.05:
        economic_power_index = -500 * save_percent + 85
    elif save_percent >= 0.05 and save_percent < 0.1:
        economic_power_index = -1200 * save_percent + 120
    else:
        economic_power_index = 0

    return EconomicPowerCountResp(
        power_factor=CountItem(
            index=power_factor_info["kpi_x"],
            save_charge=power_factor_info["save_charge"],
            desc=power_factor_info["desc"],
        ),
        pcvf=CountItem(
            index=pcvf_info["kpi_x"], save_charge=pcvf_info["save_charge"],
            desc=pcvf_info["desc"],
        ),
        power_save=CountItem(
            index=power_save_info["kpi_x"],
            save_charge=power_save_info["save_charge"],
            desc=power_save_info["desc"],
        ),
        md_space=CountItem(
            index=md_space_info["kpi_x"],
            save_charge=md_space_info["save_charge"],
            desc=md_space_info["desc"],
        ),
        avg_price=avg_price,
        save_percent=save_percent,
        economic_power_index=economic_power_index,
        md_space_p=md_space_p,
        mean_load_factor=mean_load_factor,
        max_save_charge=max_save_charge,
    )


@summary("工厂版首页统计信息-智电U")
async def post_zd_info_factory(req, body: CountInfoReq) -> CountInfoProxyResp:
    # 1. 获取cid
    cid = body.cid
    cid_list = [cid]
    total_monitor, safe_operation_days, total_power, total_alarm, co2 = \
        await post_zd_info_factory_service(cid_list)
    return CountInfoProxyResp(
        total_monitor=total_monitor,
        safe_operation_days=safe_operation_days,
        total_power=total_power,
        total_alarm=total_alarm,
        co2=co2
    )


@summary("工厂版运行统计-风险成本-智电U")
async def post_risk_cost(req, body: RiskCostReq) -> RiskCostResp:
    # 1. 获取参数
    cid = body.cid
    # 2. 调service获取数据
    risk_list, cost_list = await risk_cost_service(cid)
    return RiskCostResp(risk_data=risk_list, cost_data=cost_list)


@summary("工厂版首页统计信息-扬尘")
async def post_info_yang_chen(req, body: CountInfoReq) -> IycResp:
    # 1. 获取cid
    cid = body.cid
wang.wenrong's avatar
wang.wenrong committed
200
    # return await info_yang_chen_service(cid)
wang.wenrong's avatar
wang.wenrong committed
201
    return IycResp(total_point=20, air_quality=10, safe_operation_days=11,
ZZH's avatar
ZZH committed
202 203
                   total_water=132.80, total_kwh=0)  # 扬尘接口,返回空数据

lcn's avatar
lcn committed
204 205 206 207 208

@summary("工厂版首页地图信息-扬尘")
async def post_info_yang_chen_map(req, body: CountInfoReq) -> IycmResp:
    # 1. 获取cid
    cid = body.cid
wang.wenrong's avatar
wang.wenrong committed
209
    # return await info_yang_chen_map_service(cid)
wang.wenrong's avatar
wang.wenrong committed
210 211
    return IycmResp(safety_index=89, today_alarm=5, total_max_pm25=27.12,
                    center_address=[], range_address=[])
lcn's avatar
lcn committed
212

ZZH's avatar
ZZH committed
213

lcn's avatar
lcn committed
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
@summary("获取首页用电经济指数")
async def post_electric_economic_index_new(request,
                                           body: CountInfoReq) -> EconomicPowerCountNewResp:
    cid = int(body.cid)

    count_info_map = await optimization_count_info_new(cid)

    power_factor_info = count_info_map["power_factor"]
    pcvf_info = count_info_map["pcvf"]
    power_save_info = count_info_map["power_save"]
    md_space_info = count_info_map["md_space"]
    avg_price = count_info_map["avg_price"]
    md_space_p = count_info_map["md_space_p"]
    mean_load_factor = count_info_map["mean_load_factor"]
    save_percent = count_info_map["save_percent"]
    max_save_charge = sum(
        [
            i["save_charge"] if i["save_charge"] else 0
            for i in
            [power_factor_info, pcvf_info, power_save_info, md_space_info]
        ]
    )
    if save_percent == "":
        economic_power_index = 100
    elif 0 <= save_percent < 0.005:
        economic_power_index = -2000 * save_percent + 100
    elif 0.005 <= save_percent < 0.02:
        economic_power_index = -1000 * save_percent + 95
    elif 0.02 <= save_percent < 0.05:
        economic_power_index = -500 * save_percent + 85
    elif 0.05 <= save_percent < 0.1:
        economic_power_index = -1200 * save_percent + 120
    else:
        economic_power_index = 0
    return EconomicPowerCountNewResp(
        power_factor=CountItemNew(
            index=power_factor_info["kpi_x"],
            save_charge=power_factor_info["save_charge"],
            desc=power_factor_info["desc"],
            space=power_factor_info["space"]
        ),
        pcvf=CountItemNew(
            index=pcvf_info["kpi_x"], save_charge=pcvf_info["save_charge"],
            desc=pcvf_info["desc"], space=pcvf_info["space"]
        ),
        power_save=CountItemNew(
            index=power_save_info["kpi_x"],
            save_charge=power_save_info["save_charge"],
            desc=power_save_info["desc"],
            space=power_save_info["space"]
        ),
        md_space=CountItemNew(
            index=md_space_info["kpi_x"],
            save_charge=md_space_info["save_charge"],
            desc=md_space_info["desc"],
            space=md_space_info["space"]
        ),
        avg_price=avg_price,
        save_percent=save_percent,
        economic_power_index=round(economic_power_index),
        md_space_p=md_space_p,
        mean_load_factor=mean_load_factor,
        max_save_charge=max_save_charge,
    )


@summary("首页报警统计-等级类型排名-智电U")
async def post_rank_type_ranking(req, body: CidReq) -> RtrResp:
    # 1. 获取参数
    cid = body.cid
    # 2. 调service获取数据
    return await rank_type_ranking_service(cid)


@summary("首页状态监测-智电U")
async def post_condition_monitor(req, body: CidReq) -> CmResp:
    """最近5min报警"""
    # 1. 获取参数
    cid = body.cid
    # 2. 调service获取数据
    return await condition_monitor_service(cid)


@summary("首页-今日报警平均电价等-智电U")
async def post_alarm_price_costtl(req, body: CidReq) -> ApcResp:
    # 1. 获取参数
    cid = body.cid
    # 2. 调service获取数据
    return await alarm_price_costtl_service(cid)


@summary("首页-用电安全指数等")
async def post_alarm_safe_index(req, body: CidReq) -> AsiResp:
    # 1. 获取参数
    cid = body.cid
    # 2. 调service获取数据
    return await alarm_safe_index_service(cid)


@summary("首页-健康指数等")
async def post_health_status_index(req, body: CidReq) -> HsiResp:
    # 1. 获取参数
    cid = body.cid
    # 2. 调service获取数据
ZZH's avatar
ZZH committed
318
    # return await health_status_index_service(cid)
lcn's avatar
lcn committed
319 320 321 322 323 324 325 326


@summary("首页-全部指数")
async def post_all_index_info(req, body: CidReq) -> AiiResp:
    # 1. 获取参数
    cid = body.cid
    # 2. 调service获取数据
    return await all_index_info_service(cid)