fix(douyu): aggregate audience trend by minute

This commit is contained in:
liuwei
2026-04-09 09:50:21 +08:00
parent 4e2dea86af
commit 5dfc17f811
2 changed files with 86 additions and 22 deletions

View File

@@ -891,18 +891,18 @@ class DouyuPlugin(MessagePluginInterface):
@staticmethod
def _normalize_audience_points(points: List[Dict[str, Any]], limit: int = 720) -> List[Dict[str, Any]]:
normalized: List[Dict[str, Any]] = []
seen = set()
normalized_map: Dict[str, Dict[str, Any]] = {}
for item in points or []:
timestamp = str(item.get("timestamp") or "").strip()
if not timestamp or timestamp in seen:
if not timestamp:
continue
seen.add(timestamp)
normalized.append({
minute_key = timestamp[:16]
normalized_map[minute_key] = {
"timestamp": timestamp,
"vip_count": int(item.get("vip_count", 0) or 0),
"diamond_count": int(item.get("diamond_count", 0) or 0),
})
}
normalized = list(normalized_map.values())
normalized.sort(key=lambda row: row.get("timestamp", ""))
if len(normalized) > limit:
normalized = normalized[-limit:]
@@ -1183,7 +1183,16 @@ class DouyuPlugin(MessagePluginInterface):
def _build_audience_trend(self, sessions: List[Dict[str, Any]]) -> Dict[str, Any]:
points: List[Dict[str, Any]] = []
segment_start_times: List[str] = []
segment_end_times: List[str] = []
for session in sessions:
for segment in session.get("segments", []) or []:
start_time = str(segment.get("start_time") or "").strip()
end_time = str(segment.get("end_time") or "").strip()
if start_time:
segment_start_times.append(start_time)
if end_time:
segment_end_times.append(end_time)
for item in session.get("audience_points", []) or []:
point = {
"timestamp": str(item.get("timestamp") or "").strip(),
@@ -1198,6 +1207,16 @@ class DouyuPlugin(MessagePluginInterface):
vip_values = [int(item.get("vip_count", 0) or 0) for item in points]
diamond_values = [int(item.get("diamond_count", 0) or 0) for item in points]
labels = [str(item.get("timestamp") or "")[-8:-3] for item in points]
session_start = min(segment_start_times) if segment_start_times else ""
session_end = max(segment_end_times) if segment_end_times else str(points[-1].get("timestamp") or "")
first_point_time = str(points[0].get("timestamp") or "")
last_point_time = str(points[-1].get("timestamp") or "")
leading_gap_minutes = 0
if session_start and first_point_time:
start_dt = self._parse_session_time(session_start)
point_dt = self._parse_session_time(first_point_time)
if start_dt and point_dt:
leading_gap_minutes = max(int((point_dt - start_dt).total_seconds() // 60), 0)
return {
"points": points,
"summary": {
@@ -1209,6 +1228,11 @@ class DouyuPlugin(MessagePluginInterface):
"diamond_max": max(diamond_values),
"diamond_latest": diamond_values[-1],
"labels": labels,
"session_start": session_start,
"session_end": session_end,
"first_point_time": first_point_time,
"last_point_time": last_point_time,
"leading_gap_minutes": leading_gap_minutes,
},
}