@@ -26,8 +26,9 @@ import (
26
26
// take a few hundred milliseconds, which would ruin page load times and
27
27
// database performance if in the hot path.
28
28
type Cache struct {
29
- database database.Store
30
- log slog.Logger
29
+ database database.Store
30
+ log slog.Logger
31
+ intervals Intervals
31
32
32
33
deploymentDAUResponses atomic.Pointer [codersdk.DeploymentDAUsResponse ]
33
34
templateDAUResponses atomic.Pointer [map [uuid.UUID ]codersdk.TemplateDAUsResponse ]
@@ -54,23 +55,24 @@ func New(db database.Store, log slog.Logger, intervals Intervals) *Cache {
54
55
ctx , cancel := context .WithCancel (context .Background ())
55
56
56
57
c := & Cache {
57
- database : db ,
58
- log : log ,
59
- done : make (chan struct {}),
60
- cancel : cancel ,
58
+ database : db ,
59
+ intervals : intervals ,
60
+ log : log ,
61
+ done : make (chan struct {}),
62
+ cancel : cancel ,
61
63
}
62
64
go func () {
63
65
var wg sync.WaitGroup
64
66
defer close (c .done )
65
67
wg .Add (1 )
66
68
go func () {
67
- wg .Done ()
68
- c .run (ctx , intervals .TemplateDAUs , c .refreshTemplateDAUs )
69
+ defer wg .Done ()
70
+ c .run (ctx , "template daus" , intervals .TemplateDAUs , c .refreshTemplateDAUs )
69
71
}()
70
72
wg .Add (1 )
71
73
go func () {
72
- wg .Done ()
73
- c .run (ctx , intervals .DeploymentStats , c .refreshDeploymentStats )
74
+ defer wg .Done ()
75
+ c .run (ctx , "deployment stats" , intervals .DeploymentStats , c .refreshDeploymentStats )
74
76
}()
75
77
wg .Wait ()
76
78
}()
@@ -228,7 +230,8 @@ func (c *Cache) refreshDeploymentStats(ctx context.Context) error {
228
230
}
229
231
c .deploymentStatsResponse .Store (& codersdk.DeploymentStats {
230
232
AggregatedFrom : from ,
231
- UpdatedAt : database .Now (),
233
+ CollectedAt : database .Now (),
234
+ RefreshingAt : database .Now ().Add (c .intervals .DeploymentStats ),
232
235
WorkspaceConnectionLatencyMS : codersdk.WorkspaceConnectionLatencyMS {
233
236
P50 : deploymentStats .WorkspaceConnectionLatency50 ,
234
237
P95 : deploymentStats .WorkspaceConnectionLatency95 ,
@@ -243,7 +246,7 @@ func (c *Cache) refreshDeploymentStats(ctx context.Context) error {
243
246
return nil
244
247
}
245
248
246
- func (c * Cache ) run (ctx context.Context , interval time.Duration , refresh func (context.Context ) error ) {
249
+ func (c * Cache ) run (ctx context.Context , name string , interval time.Duration , refresh func (context.Context ) error ) {
247
250
ticker := time .NewTicker (interval )
248
251
defer ticker .Stop ()
249
252
@@ -260,7 +263,7 @@ func (c *Cache) run(ctx context.Context, interval time.Duration, refresh func(co
260
263
}
261
264
c .log .Debug (
262
265
ctx ,
263
- " metrics refreshed" ,
266
+ name + " metrics refreshed" ,
264
267
slog .F ("took" , time .Since (start )),
265
268
slog .F ("interval" , interval ),
266
269
)
0 commit comments