10000 add activity bump to StatsDBReporter.Report · coder/coder@64a5aad · GitHub
[go: up one dir, main page]

Skip to content

Commit 64a5aad

Browse files
committed
add activity bump to StatsDBReporter.Report
1 parent f23d480 commit 64a5aad

File tree

2 files changed

+68
-23
lines changed

2 files changed

+68
-23
lines changed

coderd/agentapi/stats.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,27 @@ func (a *StatsAPI) UpdateStats(ctx context.Context, req *agentproto.UpdateStatsR
7070
)
7171

7272
now := a.now()
73-
if req.Stats.ConnectionCount > 0 {
74-
var nextAutostart time.Time
75-
if workspace.AutostartSchedule.String != "" {
76-
templateSchedule, err := (*(a.TemplateScheduleStore.Load())).Get(ctx, a.Database, workspace.TemplateID)
77-
// If the template schedule fails to load, just default to bumping
78-
// without the next transition and log it.
79-
if err != nil {
80-
a.Log.Error(ctx, "failed to load template schedule bumping activity, defaulting to bumping by 60min",
81-
slog.F("workspace_id", workspace.ID),
82-
slog.F("template_id", workspace.TemplateID),
83-
slog.Error(err),
84-
)
85-
} else {
86-
next, allowed := schedule.NextAutostart(now, workspace.AutostartSchedule.String, templateSchedule)
87-
if allowed {
88-
nextAutostart = next
89-
}
90-
}
91-
}
92-
ActivityBumpWorkspace(ctx, a.Log.Named("activity_bump"), a.Database, workspace.ID, nextAutostart)
93-
}
73+
// if req.Stats.ConnectionCount > 0 {
74+
// var nextAutostart time.Time
75+
// if workspace.AutostartSchedule.String != "" {
76+
// templateSchedule, err := (*(a.TemplateScheduleStore.Load())).Get(ctx, a.Database, workspace.TemplateID)
77+
// // If the template schedule fails to load, just default to bumping
78+
// // without the next transition and log it.
79+
// if err != nil {
80+
// a.Log.Error(ctx, "failed to load template schedule bumping activity, defaulting to bumping by 60min",
81+
// slog.F("workspace_id", workspace.ID),
82+
// slog.F("template_id", workspace.TemplateID),
83+
// slog.Error(err),
84+
// )
85+
// } else {
86+
// next, allowed := schedule.NextAutostart(now, workspace.AutostartSchedule.String, templateSchedule)
87+
// if allowed {
88+
// nextAutostart = next
89+
// }
90+
// }
91+
// }
92+
// ActivityBumpWorkspace(ctx, a.Log.Named("activity_bump"), a.Database, workspace.ID, nextAutostart)
93+
// }
9494

9595
var errGroup errgroup.Group
9696
errGroup.Go(func() error {

coderd/workspaceapps/stats.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@ package workspaceapps
33
import (
44
"context"
55
"sync"
6+
"sync/atomic"
67
"time"
78

89
"github.com/google/uuid"
910
"golang.org/x/xerrors"
1011

1112
"cdr.dev/slog"
1213

14+
"github.com/coder/coder/v2/coderd/agentapi"
1315
"github.com/coder/coder/v2/coderd/database"
1416
"github.com/coder/coder/v2/coderd/database/dbauthz"
1517
"github.com/coder/coder/v2/coderd/database/dbtime"
18+
"github.com/coder/coder/v2/coderd/schedule"
1619
"github.com/coder/coder/v2/coderd/util/slice"
1720
)
1821

@@ -59,8 +62,10 @@ var _ StatsReporter = (*StatsDBReporter)(nil)
5962

6063
// StatsDBReporter writes workspace app StatsReports to the database.
6164
type StatsDBReporter struct {
62-
db database.Store
63-
batchSize int
65+
db database.Store
66+
logger slog.Logger
67+
templateScheduleStore *atomic.Pointer[schedule.TemplateScheduleStore]
68+
batchSize int
6469
}
6570

6671
// NewStatsDBReporter returns a new StatsDBReporter.
@@ -139,6 +144,36 @@ func (r *StatsDBReporter) Report(ctx context.Context, stats []StatsReport) error
139144
return err
140145
}
141146

147+
workspaces, err := tx.GetWorkspaces(ctx, database.GetWorkspacesParams{
148+
WorkspaceIds: uniqueIDs,
149+
})
150+
if err != nil {
151+
return xerrors.Errorf("getting workspaces: %w", err)
152+
}
153+
154+
// TODO: This probably needs batching to handle larger deployments
155+
for _, workspace := range workspaces {
156+
var nextAutostart time.Time
157+
if workspace.AutostartSchedule.String != "" {
158+
templateSchedule, err := (*(r.templateScheduleStore.Load())).Get(ctx, r.db, workspace.TemplateID)
159+
// If the template schedule fails to load, just default to bumping
160+
// without the next transition and log it.
161+
if err != nil {
162+
r.logger.Error(ctx, "failed to load template schedule bumping activity, defaulting to bumping by 60min",
163+
slog.F("workspace_id", workspace.ID),
164+
slog.F("template_id", workspace.TemplateID),
165+
slog.Error(err),
166+
)
167+
} else {
168+
next, allowed := schedule.NextAutostart(dbtime.Now(), workspace.AutostartSchedule.String, templateSchedule)
169+
if allowed {
170+
nextAutostart = next
171+
}
172+
}
173+
}
174+
agentapi.ActivityBumpWorkspace(ctx, r.logger.Named("activity_bump"), r.db, workspace.ID, nextAutostart)
175+
}
176+
142177
return nil
143178
}, nil)
144179
if err != nil {
@@ -252,6 +287,16 @@ func (sc *StatsCollector) Collect(report StatsReport) {
252287
sc.opts.Logger.Debug(sc.ctx, "collected workspace app stats", slog.F("report", report))
253288
}
254289

290+
func (sc *StatsCollector) CollectAndFlush(ctx context.Context, report StatsReport) error {
291+
sc.Collect(report)
292+
err := sc.flush(ctx)
293+
if err != nil {
294+
return xerrors.Errorf("flushing collector: %w", err)
295+
}
296+
297+
return nil
298+
}
299+
255300
// rollup performs stats rollup for sessions that fall within the
256301
// configured rollup window. For sessions longer than the window,
257302
// we report them individually.

0 commit comments

Comments
 (0)
0