Update dashboard statistics caching and retrieval logic
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
- Increased the cache duration for dashboard statistics from 60 seconds to 5 minutes, improving data freshness and reducing load on the backend. - Introduced a stale cache mechanism that allows retrieval of stale statistics while refreshing them in the background, enhancing user experience by providing quicker access to data. - Updated the statistics repository to handle the new caching logic, ensuring accurate and timely statistics reporting. - Added tests to validate the new caching behavior and ensure the integrity of statistics retrieval. This update optimizes the dashboard's performance and responsiveness by improving the caching strategy for statistics.
This commit is contained in:
@@ -3,14 +3,28 @@ package dashboard
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"tm/pkg/ai_summarizer"
|
||||
"tm/pkg/logger"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
type noopTestLogger struct{}
|
||||
|
||||
func (noopTestLogger) Debug(string, map[string]interface{}) {}
|
||||
func (noopTestLogger) Info(string, map[string]interface{}) {}
|
||||
func (noopTestLogger) Warn(string, map[string]interface{}) {}
|
||||
func (noopTestLogger) Error(string, map[string]interface{}) {}
|
||||
func (noopTestLogger) Fatal(string, map[string]interface{}) {}
|
||||
func (l noopTestLogger) WithFields(map[string]interface{}) logger.Logger {
|
||||
return l
|
||||
}
|
||||
func (noopTestLogger) Sync() error { return nil }
|
||||
|
||||
type mockProcedureDocumentsLister struct {
|
||||
procedures []ai_summarizer.ProcedureDocumentsSummary
|
||||
dailyCounts map[string]int64
|
||||
@@ -90,6 +104,7 @@ func TestResolveScrapedTendersScopeUsesMinIOFolders(t *testing.T) {
|
||||
procedures: []ai_summarizer.ProcedureDocumentsSummary{
|
||||
{ContractFolderID: "PROC-1", DocumentCount: 2},
|
||||
},
|
||||
dailyCounts: map[string]int64{"2026-06-28": 2},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -103,6 +118,12 @@ func TestResolveScrapedTendersScopeUsesMinIOFolders(t *testing.T) {
|
||||
if !scope.fromMinIO {
|
||||
t.Fatal("expected MinIO-backed scope")
|
||||
}
|
||||
if scope.totalTenderCount != 1 {
|
||||
t.Fatalf("expected total tender count 1, got %d", scope.totalTenderCount)
|
||||
}
|
||||
if scope.dailyCounts == nil {
|
||||
t.Fatal("expected MinIO daily counts")
|
||||
}
|
||||
in, ok := scope.match["contract_folder_id"].(bson.M)
|
||||
if !ok {
|
||||
t.Fatalf("expected contract_folder_id filter, got %#v", scope.match)
|
||||
@@ -113,6 +134,44 @@ func TestResolveScrapedTendersScopeUsesMinIOFolders(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveScrapedTendersScopeKeepsMinIOCountsWhenTooManyFolders(t *testing.T) {
|
||||
procedures := make([]ai_summarizer.ProcedureDocumentsSummary, maxScrapedFolderInClause+1)
|
||||
for i := range procedures {
|
||||
procedures[i] = ai_summarizer.ProcedureDocumentsSummary{
|
||||
ContractFolderID: fmt.Sprintf("PROC-%d", i),
|
||||
DocumentCount: 1,
|
||||
}
|
||||
}
|
||||
|
||||
repo := &repository{
|
||||
logger: noopTestLogger{},
|
||||
procedureLister: &mockProcedureDocumentsLister{
|
||||
procedures: procedures,
|
||||
dailyCounts: map[string]int64{"2026-06-28": 3},
|
||||
},
|
||||
}
|
||||
|
||||
scope, err := repo.resolveScrapedTendersScope(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if scope.zero {
|
||||
t.Fatal("expected non-zero scope")
|
||||
}
|
||||
if !scope.fromMinIO {
|
||||
t.Fatal("expected MinIO-backed scope")
|
||||
}
|
||||
if scope.totalTenderCount != int64(len(procedures)) {
|
||||
t.Fatalf("expected total tender count %d, got %d", len(procedures), scope.totalTenderCount)
|
||||
}
|
||||
if scope.dailyCounts == nil {
|
||||
t.Fatal("expected MinIO daily counts to be preserved")
|
||||
}
|
||||
if scope.match != nil {
|
||||
t.Fatalf("expected no Mongo $in filter for large MinIO scope, got %#v", scope.match)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveScrapedTendersScopeMongoFallbackWhenMinIOEmpty(t *testing.T) {
|
||||
repo := &repository{
|
||||
procedureLister: &mockProcedureDocumentsLister{},
|
||||
|
||||
Reference in New Issue
Block a user