Commit 5835292c authored by wang.wenrong's avatar wang.wenrong

精细监测

parent 7cbc839a
...@@ -57,7 +57,7 @@ class FineMonitorChartResp(Model, DbErr, ParamErr): ...@@ -57,7 +57,7 @@ class FineMonitorChartResp(Model, DbErr, ParamErr):
i: list = List("电流").items(Chart) i: list = List("电流").items(Chart)
v: list = List("电压").items(Chart) v: list = List("电压").items(Chart)
ctnum: int = Int("接线方式:包含两表法、三表法").eg(3) ctnum: int = Int("接线方式:包含两表法、三表法").eg(3)
@classmethod @classmethod
def example(cls): def example(cls):
return { return {
...@@ -95,3 +95,39 @@ class FineMonitorInfoResp(Model, DbErr, ParamErr): ...@@ -95,3 +95,39 @@ class FineMonitorInfoResp(Model, DbErr, ParamErr):
精确监测-指标统计返回 精确监测-指标统计返回
''' '''
info_list: list = List("指标统计列表").items(Statistics) info_list: list = List("指标统计列表").items(Statistics)
@dataclass
class ElectricIndexParam(Model):
item: str = Str("指标").eg("有功功率P(kW)")
max: float = Float("最大值").eg(3081.60)
max_time: str = Str("最大时间").eg("2022-12-24 23:59:59")
min: float = Float("最小值").eg(324)
min_time: str = Str("最小时间").eg("2022-12-24 12:12:12")
avg: float = Float("平均值").eg(2311.12)
@dataclass
class ElectricIndexListReq(Model):
cid: Cid
mtid: int = Int("单个mtid").eg(1)
start_time: str = Str("开始时间").eg("2022-12-24 00:00:00")
end_time: str = Str("结束时间").eg("2022-12-24 23:59:59")
@dataclass
class ElectricIndexParam(Model):
type: str = Str("类型").eg("temperature")
item: str = Str("指标").eg("有功功率P(kW)")
max: float = Float("最大值").eg(3081.60)
max_time: str = Str("最大时间").eg("2022-12-24 23:59:59")
min: float = Float("最小值").eg(324)
min_time: str = Str("最小时间").eg("2022-12-24 12:12:12")
avg: float = Float("平均值").eg(2311.12)
@dataclass
class ElectricIndexListResp(Model):
ctnum: int = Int("接线方式 2-两表法 3-三表法").eg(1)
general_param: list = List("常规参数").items(ElectricIndexParam)
electric_quality: list = List("电能质量").items(ElectricIndexParam)
safe_param: list = List("安全参数").items(ElectricIndexParam)
...@@ -174,3 +174,60 @@ async def get_point_1day_chart_dao(mtid, stats_items, date_start, date_end): ...@@ -174,3 +174,60 @@ async def get_point_1day_chart_dao(mtid, stats_items, date_start, date_end):
async with MysqlUtil() as conn: async with MysqlUtil() as conn:
data = await conn.fetchall(sql, ) data = await conn.fetchall(sql, )
return data return data
async def get_mtid_by_pid_dao(pid):
sql = f"""
SELECT
mtid
FROM
point
WHERE
pid = {pid}
"""
async with MysqlUtil() as conn:
data = await conn.fetchone(sql, )
return data
async def get_point_monitor_dao(id_value, field="m.mtid"):
sql = f"SELECT p.pid,m.meter_no,m.sid,p.ctr,p.ptr,p.ctnum,p.vc,p.tc," \
f"p.imax FROM `point` p INNER JOIN monitor m on m.mtid=p.mtid " \
f"where m.demolished = 0 and {field}=%s;"
async with MysqlUtil() as conn:
data = await conn.fetchone(sql, args=(id_value,))
return data
async def electric_index_list_dao(table_name, point_id, start, end):
"""
获取用电数据
:param table_name:
:param point_id:
:param start:
:param end:
:return:
"""
sql = f"SELECT * FROM {table_name} where pid=%s and create_time " \
f"BETWEEN %s and %s ORDER BY create_time desc"
async with MysqlUtil() as conn:
result = await conn.fetchall(sql, args=(point_id, start, end))
return result or []
async def electric_index_location_dao(table_name, mtid, start, end):
"""
获取安全数据
:param table_name:
:param mtid:
:param start:
:param end:
:return:
"""
location_sql = "SELECT mtid,lid,item,ad_type FROM location WHERE mtid=%s"
datas_sql = f"SELECT * from {table_name} where mtid = %s and create_time" \
f" BETWEEN '{start}' and '{end}' order by create_time desc"
async with MysqlUtil() as conn:
location_datas = await conn.fetchall(location_sql, args=(mtid,))
result = await conn.fetchall(datas_sql, args=(mtid,))
return location_datas or [], result or []
...@@ -2,6 +2,7 @@ from operator import itemgetter ...@@ -2,6 +2,7 @@ from operator import itemgetter
from itertools import groupby from itertools import groupby
from pot_libs.common.components.query import Range, Equal, Filter, InGroup, \ from pot_libs.common.components.query import Range, Equal, Filter, InGroup, \
Sort Sort
from pot_libs.common.dao.common_dao import get_fields_by_mtid
from pot_libs.es_util import es_helper from pot_libs.es_util import es_helper
from pot_libs.es_util.es_query import EsQuery from pot_libs.es_util.es_query import EsQuery
from pot_libs.logger import log from pot_libs.logger import log
...@@ -9,21 +10,25 @@ from pot_libs.common.components.query import PageRequest ...@@ -9,21 +10,25 @@ from pot_libs.common.components.query import PageRequest
from pot_libs.utils.pendulum_wrapper import my_pendulum from pot_libs.utils.pendulum_wrapper import my_pendulum
from unify_api.modules.anshiu.dao.fine_monitor_dao import get_aiao_1min_dao, \ from unify_api.modules.anshiu.dao.fine_monitor_dao import get_aiao_1min_dao, \
get_aiao_1day_dao, get_aiao_15min_dao, get_point_15min_chart_dao, \ get_aiao_1day_dao, get_aiao_15min_dao, get_point_15min_chart_dao, \
get_point_1day_chart_dao, get_point_1min_chart_dao get_point_1day_chart_dao, get_point_1min_chart_dao, get_point_monitor_dao, \
electric_index_list_dao, electric_index_location_dao
from unify_api.utils import time_format from unify_api.utils import time_format
from unify_api.modules.electric.procedures.electric_util import ( from unify_api.modules.electric.procedures.electric_util import (
get_wiring_type get_wiring_type
) )
from unify_api.modules.anshiu.components.fine_monitor_cps import ( from unify_api.modules.anshiu.components.fine_monitor_cps import (
Statistics, Chart Statistics, Chart, ElectricIndexParam
) )
from unify_api.modules.anshiu.procedures.fine_monitor_pds import ( from unify_api.modules.anshiu.procedures.fine_monitor_pds import (
get_es_point_1min_data, get_es_point_15min_data, get_es_point_15min_data,
get_es_aiao_15min_data, get_threshold_by_location, get_aiao_1min_pds, get_es_aiao_15min_data, get_threshold_by_location, get_aiao_1min_pds,
get_aiao_data_pds, get_point_1min_chart_pds, get_point_data_chart_pds get_aiao_data_pds, get_point_1min_chart_pds, get_point_data_chart_pds,
GENERAL_PARAM_FIELD_2, ELECTRIC_QUALITY_FIELD_2,
GENERAL_PARAM_FIELD_3, ELECTRIC_QUALITY_FIELD_3, cal_electic_value,
cal_aiao_value, cal_pt_value
) )
from unify_api.utils.time_format import convert_timestamp_to_str from unify_api.utils.time_format import convert_timestamp_to_str, get_time_diff
async def get_adio_chart_data(location_group, location_info, date_start, async def get_adio_chart_data(location_group, location_info, date_start,
...@@ -89,12 +94,14 @@ async def get_point_chart_data(point_id, date_start, date_end, intervel, ...@@ -89,12 +94,14 @@ async def get_point_chart_data(point_id, date_start, date_end, intervel,
] ]
if 15 * 60 >= intervel > 60: if 15 * 60 >= intervel > 60:
elec_data = await get_point_15min_chart_dao(mtid, stats_items, date_start, elec_data = await get_point_15min_chart_dao(mtid, stats_items,
date_start,
date_end) date_end)
power, i, u = await get_point_data_chart_pds(ctnum, slots, elec_data) power, i, u = await get_point_data_chart_pds(ctnum, slots, elec_data)
elif intervel == 86400: elif intervel == 86400:
elec_data = await get_point_1day_chart_dao(mtid, stats_items, date_start, elec_data = await get_point_1day_chart_dao(mtid, stats_items,
date_start,
date_end) date_end)
power, i, u = await get_point_data_chart_pds(ctnum, slots, elec_data) power, i, u = await get_point_data_chart_pds(ctnum, slots, elec_data)
else: else:
...@@ -299,3 +306,164 @@ async def get_point_info_data(point_id, start_time, ...@@ -299,3 +306,164 @@ async def get_point_info_data(point_id, start_time,
common_indexes.append(elec_index) common_indexes.append(elec_index)
return common_indexes return common_indexes
async def electric_index_list_service(mtid, start_time, end_time,
param_types=None):
"""
用电指标数据列表
:param mtid:
:param start_time:
:param end_time:
:param param_types:
:return:
"""
general_param, electric_quality, safe_param = [], [], []
# 1,获取信息
monitor = await get_fields_by_mtid(mtid)
if not monitor:
return -1, general_param, electric_quality, safe_param
time_diff = get_time_diff(start_time, end_time)
if time_diff <= 24 * 3600:
table_name = "point_15min_electric"
aiao_table_name = "location_15min_aiao"
else:
table_name = "point_1day_electric"
aiao_table_name = "location_1day_aiao"
# 字段是否需要组装mtid(前端下载要求的)
is_merge_mtid = mtid if param_types else None
if monitor.get("m_type") == 126:
pt_info = await get_fields_by_mtid(mtid, "pt_info", "pt_id")
if not pt_info:
return -1, general_param, electric_quality, safe_param
pt_id = pt_info.get("pt_id")
# 电容
ctnum, pt_datas = 0, []
# 安全参数
if not param_types or 'safe_param' in param_types:
safe_param = cal_pt_value(pt_datas, is_merge_mtid)
if not param_types:
safe_param = electric_index_list_return_data(safe_param)
else:
monitor_info = await get_point_monitor_dao(mtid)
if not monitor_info:
return -1, general_param, electric_quality, safe_param
ctnum = monitor_info.get("ctnum") or 3
datas, locations_datas, aiao_datas = [], [], []
if (not param_types or 'general_param' in param_types or
'electric_quality' in param_types):
datas = await electric_index_list_dao(table_name,
monitor_info.get("pid"),
start_time,
end_time)
if not param_types or 'safe_param' in param_types:
locations_datas, aiao_datas = await electric_index_location_dao(
aiao_table_name, mtid, start_time, end_time)
# 2,封装信息
if ctnum == 2:
general_param_field = GENERAL_PARAM_FIELD_2
electric_quality_field = ELECTRIC_QUALITY_FIELD_2
else:
general_param_field = GENERAL_PARAM_FIELD_3
electric_quality_field = ELECTRIC_QUALITY_FIELD_3
# 常规参数统计
if not param_types or 'general_param' in param_types:
general_param = cal_electic_value(datas, general_param_field,
is_merge_mtid)
if not param_types:
general_param = electric_index_list_return_data(general_param)
# 电能质量统计
if not param_types or 'electric_quality' in param_types:
electric_quality = cal_electic_value(datas, electric_quality_field,
is_merge_mtid)
if not param_types:
electric_quality = electric_index_list_return_data(
electric_quality)
# 安全参数
if not param_types or 'safe_param' in param_types:
safe_param = cal_aiao_value(locations_datas, aiao_datas,
is_merge_mtid)
if not param_types:
safe_param = electric_index_list_return_data(safe_param)
# 3,返回信息
return ctnum, general_param, electric_quality, safe_param
async def electric_index_export_service(cid, mtids, start_time, end_time,
param_types=None):
"""
用电指标数据导出
:param cid:
:param mtids:
:param start_time:
:param end_time:
:param param_types:
:return:
"""
general_param_list = []
electric_quality_list = []
safe_param_list = []
ctnums = []
for mtid in mtids:
ctnum, general_one, electric_one, safe_one = await \
electric_index_list_service(mtid, start_time, end_time,
param_types)
if ctnum == 0:
return 0, [], [], []
general_param_list.extend(general_one)
electric_quality_list.extend(electric_one)
safe_param_list.extend(safe_one)
ctnums.append(ctnum)
if len(set(ctnums)) > 1:
return -1, [], [], []
general_param, electric_quality, safe_param = [], [], []
# 常规参数
if 'general_param' in param_types:
general_param = electric_index_export_return_data(general_param_list)
# 用电质量
if 'electric_quality' in param_types:
electric_quality = electric_index_export_return_data(
electric_quality_list)
# 安全参数
if 'safe_param' in param_types:
safe_param = electric_index_export_return_data(safe_param_list)
# 2,返回信息
return 1, general_param, electric_quality, safe_param
def electric_index_list_return_data(datas):
"""
用电指标列表数据打包
:param datas:
:return:
"""
index_list = []
for data in datas:
electric_index = ElectricIndexParam(**data)
index_list.append(electric_index)
return index_list
def electric_index_export_return_data(datas):
"""
用电指标导出数据打包
:param datas:
:return:
"""
export_dict = dict()
for data in datas:
if data["index"] not in export_dict:
export_dict[data["index"]] = data
else:
export_dict[data["index"]].update(data)
return list(export_dict.values())
...@@ -2,18 +2,19 @@ from pot_libs.sanic_api import summary, examples ...@@ -2,18 +2,19 @@ from pot_libs.sanic_api import summary, examples
from pot_libs.common.components.query import PageRequest from pot_libs.common.components.query import PageRequest
from pot_libs.logger import log from pot_libs.logger import log
from pot_libs.utils.pendulum_wrapper import my_pendulum from pot_libs.utils.pendulum_wrapper import my_pendulum
from unify_api.modules.anshiu.dao.fine_monitor_dao import get_mtid_by_pid_dao
from unify_api.utils.request_util import filed_value_from_list from unify_api.utils.request_util import filed_value_from_list
from unify_api.utils import time_format from unify_api.utils import time_format
from unify_api.modules.anshiu.components.fine_monitor_cps import ( from unify_api.modules.anshiu.components.fine_monitor_cps import (
FineMonitorChartReq, FineMonitorInfoReq, FineMonitorChartResp, FineMonitorChartReq, FineMonitorInfoReq, FineMonitorChartResp,
FineMonitorInfoResp FineMonitorInfoResp, ElectricIndexListResp
) )
from unify_api.modules.anshiu.procedures.fine_monitor_pds import ( from unify_api.modules.anshiu.procedures.fine_monitor_pds import (
get_location_by_ids, get_threshold_by_location, get_mtid_by_location_ids get_location_by_ids, get_threshold_by_location, get_mtid_by_location_ids
) )
from unify_api.modules.anshiu.service.fine_monitor_serv import ( from unify_api.modules.anshiu.service.fine_monitor_serv import (
get_adio_chart_data, get_point_chart_data, get_adio_info_data, get_adio_chart_data, get_point_chart_data, get_adio_info_data,
get_point_info_data get_point_info_data, electric_index_list_service
) )
...@@ -69,19 +70,12 @@ async def post_fine_monitor_chart(request, ...@@ -69,19 +70,12 @@ async def post_fine_monitor_chart(request,
ctnum=ctnum) ctnum=ctnum)
@summary("精细监测-指标统计") @summary("精细监测-指标统计1")
async def post_fine_monitor_info(request, async def post_fine_monitor_info1(request,
body: FineMonitorInfoReq) -> FineMonitorInfoResp: body: FineMonitorInfoReq) -> FineMonitorInfoResp:
try: try:
date_start = body.start date_start = body.start
date_end = body.end date_end = body.end
# 起始时间转化为时间戳
start_timestamp = time_format.get_date_timestamp(date_start)
end_timestamp = time_format.get_date_timestamp(date_end)
# 起始时间转化为es时间格式
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 point_id = body.pid
...@@ -105,11 +99,31 @@ async def post_fine_monitor_info(request, ...@@ -105,11 +99,31 @@ async def post_fine_monitor_info(request,
info_list = [] info_list = []
# 环境相关数据 # 环境相关数据
adio_list = await get_adio_info_data(location_group, adio_list = await get_adio_info_data(location_group,
location_info, start_timestamp, location_info, date_start,
end_timestamp) date_end)
# 用电相关数据 # 用电相关数据
point_list = await get_point_info_data(point_id, es_start_dt, point_list = await get_point_info_data(point_id, date_start,
es_end_dt) date_end)
info_list.extend(adio_list) info_list.extend(adio_list)
info_list.extend(point_list) info_list.extend(point_list)
return FineMonitorInfoResp(info_list=info_list) return FineMonitorInfoResp(info_list=info_list)
@summary("精细监测-指标统计")
async def post_fine_monitor_info(request,
body: FineMonitorInfoReq) -> FineMonitorInfoResp:
mtid = await get_mtid_by_pid_dao(body.pid)
mtid = mtid["mtid"]
# 1,获取参数
start_time = body.start
end_time = body.end
# 2,获取信息
ctnum, general_param, electric_quality, safe_param = await \
electric_index_list_service(mtid, start_time, end_time)
if ctnum == -1:
raise Exception("监测点出错")
# 3,返回信息
info_list = []
info_list.extend(general_param)
info_list.extend(safe_param)
return FineMonitorInfoResp(info_list=info_list)
...@@ -1532,4 +1532,16 @@ def time_pick_transf_new(start, end): ...@@ -1532,4 +1532,16 @@ def time_pick_transf_new(start, end):
dt = start_f.add(months=1 * i).format("YYYY-MM") dt = start_f.add(months=1 * i).format("YYYY-MM")
dt_str = str(dt) dt_str = str(dt)
slots.append(dt_str) slots.append(dt_str)
return intervel, slots return intervel, slots
\ No newline at end of file
def get_time_diff(start, end):
"""
获取时间段内的间隔
:param start:
:param end:
:return:
"""
start_f = my_pendulum.from_format(start, 'YYYY-MM-DD HH:mm:ss')
end_f = my_pendulum.from_format(end, 'YYYY-MM-DD HH:mm:ss')
diff = end_f.int_timestamp - start_f.int_timestamp
return diff
\ No newline at end of file
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