diff --git a/backend/services/daily_volume.go b/backend/services/daily_volume.go index e85241d..dc38056 100644 --- a/backend/services/daily_volume.go +++ b/backend/services/daily_volume.go @@ -66,14 +66,41 @@ func CaptureDailyVolume(source *models.LogSource, day time.Time, partial bool) e partial = true } - // 只查总量(不带 namespace 条件),一次拿到该源当天的条数与体积 - // 整天(24h)的 index/stats:大集群上比 15 分钟的查询慢得多, - // 实测 20s 会超时(这也是之前每日量接口卡 20s 的原因)。给 90s。 - ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second) + // ── 体积:index/volume_range(只读 TSDB chunk 元数据,不解码日志内容)── + // 实测同一条件 24 小时:index/stats 要 20s(遇超时),volume_range 111ms。 + // 用户关心的"每日日志量"就是体积,所以主路径走它。 + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() - stats, err := LokiStatsFor(ctx, source, `{namespace=~".+"}`, start, end) - if err != nil { - return err + buckets, volErr := LokiHistogram(ctx, source, `{namespace=~".+"}`, start, end) + var totalBytes int64 + for _, b := range buckets { + switch v := b["count"].(type) { + case int64: + totalBytes += v + case float64: + totalBytes += int64(v) + } + } + + // ── 条数:只能 index/stats,慢(整天约 20s)── + // 降级为"尽力而为":拿不到就把条数留 0,绝不让它拖住体积入库。 + // 前端图只画体积,条数缺失不影响可读性。 + var totalEntries int64 + entryCtx, entryCancel := context.WithTimeout(context.Background(), 25*time.Second) + if stats, err := LokiStatsFor(entryCtx, source, `{namespace=~".+"}`, start, end); err == nil { + totalEntries = stats.Entries + if totalBytes == 0 { + totalBytes = stats.Bytes + } + } else { + log.Printf("[daily-volume] %s 条数统计超时/失败(不影响体积): %v", dateKey(day), err) + } + entryCancel() + if volErr != nil && totalBytes == 0 { + return volErr + } + if totalBytes == 0 { + return nil } date := dateKey(day) @@ -95,8 +122,11 @@ func CaptureDailyVolume(source *models.LogSource, day time.Time, partial bool) e } // 同一天同源:覆盖(幂等) if snap.Date == date && snap.SourceID == source.ID && snap.Namespace == "" { - snap.Bytes = stats.Bytes - snap.Entries = stats.Entries + snap.Bytes = totalBytes + // 条数拿不到时保留上一次的值,避免"一次超时把历史条数清零" + if totalEntries > 0 { + snap.Entries = totalEntries + } snap.Partial = partial snap.CapturedAt = time.Now() db.RequestSave() @@ -109,8 +139,8 @@ func CaptureDailyVolume(source *models.LogSource, day time.Time, partial bool) e Date: date, SourceID: source.ID, Namespace: "", - Bytes: stats.Bytes, - Entries: stats.Entries, + Bytes: totalBytes, + Entries: totalEntries, Partial: partial, CapturedAt: time.Now(), }) diff --git a/frontend/src/pages/analysis/components/DailyVolumeChart.tsx b/frontend/src/pages/analysis/components/DailyVolumeChart.tsx index 36d9748..aa38ace 100644 --- a/frontend/src/pages/analysis/components/DailyVolumeChart.tsx +++ b/frontend/src/pages/analysis/components/DailyVolumeChart.tsx @@ -1,5 +1,5 @@ import { memo, useMemo } from 'react' -import { Area, AreaChart, CartesianGrid, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts' +import { Bar, BarChart, Cell, CartesianGrid, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts' import { useTranslation } from 'react-i18next' import { Button } from '@/components/ui/button' import type { DailyVolumePoint } from '@/api/logs' @@ -56,11 +56,11 @@ export const DailyVolumeChart = memo(function DailyVolumeChart({ points, days, o {hasData ? (