power_cps.py 1.49 KB
Newer Older
lcn's avatar
lcn committed
1 2 3 4 5 6 7
from pot_libs.es_util.es_utils import EsUtil
from pot_libs.mysql_util.mysql_util import MysqlUtil
from unify_api import constants


async def power_use_count(company_ids):
    query_body = {
ZZH's avatar
ZZH committed
8
        "query": {"bool": {"filter": [{"terms": {"cid": company_ids}}, ]}},
lcn's avatar
lcn committed
9 10 11
        "size": 0,
        "aggs": {"kwh": {"sum": {"field": "kwh"}}},
    }
lcn's avatar
lcn committed
12
    
lcn's avatar
lcn committed
13
    async with EsUtil() as es:
ZZH's avatar
ZZH committed
14 15
        es_result = await es.search_origin(body=query_body,
                                           index=constants.COMPANY_15MIN_POWER)
lcn's avatar
lcn committed
16
    
ZZH's avatar
ZZH committed
17 18
    total_power = round(
        es_result.get("aggregations", {}).get("kwh", {}).get("value") or 0, 2)
lcn's avatar
lcn committed
19 20 21
    return total_power


ZZH's avatar
ZZH committed
22
async def load_cmpy_power(cids):
lcn's avatar
lcn committed
23
    sql = "SELECT sum(kwh) kwh FROM `company_1day_power` where cid in %s"
lcn's avatar
lcn committed
24 25 26 27 28 29 30 31 32
    async with MysqlUtil() as conn:
        data = await conn.fetchone(sql, args=(cids,))
    total_power = round(data.get("kwh") or 0, 2)
    return total_power


async def inline_power_use_info(inline_ids, month_str):
    sql = "SELECT inlid, sum(kwh) kwh, sum(charge) charge, sum(p) p FROM " \
          "`inline_1day_power` where inlid in %s  and " \
lcn's avatar
lcn committed
33 34
          f"DATE_FORMAT(create_time, '%%Y-%%m-%%d')='{month_str}' GROUP BY " \
          f"inlid"
lcn's avatar
lcn committed
35
    async with MysqlUtil() as conn:
ZZH's avatar
ZZH committed
36
        datas = await conn.fetchall(sql, args=(inline_ids,))
lcn's avatar
lcn committed
37 38 39 40 41 42 43 44 45
    inline_power_info_map = {
        i["inlid"]: {
            "kwh": i["kwh"],
            "charge": i["charge"],
            "p": i["p"],
        }
        for i in datas
    }
    return inline_power_info_map