Commit 6d640792 authored by peng.xiaozhe's avatar peng.xiaozhe

首页 报警排名rank-type-ranking

parent 1c264a88
......@@ -164,6 +164,35 @@ async def proxy_safe_run_info(cids, start_time_str=None, end_time_str=None):
async def alarm_time_distribution(company_ids, start, end):
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:
datas = await conn.fetchall(sql, args=(company_ids, ))
time_distribution_map = {"day_alarm_cnt": 0, "night_alarm_cnt": 0, "morning_alarm_cnt": 0}
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):
start_dt = datetime.strptime(start, "%Y-%m-%d %H:%M:%S")
end_dt = datetime.strptime(end, "%Y-%m-%d %H:%M:%S")
es_start_str = datetime(year=start_dt.year, month=start_dt.month, day=start_dt.day).strftime(
......
......@@ -13,7 +13,8 @@ from pot_libs.mysql_util.mysql_util import MysqlUtil
from unify_api import constants
from unify_api.constants import Importance, EVENT_TYPE_MAP, SDU_ALARM_LIST
from unify_api.modules.alarm_manager.dao.list_static_dao import \
sdu_alarm_importance_dao, alarm_aggs_importance
sdu_alarm_importance_dao, alarm_aggs_importance, \
sdu_alarm_importance_dao_new15
from unify_api.modules.common.dao.common_dao import monitor_point_join, \
monitor_by_cid
from unify_api.modules.common.procedures.common_utils import get_electric_index
......@@ -1681,7 +1682,7 @@ async def cid_alarm_importance_count(cid, start, end):
"""计算工厂报警数, 按报警等级"""
monitor_point_list = await monitor_point_join(cid)
point_list = [i["pid"] for i in monitor_point_list]
es_res = await sdu_alarm_importance_dao(start, end, point_list)
es_res = await sdu_alarm_importance_dao_new15(start, end, point_list)
es_res_key = {i["key"]: i for i in es_res}
res_list = []
......
......@@ -23,6 +23,7 @@ from unify_api.modules.common.procedures.points import get_points
from unify_api.modules.home_page.procedures.count_info_pds import \
datetime_to_timestamp
from unify_api.utils.es_query_body import agg_statistics
from unify_api.utils.time_format import last30_day_range_today
async def proxy_alarm_score(cids):
......@@ -129,6 +130,80 @@ async def security_level_count(cids):
async def alarm_percentage_count(cids):
start, end = last30_day_range_today()
importance_sql = f"""
SELECT
COUNT(*) doc_count,
importance
FROM
point_1min_event pevent
WHERE
cid = %s
AND pevent.event_datetime >= '{start}'
AND pevent.event_datetime <= '{end}'
GROUP BY
pevent.importance
"""
event_type_sql = f"""
SELECT
COUNT(*) doc_count,
event_type
FROM
point_1min_event pevent
WHERE
cid = %s
AND pevent.event_datetime >= '{start}'
AND pevent.event_datetime <= '{end}'
GROUP BY
pevent.event_type;
"""
async with MysqlUtil() as conn:
event_type_data = await conn.fetchall(event_type_sql, args=(cids,))
importance_data = await conn.fetchall(importance_sql, args=(cids,))
first_alarm_cnt, second_alarm_cnt, third_alarm_cnt = 0, 0, 0
for bucket in importance_data:
if bucket["importance"] == Importance.First.value:
first_alarm_cnt += bucket["doc_count"]
elif bucket["importance"] == Importance.Second.value:
second_alarm_cnt += bucket["doc_count"]
elif bucket["importance"] == Importance.Third.value:
third_alarm_cnt += bucket["doc_count"]
temperature_cnt, residual_current_cnt, electric_param_cnt = 0, 0, 0
for bucket in event_type_data:
if bucket["event_type"] in [
"overTemp",
"overTempRange1min",
"overTempRange15min",
"overTempTrendDaily",
"overTempTrendQuarterly",
]:
temperature_cnt += bucket["doc_count"]
elif bucket["event_type"] in [
"overResidualCurrent",
]:
residual_current_cnt += bucket["doc_count"]
else:
electric_param_cnt += bucket["doc_count"]
time_distribution_map = await alarm_time_distribution(cids, start,
end)
alarm_percentage_map = {
"first_alarm_cnt": first_alarm_cnt,
"second_alarm_cnt": second_alarm_cnt,
"third_alarm_cnt": third_alarm_cnt,
"temperature_cnt": temperature_cnt,
"residual_current_cnt": residual_current_cnt,
"electric_param_cnt": electric_param_cnt,
"day_alarm_cnt": time_distribution_map["day_alarm_cnt"],
"night_alarm_cnt": time_distribution_map["night_alarm_cnt"],
"morning_alarm_cnt": time_distribution_map["morning_alarm_cnt"],
}
return alarm_percentage_map
async def alarm_percentage_count_old(cids):
now = datetime.now()
end_timestamp = datetime_to_timestamp(now)
start_timestamp = datetime_to_timestamp(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment