From 12d1cabf7e5aaa89a87a248bfb24d8c5748206f0 Mon Sep 17 00:00:00 2001 From: Mazyar Date: Tue, 30 Jun 2026 22:10:30 +0330 Subject: [PATCH] Refactor dashboard statistics retrieval to improve performance - Updated the Statistics method in the dashboard service to initiate a background refresh for statistics while serving a placeholder report, enhancing responsiveness during cache warming. - Removed blocking calls to load statistics directly, allowing for faster response times. - Improved logging to indicate when placeholder statistics are served, providing better visibility into the caching process. This update optimizes the dashboard's performance by ensuring that users receive immediate feedback while the system prepares accurate statistics in the background. --- internal/dashboard/service.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/internal/dashboard/service.go b/internal/dashboard/service.go index b8b462c..2bdef2a 100644 --- a/internal/dashboard/service.go +++ b/internal/dashboard/service.go @@ -223,19 +223,13 @@ func (s *service) Statistics(ctx context.Context, query StatisticsQuery) (*Stati return redisCached, nil } - result, err := s.loadStatistics(ctx, days) - if err == nil { - return result, nil - } - - if redisCached, ok := s.getRedisStatistics(ctx, days); ok { - s.storeStatistics(days, redisCached) - return redisCached, nil - } - - s.logger.Error("Serving empty dashboard statistics after fetch failure", map[string]interface{}{ - "days": days, - "error": err.Error(), + // Cold cache: the underlying load performs a full MinIO bucket scan plus Mongo + // aggregations that can take minutes. Never block the request on it. Kick off a + // background refresh (deduped via singleflight) and serve an empty report so the + // endpoint stays fast; subsequent requests get full data once the cache warms. + go s.refreshStatistics(days) + s.logger.Info("Serving placeholder dashboard statistics while cache warms", map[string]interface{}{ + "days": days, }) return emptyStatisticsReport(days), nil }