Commit 4c7a3d80 authored by lcn's avatar lcn

bug修复

parent c3c7703f
......@@ -20,7 +20,8 @@ from unify_api.modules.electric.procedures.electric_util import \
load_point_ctnum
from datetime import datetime
from unify_api.constants import REAL_EXP_TIME
from unify_api.utils.time_format import CST, YMD_Hms, timestamp2dts
from unify_api.utils.time_format import CST, YMD_Hms, timestamp2dts, \
time_pick_transf
from unify_api.modules.common.procedures.location_temp_rcurrent import \
location_stats_statics
from unify_api.modules.electric.components.electric import (
......@@ -76,12 +77,12 @@ async def elec_current_storeys_service(storeys):
"ua": "", "ia": "", "ub": "", "ib": "", "uc": "", "ic": "",
"pttl": "", "qttl": "", "freq": "", "costtl": "", "lf": "",
"sdu_i": "", "sdu_u": "", }
if storey_name in elec_data:
elec_data[storey_name].append(res_dic)
else:
elec_data[storey_name] = [res_dic]
# 转换成list格式, 可以按照storey_name排序
if elec_data:
# 房间排序, 并返回数据转化为list
......@@ -148,7 +149,7 @@ async def qual_current_storeys_service(storeys):
qual_data[storey_name].append(res_dic)
else:
qual_data[storey_name] = [res_dic]
# 转换成list格式, 可以按照storey_name排序
if qual_data:
# 房间排序, 并返回数据转化为list
......@@ -229,7 +230,7 @@ async def elec_card_level_service(point_list):
"costtl_threshold": COSTTL_THRESHOLD,
"lf_threshold": LF_THRESHOLD,
}
ret_data[m_type].append(res_dic)
return EclResp(inline=ret_data["inline"],
transformer=ret_data["transformer"],
......@@ -263,15 +264,15 @@ async def qual_current_level_service(point_list):
# 初始化返回dic
rt_ele = get_sdu_i_and_u(rt_ele, ctnum)
time_str = timestamp2dts(rt_ele["ts"], YMD_Hms)
fdia = round_2(rt_ele.get("fdia"))
fdib = round_2(rt_ele.get("fdib"))
fdic = round_2(rt_ele.get("fdic"))
thdia = round_4(rt_ele.get("thdia"))
thdib = round_4(rt_ele.get("thdib"))
thdic = round_4(rt_ele.get("thdic"))
res_dic = {
"name": m_name,
"point_id": point_id,
......@@ -279,7 +280,7 @@ async def qual_current_level_service(point_list):
"real_time": time_str,
# 电流/电压谐波畸变率
"thdia": thdia, "thdib": thdib, "thdic": thdic,
"thdua": round_4(rt_ele.get("thdua")),
"thdub": round_4(rt_ele.get("thdub")),
"thduc": round_4(rt_ele.get("thduc")),
......@@ -296,7 +297,7 @@ async def qual_current_level_service(point_list):
"uc_dev": round_4(rt_ele.get("uc_dev")),
"uab_dev": round_4(rt_ele.get("uab_dev")),
"ucb_dev": round_4(rt_ele.get("ucb_dev")),
# 电流总谐波有效值 = 基波电流 * 电流总谐波畸变率
"thdia_virtual": round_2(multiplication_two(fdia, thdia)),
"thdib_virtual": round_2(multiplication_two(fdib, thdib)),
......@@ -316,7 +317,7 @@ async def qual_current_level_service(point_list):
"real_time": "",
# 电流/电压谐波畸变率
"thdia": "", "thdib": "", "thdic": "",
"thdua": "", "thdub": "", "thduc": "",
"thduab": "", "thducb": "",
# 基波电流
......@@ -332,7 +333,7 @@ async def qual_current_level_service(point_list):
"thdib_virtual": "",
"thdic_virtual": "",
}
ret_data[m_type].append(res_dic)
return QclResp(inline=ret_data["inline"],
transformer=ret_data["transformer"],
......@@ -346,12 +347,14 @@ async def elec_index_service(cid, point_id, start, end):
ctnum = await load_point_ctnum(point_id)
ctnum = ctnum if ctnum == 2 else 3
now = str(datetime.now())
if start[:10] == now[:10] and end[:10] == now[:10]:
table_name = "point_15min_electric"
redi_table_name = "location_15min_aiao"
else:
intervel, _ = time_pick_transf(start, end)
if intervel == 86400:
table_name = "point_1day_electric"
redi_table_name = "location_1day_aiao"
else:
table_name = "point_15min_electric"
redi_table_name = "location_15min_aiao"
if ctnum == 2:
common_items = ["lf_mean", "lf_min", "lf_max", "pttl_mean", "pttl_min",
"pttl_max", "qttl_mean", "qttl_min", "qttl_max",
......@@ -514,7 +517,7 @@ async def elec_current_service(point_id):
if not p_info or not p_info["mtid"]:
msg = f"没有监测点:{point_id} monitor信息,请联系运维人员!"
raise BusinessException(message=msg)
now_ts = pendulum.now(tz=CST).int_timestamp
d_rt_ele, ts = None, now_ts
try:
......@@ -529,27 +532,27 @@ async def elec_current_service(point_id):
for k in rt_ele_hr.keys():
if k not in rt_ele.keys():
rt_ele[k] = rt_ele_hr[k]
d_rt_ele, ts = rt_ele, rt_ele["ts"]
elif rt_ele:
rt_ele = json.loads(rt_ele)
if now_ts - rt_ele["ts"] <= REAL_EXP_TIME:
d_rt_ele, ts = rt_ele, rt_ele["ts"]
elif rt_ele_hr:
rt_ele_hr = json.loads(rt_ele_hr)
if now_ts - rt_ele_hr["ts"] <= REAL_EXP_TIME:
d_rt_ele, ts = rt_ele_hr, rt_ele_hr["ts"]
except Exception as e:
log.error(f"parse real time electric error, pid:{point_id}")
log.exception(e)
time_str = timestamp2dts(ts, YMD_Hms)
if d_rt_ele is None:
return time_str, None
# 识电U只有一项有数据,返回具体的项
ctnum = d_rt_ele.get("ctnum") or 3
return time_str, get_sdu_i_and_u(d_rt_ele, ctnum)
......@@ -561,27 +564,27 @@ async def batch_load_rt_ele(mtids):
try:
db = SETTING.mysql_db
keys = [f"real_time:electric:{db}:{mtid}" for mtid in mtids]
i_size = 500
lst_rt_ele = []
for i in range(math.ceil(len(mtids) / i_size)):
pipe = await RedisUtils().client.pipeline()
for key in keys[i_size * i:i_size * (i + 1)]:
await pipe.get(key)
lst_rt_ele += await pipe.execute()
for rt_ele in lst_rt_ele:
if rt_ele is None:
continue
rt_ele = json.loads(rt_ele)
if now_ts - rt_ele["ts"] <= REAL_EXP_TIME:
d_rt_ele[rt_ele["mtid"]] = rt_ele
except Exception as e:
log.error(f"batch load real time electric error, mtids:{mtids}")
log.exception(e)
return d_rt_ele
......@@ -592,22 +595,22 @@ async def batch_load_rt_ele_with_hr(mtids):
db = SETTING.mysql_db
keys = [f"real_time:electric:{db}:{mtid}" for mtid in mtids]
key_hrs = [f"real_time:electric_hr:{db}:{mtid}" for mtid in mtids]
i_size = 500
lst_rt_ele, lst_rt_ele_hr = [], []
for i in range(math.ceil(len(mtids) / i_size)):
pipe = await RedisUtils().client.pipeline()
for key in keys[i_size * i:i_size * (i + 1)]:
await pipe.get(key)
lst_rt_ele += await pipe.execute()
pipe = await RedisUtils().client.pipeline()
for key in key_hrs[i_size * i:i_size * (i + 1)]:
await pipe.get(key)
lst_rt_ele_hr += await pipe.execute()
for i, mtid in enumerate(mtids):
rt_ele, rt_ele_hr = lst_rt_ele[i], lst_rt_ele_hr[i]
if rt_ele and rt_ele_hr:
......@@ -615,36 +618,36 @@ async def batch_load_rt_ele_with_hr(mtids):
if rt_ele["mtid"] != mtid or rt_ele_hr["mtid"] != mtid:
log.error(f"batch_load_rt_ele error, mtid:{mtid}")
continue
if now_ts - rt_ele["ts"] <= REAL_EXP_TIME:
if now_ts - rt_ele_hr["ts"] <= REAL_EXP_TIME:
for k in rt_ele_hr.keys():
if k not in rt_ele.keys():
rt_ele[k] = rt_ele_hr[k]
d_rt_ele[mtid] = rt_ele
elif rt_ele:
rt_ele = json.loads(rt_ele)
if rt_ele["mtid"] != mtid:
log.error(f"load_rt_ele error, mtid:{mtid}")
continue
if now_ts - rt_ele["ts"] <= REAL_EXP_TIME:
d_rt_ele[mtid] = rt_ele
elif rt_ele_hr:
rt_ele_hr = json.loads(rt_ele_hr)
if rt_ele_hr["mtid"] != mtid:
log.error(f"load_rt_ele_hr error, mtid:{mtid}")
continue
if now_ts - rt_ele_hr["ts"] <= REAL_EXP_TIME:
d_rt_ele[mtid] = rt_ele_hr
except Exception as e:
log.error(f"batch load real time electric error, mtids:{mtids}")
log.exception(e)
return d_rt_ele
......
......@@ -53,15 +53,15 @@ async def post_elec_history(req, body: PageRequest) -> ElecHistoryResponse:
except:
log.error("param error, ranges is NULL")
raise ParamException(message="param error, ranges is NULL")
try:
intervel, slots = time_format.time_pick_transf(date_start, date_end)
except:
log.error("param error, date format error")
raise ParamException(message="param error, date format error")
point_id = filed_value_from_list(body.filter.equals, "point_id")
if point_id <= 0 or not point_id:
log.warning("param exception, equals is NULL, no point_id")
raise ParamException(
......@@ -162,12 +162,12 @@ async def post_elec_current(req, body: PageRequest) -> ElecCurrentResponse:
for k, v in res.items():
if isinstance(v, (str, int)):
continue
res[k] = add_random_change(v)
except Exception as e:
log.error(f"post_elec_current service error:{str(e)}")
raise BusinessException(message=f"{str(e)}")
return ElecCurrentResponse(real_time=time_str,
**{k: v for k, v in res.items()
if k in [field.name for field in
......@@ -181,7 +181,7 @@ async def post_elec_index(req, body: PageRequest) -> ElecIndexResponse:
cid = req.json.get("cid")
# 1. 获取point_id
point_id = filed_value_from_list(body.filter.equals, "point_id")
if not point_id or point_id <= 0:
msg = "param exception, equals is NULL, no point_id"
log.warning(msg)
......@@ -208,20 +208,20 @@ async def post_qual_history(req, body: PageRequest) -> QualHistoryResponse:
except:
log.error("param error, ranges is NULL")
raise ParamException(message="param error, ranges is NULL")
try:
intervel, slots = time_format.time_pick_transf(date_start, date_end)
except:
log.error("param error, date format error")
raise ParamException(message="param error, date format error")
point_id = filed_value_from_list(body.filter.equals, "point_id")
if not point_id or point_id <= 0:
msg = "param exception, equals is NULL, no point_id"
log.warning(msg)
raise ParamException(message=msg)
return await qual_history_service(date_start, date_end, intervel, slots,
point_id)
......@@ -280,7 +280,7 @@ async def qual_history_service(start, end, intervel, slots, pid):
else:
for stats_item in stats_items:
elec_data[stats_item].append("")
voltage_dev = VoltageDev(
**{
k.rsplit("_", 1)[0]: v
......@@ -343,7 +343,7 @@ async def qual_history_service(start, end, intervel, slots, pid):
):
sdu_i = "ic"
sdu_u = "uc"
return QualHistoryResponse(
ctnum=ctnum,
voltage_dev=voltage_dev,
......@@ -367,40 +367,40 @@ async def post_qual_current(req, body: PageRequest) -> QualCurrentResponse:
except Exception as e:
log.warning(f"param exception, equals is NULL, no pid, e:{e}")
raise ParamException(message="param exception, equals is NULL, no pid")
if point_id <= 0 or not point_id or cid <= 0:
log.warning("param exception, equals is NULL, no point_id")
raise ParamException(message="param exception, equals is NULL, no pid")
try:
time_str, d_rt_ele = await elec_current_service(point_id)
if d_rt_ele is None:
d_rt_ele = {}
# 加些随机变化(防止数据一直不变化)
for k, v in d_rt_ele.items():
if isinstance(v, (str, int)):
continue
d_rt_ele[k] = add_random_change(v)
except Exception as e:
log.error(f"post_qual_current service error:{str(e)}")
raise BusinessException(message=f"{str(e)}")
d_vol_harm, d_cur_harm = {}, {}
for k in [field.name for field in fields(VoltageHarmonicRate)]:
d_vol_harm[k] = d_rt_ele.get(k) or ""
voltage_harmonic = VoltageHarmonicRate(**d_vol_harm)
for k in [field.name for field in fields(CurrentHarmonicRate)]:
d_cur_harm[k] = d_rt_ele.get(k) or ""
current_harmonic = CurrentHarmonicRate(**d_cur_harm)
health_index = await health_score.load_health_index(cid, point_id)
ret_items = ["ua_dev", "ub_dev", "uc_dev", "uab_dev", "ucb_dev",
"freq_dev", "ubl", "ibl", "sdu_i", "sdu_u"]
return QualCurrentResponse(
ctnum=d_rt_ele["ctnum"],
ctnum=d_rt_ele.get("ctnum") or 3,
real_time=time_str,
health_index=health_index,
voltage_harmonic=voltage_harmonic,
......
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