Commit 8080048e authored by ZZH's avatar ZZH

remove es 2023-7-20

parent ea3c9232
import pendulum import pendulum
from pot_libs.es_util.es_utils import EsUtil
from pot_libs.mysql_util.mysql_util import MysqlUtil from pot_libs.mysql_util.mysql_util import MysqlUtil
from pot_libs.logger import log
from unify_api.constants import POINT_1MIN_EVENT, SDU_ALARM_LIST from unify_api.constants import POINT_1MIN_EVENT, SDU_ALARM_LIST
from unify_api.utils.time_format import convert_es_str from unify_api.utils.time_format import convert_es_str
...@@ -43,58 +41,32 @@ async def new_list_alarm_dao(cid, points, start, end, importance, offset, ...@@ -43,58 +41,32 @@ async def new_list_alarm_dao(cid, points, start, end, importance, offset,
return total.get('total_count', 0), data return total.get('total_count', 0), data
async def wx_list_alarm_dao(cids, product, start, end): async def wx_list_alarm_dao(cids, start, end):
"""小程序消息列表, 取当前范围最新40条""" """小程序消息列表, 取当前范围最新40条"""
cid = cids[0] if isinstance(cids, (list, tuple)) else cids
# alarm_list = list(EVENT_TYPE_MAP.keys()) # 安电u cond_lst = [f"cid={cid}", f"event_type in {tuple(SDU_ALARM_LIST)}"]
#
# if product == Product.RecognitionElectric.value: # 识电u
alarm_list = SDU_ALARM_LIST
query_body = {
"size": 40,
"query": {
"bool": {
"must": [
{
"terms": {
"cid": cids
}
},
{
"terms": {
"type.keyword": alarm_list
}
}
]
}
},
"sort": [
{
"datetime": {
"order": "desc"
}
}
]
}
if start and end: if start and end:
start_es = convert_es_str(start) s_dts = convert_es_str(start)
end_es = convert_es_str(end) e_dts = convert_es_str(end)
query_body["query"]["bool"]["must"].append( cond_lst.append(f"event_datetime BETWEEN '{s_dts}' and '{e_dts}'")
{ else:
"range": { now_date = pendulum.now()
"datetime": { time_format = "%Y-%m-%d %H:%M:%S"
"gte": start_es, end_date = str(now_date.strftime(time_format))
"lte": end_es cond_lst.append(f"event_datetime < '{end_date}'")
}
} cond_str = " AND ".join(cond_lst)
} async with MysqlUtil() as conn:
) sql = f"select count(*) cnt from point_1min_event WHERE {cond_str};"
total = await conn.fetchone(sql)
log.info(f"index:{index}--query_body:{query_body}") total_count = total.get("cnt", 0)
async with EsUtil() as es: if total_count <= 0:
es_re = await es.search_origin(body=query_body, index=index) return 0, []
return es_re
sql = f"select * from point_1min_event WHERE {cond_str} " \
f"order by event_datetime desc limit 40;"
data = await conn.fetchall(sql)
return total_count, data
async def list_alarm_zdu_dao(cid, points, start, end, importance, event_type): async def list_alarm_zdu_dao(cid, points, start, end, importance, event_type):
...@@ -123,50 +95,17 @@ async def list_alarm_zdu_dao(cid, points, start, end, importance, event_type): ...@@ -123,50 +95,17 @@ async def list_alarm_zdu_dao(cid, points, start, end, importance, event_type):
async def wx_list_alarm_zdu_dao(cid, points, start, end): async def wx_list_alarm_zdu_dao(cid, points, start, end):
"""智电u, wx小程序, 取当前范围最新100条""" """智电u, wx小程序, 取当前范围最新100条"""
mid_li = [f"cid={cid}"]
query_body = { if len(points) == 1:
"size": 100, mid_li.append(f"pid={points[0]}")
"query": { else:
"bool": { mid_li.append(f"pid in {tuple(points)}")
"must": [
{
"term": {
"cid": cid
}
},
{
"terms": {
"point_id": points
}
}
]
}
},
"sort": [
{
"datetime": {
"order": "desc"
}
}
]
}
if start and end: if start and end:
start_es = convert_es_str(start) mid_li.append(f"event_datetime BETWEEN '{start}' and '{end}'")
end_es = convert_es_str(end) sql = f"select * from point_1min_event where {' and '.join(mid_li)} " \
query_body["query"]["bool"]["must"].append( f"order by event_datetime desc limit 100;"
{ async with MysqlUtil() as conn:
"range": { return await conn.fetchall(sql)
"datetime": {
"gte": start_es,
"lte": end_es
}
}
}
)
log.info(f"index:{index}--query_body:{query_body}")
async with EsUtil() as es:
es_re = await es.search_origin(body=query_body, index=index)
return es_re
async def zdu_alarm_sort_dao(cid, start, end, page_size, page_num): async def zdu_alarm_sort_dao(cid, start, end, page_size, page_num):
......
...@@ -2,8 +2,7 @@ from unify_api import constants ...@@ -2,8 +2,7 @@ from unify_api import constants
from unify_api.modules.alarm_manager.components.list_alarm import \ from unify_api.modules.alarm_manager.components.list_alarm import \
ListAlarmResponse, Alarm ListAlarmResponse, Alarm
from unify_api.modules.alarm_manager.dao.alarm_setting_dao import \ from unify_api.modules.alarm_manager.dao.alarm_setting_dao import \
company_extend_dao, list_alarm_data_dao, get_list_alarm_dao, \ company_extend_dao, get_list_alarm_dao, get_total_list_alarm_dao
get_total_list_alarm_dao
from unify_api.modules.alarm_manager.dao.list_alarm_dao import \ from unify_api.modules.alarm_manager.dao.list_alarm_dao import \
wx_list_alarm_dao, \ wx_list_alarm_dao, \
wx_list_alarm_zdu_dao, list_alarm_zdu_dao, new_list_alarm_dao wx_list_alarm_zdu_dao, list_alarm_zdu_dao, new_list_alarm_dao
...@@ -94,43 +93,36 @@ async def new_list_alarm_service(cid, storeys, offset, page_size, start, end, ...@@ -94,43 +93,36 @@ async def new_list_alarm_service(cid, storeys, offset, page_size, start, end,
async def wx_list_alarm_service(cids, product, start, end): async def wx_list_alarm_service(cids, product, start, end):
"""小程序消息列表""" """小程序消息列表"""
# 1. es查询结果 total, results = await wx_list_alarm_dao(cids, start, end)
es_res = await wx_list_alarm_dao(cids, product, start, end) if not results:
if not es_res["hits"]["hits"]:
return ListAlarmResponse(total=0, rows=[]) return ListAlarmResponse(total=0, rows=[])
# 2. 构建返回数据
cid_info_map = await get_cid_info(all=True) cid_info_map = await get_cid_info(all=True)
rows = [] rows = []
for info in es_res["hits"]["hits"]: for res in results:
es_id = info["_id"] es_id = res.get("id")
source = info["_source"] type = res.get("event_type")
type = source.get("type")
mode = source.get("mode")
type_str = constants.SDU_EVENT_TYPE_MAP.get(type, type) type_str = constants.SDU_EVENT_TYPE_MAP.get(type, type)
point_id = source.get("point_id") point_id = res.get("pid")
location_id = source.get("location_id") date_time = res.get("event_datetime")
date_time = source.get("datetime")
dt = time_format.convert_to_dt(date_time) dt = time_format.convert_to_dt(date_time)
date_time = time_format.convert_dt_to_timestr(dt) date_time = time_format.convert_dt_to_timestr(dt)
event_duration = source.get("event_duration") event_duration = res.get("event_duration")
cid = int(source.get("cid")) if source.get("cid") else source.get( cid = int(res.get("cid")) if res.get("cid") else res.get("cid")
"cid")
alarm = Alarm( alarm = Alarm(
point_id=point_id, point_id=point_id,
name=source.get("name"), name=res.get("name"),
importance=source.get("importance"), importance=res.get("importance"),
date_time=date_time, date_time=date_time,
type=type, type=type,
type_name=type_str, type_name=type_str,
description=source.get("message"), description=res.get("message"),
es_id=es_id, es_id=es_id,
event_duration=event_duration, event_duration=event_duration,
company_name=cid_info_map.get(cid, {}).get("fullname", ""), company_name=cid_info_map.get(cid, {}).get("fullname", ""),
) )
rows.append(alarm) rows.append(alarm)
# total小程序不分页, 返回了但是不用 return ListAlarmResponse(total=total, rows=rows)
real_total = es_res["hits"]["total"]
return ListAlarmResponse(total=real_total, rows=rows)
async def list_alarm_zdu_service(cid, point_list, page_num, page_size, start, async def list_alarm_zdu_service(cid, point_list, page_num, page_size, start,
...@@ -193,26 +185,26 @@ async def list_alarm_zdu_service(cid, point_list, page_num, page_size, start, ...@@ -193,26 +185,26 @@ async def list_alarm_zdu_service(cid, point_list, page_num, page_size, start,
async def wx_list_alarm_zdu_service(cid, point_list, start, end): async def wx_list_alarm_zdu_service(cid, point_list, start, end):
"""小程序消息列表-智电u""" """小程序消息列表-智电u"""
# 1. es查询结果 # 1. es查询结果
es_res = await wx_list_alarm_zdu_dao(cid, point_list, start, end) results = await wx_list_alarm_zdu_dao(cid, point_list, start, end)
if not es_res["hits"]["hits"]: if not results:
return ListAlarmResponse(total=0, rows=[]) return ListAlarmResponse(total=0, rows=[])
# 2. 获取工厂, 报警type对应的描述信息 # 2. 获取工厂, 报警type对应的描述信息
event_dic = await company_extend_dao(cid) event_dic = await company_extend_dao(cid)
event_dic_map = {event["key"]: event for event in event_dic} event_dic_map = {event["key"]: event for event in event_dic}
# 3. 构建返回数据 # 3. 构建返回数据
rows = [] rows = []
for info in es_res["hits"]["hits"]: for res in results:
es_id = info["_id"] es_id = res.get("id")
source = info["_source"] type = res.get("event_type")
type = source.get("type") mode = res.get("event_mode")
mode = source.get("mode")
type_str = constants.EVENT_TYPE_MAP.get(type, type) type_str = constants.EVENT_TYPE_MAP.get(type, type)
point_id = source.get("point_id") point_id = res.get("pid")
location_id = source.get("location_id") location_id = res.get("lid")
date_time = source.get("datetime") date_time = res.get("event_datetime")
dt = time_format.convert_to_dt(date_time) dt = time_format.convert_to_dt(date_time)
date_time = time_format.convert_dt_to_timestr(dt) date_time = time_format.convert_dt_to_timestr(dt)
event_duration = source.get("event_duration") event_duration = res.get("event_duration")
if point_id and mode == "scope": if point_id and mode == "scope":
url = "/scope_details?doc_id=%s" % es_id url = "/scope_details?doc_id=%s" % es_id
redirect_type = "scope" redirect_type = "scope"
...@@ -224,8 +216,8 @@ async def wx_list_alarm_zdu_service(cid, point_list, start, end): ...@@ -224,8 +216,8 @@ async def wx_list_alarm_zdu_service(cid, point_list, start, end):
redirect_type = "" redirect_type = ""
alarm = Alarm( alarm = Alarm(
name=source.get("name"), name=res.get("name"),
importance=source.get("importance"), importance=res.get("importance"),
date_time=date_time, date_time=date_time,
type=type, type=type,
type_name=type_str, type_name=type_str,
...@@ -235,13 +227,12 @@ async def wx_list_alarm_zdu_service(cid, point_list, start, end): ...@@ -235,13 +227,12 @@ async def wx_list_alarm_zdu_service(cid, point_list, start, end):
url=url, url=url,
event_duration=event_duration, event_duration=event_duration,
point_id=point_id, point_id=point_id,
content=source.get("message"), content=res.get("message"),
) )
rows.append(alarm) rows.append(alarm)
# total小程序不分页, 返回了但是不用 # total小程序不分页, 返回了但是不用
total = es_res["hits"]["total"] return ListAlarmResponse(total=len(results), rows=rows)
return ListAlarmResponse(total=total, rows=rows)
async def list_alarm_service(cid, point_id, start, end, importance, page_size, async def list_alarm_service(cid, point_id, start, end, importance, page_size,
......
import calendar
from datetime import datetime from datetime import datetime
import pendulum 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 pot_libs.mysql_util.mysql_util import MysqlUtil
from unify_api import constants from unify_api import constants
from unify_api.constants import CST from unify_api.constants import CST
...@@ -50,8 +45,6 @@ async def today_alarm_cnt(cids): ...@@ -50,8 +45,6 @@ async def today_alarm_cnt(cids):
return cid_alarm_map return cid_alarm_map
async def proxy_safe_run_info(cids, start_time_str=None, async def proxy_safe_run_info(cids, start_time_str=None,
end_time_str=None): end_time_str=None):
""" """
...@@ -149,65 +142,10 @@ async def alarm_time_distribution(company_ids, start, end): ...@@ -149,65 +142,10 @@ async def alarm_time_distribution(company_ids, start, end):
"morning_alarm_cnt": 0} "morning_alarm_cnt": 0}
for data in datas: for data in datas:
hour = int(data["event_hour"]) hour = int(data["event_hour"])
if hour >= 6 and hour < 18: if 6 <= hour < 18:
time_distribution_map["day_alarm_cnt"] += data["event_count"] time_distribution_map["day_alarm_cnt"] += data["event_count"]
elif hour >= 18 and hour <= 23: elif 18 <= hour <= 23:
time_distribution_map["night_alarm_cnt"] += data["event_count"] time_distribution_map["night_alarm_cnt"] += data["event_count"]
else: else:
time_distribution_map["morning_alarm_cnt"] += data["event_count"] time_distribution_map["morning_alarm_cnt"] += data["event_count"]
return time_distribution_map 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(
"%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}}]
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",
}
}
},
}
},
}
log.info("alarm time distribute query_body={}".format(query_body))
async with EsUtil() as es:
es_result = await es.search_origin(body=query_body,
index=constants.POINT_1MIN_EVENT)
print(f"alarm time distribute es_result = {es_result}")
buckets = es_result["aggregations"]["cid_aggs"]["buckets"] or []
time_distribution_map = {"day_alarm_cnt": 0, "night_alarm_cnt": 0,
"morning_alarm_cnt": 0}
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
from datetime import datetime
from pot_libs.es_util.es_utils import EsUtil
from pot_libs.mysql_util.mysql_util import MysqlUtil from pot_libs.mysql_util.mysql_util import MysqlUtil
from unify_api import constants
async def month_md_space(inline_id: int, month_list: list): async def month_md_space(inline_id: int, month_list: list):
......
...@@ -5,12 +5,9 @@ This is a tornado process and responds request from web back server. ...@@ -5,12 +5,9 @@ This is a tornado process and responds request from web back server.
""" """
import pendulum import pendulum
import datetime
import pandas as pd import pandas as pd
from pot_libs.es_util.es_utils import EsUtil
from pot_libs.mysql_util.mysql_util import MysqlUtil from pot_libs.mysql_util.mysql_util import MysqlUtil
from unify_api.constants import ENERGY_INVESTMENT_ESS, \ from unify_api.constants import ENERGY_INVESTMENT_ESS
INLINE_15MIN_POWER_ESINDEX, INLINE_1DAY_POWER_ESINDEX
from unify_api.modules.energy_optimize.service.ess_optimation_tool import \ from unify_api.modules.energy_optimize.service.ess_optimation_tool import \
EssOptimizationTool EssOptimizationTool
from unify_api.modules.energy_optimize.service.ess_utils import \ from unify_api.modules.energy_optimize.service.ess_utils import \
......
...@@ -6,10 +6,8 @@ This is a tornado process and responds request from web back server. ...@@ -6,10 +6,8 @@ This is a tornado process and responds request from web back server.
import pendulum import pendulum
import datetime import datetime
import pandas as pd import pandas as pd
from pot_libs.es_util.es_utils import EsUtil
from pot_libs.mysql_util.mysql_util import MysqlUtil from pot_libs.mysql_util.mysql_util import MysqlUtil
from unify_api.constants import ENERGY_INVESTMENT_PV, \ from unify_api.constants import ENERGY_INVESTMENT_PV
INLINE_15MIN_POWER_ESINDEX, INLINE_1DAY_POWER_ESINDEX
from unify_api.modules.energy_optimize.service.energy_store_optimize import \ from unify_api.modules.energy_optimize.service.energy_store_optimize import \
AutoDic AutoDic
from unify_api.modules.energy_optimize.service.ess_utils import \ from unify_api.modules.energy_optimize.service.ess_utils import \
......
from pot_libs.logger import log
from unify_api.utils.time_format import convert_es_str
from pot_libs.es_util.es_utils import EsUtil
from pot_libs.es_util.es_query import EsQuery
from pot_libs.common.components.query import PageRequest, Equal, Filter
from unify_api.modules.zhiwei_u.components.scope_operations_cps import \
ScopeDetailsResponse
from pot_libs.mysql_util.mysql_util import MysqlUtil from pot_libs.mysql_util.mysql_util import MysqlUtil
......
from pot_libs.es_util.es_utils import EsUtil
from pot_libs.settings import SETTING from pot_libs.settings import SETTING
from unify_api.modules.anshiu.dao.fine_monitor_dao import get_mtid_by_pid_dao, \ from unify_api.modules.anshiu.dao.fine_monitor_dao import get_mtid_by_pid_dao, \
get_sid_by_mtid_dao, get_mtids_by_pids_dao get_sid_by_mtid_dao, get_mtids_by_pids_dao
from unify_api.utils.common_utils import make_tdengine_data_as_list from unify_api.utils.common_utils import make_tdengine_data_as_list
from unify_api.utils.taos_new import get_td_engine_data from unify_api.utils.taos_new import get_td_engine_data
from unify_api.utils.time_format import convert_es_str, CST
from unify_api.modules.zhiwei_u.config import SCOPE_DATABASE
from pot_libs.mysql_util.mysql_util import MysqlUtil from pot_libs.mysql_util.mysql_util import MysqlUtil
......
...@@ -21,16 +21,9 @@ from unify_api.modules.zhiwei_u.dao.scope_operations_dao import \ ...@@ -21,16 +21,9 @@ from unify_api.modules.zhiwei_u.dao.scope_operations_dao import \
select_cname_by_cids, select_sid_by_pids select_cname_by_cids, select_sid_by_pids
from unify_api.modules.zhiwei_u.components.scope_operations_cps import \ from unify_api.modules.zhiwei_u.components.scope_operations_cps import \
SearchScopeResq, Alarm SearchScopeResq, Alarm
from pot_libs.common.components.query import (
PageRequest,
Equal,
Filter
)
from unify_api.modules.zhiwei_u.dao.warning_operations_dao import get_username from unify_api.modules.zhiwei_u.dao.warning_operations_dao import get_username
from pot_libs.common.components.responses import Success from pot_libs.common.components.responses import Success
from unify_api.modules.electric.procedures.electric_util import get_wiring_type from unify_api.modules.electric.procedures.electric_util import get_wiring_type
from pot_libs.es_util.es_utils import EsUtil
from pot_libs.es_util.es_query import EsQuery
from pot_libs.logger import log from pot_libs.logger import log
from unify_api.modules.zhiwei_u.components.scope_operations_cps import \ from unify_api.modules.zhiwei_u.components.scope_operations_cps import \
ScopeDetailsResponse, ScopeContent ScopeDetailsResponse, ScopeContent
......
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