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