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
@@ -1,6 +1,7 @@
package app
import (
"context"
"database/sql"
"encoding/json"
"errors"
@@ -9,6 +10,8 @@ import (
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDecideHeartbeatPrimaryLease(t *testing.T) {
@@ -111,6 +114,42 @@ func TestMonitoringHeartbeatPrimaryLeaseMetrics(t *testing.T) {
}
}
func TestLoadMonitoringBrandDailyAggregateUsesLatestQuestionPlatformSamples(t *testing.T) {
tx := &captureDailyMonitorTaskTx{}
_, err := (&MonitoringCallbackService{}).loadMonitoringBrandDailyAggregate(
context.Background(),
tx,
3,
3,
time.Date(2026, 6, 29, 0, 0, 0, 0, monitoringBusinessLocation()),
)
require.NoError(t, err)
require.Len(t, tx.queryRowSQLs, 5)
combined := normalizeSQLWhitespace(strings.Join(tx.queryRowSQLs, " "))
assert.Contains(t, combined, "SELECT DISTINCT ON (business_date, question_id, ai_platform_id)")
assert.Contains(t, combined, "p.brand_mentioned IS TRUE AND p.sentiment_label = 'positive'")
}
func TestLoadMonitoringPlatformDailyAggregatesUsesLatestQuestionPlatformSamples(t *testing.T) {
tx := &captureDailyMonitorTaskTx{}
_, err := (&MonitoringCallbackService{}).loadMonitoringPlatformDailyAggregates(
context.Background(),
tx,
3,
3,
time.Date(2026, 6, 29, 0, 0, 0, 0, monitoringBusinessLocation()),
[]monitoringPlatformMetadata{{ID: "doubao", Name: "豆包"}},
)
require.NoError(t, err)
require.GreaterOrEqual(t, len(tx.querySQLs), 2)
combined := normalizeSQLWhitespace(strings.Join(tx.querySQLs, " "))
assert.Contains(t, combined, "SELECT DISTINCT ON (business_date, question_id, ai_platform_id)")
}
func TestDecideMonitoringHeartbeatSnapshot(t *testing.T) {
now := time.Date(2026, 4, 24, 3, 0, 0, 0, time.UTC)
base := monitoringPlatformAccessSnapshotState{