debug:推荐接口返回redis值
All checks were successful
git commit AiDA python develop 分支构建部署 / scheduled_deploy (push) Has been skipped
All checks were successful
git commit AiDA python develop 分支构建部署 / scheduled_deploy (push) Has been skipped
This commit is contained in:
@@ -178,47 +178,23 @@ async def recommend(
|
||||
@router.get("/redis/user_pref")
|
||||
async def get_all_user_preferences():
|
||||
"""
|
||||
获取所有以 user_pref 为前缀的 Redis key 信息,按 account_id 分组
|
||||
获取所有以 user_pref 为前缀的 Redis key 信息
|
||||
"""
|
||||
try:
|
||||
from app.service.utils.redis_utils import Redis
|
||||
from app.service.recommendation_system.config import REDIS_KEY_USER_PREF_PREFIX
|
||||
import json
|
||||
|
||||
# 扫描所有匹配 user_pref:* 的 key
|
||||
pattern = f"{REDIS_KEY_USER_PREF_PREFIX}:*"
|
||||
keys = Redis.scan_keys(pattern)
|
||||
|
||||
# 按 account_id 分组
|
||||
# 直接返回所有 key 和原始 value
|
||||
result = {}
|
||||
for key in keys:
|
||||
# 解析 key 格式: user_pref:{account_id}:{category}
|
||||
parts = key.split(':')
|
||||
if len(parts) >= 3:
|
||||
account_id = parts[1]
|
||||
category = parts[2]
|
||||
|
||||
# 读取对应的值
|
||||
value = Redis.read(key)
|
||||
if value:
|
||||
try:
|
||||
vector = json.loads(value)
|
||||
if account_id not in result:
|
||||
result[account_id] = {}
|
||||
result[account_id][category] = {
|
||||
'key': key,
|
||||
'preference_vector': vector,
|
||||
'vector_length': len(vector)
|
||||
}
|
||||
except json.JSONDecodeError:
|
||||
# 如果 JSON 解析失败,保存原始值
|
||||
if account_id not in result:
|
||||
result[account_id] = {}
|
||||
result[account_id][category] = {
|
||||
'key': key,
|
||||
'raw_value': value,
|
||||
'error': 'JSON decode failed'
|
||||
}
|
||||
# 读取对应的值
|
||||
value = Redis.read(key)
|
||||
if value:
|
||||
result[key] = value
|
||||
|
||||
return result
|
||||
|
||||
|
||||
Reference in New Issue
Block a user