less-norm-2

This commit is contained in:
2024-09-27 14:14:51 +03:00
parent 5606c69035
commit 98b842ef18
2 changed files with 53 additions and 43 deletions

View File

@@ -9,15 +9,18 @@ logger = logging.getLogger("state.scan")
async def get_all_pattern(uid):
pattern = f"removed:{uid}:*"
# Create a dictionary to hold the keys and values
# Create a list to hold the values
texts = []
# Use scan_iter to find all keys matching the pattern
async for key in redis.scan_iter(pattern):
# Fetch the value for each key
value = await redis.get(key)
if value:
texts.append(value.decode("utf-8"))
try:
# Fetch the value for each key
value = await redis.get(key)
if value:
texts.append(value.decode("utf-8"))
except Exception as e:
logger.error(f"Error fetching key {key}: {e}")
return texts
@@ -25,15 +28,22 @@ async def get_all_pattern(uid):
async def get_average_pattern(pattern):
scores = []
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")
scr = await redis.get(key)
# Ensure scr is not None before converting to int
if scr is not None:
try:
score = int(scr) # Convert to integer
scores.append(score)
except ValueError:
logger.warning(f"Value for key {key} is not an integer: {scr}")
logger.debug(f"Found {len(scores)} messages")
toxic_score = math.floor(sum(scores) / len(scores)) if scores else 0
except Exception:
pass
except Exception as e:
logger.error(f"Error while calculating average: {e}")
finally:
return toxic_score
return toxic_score