From 773a01cdb153c9002d87bbeb3a0acc897c790760 Mon Sep 17 00:00:00 2001 From: Untone Date: Fri, 27 Sep 2024 13:34:44 +0300 Subject: [PATCH] average-stab --- handlers/messages_routing.py | 10 ++++------ state/scan.py | 21 +++++++++++++-------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/handlers/messages_routing.py b/handlers/messages_routing.py index cd0b71d..b14f53c 100644 --- a/handlers/messages_routing.py +++ b/handlers/messages_routing.py @@ -48,12 +48,10 @@ async def messages_routing(msg, state): reply_to_msg_id = reply_msg.get("message_id") if not reply_to_msg_id and latest_toxic_message_id: reply_to_msg_id = int(latest_toxic_message_id) - try: - # count average between all of messages - toxic_pattern = f"toxic:{cid}:{uid}:*" - toxic_score = await get_average_pattern(toxic_pattern) - except Exception: - pass + + # count average between all of messages + toxic_pattern = f"toxic:{cid}:{uid}:*" + toxic_score = await get_average_pattern(toxic_pattern) # current mesasage toxicity if reply_to_msg_id: diff --git a/state/scan.py b/state/scan.py index 3271a2c..5fd62c1 100644 --- a/state/scan.py +++ b/state/scan.py @@ -24,11 +24,16 @@ async def get_all_pattern(uid): async def get_average_pattern(pattern): scores = [] - async for key in redis.scan_iter(pattern): - scr = await redis.get(str(key)) - scr = int(scr) - if isinstance(scr, int): - scores.append(scr) - logger.debug(f"found {len(scores)} messages") - toxic_score = math.floor(sum(scores) / len(scores)) if scores else 0 - return toxic_score + toxic_score = 0 + try: + async for key in redis.scan_iter(pattern): + scr = await redis.get(str(key)) + scr = int(scr) + if isinstance(scr, int): + scores.append(scr) + logger.debug(f"found {len(scores)} messages") + toxic_score = math.floor(sum(scores) / len(scores)) if scores else 0 + except Exception: + pass + finally: + return toxic_score