droboticflow/internal/store/workforce_report_cache.go
2026-11-13 22:31:44 -08:00

133 lines
4.1 KiB
Go

package store
import (
"context"
"encoding/json"
"fmt"
"time"
"droboticflow/internal/queryserving"
"droboticflow/internal/workforcestats"
)
// 128 entries (ttlResultCache's bound) at 512KiB give a 64MiB payload bound.
// Raw history snapshots are deliberately not retained by the dashboard reader.
const workforceReportMaxBytes = 512 << 10
var workforceReportCache = &ttlResultCache[[]byte]{now: time.Now}
// LoadWorkforceReportCached preserves the shared calculator and short-staleness
// contract. Only summary callers use this path; detail readers stay uncached.
// Cache keys contain every query scope, never an authorization decision.
func (s *MongoStore) LoadWorkforceReportCached(ctx context.Context, org string, q workforcestats.Query) (workforcestats.Report, error) {
if report, used, err := s.cachedPublishedWorkforce(ctx, org, q); used || err != nil {
return report, err
}
if err := ctx.Err(); err != nil {
return workforcestats.Report{}, err
}
if q.Details {
d, err := s.LoadWorkforceStats(ctx, org, q)
if err != nil {
return workforcestats.Report{}, err
}
return workforcestats.Build(d, q), nil
}
target, err := s.reportReplica(ctx)
if err != nil {
return workforcestats.Report{}, err
}
if target != nil {
report, used, e := target.loadWorkforceFactReport(ctx, org, q)
if e != nil || used {
return report, e
}
} else if s.queryReplicaFactStore {
report, used, e := s.loadWorkforceFactReport(ctx, org, q)
if e != nil || used {
return report, e
}
}
epoch := int64(0)
identity := fmt.Sprintf("%p:%s", s.db.Client(), s.db.Name())
if target != nil {
epoch, err = target.countFactEpoch(ctx)
if err != nil {
return workforcestats.Report{}, err
}
identity += fmt.Sprintf("/%p:%s", target.db.Client(), target.db.Name())
}
keyQuery := q
if keyQuery.GroupBy == "" {
keyQuery.GroupBy = "member"
}
if !keyQuery.From.IsZero() {
keyQuery.From = keyQuery.From.UTC().Truncate(time.Minute)
}
if !keyQuery.To.IsZero() {
keyQuery.To = keyQuery.To.UTC().Truncate(time.Minute)
}
key := queryserving.Key("workforce-summary/v1", identity, org, epoch, keyQuery)
if raw, ok := workforceReportCache.get(key, dashboardStatsCacheTTL); ok {
var report workforcestats.Report
if json.Unmarshal(raw, &report) == nil {
return report, nil
}
}
var shared queryserving.Cache
sharedKey := ""
if target != nil {
shared = target.SharedQueryCache()
sharedKey = queryserving.Key("workforce-summary/v1", queryReplicaNamespace(s.db.Name()), s.db.Name(), target.db.Name(), org, epoch, keyQuery)
}
if shared != nil {
if raw, ok := shared.Get(ctx, sharedKey); ok && len(raw) <= workforceReportMaxBytes {
var report workforcestats.Report
if json.Unmarshal(raw, &report) == nil && len(report.Details) == 0 {
workforceReportCache.set(key, raw, dashboardStatsCacheTTL)
return report, nil
}
}
}
d, err := s.LoadWorkforceStats(ctx, org, q)
if err != nil {
return workforcestats.Report{}, err
}
report := workforcestats.Build(d, q)
// This endpoint exposes metrics, not return-detail history. Its separate
// detail endpoint retains the original calculator and immutable text reads.
report.Details = nil
if raw, err := json.Marshal(report); err == nil && len(raw) <= workforceReportMaxBytes {
workforceReportCache.set(key, raw, dashboardStatsCacheTTL)
if shared != nil {
shared.Put(ctx, sharedKey, raw, dashboardStatsCacheTTL)
}
}
return report, nil
}
// LoadWorkforceReport preserves uncached legacy summary behavior when read
// replicas are disabled; the enabled replacement reads only published facts.
func (s *MongoStore) LoadWorkforceReport(ctx context.Context, org string, q workforcestats.Query) (workforcestats.Report, error) {
if report, used, err := s.cachedPublishedWorkforce(ctx, org, q); used || err != nil {
return report, err
}
target, err := s.reportReplica(ctx)
if err != nil {
return workforcestats.Report{}, err
}
if target != nil {
report, used, e := target.loadWorkforceFactReport(ctx, org, q)
if e != nil || used {
return report, e
}
}
d, err := s.LoadWorkforceStats(ctx, org, q)
if err != nil {
return workforcestats.Report{}, err
}
return workforcestats.Build(d, q), nil
}