Commit ad6721cd authored by lcn's avatar lcn

bug修复

parent a43a1bf1
......@@ -5,11 +5,14 @@ import aioredis
from pot_libs.settings import SETTING
from unify_api import constants
from unify_api.constants import POINT_LEVEL_MAP
from unify_api.modules.adio.components.adio import AdioIndex
from unify_api.modules.adio.components.adio_card_cps import AcResp
from unify_api.modules.adio.dao.adio_card_dao import \
monitor_location_join_by_locations, alarm_setting_by_locations
from unify_api.modules.adio.dao.adio_dao import get_location_15min_dao
from unify_api.modules.common.procedures.list_point_pds import \
monitor_map_point_location
from unify_api.utils.common_utils import round_2
ADIO_CURRENT = "adio_current"
......@@ -57,7 +60,7 @@ async def post_adio_card_service(location_list, cid):
real_tt = adio_info.get("timestamp", 0)
if (time_now - real_tt) <= constants.REAL_EXP_TIME:
adio_value = round(adio_info.get("value", 0), 2)
threshold = alarm_dic[location_id]["threshold"]
if m_name in ret_data[m_type]:
if location_type == "temperature":
......@@ -106,3 +109,40 @@ async def post_adio_card_service(location_list, cid):
power_dist=ret_data["power_dist"],
device=ret_data["device"],
)
async def get_location_15min_service(location_info, lid, start, end):
value_max, value_min, value_avg = [], [], []
value_max_time, value_min_time = [], []
datas = await get_location_15min_dao(lid, start, end)
for data in datas:
value_max.append(data.get("value_max"))
value_min.append(data.get("value_min"))
value_avg.append(data.get("value_avg"))
value_max_time.append(data.get("value_max_time"))
value_min_time.append(data.get("value_min_time"))
if value_max:
value_max_max = max([m for m in value_max])
value_max_max_index = value_max.index(value_max_max)
value_max_time_data = value_max_time[value_max_max_index]
else:
value_max_max, value_max_time_data = "", ""
if value_min:
value_min_min = min([m for m in value_min])
value_min_min_index = value_min.index(value_min_min)
value_min_time_data = value_min_time[value_min_min_index]
else:
value_min_min, value_min_time_data = "", ""
value_avg_list = [m for m in value_avg]
value_avg_data = sum(value_avg_list) / len(value_avg_list) \
if value_avg_list else 0
adio_index = AdioIndex(
type=location_info[lid]["type"],
item=location_info[lid]["item"],
max=round_2(value_max_max),
max_time=str(value_max_time_data) if value_max_time_data else "",
min=round_2(value_min_min),
min_time=str(value_min_time_data) if value_min_time_data else "",
avg=round_2(value_avg_data),
)
return adio_index
......@@ -2,6 +2,7 @@
#
# Author:jing
# Date: 2020/7/9
import asyncio
import json
import time
......@@ -12,6 +13,7 @@ from pot_libs.sanic_api import summary, description, examples
from pot_libs.mysql_util.mysql_util import MysqlUtil
from pot_libs.settings import SETTING
from pot_libs.logger import log
from unify_api.modules.adio.service.adio_card import get_location_15min_service
from unify_api.utils import time_format
from unify_api.utils.time_format import CST, YMD_Hms, timestamp2dts
from unify_api import constants
......@@ -45,7 +47,7 @@ async def post_adio_history(req, body: PageRequest) -> AdioHistoryResponse:
log.error("para error, ranges is NULL")
return AdioHistoryResponse(temperature=[], residual_current=[],
time_slots=[])
try:
# 形如 interval = 900 slots=['00:00', '00:15', '00:30'
intervel, slots = time_format.time_pick_transf(date_start, date_end)
......@@ -53,19 +55,19 @@ async def post_adio_history(req, body: PageRequest) -> AdioHistoryResponse:
log.error("para error, date format error")
return AdioHistoryResponse(temperature=[], residual_current=[],
time_slots=[])
try:
location_group = body.filter.in_groups[0].group
except:
log.warning("para exception, in_groups is NULL, no location_id")
return AdioHistoryResponse(temperature=[], residual_current=[],
time_slots=slots)
if not location_group:
log.warning("para exception, in_groups is NULL, no location_id")
return AdioHistoryResponse(temperature=[], residual_current=[],
time_slots=slots)
# 3.获取温度曲线和漏电流曲线数据
# 动态漏电流阈值
sql = "select threshold from soe_config_record where lid in %s " \
......@@ -127,19 +129,19 @@ async def post_adio_current(req, body: PageRequest) -> AdioCurrentResponse:
except:
log.warning("para exception, in_groups is NULL, no location_id")
return AdioCurrentResponse(temperature=[], residual_current=[])
# location_ids
location_group = in_group.group
if not location_group:
log.warning("para exception, in_groups is NULL, no location_id")
return AdioCurrentResponse(temperature=[], residual_current=[])
# 读取location表信息
location_info = await get_location_dao(location_group)
if not location_info:
log.warning("location_id error location_info empty")
return AdioCurrentResponse(temperature=[], residual_current=[])
# load real time adio
lids = list(location_info.keys())
prefix = f"real_time:adio:{SETTING.mysql_db}"
......@@ -149,7 +151,7 @@ async def post_adio_current(req, body: PageRequest) -> AdioCurrentResponse:
d_rt_adio = {adio["lid"]: adio for adio in rt_adios}
if not d_rt_adio:
return AdioCurrentResponse(temperature=[], residual_current=[])
temp_lst = []
rc_lst = []
for lid, loc_info in location_info.items():
......@@ -180,13 +182,13 @@ async def post_adio_current_bak(req, body: PageRequest) -> AdioCurrentResponse:
except:
log.warning("para exception, in_groups is NULL, no location_id")
return AdioCurrentResponse(temperature=[], residual_current=[])
# location_ids
location_group = in_group.group
if not location_group:
log.warning("para exception, in_groups is NULL, no location_id")
return AdioCurrentResponse(temperature=[], residual_current=[])
# 读取location表信息
location_info = await get_location_dao(location_group)
temperature = []
......@@ -199,7 +201,7 @@ async def post_adio_current_bak(req, body: PageRequest) -> AdioCurrentResponse:
except Exception:
log.error("redis error")
return AdioCurrentResponse().db_error()
time_now = int(time.time())
if adio_info:
real_tt = adio_info.get("timestamp", 0)
......@@ -211,7 +213,7 @@ async def post_adio_current_bak(req, body: PageRequest) -> AdioCurrentResponse:
else:
adio_value = ""
time_str = time_format.get_datetime_str(time_now)
if item_info.get("type") == "residual_current":
adio_current = AdioCurrent(
type="residual_current", item="漏电流", real_time=time_str,
......@@ -226,7 +228,7 @@ async def post_adio_current_bak(req, body: PageRequest) -> AdioCurrentResponse:
value=adio_value,
)
temperature.append(adio_current)
return AdioCurrentResponse(temperature=temperature,
residual_current=residual_current)
......@@ -240,14 +242,14 @@ async def post_adio_index(req, body: PageRequest) -> AdioIndexResponse:
except:
log.warning("para exception, in_groups is NULL, no location_id")
return AdioIndexResponse(adio_indexes=[])
if not location_group:
log.warning("para exception, in_groups is NULL, no location_id")
return AdioIndexResponse(adio_indexes=[])
# # load location表信息
location_info = await get_location_dao(location_group)
# 获取时间
try:
start = body.filter.ranges[0].start
......@@ -255,40 +257,8 @@ async def post_adio_index(req, body: PageRequest) -> AdioIndexResponse:
except:
log.error("para error, ranges is NULL")
return AdioIndexResponse(adio_indexes=[])
adio_indexes = []
for lid in location_group:
value_max, value_min, value_avg = [], [], []
value_max_time, value_min_time = [], []
datas = await get_location_15min_dao(lid, start, end)
for data in datas:
value_max.append(data.get("value_max"))
value_min.append(data.get("value_min"))
value_avg.append(data.get("value_avg"))
value_max_time.append(data.get("value_max_time"))
value_min_time.append(data.get("value_min_time"))
if value_max:
value_max_max = max([m for m in value_max])
value_max_max_index = value_max.index(value_max_max)
value_max_time_data = value_max_time[value_max_max_index]
else:
value_max_max, value_max_time_data = "", ""
if value_min:
value_min_min = min([m for m in value_min])
value_min_min_index = value_min.index(value_min_min)
value_min_time_data = value_min_time[value_min_min_index]
else:
value_min_min, value_min_time_data = "", ""
value_avg_list = [m for m in value_avg]
value_avg_data = sum(value_avg_list) / len(value_avg_list) \
if value_avg_list else 0
adio_index = AdioIndex(
type=location_info[lid]["type"],
item=location_info[lid]["item"],
max=round_2(value_max_max),
max_time=str(value_max_time_data) if value_max_time_data else "",
min=round_2(value_min_min),
min_time=str(value_min_time_data) if value_min_time_data else "",
avg=round_2(value_avg_data),
)
adio_indexes.append(adio_index)
tasks = [get_location_15min_service(location_info, lid, start, end) for lid
in location_group]
adio_index_result = await asyncio.gather(*tasks)
adio_indexes = list(adio_index_result)
return AdioIndexResponse(adio_indexes=adio_indexes)
......@@ -47,15 +47,14 @@ async def equip_run_list(company_id, point_ids, start_time, end_time,
'''
async with MysqlUtil() as conn:
raw_sql = "SELECT {} from scope_equip_run_record s " \
"left join (select pid,max(id) max_id " \
"left join (select pid,max(start_time) start_time " \
"from scope_equip_run_record group by pid) sp " \
"on s.pid = sp.pid " \
"left join point p on s.pid=p.pid " \
"left join monitor_reuse r on p.mtid = r.mtid " \
"where " \
"(p.cid=%s or r.cid = %s) and s.start_time " \
"BETWEEN %s and %s and " \
"(s.end_time > 0 or (s.end_time = 0 and s.id = sp.max_id)) "
"where (p.cid=%s or r.cid = %s) and s.start_time " \
"BETWEEN %s and %s and (s.end_time > 0 or " \
"(s.end_time = 0 and s.start_time = sp.start_time)) "
if point_ids:
raw_sql += " and s.pid in %s"
args = (
......@@ -115,21 +114,21 @@ async def equip_run_statistics(company_id, point_ids, start_time, end_time):
'''
获取运行统计数据
'''
dura_time = "case when end_time > 0 then end_time-start_time else 0 end"
dura_time = "case when end_time > 0 then end_time-s.start_time else 0 end"
async with MysqlUtil() as conn:
count_sql = f"SELECT count(*) as total_count," \
f"avg({dura_time}) as avg_time," \
f"sum({dura_time}) as all_time," \
f"max({dura_time}) as max_time " \
"from scope_equip_run_record s " \
"left join (select pid,max(id) max_id from " \
"left join (select pid,max(start_time) start_time from " \
"scope_equip_run_record group by pid) sp " \
"on s.pid = sp.pid " \
"left join point p on s.pid=p.pid " \
"left join monitor_reuse r on p.mtid = r.mtid " \
"where (p.cid=%s or r.cid = %s) " \
"and s.start_time BETWEEN %s and %s and (s.end_time > 0 " \
"or (s.end_time = 0 and s.id = sp.max_id)) "
"or (s.end_time = 0 and s.start_time = sp.start_time)) "
if point_ids:
count_sql += " and s.pid in %s"
args = (
......
......@@ -63,7 +63,7 @@ async def points_monitor_by_cid(cids):
async def get_point_monitor_dao(pid):
sql = "SELECT m.meter_no,m.mtid,m.sid," \
"p.ctr,p.ptr,p.ctnum,p.vc,p.tc,p.imax" \
"p.ctr,p.ptr,p.ctnum,p.vc,p.tc,p.imax " \
"FROM `point` p INNER JOIN monitor m on m.mtid=p.mtid " \
"where p.pid=%s and m.demolished = 0"
async with MysqlUtil() as conn:
......@@ -245,10 +245,11 @@ async def detection_point_by_cid(cids):
async def monitor_page_by_cid(cids, page_num, page_size):
"""根据cids查询项目名称/总数以及分页"""
sql = "SELECT a.cid, b.shortname name, count(a.cid) count_num " \
"FROM monitor a " \
sql = "SELECT a.cid, b.create_time,b.shortname name, count(a.cid) " \
"count_num FROM monitor a " \
"left join company b on a.cid=b.cid " \
"where a.cid in %s and a.demolished = 0 GROUP BY a.cid limit %s, %s;"
"where a.cid in %s and a.demolished = 0 " \
"GROUP BY a.cid order by b.create_time desc limit %s, %s;"
async with MysqlUtil() as conn:
info_list = await conn.fetchall(sql=sql, args=(
tuple(cids), (page_num - 1) * page_size, page_size))
......
......@@ -157,6 +157,7 @@ async def post_elec_current(req, body: PageRequest) -> ElecCurrentResponse:
raise ParamException(message="param exception, equals NULL, no pid")
try:
time_str, res = await elec_current_service(point_id)
res = res or {}
# 加些随机变化(防止数据一直不变化)
for k, v in res.items():
if isinstance(v, (str, int)):
......
from datetime import datetime
from pot_libs.utils.time_format import convert_timestamp_to_str_no_sec
from unify_api.modules.common.dao.common_dao import monitor_by_cid, \
company_by_cids, monitor_point_storey_join, company_by_cid, \
detection_point_by_cid, monitor_page_by_cid, start_time_by_cids, \
......@@ -107,16 +108,10 @@ async def hardware_info_management_service(cids):
async def hardware_info_list_manage_service(cids, page_num, page_size):
info_list = await monitor_page_by_cid(cids, page_num, page_size)
page_cids = [info.get("cid") for info in info_list]
start_times = await start_time_by_cids(page_cids)
for start_time in start_times:
create_time_timestamp = start_time["create_time"]
start_strftime = datetime.strftime(
datetime.fromtimestamp(create_time_timestamp), "%Y-%m-%d %H:%M")
start_time["create_time"] = start_strftime
for index, info in enumerate(info_list):
if start_time["cid"] == info["cid"]:
info_list[index].update(start_time)
continue
for key, value in enumerate(info_list):
if value["create_time"]:
info_list[key]["create_time"] = convert_timestamp_to_str_no_sec(
value["create_time"])
tcs_counts = await tcs_runtime_by_cids(page_cids)
if len(tcs_counts) != len(page_cids):
tcs_cids = [tcs.get("cid") for tcs in tcs_counts]
......
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