2024-09-27 06:23:55 +00:00
|
|
|
from state.redis import redis
|
|
|
|
import logging
|
|
|
|
import math
|
|
|
|
|
|
|
|
# Create a logger instance
|
|
|
|
logger = logging.getLogger("state.scan")
|
2024-09-27 08:28:41 +00:00
|
|
|
|
2024-09-27 06:23:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def get_all_pattern(uid):
|
|
|
|
pattern = f"removed:{uid}:*"
|
|
|
|
|
|
|
|
# Create a dictionary to hold the keys and 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"))
|
|
|
|
|
|
|
|
return texts
|
|
|
|
|
|
|
|
|
|
|
|
async def get_average_pattern(pattern):
|
|
|
|
scores = []
|
|
|
|
async for key in redis.scan_iter(pattern):
|
|
|
|
scr = await redis.get(key)
|
|
|
|
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
|