29 lines
1.2 KiB
Python
29 lines
1.2 KiB
Python
import tqdm
|
|
import sys
|
|
|
|
NICKNAME_LOG_INDEX = "ds-logs-live-log_return_to_lobby"
|
|
REQUEST_TIMEOUT_SECONDS = 180
|
|
|
|
def fetch_nicknames(client, uids):
|
|
if not uids: return {}
|
|
query = {
|
|
"size": 0, "query": {"terms": {"uid.keyword": list(uids)}},
|
|
"aggs": {"users": {"terms": {"field": "uid.keyword", "size": len(uids)},
|
|
"aggs": {"latest_nickname": {"top_hits": {
|
|
"sort": [{"@timestamp": {"order": "desc"}}], "_source": {"includes": ["body.nickname"]}, "size": 1
|
|
}}}
|
|
}}
|
|
}
|
|
nickname_map = {}
|
|
try:
|
|
response = client.search(index=NICKNAME_LOG_INDEX, body=query, request_timeout=REQUEST_TIMEOUT_SECONDS)
|
|
buckets = response.get("aggregations", {}).get("users", {}).get("buckets", [])
|
|
for bucket in buckets:
|
|
uid = bucket.get("key")
|
|
latest_hit = bucket.get("latest_nickname", {}).get("hits", {}).get("hits", [{}])[0]
|
|
nickname = latest_hit.get("_source", {}).get("body", {}).get("nickname", "N/A")
|
|
if uid: nickname_map[uid] = nickname
|
|
return nickname_map
|
|
except Exception as e:
|
|
tqdm.write(f" - ❌ 닉네임 조회 중 오류: {e}", file=sys.stderr)
|
|
return {} |