Update MinIO bucket configuration and enhance dashboard service caching
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
- Changed the default MinIO bucket name from "opplens-documents" to "opplens" across multiple configuration files. - Introduced caching for dashboard statistics in the service layer to improve performance and reduce redundant data fetching. - Implemented a mutex for thread-safe access to cached statistics, ensuring data integrity during concurrent requests. This update streamlines the configuration for the AI summarizer and optimizes the dashboard service, enhancing overall system efficiency.
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"tm/pkg/logger"
|
||||
)
|
||||
@@ -15,6 +16,7 @@ const (
|
||||
defaultCountriesLimit = 6
|
||||
defaultListLimit = 5
|
||||
maxListLimit = 20
|
||||
statisticsCacheTTL = 60 * time.Second
|
||||
)
|
||||
|
||||
// Service defines dashboard business operations.
|
||||
@@ -28,14 +30,25 @@ type Service interface {
|
||||
Statistics(ctx context.Context, query StatisticsQuery) (*StatisticsReportResponse, error)
|
||||
}
|
||||
|
||||
type statisticsCacheEntry struct {
|
||||
expiresAt time.Time
|
||||
value *StatisticsReportResponse
|
||||
}
|
||||
|
||||
type service struct {
|
||||
repo Repository
|
||||
logger logger.Logger
|
||||
repo Repository
|
||||
logger logger.Logger
|
||||
statisticsMu sync.Mutex
|
||||
statistics map[int]statisticsCacheEntry
|
||||
}
|
||||
|
||||
// NewService creates a dashboard service.
|
||||
func NewService(repo Repository, log logger.Logger) Service {
|
||||
return &service{repo: repo, logger: log}
|
||||
return &service{
|
||||
repo: repo,
|
||||
logger: log,
|
||||
statistics: make(map[int]statisticsCacheEntry),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *service) Summary(ctx context.Context, query SummaryQuery) (*SummaryResponse, error) {
|
||||
@@ -160,6 +173,10 @@ func (s *service) Recent(ctx context.Context, query RecentQuery) (*RecentRespons
|
||||
|
||||
func (s *service) Statistics(ctx context.Context, query StatisticsQuery) (*StatisticsReportResponse, error) {
|
||||
days := trendDays(query.Days)
|
||||
if cached, ok := s.cachedStatistics(days); ok {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
startUnix := trendStartUnix(days)
|
||||
|
||||
s.logger.Info("Fetching dashboard statistics report", map[string]interface{}{
|
||||
@@ -174,9 +191,37 @@ func (s *service) Statistics(ctx context.Context, query StatisticsQuery) (*Stati
|
||||
return nil, fmt.Errorf("dashboard statistics: %w", err)
|
||||
}
|
||||
|
||||
s.storeStatistics(days, out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *service) cachedStatistics(days int) (*StatisticsReportResponse, bool) {
|
||||
now := time.Now()
|
||||
|
||||
s.statisticsMu.Lock()
|
||||
defer s.statisticsMu.Unlock()
|
||||
|
||||
entry, ok := s.statistics[days]
|
||||
if !ok || now.After(entry.expiresAt) {
|
||||
if ok {
|
||||
delete(s.statistics, days)
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return entry.value, true
|
||||
}
|
||||
|
||||
func (s *service) storeStatistics(days int, value *StatisticsReportResponse) {
|
||||
s.statisticsMu.Lock()
|
||||
defer s.statisticsMu.Unlock()
|
||||
|
||||
s.statistics[days] = statisticsCacheEntry{
|
||||
expiresAt: time.Now().Add(statisticsCacheTTL),
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func closingWindowHours(hours int) int {
|
||||
if hours <= 0 {
|
||||
return defaultClosingWindowHours
|
||||
|
||||
Reference in New Issue
Block a user