Commit 14562472 authored by wang.wenrong's avatar wang.wenrong

Merge branch 'wwr' into 'develop'

Wwr

See merge request !27
parents 76e2d021 665c4024
......@@ -268,7 +268,7 @@ async def list_alarm_zdu_dao_new15(cid, points, start,
async def wx_list_alarm_zdu_dao(cid, points, start, end):
"""智电u, wx小程序, 取当前范围最新100条"""
query_body = {
"size": 100,
"query": {
......@@ -346,7 +346,7 @@ async def zdu_alarm_sort_dao(cid, start, end, page_size, page_num):
return data
async def alarm_content_time_distribution_dao(cid, start, end,):
async def alarm_content_time_distribution_dao(cid, start, end, ):
"""
智电温度漏电流电参数分布
"""
......@@ -371,4 +371,26 @@ async def alarm_content_time_distribution_dao(cid, start, end,):
"""
async with MysqlUtil() as conn:
elec_data = await conn.fetchall(sql, )
return elec_data
return elec_data if elec_data else []
async def zdu_summary_dao(cid, start, end):
sql = f"""
SELECT
pid,
count(*) coun
FROM
point_1min_event
WHERE
cid = {cid}
AND event_datetime BETWEEN '{start}'
AND '{end}' AND pid is not null
GROUP BY
pid
"""
async with MysqlUtil() as conn:
summary_data = await conn.fetchall(sql, )
total_alarm_cnt = sum([i.get('coun') for i in summary_data])
alarm_points = len(summary_data)
return total_alarm_cnt, alarm_points
......@@ -187,7 +187,7 @@ async def sdu_alarm_limit_type_dao_new15(start, end, points):
f"event_datetime BETWEEN '{start}' and '{end}' " \
f"and event_type in {tuple(SDU_ALARM_LIST)} GROUP BY pid, event_type"
async with MysqlUtil() as conn:
datas = await conn.fetchall(sql, args=(points, ))
datas = await conn.fetchall(sql, args=(points,))
return datas
......@@ -366,57 +366,24 @@ async def zdu_alarm_aggs_date_impotent(cid, start, end):
"""zdu求安全运行
根据每日聚合,再根据等级聚合
"""
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(
"%Y-%m-%dT%H:%M:%S+08:00")
es_end_str = end_dt.strftime("%Y-%m-%dT%H:%M:%S+08:00")
query_body = {
"size": 0,
"query": {
"bool": {
"must": [
{
"term": {
"cid": cid
}
},
{
"range": {
"datetime": {
"gte": es_start_str,
"lte": es_end_str
}
}
}
]
}
},
"aggs": {
"date_day": {
"date_histogram": {
"field": "datetime",
"interval": "day",
"time_zone": "+08:00",
"format": "yyyy-MM-dd",
"min_doc_count": 0,
"extended_bounds": {"min": start_dt.strftime("%Y-%m-%d"),
"max": end_dt.strftime(
"%Y-%m-%d")},
},
"aggs": {"important_cnt": {"terms": {"field": "importance"}}}
}
}
}
log.info("query_body={}".format(query_body))
async with EsUtil() as es:
es_result = await es.search_origin(body=query_body,
index=constants.POINT_1MIN_EVENT)
return es_result["aggregations"]["date_day"]["buckets"]
sql = f"""
SELECT DISTINCT
DATE_FORMAT( event_datetime, '%m-%d' ) dat
FROM
point_1min_event
WHERE
cid = {cid}
AND event_datetime BETWEEN '{start}'
AND '{end}'
AND importance !=1
"""
async with MysqlUtil() as conn:
datas = await conn.fetchall(sql)
return len(datas) if datas else 0
async def sdu_alarm_aggs_date_importance(cid): # todo: 扬尘es待改
async def sdu_alarm_aggs_date_importance(cid): # todo: 扬尘es待改
"""按日期,再按等级聚合"""
query_body = {
"size": 0,
......
......@@ -24,7 +24,7 @@ from unify_api.modules.home_page.procedures.security_info_pds import \
alarm_count_info_new15
from unify_api.utils.common_utils import round_1, division_two
from unify_api.modules.alarm_manager.dao.list_alarm_dao import \
zdu_alarm_sort_dao, alarm_content_time_distribution_dao
zdu_alarm_sort_dao, alarm_content_time_distribution_dao, zdu_summary_dao
async def sdu_alarm_statistics_service(cids, start, end, product):
......@@ -464,23 +464,17 @@ async def zdu_content_distribution_service(cid, start, end, product):
async def zdu_summary_service(cid, start, end, product):
"""报警统计-统计概况信息-智电u"""
# 1. 报警总数, 报警监测点数, 平均报警 = 报警总数除以报警监测点
total_alarm_cnt, alarm_points = await zdu_summary_info(cid, start, end)
# total_alarm_cnt, alarm_points = await zdu_summary_info(cid, start, end)
total_alarm_cnt, alarm_points = await zdu_summary_dao(cid, start, end)
# 2. 安全运行, 本月累计安全运行天数,从月初算起, 未出现一级报警则加一天
es_buckets = await zdu_alarm_aggs_date_impotent(cid, start, end)
total_day = len(es_buckets)
has_alarm = 0
for bucket in es_buckets:
inner_buckets = bucket["important_cnt"]["buckets"]
for ib in inner_buckets:
if ib["key"] == 1:
has_alarm += 1
safe_run = await zdu_alarm_aggs_date_impotent(cid, start, end)
# 3. 时段分布, 白天/黑夜/凌晨
time_distribution_map = await alarm_time_distribution([cid], start, end)
return ZsResp(
total_alarm_cnt=total_alarm_cnt,
alarm_points_cnt=alarm_points,
aver_alarm=round_1(division_two(total_alarm_cnt, alarm_points)),
safe_run=total_day - has_alarm,
safe_run=safe_run,
time_interval_distribution=TimeCount(
daytime_cnt=time_distribution_map["day_alarm_cnt"],
night_cnt=time_distribution_map["night_alarm_cnt"],
......
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