Refactor dashboard statistics retrieval to improve performance
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
- 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.
This commit is contained in:
@@ -223,19 +223,13 @@ func (s *service) Statistics(ctx context.Context, query StatisticsQuery) (*Stati
|
|||||||
return redisCached, nil
|
return redisCached, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := s.loadStatistics(ctx, days)
|
// Cold cache: the underlying load performs a full MinIO bucket scan plus Mongo
|
||||||
if err == nil {
|
// aggregations that can take minutes. Never block the request on it. Kick off a
|
||||||
return result, nil
|
// 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)
|
||||||
if redisCached, ok := s.getRedisStatistics(ctx, days); ok {
|
s.logger.Info("Serving placeholder dashboard statistics while cache warms", map[string]interface{}{
|
||||||
s.storeStatistics(days, redisCached)
|
|
||||||
return redisCached, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
s.logger.Error("Serving empty dashboard statistics after fetch failure", map[string]interface{}{
|
|
||||||
"days": days,
|
"days": days,
|
||||||
"error": err.Error(),
|
|
||||||
})
|
})
|
||||||
return emptyStatisticsReport(days), nil
|
return emptyStatisticsReport(days), nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user