Commit 1599f549 authored by ZZH's avatar ZZH

monitor name support multi lang 2024-05-30

parent 2c70ab26
...@@ -615,3 +615,24 @@ E_TYPE_LANG = { ...@@ -615,3 +615,24 @@ E_TYPE_LANG = {
"de_DE": "" "de_DE": ""
}, },
} }
PHASE_LINE_LANG = {
"A相": {
"en_US": "Phase A",
"de_DE": ""
},
"B相": {
"en_US": "Phase B",
"de_DE": ""
},
"C相": {
"en_US": "Phase C",
"de_DE": ""
},
"N线": {
"en_US": "Neutral Line",
"de_DE": ""
},
}
# -*- coding:utf-8 -*-
import json
from unify_api import constants 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
...@@ -11,8 +13,12 @@ from unify_api.modules.common.procedures.points import points_by_storeys ...@@ -11,8 +13,12 @@ from unify_api.modules.common.procedures.points import points_by_storeys
from unify_api.modules.electric.dao.electric_dao import \ from unify_api.modules.electric.dao.electric_dao import \
monitor_point_join_points monitor_point_join_points
from unify_api.utils import time_format from unify_api.utils import time_format
from unify_api.modules.common.dao.common_dao import load_user_lang from unify_api.modules.common.dao.common_dao import (
from unify_api.modules.common.procedures.multi_lang import load_event_msg load_user_lang, load_monitor_names
)
from unify_api.modules.common.procedures.multi_lang import (
load_event_msg, load_phase_line
)
async def new_list_alarm_service(cid, storeys, offset, page_size, start, end, async def new_list_alarm_service(cid, storeys, offset, page_size, start, end,
...@@ -238,69 +244,90 @@ async def wx_list_alarm_zdu_service(cid, point_list, start, end): ...@@ -238,69 +244,90 @@ async def wx_list_alarm_zdu_service(cid, point_list, start, end):
return ListAlarmResponse(total=len(results), rows=rows) return ListAlarmResponse(total=len(results), rows=rows)
async def pack_alarm_records(records, pid, lang):
alarm_recs, mtr_names = [], {}
if lang != "zh_CN":
mtids = [r["mtid"] for r in records]
mtr_names = await load_monitor_names(mtids, lang)
for r in records:
event_type = r.get("event_type")
event_duration = r.get("event_duration")
type_str = constants.EVENT_TYPE_MAP.get(event_type)
es_id = r.get("id")
if pid and r.get("event_mode") == "scope":
url, redirect_type = f"/scope_details?doc_id={es_id}", "scope"
elif r.get("lid") and event_type in constants.TEMP_SCOPE_URL_TYPE:
url, redirect_type = f"/temp_trend?doc_id={es_id}", "temp_trend"
else:
url, redirect_type = None, ""
mtr_name = r.get("name")
if lang == "zh_CN":
event_msg = r.get("message")
else:
event_param = r["event_param"]
if event_param:
event_param = json.loads(r["event_param"])
event_msg = load_event_msg(event_type, event_param, lang)
if not event_msg:
event_msg = r.get("message")
phase_line = load_phase_line(mtr_name, lang)
name = mtr_names[r["mtid"]]
mtr_name = f"{name}{phase_line}" if phase_line else name
alarm = Alarm(
name=mtr_name,
importance=r.get("importance"),
date_time=str(r.get("event_datetime")),
type=event_type,
type_name=type_str,
description=event_msg,
redirect_type=redirect_type,
es_id=es_id,
url=url,
event_duration=round(event_duration)
if isinstance(event_duration, float) else event_duration,
company_name=r.get("fullname") or '',
)
alarm_recs.append(alarm)
return alarm_recs
async def list_alarm_service(cid, user_id, point_id, start, end, importance, async def list_alarm_service(cid, user_id, point_id, start, end, importance,
page_size, page_num, alarm_type): page_size, page_num, alarm_type):
li = ["event_mode!='scope'"] conds = ["event_mode!='scope'"]
if point_id: if point_id:
li.append(f"pid={point_id}") conds.append(f"pid={point_id}")
else: else:
if not isinstance(cid, list): if not isinstance(cid, list):
cid = [cid] cid = [cid]
cid_where = str(tuple(cid)).replace(",)", ")") cid_where = str(tuple(cid)).replace(",)", ")")
li.append(f"cid in {cid_where}") conds.append(f"cid in {cid_where}")
if start and end: if start and end:
li.append(f"event_datetime BETWEEN '{start}' and '{end}'") conds.append(f"event_datetime BETWEEN '{start}' and '{end}'")
if importance: if importance:
if len(importance) == 1: if len(importance) == 1:
li.append(f"importance={importance[0]}") conds.append(f"importance={importance[0]}")
else: else:
li.append(f"importance in {str(tuple(importance)).strip(',')}") conds.append(f"importance in {str(tuple(importance)).strip(',')}")
else: else:
li.append("importance in (1, 2, 3)") conds.append("importance in (1, 2, 3)")
if alarm_type: if alarm_type:
if len(alarm_type) == 1: if len(alarm_type) == 1:
li.append(f"event_type='{alarm_type[0]}'") conds.append(f"event_type='{alarm_type[0]}'")
else: else:
li.append(f"event_type in {str(tuple(alarm_type)).strip(',')}") conds.append(f"event_type in {str(tuple(alarm_type)).strip(',')}")
mid_sql = " and ".join(li)
total = await get_total_list_alarm_dao(mid_sql)
mid_sql2 = " and ".join(["point_1min_event." + i for i in li])
datas = await get_list_alarm_dao(mid_sql2, page_size, page_num)
lang = await load_user_lang(user_id)
rows = []
for data in datas:
event_type = data.get("event_type")
event_duration = data.get("event_duration")
type_str = constants.EVENT_TYPE_MAP.get(event_type)
location_id = data.get("lid")
es_id = data.get("id")
if point_id and data.get("event_mode") == "scope":
url = "/scope_details?doc_id=%s" % es_id
redirect_type = "scope"
elif location_id and type in constants.TEMP_SCOPE_URL_TYPE:
url = "/temp_trend?doc_id=%s" % es_id
redirect_type = "temp_trend"
else:
url = None
redirect_type = ""
if lang == "zh_CN": total = await get_total_list_alarm_dao(" AND ".join(conds))
event_msg = data.get("message")
else: conds_str = " AND ".join(["point_1min_event." + i for i in conds])
event_msg = load_event_msg(event_type, data["event_param"], lang) alarm_lst = await get_list_alarm_dao(conds_str, page_size, page_num)
alarm = Alarm( lang = await load_user_lang(user_id)
name=data.get("name"), alarm_records = await pack_alarm_records(alarm_lst, point_id, lang)
importance=data.get("importance"), return ListAlarmResponse(total=total, rows=alarm_records)
date_time=str(data.get("event_datetime")),
type=event_type,
type_name=type_str,
description=event_msg,
redirect_type=redirect_type,
es_id=es_id,
url=url,
event_duration=round(event_duration)
if isinstance(event_duration, float) else event_duration,
company_name=data.get("fullname") or '',
)
rows.append(alarm)
return ListAlarmResponse(total=total, rows=rows)
...@@ -357,3 +357,10 @@ async def load_point_pttl_mean(start, end, pid): ...@@ -357,3 +357,10 @@ async def load_point_pttl_mean(start, end, pid):
async with MysqlUtil() as conn: async with MysqlUtil() as conn:
datas = await conn.fetchall(sql, args=(pid,)) datas = await conn.fetchall(sql, args=(pid,))
return datas return datas
async def load_monitor_names(mtids, lang):
sql = f"SELECT mtid, {lang} FROM power_iot.monitor_name WHERE mtid IN %s;"
async with MysqlUtil() as conn:
return {r["mtid"]: r[lang] for r in
await conn.fetchall(sql, (tuple(mtids),))}
...@@ -3,9 +3,8 @@ ...@@ -3,9 +3,8 @@
DATE:2024/5/30 11:34 DATE:2024/5/30 11:34
""" """
import json from pot_libs.logger import log
from unify_api.constants import E_TYPE_LANG, PHASE_LINE_LANG
from unify_api.constants import E_TYPE_LANG
def load_event_msg(e_type, event_param, lang): def load_event_msg(e_type, event_param, lang):
...@@ -16,4 +15,17 @@ def load_event_msg(e_type, event_param, lang): ...@@ -16,4 +15,17 @@ def load_event_msg(e_type, event_param, lang):
if not msg_fmt: if not msg_fmt:
return "" return ""
return msg_fmt.format(**json.loads(event_param)) return msg_fmt.format(**event_param)
def load_phase_line(mtr_name, lang):
lst = []
try:
lst = mtr_name.split("_")
except Exception as e:
log.error(f"load_phase_line error, mtr_name:{mtr_name}, {e}")
finally:
if len(lst) > 1:
return PHASE_LINE_LANG.get(lst[-1], {}).get(lang)
return ""
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