Commit 444ec2cc authored by wang.wenrong's avatar wang.wenrong

Merge branch 'wwr' into 'develop'

安识U-精细监测

See merge request !34
parents 4a5aa641 7cbc839a
......@@ -2,27 +2,175 @@ from pot_libs.settings import SETTING
from unify_api.utils.common_utils import make_tdengine_data_as_list
from unify_api.utils.taos_new import get_td_table_name, get_td_engine_data
from unify_api.utils.exc_util import BusinessException
from pot_libs.mysql_util.mysql_util import MysqlUtil
async def get_aiao_1min_data(monitor_info, start_time, end_time, su_table):
"""
:param monitor_info: pass
:param start_time: 开始时间
:param end_time: 结束时间
:param su_table: 超级表名
:return select_val对应在数据库中对应的值
"""
mtid = monitor_info["mtid"]
async def get_aiao_1min_dao(mtid, start_time, end_time):
# 查1min温度漏电流
sid_data = await get_sid_by_mtid_dao(mtid)
sid = sid_data['sid'].lower()
su_table = "new_adio_stb"
td_mt_table = get_td_table_name(su_table, mtid)
url = f"{SETTING.stb_url}db_adio"
# td的精度过高,采用 >= start and < end的形式查询
sql = f" select temp1, temp2, temp3, temp4 from {su_table} " \
f" where TBNAME = '{td_mt_table}' " \
f" and ts >= '{start_time}' AND ts <'{end_time}' "
sql = f"select last_row( ts, temp1, temp2, temp3, temp4, residual_current) " \
f"from adio_stb where TBNAME = '{td_mt_table}' and " \
f"ts >= '{start_time}' AND ts <'{end_time}' " \
f"Interval(60s) order by ts asc"
is_succ, results = await get_td_engine_data(url, sql)
if not results:
su_table = "old_adio_stb"
td_mt_table = get_td_table_name(su_table, sid)
sql = f"select last_row( ts, temp1, temp2, temp3, temp4, residual_current) " \
f"from adio_stb where TBNAME = '{td_mt_table}' and " \
f"ts >= '{start_time}' AND ts <'{end_time}' " \
f"Interval(60s) order by ts asc"
is_succ, results = await get_td_engine_data(url, sql)
if not is_succ:
raise BusinessException()
td_datas = make_tdengine_data_as_list(results)
if not td_datas:
return ""
return td_datas[0]
return td_datas
async def get_sid_by_mtid_dao(mtid):
# 查15min温度漏电流
sql = f"""
SELECT
sid
FROM
monitor
WHERE
mtid = {mtid}
"""
async with MysqlUtil() as conn:
data = await conn.fetchone(sql, )
return data
async def get_aiao_15min_dao(mtid, start_time, end_time):
# 查15min温度漏电流
sql = f"""
SELECT
DATE_FORMAT(create_time,'%H:%i') create_time,
value_avg,
ad_field
FROM
location_15min_aiao
WHERE
create_time > "{start_time}"
AND create_time < "{end_time}"
AND mtid = {mtid}
"""
async with MysqlUtil() as conn:
data = await conn.fetchall(sql, )
return data
async def get_aiao_1day_dao(mtid, start_time, end_time):
sql = f"""
SELECT
DATE_FORMAT(create_time,'%m-%d') create_time,
value_avg,
ad_field
FROM
location_1day_aiao
WHERE
create_time > "{start_time}"
AND create_time < "{end_time}"
AND mtid = {mtid}
"""
async with MysqlUtil() as conn:
data = await conn.fetchall(sql, )
return data
async def get_point_1min_chart_dao(mtid, ctnum, start_time, end_time):
if ctnum == 2:
stats_items = [
"pttl",
"qttl",
"uab",
"ucb",
"ia",
"ic",
]
else:
stats_items = [
"pttl",
"qttl",
"ua",
"ub",
"uc",
"ia",
"ib",
"ic",
]
# 查1min温度漏电流
sid_data = await get_sid_by_mtid_dao(mtid)
sid = sid_data['sid'].lower()
su_table = "new_electric_stb"
td_mt_table = get_td_table_name(su_table, mtid)
url = f"{SETTING.stb_url}db_electric"
# td的精度过高,采用 >= start and < end的形式查询
stats_items.insert(0, 'ts')
sql = f"select last_row({','.join(stats_items)}) " \
f"from electric_stb where TBNAME = '{td_mt_table}' and " \
f"ts >= '{start_time}' AND ts <'{end_time}' " \
f"Interval(60s) order by ts asc"
is_succ, results = await get_td_engine_data(url, sql)
if not results:
su_table = "old_electric_stb"
td_mt_table = get_td_table_name(su_table, sid)
sql = f"select last_row( {','.join(stats_items)}) " \
f"from electric_stb where TBNAME = '{td_mt_table}' and " \
f"ts >= '{start_time}' AND ts <'{end_time}' " \
f"Interval(60s) order by ts asc"
is_succ, results = await get_td_engine_data(url, sql)
if not is_succ:
raise BusinessException()
td_datas = make_tdengine_data_as_list(results)
if not td_datas:
return ""
return td_datas
async def get_point_15min_chart_dao(mtid, stats_items, date_start, date_end):
# 查15min温度漏电流
sql = f"""
SELECT
DATE_FORMAT(create_time,'%H:%i') create_time,
{','.join(stats_items)}
FROM
point_15min_electric
WHERE
create_time > "{date_start}"
AND create_time < "{date_end}"
AND mtid = {mtid}
"""
async with MysqlUtil() as conn:
data = await conn.fetchall(sql, )
return data
async def get_point_1day_chart_dao(mtid, stats_items, date_start, date_end):
sql = f"""
SELECT
DATE_FORMAT(create_time,'%m-%d') create_time,
{','.join(stats_items)}
FROM
point_1day_electric
WHERE
create_time > "{date_start}"
AND create_time < "{date_end}"
AND mtid = {mtid}
"""
async with MysqlUtil() as conn:
data = await conn.fetchall(sql, )
return data
......@@ -9,7 +9,7 @@ from unify_api.modules.anshiu.components.fine_monitor_cps import (
FineMonitorInfoResp
)
from unify_api.modules.anshiu.procedures.fine_monitor_pds import (
get_location_by_ids, get_threshold_by_location
get_location_by_ids, get_threshold_by_location, get_mtid_by_location_ids
)
from unify_api.modules.anshiu.service.fine_monitor_serv import (
get_adio_chart_data, get_point_chart_data, get_adio_info_data,
......@@ -26,7 +26,7 @@ async def post_fine_monitor_chart(request,
# 起始时间转化为时间戳
start_timestamp = time_format.get_date_timestamp(date_start)
end_timestamp = time_format.get_date_timestamp(date_end)
# 计算间隔与坐标点
intervel, slots = time_format.time_pick_transf(date_start, date_end)
# 获取监测点
......@@ -40,30 +40,33 @@ async def post_fine_monitor_chart(request,
except Exception as e:
log.error('get_fine_monitor_chart_error ' + str(e))
return FineMonitorChartResp.param_error()
# 获取location表的信息
try:
location_info = await get_location_by_ids(location_group)
location_info = await get_mtid_by_location_ids(location_group)
except Exception as e:
log.error('get_fine_monitor_chart_error ' + e)
return FineMonitorChartResp.db_error()
# 获取温度及漏电流数据
temperature_list, residual_currents_list = await get_adio_chart_data(
location_group, location_info, date_start, date_end,
intervel, slots)
# 电力数据 power_list、电流曲线、电压曲线
power_list, i_list, v_list, ctnum = await get_point_chart_data(point_id,
start_timestamp,
end_timestamp, \
intervel, slots)
power_list, i_list, v_list, ctnum = await get_point_chart_data(
point_id,
date_start,
date_end,
intervel,
slots)
# 获取温度与漏电流的曲线数据
# 获取用电数据
return FineMonitorChartResp(time_slots=slots,
temperature=temperature_list,
residual_current=residual_currents_list,
power=power_list, i=i_list, v=v_list,ctnum=ctnum)
power=power_list, i=i_list, v=v_list,
ctnum=ctnum)
@summary("精细监测-指标统计")
......@@ -79,7 +82,7 @@ async def post_fine_monitor_info(request,
es_start_dt = my_pendulum.from_format(date_start,
"YYYY-MM-DD HH:mm:ss")
es_end_dt = my_pendulum.from_format(date_end, "YYYY-MM-DD HH:mm:ss")
# 获取监测点
point_id = body.pid
if not point_id or point_id <= 0:
......@@ -91,14 +94,14 @@ async def post_fine_monitor_info(request,
except Exception as e:
log.error('get_fine_monitor_info ' + str(e))
return FineMonitorInfoResp.param_error()
# 获取location表的信息
try:
location_info = await get_location_by_ids(location_group)
except Exception as e:
log.error('get_fine_monitor_chart_error ' + e)
return FineMonitorChartResp.db_error()
info_list = []
# 环境相关数据
adio_list = await get_adio_info_data(location_group,
......
import argparse
from dataclasses import dataclass
import pendulum
from chinese_calendar import is_workday, is_holiday
from datetime import datetime
import re
......@@ -178,5 +180,7 @@ def make_tdengine_data_as_list(tdengine_data):
meta[0] for meta in tdengine_data["column_meta"]]
result = []
for res in tdengine_data["data"]:
res[0] = pendulum.parse(res[0]).format("HH:mm")
result.append(dict(zip(head, res)))
return result
......@@ -154,6 +154,8 @@ def get_td_table_name(topic, id):
"smoke_soe_stb": "smoke_soe_stb%s", # 烟感报警
"old_adio_stb": "s_%s_a", # 安电--旧 这里是%s是sid
"new_adio_stb": "mt%s_adi", # 安电——新
"old_electric_stb": "s_%s_a",
"new_electric_stb": "mt%s_ele",
}
table_name = topic_map.get(topic)
......
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