Commit d9f4a706 authored by ZZH's avatar ZZH

real time aiao 2023-6-16

parent 6348a28e
...@@ -129,7 +129,7 @@ async def normal_rate_of_location(cid): ...@@ -129,7 +129,7 @@ async def normal_rate_of_location(cid):
"""获取温度和漏电流达标率""" """获取温度和漏电流达标率"""
d_stats = {"residual_current": {"total": 0, "normal": 0}, d_stats = {"residual_current": {"total": 0, "normal": 0},
"temperature": {"total": 0, "normal": 0}, } "temperature": {"total": 0, "normal": 0}, }
sql = "select l.lid, l.item, l.ad_type type, s.threshold from " \ sql = "select l.lid, l.ad_type type, s.threshold from " \
"location l left join soe_config_record s on l.lid=s.lid " \ "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 " \ "where l.cid = %s and s.enable=1 and l.ad_type in %s " \
"and s.etype in %s;" "and s.etype in %s;"
...@@ -137,50 +137,51 @@ async def normal_rate_of_location(cid): ...@@ -137,50 +137,51 @@ async def normal_rate_of_location(cid):
locs = await conn.fetchall(sql, (cid, locs = await conn.fetchall(sql, (cid,
("residual_current", "temperature"), ("residual_current", "temperature"),
("overTemp", "overResidualCurrent"))) ("overTemp", "overResidualCurrent")))
loc_infos = {loc["lid"]: loc for loc in locs}
lids = list(loc_infos.keys())
if not lids:
return "100%", "100%"
location_map = {loc["lid"]: loc for loc in locs}
lids = list(location_map.keys())
adio_currents = []
if lids:
prefix = f"real_time:adio:{SETTING.mysql_db}" prefix = f"real_time:adio:{SETTING.mysql_db}"
keys = [f"{prefix}:{lid}" for lid in lids] keys = [f"{prefix}:{lid}" for lid in lids]
adio_currents = await RedisUtils().mget(keys) rt_rlts = await RedisUtils().mget(keys)
log.info(f"adio_currents:{adio_currents}") rt_adios = [json.loads(r) for r in rt_rlts if r]
d_rt_adio = {adio["lid"]: adio for adio in rt_adios}
adio_infos = {}
for index, item_byte in enumerate(adio_currents): now_ts = pendulum.now(tz=CST).int_timestamp
if item_byte: for lid, d_loc in loc_infos.items():
adio_infos[lids[index]] = json.loads(item_byte.decode()) d_stats[d_loc["type"]]["total"] += 1
if lid not in d_rt_adio:
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 = d_adio.get("timestamp", 0)
if (time_now - real_tt) > constants.REAL_EXP_TIME:
# 超过4小时的值不统计在normal里
log.warn(f"adio_current location_id={lid} has expire!")
continue continue
if (isinstance(d_loc["threshold"], float) and if not isinstance(d_loc["threshold"], (float, int)):
d_adio["value"] < d_loc["threshold"]): continue
d_stats[d_loc["type"]]["normal"] += 1
if d_stats["temperature"]["total"] == 0: try:
temp_qr = "100%" d_adio = d_rt_adio[lid]
else: if (now_ts - d_adio["ts"]) > constants.REAL_EXP_TIME:
norm = d_stats["temperature"]["normal"] log.warn(f"adio_current location_id={lid} has expire!")
total = d_stats["temperature"]["total"] continue
temp_qr = str(round((norm / total) * 100, )) + "%"
if d_stats["residual_current"]["total"] == 0: if d_adio["v"] < d_loc["threshold"]:
rc_qr = "100%" d_stats[d_loc["type"]]["normal"] += 1
else: except Exception as e:
norm = d_stats["residual_current"]["normal"] log.exception(f"parse real time adio:{d_adio} exc, e:{e}")
total = d_stats["residual_current"]["total"]
rc_qr = str(round((norm / total) * 100)) + "%" if d_stats["temperature"]["total"] == 0:
return temp_qr, rc_qr temp_qr = "100%"
else:
norm = d_stats["temperature"]["normal"]
total = d_stats["temperature"]["total"]
temp_qr = str(round((norm / total) * 100, )) + "%"
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