fix: enhance monitoring metrics with latest sample aggregation and derived metrics calculations
Backend CI / Backend (push) Successful in 21m2s

This commit is contained in:
2026-06-29 17:21:38 +08:00
parent b9b1db6e5d
commit 4040d22605
5 changed files with 356 additions and 89 deletions
@@ -173,12 +173,20 @@ func (s *MonitoringCallbackService) loadMonitoringBrandDailyAggregate(ctx contex
if err := tx.QueryRow(ctx, `
SELECT COUNT(*), MAX(completed_at)
FROM question_monitor_runs
WHERE tenant_id = $1
AND brand_id = $2
AND collector_type = $3
AND business_date = $4::date
AND status = 'succeeded'
FROM (
SELECT DISTINCT ON (business_date, question_id, ai_platform_id)
completed_at,
updated_at,
created_at,
id
FROM question_monitor_runs
WHERE tenant_id = $1
AND brand_id = $2
AND collector_type = $3
AND business_date = $4::date
AND status = 'succeeded'
ORDER BY business_date, question_id, ai_platform_id, COALESCE(completed_at, updated_at, created_at) DESC NULLS LAST, id DESC
) latest_runs
`, tenantID, brandID, monitoringCollectorType, dateText).Scan(&agg.ActualSampleCount, &agg.SnapshotUpdatedAt); err != nil {
return agg, monitoringInternalError(50041, "run_projection_lookup_failed", "failed to load monitoring run counts", err)
}
@@ -188,15 +196,27 @@ func (s *MonitoringCallbackService) loadMonitoringBrandDailyAggregate(ctx contex
COUNT(*) FILTER (WHERE p.brand_mentioned IS TRUE)::bigint,
COUNT(*) FILTER (WHERE p.brand_mention_position = 'top1')::bigint,
COUNT(*) FILTER (WHERE p.first_recommended IS TRUE)::bigint,
COUNT(*) FILTER (WHERE p.sentiment_label = 'positive')::bigint
FROM question_monitor_runs r
COUNT(*) FILTER (WHERE p.brand_mentioned IS TRUE AND p.sentiment_label = 'positive')::bigint
FROM (
SELECT DISTINCT ON (business_date, question_id, ai_platform_id)
id,
tenant_id,
business_date,
question_id,
ai_platform_id,
completed_at,
updated_at,
created_at
FROM question_monitor_runs
WHERE tenant_id = $1
AND brand_id = $2
AND collector_type = $3
AND business_date = $4::date
AND status = 'succeeded'
ORDER BY business_date, question_id, ai_platform_id, COALESCE(completed_at, updated_at, created_at) DESC NULLS LAST, id DESC
) r
LEFT JOIN question_monitor_parse_results p
ON p.tenant_id = r.tenant_id AND p.run_id = r.id
WHERE r.tenant_id = $1
AND r.brand_id = $2
AND r.collector_type = $3
AND r.business_date = $4::date
AND r.status = 'succeeded'
`, tenantID, brandID, monitoringCollectorType, dateText).Scan(
&agg.MentionedCount,
&agg.Top1MentionedCount,
@@ -280,14 +300,24 @@ func (s *MonitoringCallbackService) loadMonitoringPlatformDailyAggregates(ctx co
COUNT(*) FILTER (WHERE p.brand_mentioned IS TRUE)::bigint AS mentioned_count,
COUNT(*) FILTER (WHERE p.brand_mention_position = 'top1')::bigint AS top1_mentioned_count,
MAX(r.completed_at) AS last_sampled_at
FROM question_monitor_runs r
FROM (
SELECT DISTINCT ON (business_date, question_id, ai_platform_id)
id,
tenant_id,
ai_platform_id,
completed_at,
updated_at,
created_at
FROM question_monitor_runs
WHERE tenant_id = $1
AND brand_id = $2
AND collector_type = $3
AND business_date = $4::date
AND status = 'succeeded'
ORDER BY business_date, question_id, ai_platform_id, COALESCE(completed_at, updated_at, created_at) DESC NULLS LAST, id DESC
) r
LEFT JOIN question_monitor_parse_results p
ON p.tenant_id = r.tenant_id AND p.run_id = r.id
WHERE r.tenant_id = $1
AND r.brand_id = $2
AND r.collector_type = $3
AND r.business_date = $4::date
AND r.status = 'succeeded'
GROUP BY r.ai_platform_id
`, tenantID, brandID, monitoringCollectorType, dateText)
if err != nil {