Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 93f013b

Browse files
committed
Increase period of stat refreshes
1 parent a1804a9 commit 93f013b

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

coderd/metricscache/metricscache.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import (
2626
// take a few hundred milliseconds, which would ruin page load times and
2727
// database performance if in the hot path.
2828
type Cache struct {
29-
database database.Store
30-
log slog.Logger
29+
database database.Store
30+
log slog.Logger
31+
intervals Intervals
3132

3233
deploymentDAUResponses atomic.Pointer[codersdk.DeploymentDAUsResponse]
3334
templateDAUResponses atomic.Pointer[map[uuid.UUID]codersdk.TemplateDAUsResponse]
@@ -54,23 +55,24 @@ func New(db database.Store, log slog.Logger, intervals Intervals) *Cache {
5455
ctx, cancel := context.WithCancel(context.Background())
5556

5657
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,
6163
}
6264
go func() {
6365
var wg sync.WaitGroup
6466
defer close(c.done)
6567
wg.Add(1)
6668
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)
6971
}()
7072
wg.Add(1)
7173
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)
7476
}()
7577
wg.Wait()
7678
}()
@@ -228,7 +230,8 @@ func (c *Cache) refreshDeploymentStats(ctx context.Context) error {
228230
}
229231
c.deploymentStatsResponse.Store(&codersdk.DeploymentStats{
230232
AggregatedFrom: from,
231-
UpdatedAt: database.Now(),
233+
CollectedAt: database.Now(),
234+
RefreshingAt: database.Now().Add(c.intervals.DeploymentStats),
232235
WorkspaceConnectionLatencyMS: codersdk.WorkspaceConnectionLatencyMS{
233236
P50: deploymentStats.WorkspaceConnectionLatency50,
234237
P95: deploymentStats.WorkspaceConnectionLatency95,
@@ -243,7 +246,7 @@ func (c *Cache) refreshDeploymentStats(ctx context.Context) error {
243246
return nil
244247
}
245248

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) {
247250
ticker := time.NewTicker(interval)
248251
defer ticker.Stop()
249252

@@ -260,7 +263,7 @@ func (c *Cache) run(ctx context.Context, interval time.Duration, refresh func(co
260263
}
261264
c.log.Debug(
262265
ctx,
263-
"metrics refreshed",
266+
name+" metrics refreshed",
264267
slog.F("took", time.Since(start)),
265268
slog.F("interval", interval),
266269
)

codersdk/deployment.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,14 @@ type WorkspaceConnectionLatencyMS struct {
550550
}
551551

552552
type DeploymentStats struct {
553-
AggregatedFrom time.Time `json:"aggregated_from"`
554-
UpdatedAt time.Time `json:"updated_at"`
553+
// AggregatedFrom is the time in which stats are aggregated from.
554+
// This might be back in time a specific duration or interval.
555+
AggregatedFrom time.Time `json:"aggregated_since"`
556+
// CollectedAt is the time in which stats are collected at.
557+
CollectedAt time.Time `json:"collected_at"`
558+
// RefreshingAt is the time when the next batch of stats will
559+
// be refreshed.
560+
RefreshingAt time.Time `json:"refreshing_at"`
555561

556562
WorkspacesByTransition map[WorkspaceTransition]int `json:"workspaces_by_transition"`
557563
WorkspaceConnectionLatencyMS WorkspaceConnectionLatencyMS `json:"workspace_connection_latency_ms"`

0 commit comments

Comments
 (0)