common_cps.py 7.86 KB
Newer Older
lcn's avatar
lcn committed
1 2 3 4 5 6 7 8 9
import calendar
from datetime import datetime

import pendulum

from pot_libs.es_util.es_utils import EsUtil
from pot_libs.logger import log
from pot_libs.mysql_util.mysql_util import MysqlUtil
from unify_api import constants
lcn's avatar
lcn committed
10
from unify_api.constants import CST
lcn's avatar
lcn committed
11 12 13 14 15 16


def point_day2month(dt):
    if isinstance(dt, int) or isinstance(dt, float):
        dt = pendulum.from_timestamp(dt, tz="Asia/Shanghai")
        es_index = f"{constants.POINT_1MIN_INDEX}_{dt.year}_{dt.month}"
lcn's avatar
lcn committed
17
    
lcn's avatar
lcn committed
18 19
    elif isinstance(dt, datetime):
        es_index = f"{constants.POINT_1MIN_INDEX}_{dt.year}_{dt.month}"
lcn's avatar
lcn committed
20
    
lcn's avatar
lcn committed
21 22
    else:
        es_index = constants.POINT_1MIN_INDEX
lcn's avatar
lcn committed
23
    
lcn's avatar
lcn committed
24 25 26 27 28
    return es_index


async def today_alarm_cnt(cids):
    start_time = pendulum.today(tz="Asia/Shanghai")
lcn's avatar
lcn committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    es_end_time = start_time.subtract(days=-1).format("YYYY-MM-DD HH:mm:ss")
    es_start_time = start_time.format("YYYY-MM-DD HH:mm:ss")
    
    sql = f"""
        select cid,count(*) count
        from point_1min_event pe
        left join event_type et on pe.event_type = et.e_type
        where cid in %s and et.mode = 'alarm' and event_datetime >= %s
        and event_datetime < %s
        group by cid
    """
    async with MysqlUtil() as conn:
        datas = await conn.fetchall(sql=sql,
                                    args=(cids, es_start_time, es_end_time))
    
    cid_bucket_map = {i["cid"]: i["count"] for i in datas}
    
lcn's avatar
lcn committed
46 47
    cid_alarm_map = {cid: {"today_alarm_count": 0} for cid in cids}
    for cid in cids:
lcn's avatar
lcn committed
48 49
        alarm_count = cid_bucket_map.get("cid") or 0
        cid_alarm_map[cid]["today_alarm_count"] += alarm_count
lcn's avatar
lcn committed
50 51 52
    return cid_alarm_map


lcn's avatar
lcn committed
53 54 55 56


async def proxy_safe_run_info(cids, start_time_str=None,
                                    end_time_str=None):
lcn's avatar
lcn committed
57 58 59 60 61 62 63 64 65 66
    """
    批量获取 各个工厂的安全运行天数以及今日报警数, 如果是获取月份的,那么计算这个月的安全运行天数
    :param cids:
    :return:
    """
    filters = [
        {"terms": {"cid": cids}},
        # {"term": {"mode": "alarm"}},
        {"term": {"importance": 1}},
    ]
lcn's avatar
lcn committed
67 68 69
    where = ""
    start_dt, end_dt, start_ts, end_ts = None, None, 0, 0
    now_dt = pendulum.now(tz=CST)
lcn's avatar
lcn committed
70
    if start_time_str and end_time_str:
lcn's avatar
lcn committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        start_dt = pendulum.parse(start_time_str)
        end_dt = pendulum.parse(end_time_str)
        start_ts = start_dt.int_timestamp
        end_ts = end_dt.int_timestamp
        now_ts = now_dt.int_timestamp
        if end_ts > now_ts:
            end_time_str = now_dt.format("YYYY-MM-DD HH:mm:ss")
        where += f" and event_datetime>= '{start_time_str}' and " \
                 f"event_datetime < '{end_time_str}' "
    sql = f"""
        select cid,date_format(event_datetime,"%%Y-%%m-%%d") fmt_day,
        count(*) count
        from point_1min_event
        where cid in %s {where}
        group by cid,date_format(event_datetime,"%%Y-%%m-%%d")
    """
    async with MysqlUtil() as conn:
        datas = await conn.fetchall(sql=sql, args=(cids,))
    
lcn's avatar
lcn committed
90 91 92 93
    # 获取到工厂安装时间create_time
    async with MysqlUtil() as conn:
        company_sql = "select cid, create_time from company where cid in %s"
        companys = await conn.fetchall(company_sql, (cids,))
lcn's avatar
lcn committed
94
    
lcn's avatar
lcn committed
95
    create_time_timestamp_map = {
lcn's avatar
lcn committed
96 97
        company["cid"]: pendulum.from_timestamp(
            company["create_time"], tz=CST) for company in companys
lcn's avatar
lcn committed
98
    }
lcn's avatar
lcn committed
99 100 101 102 103 104 105 106 107
    cid_alarm_map = {cid: {"today_alarm_count": 0, "safe_run_days": 0} for cid
                     in cids}
    cid_alarm_count_dict = dict()
    for data in datas:
        cid = data.get("cid")
        if cid not in cid_alarm_count_dict:
            cid_alarm_count_dict[cid] = 0
        elif data.get("count") > 0:
            cid_alarm_count_dict[cid] += 1
lcn's avatar
lcn committed
108
    for cid in cids:
lcn's avatar
lcn committed
109 110
        create_dt = create_time_timestamp_map[cid]
        total_days = (now_dt - create_dt).days + 1
lcn's avatar
lcn committed
111 112
        if start_time_str and end_time_str:
            # 计算一段时间内安全运行天数,总天数的逻辑稍微不一样
lcn's avatar
lcn committed
113 114 115 116 117
            total_days = (end_dt - start_dt).days + 1
            create_ts = create_dt.int_timestamp
            if start_ts < create_ts < end_ts:
                total_days = (end_dt - create_dt).days + 1
            elif create_ts > end_ts:
lcn's avatar
lcn committed
118
                total_days = 0
lcn's avatar
lcn committed
119 120
        
        has_alarm_days = cid_alarm_count_dict.get("cid") or 0
lcn's avatar
lcn committed
121 122 123 124 125
        safe_run_days = total_days - has_alarm_days
        cid_alarm_map[cid]["safe_run_days"] = safe_run_days
        cid_alarm_map[cid]["total_days"] = total_days
    today_alarm_map = await today_alarm_cnt(cids)
    for cid in cid_alarm_map:
lcn's avatar
lcn committed
126 127
        cid_alarm_map[cid]["today_alarm_count"] = today_alarm_map[cid][
            "today_alarm_count"]
lcn's avatar
lcn committed
128 129 130 131
    return cid_alarm_map


async def alarm_time_distribution(company_ids, start, end):
132 133 134 135 136 137 138 139 140 141 142 143 144 145
    sql = f"""
        SELECT
            HOUR (pevent.event_datetime) event_hour,
            COUNT(*) event_count
        FROM
            point_1min_event pevent
        WHERE
            cid IN %s
        AND pevent.event_datetime >= '{start}'
        AND pevent.event_datetime <= '{end}'
        GROUP BY
            HOUR (pevent.event_datetime)
    """
    async with MysqlUtil() as conn:
lcn's avatar
lcn committed
146 147 148 149
        datas = await conn.fetchall(sql, args=(company_ids,))
    
    time_distribution_map = {"day_alarm_cnt": 0, "night_alarm_cnt": 0,
                             "morning_alarm_cnt": 0}
150 151 152 153 154 155 156 157 158 159 160 161
    for data in datas:
        hour = int(data["event_hour"])
        if hour >= 6 and hour < 18:
            time_distribution_map["day_alarm_cnt"] += data["event_count"]
        elif hour >= 18 and hour <= 23:
            time_distribution_map["night_alarm_cnt"] += data["event_count"]
        else:
            time_distribution_map["morning_alarm_cnt"] += data["event_count"]
    return time_distribution_map


async def alarm_time_distribution_old(company_ids, start, end):
lcn's avatar
lcn committed
162 163
    start_dt = datetime.strptime(start, "%Y-%m-%d %H:%M:%S")
    end_dt = datetime.strptime(end, "%Y-%m-%d %H:%M:%S")
lcn's avatar
lcn committed
164 165
    es_start_str = datetime(year=start_dt.year, month=start_dt.month,
                            day=start_dt.day).strftime(
lcn's avatar
lcn committed
166 167 168 169 170 171
        "%Y-%m-%dT%H:%M:%S+08:00"
    )
    es_end_str = end_dt.strftime("%Y-%m-%dT%H:%M:%S+08:00")
    filter_list = [
        {"range": {"datetime": {"gte": es_start_str, "lte": es_end_str, }}},
        {"terms": {"cid": company_ids}}]
lcn's avatar
lcn committed
172
    
lcn's avatar
lcn committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    query_body = {
        "query": {"bool": {"filter": filter_list}},
        "size": 0,
        "aggs": {
            "cid_aggs": {
                "terms": {"field": "cid", "size": 10000},
                "aggs": {
                    "time_alarms": {
                        "date_histogram": {
                            "field": "datetime",
                            "order": {"_key": "desc"},
                            "min_doc_count": 1,
                            "interval": "hour",
                            "format": "HH",
                            "time_zone": "+08:00",
                        }
                    }
                },
            }
        },
    }
lcn's avatar
lcn committed
194
    
lcn's avatar
lcn committed
195 196
    log.info("alarm time distribute query_body={}".format(query_body))
    async with EsUtil() as es:
lcn's avatar
lcn committed
197 198
        es_result = await es.search_origin(body=query_body,
                                           index=constants.POINT_1MIN_EVENT)
lcn's avatar
lcn committed
199 200
        print(f"alarm time distribute es_result = {es_result}")
    buckets = es_result["aggregations"]["cid_aggs"]["buckets"] or []
lcn's avatar
lcn committed
201 202
    time_distribution_map = {"day_alarm_cnt": 0, "night_alarm_cnt": 0,
                             "morning_alarm_cnt": 0}
lcn's avatar
lcn committed
203 204 205 206 207 208 209 210 211 212 213
    for i in buckets:
        cid_buckets = i.get("time_alarms", {}).get("buckets", [])
        for item in cid_buckets:
            hour = int(item["key_as_string"])
            if hour >= 6 and hour < 18:
                time_distribution_map["day_alarm_cnt"] += item["doc_count"]
            elif hour >= 18 and hour <= 23:
                time_distribution_map["night_alarm_cnt"] += item["doc_count"]
            else:
                time_distribution_map["morning_alarm_cnt"] += item["doc_count"]
    return time_distribution_map