Commit 36127263 authored by ZZH's avatar ZZH

fix temp rc qr 2023-6-16

parent 1cbb81b3
...@@ -127,80 +127,57 @@ async def electric_use_info(cid): ...@@ -127,80 +127,57 @@ async def electric_use_info(cid):
async def normal_rate_of_location(cid): async def normal_rate_of_location(cid):
"""获取温度和漏电流达标率""" """获取温度和漏电流达标率"""
location_map = {} d_stats = {"residual_current": {"total": 0, "normal": 0},
location_sql = "select l.lid, l.item, l.ad_type type, s.threshold from " \ "temperature": {"total": 0, "normal": 0}, }
"location l left join soe_config_record s on l.lid=s.lid " \ sql = "select l.lid, l.item, l.ad_type type, s.threshold from " \
"where l.cid = %s and s.enable=1 and l.ad_type in %s " \ "location l left join soe_config_record s on l.lid=s.lid " \
"and s.etype in %s" "where l.cid = %s and s.enable=1 and l.ad_type in %s " \
"and s.etype in %s;"
async with MysqlUtil() as conn: async with MysqlUtil() as conn:
locations = await conn.fetchall( locs = await conn.fetchall(sql, (cid,
location_sql, args=(cid, ("residual_current", "temperature"), ("residual_current", "temperature"),
("overTemp", "overResidualCurrent"))) ("overTemp", "overResidualCurrent")))
for location in locations:
location_map[location["lid"]] = location location_map = {loc["lid"]: loc for loc in locs}
count_info_map = { lids = list(location_map.keys())
"residual_current": {"total": 0, "normal": 0},
"temperature": {"total": 0, "normal": 0},
}
location_ids = list(location_map.keys())
adio_currents = [] adio_currents = []
if location_ids: if lids:
adio_currents = await RedisUtils().hmget("adio_current", adio_currents = await RedisUtils().hmget("adio_current", *lids)
*location_ids)
adio_info_map = {} adio_infos = {}
for index, item_byte in enumerate(adio_currents): for index, item_byte in enumerate(adio_currents):
if item_byte: if item_byte:
item = json.loads(item_byte.decode()) adio_infos[lids[index]] = json.loads(item_byte.decode())
adio_info_map[location_ids[index]] = item
for location_id, location_info in location_map.items(): for lid, d_loc in location_map.items():
audio_info = adio_info_map.get(location_id) d_adio = adio_infos.get(lid)
count_info_map[location_info["type"]]["total"] += 1 d_stats[d_loc["type"]]["total"] += 1
if audio_info and audio_info.get("value"): if d_adio and d_adio.get("value"):
time_now = int(time.time()) time_now = int(time.time())
real_tt = audio_info.get("timestamp", 0) real_tt = d_adio.get("timestamp", 0)
if (time_now - real_tt) > constants.REAL_EXP_TIME: if (time_now - real_tt) > constants.REAL_EXP_TIME:
# 超过4小时的值不统计在normal里 # 超过4小时的值不统计在normal里
log.warn(f"adio_current location_id={location_id} has expire!") log.warn(f"adio_current location_id={lid} has expire!")
continue continue
if (
isinstance(location_info["threshold"], float)
and audio_info["value"] < location_info["threshold"]
):
count_info_map[location_info["type"]]["normal"] += 1
if count_info_map["temperature"]["total"] == 0:
temperature_qr = "100%"
else:
temperature_qr = (
str(
round(
(
count_info_map["temperature"]["normal"]
/ count_info_map["temperature"]["total"]
)
* 100,
)
)
+ "%"
)
if count_info_map["residual_current"]["total"] == 0: if (isinstance(d_loc["threshold"], float) and
residual_current_qr = "100%" d_adio["value"] < d_loc["threshold"]):
d_stats[d_loc["type"]]["normal"] += 1
if d_stats["temperature"]["total"] == 0:
temp_qr = "100%"
else: else:
residual_current_qr = ( norm = d_stats["temperature"]["normal"]
str( total = d_stats["temperature"]["total"]
round( temp_qr = str(round((norm / total) * 100, )) + "%"
(
count_info_map["residual_current"]["normal"]
/ count_info_map["residual_current"]["total"]
)
* 100
)
)
+ "%"
)
return temperature_qr, residual_current_qr if d_stats["residual_current"]["total"] == 0:
rc_qr = "100%"
else:
norm = d_stats["residual_current"]["normal"]
total = d_stats["residual_current"]["total"]
rc_qr = str(round((norm / total) * 100)) + "%"
return temp_qr, rc_qr
async def real_time_load(cid, end_dt=None): async def real_time_load(cid, end_dt=None):
......
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