Skip to content

Commit a60de2b

Browse files
committed
Revert "remove workspace tracker"
This reverts commit dc818d7.
1 parent dc818d7 commit a60de2b

File tree

6 files changed

+527
-17
lines changed

6 files changed

+527
-17
lines changed

coderd/coderd.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import (
6868
"github.com/coder/coder/v2/coderd/updatecheck"
6969
"github.com/coder/coder/v2/coderd/util/slice"
7070
"github.com/coder/coder/v2/coderd/workspaceapps"
71+
"github.com/coder/coder/v2/coderd/workspaceusage"
7172
"github.com/coder/coder/v2/codersdk"
7273
"github.com/coder/coder/v2/codersdk/drpc"
7374
"github.com/coder/coder/v2/codersdk/healthsdk"
@@ -203,6 +204,8 @@ type Options struct {
203204
// DatabaseRolluper rolls up template usage stats from raw agent and app
204205
// stats. This is used to provide insights in the WebUI.
205206
DatabaseRolluper *dbrollup.Rolluper
207+
// WorkspaceUsageTracker tracks workspace usage by the CLI.
208+
WorkspaceUsageTracker *workspaceusage.Tracker
206209
}
207210

208211
// @title Coder API
@@ -379,6 +382,12 @@ func New(options *Options) *API {
379382
options.DatabaseRolluper = dbrollup.New(options.Logger.Named("dbrollup"), options.Database)
380383
}
381384

385+
if options.WorkspaceUsageTracker == nil {
386+
options.WorkspaceUsageTracker = workspaceusage.New(options.Database,
387+
workspaceusage.WithLogger(options.Logger.Named("workspace_usage_tracker")),
388+
)
389+
}
390+
382391
ctx, cancel := context.WithCancel(context.Background())
383392
r := chi.NewRouter()
384393

@@ -423,7 +432,8 @@ func New(options *Options) *API {
423432
options.Database,
424433
options.Pubsub,
425434
),
426-
dbRolluper: options.DatabaseRolluper,
435+
dbRolluper: options.DatabaseRolluper,
436+
workspaceUsageTracker: options.WorkspaceUsageTracker,
427437
}
428438

429439
api.AppearanceFetcher.Store(&appearance.DefaultFetcher)
@@ -1265,13 +1275,13 @@ type API struct {
12651275
healthCheckGroup *singleflight.Group[string, *healthsdk.HealthcheckReport]
12661276
healthCheckCache atomic.Pointer[healthsdk.HealthcheckReport]
12671277

1268-
statsBatcher *batchstats.Batcher
1269-
statsCollector workspaceapps.StatsCollector
1278+
statsBatcher *batchstats.Batcher
12701279

12711280
Acquirer *provisionerdserver.Acquirer
12721281
// dbRolluper rolls up template usage stats from raw agent and app
12731282
// stats. This is used to provide insights in the WebUI.
1274-
dbRolluper *dbrollup.Rolluper
1283+
dbRolluper *dbrollup.Rolluper
1284+
workspaceUsageTracker *workspaceusage.Tracker
12751285
}
12761286

12771287
// Close waits for all WebSocket connections to drain before returning.
@@ -1310,6 +1320,7 @@ func (api *API) Close() error {
13101320
_ = (*coordinator).Close()
13111321
}
13121322
_ = api.agentProvider.Close()
1323+
api.workspaceUsageTracker.Close()
13131324
return nil
13141325
}
13151326

coderd/coderdtest/coderdtest.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import (
7171
"github.com/coder/coder/v2/coderd/util/ptr"
7272
"github.com/coder/coder/v2/coderd/workspaceapps"
7373
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
74+
"github.com/coder/coder/v2/coderd/workspaceusage"
7475
"github.com/coder/coder/v2/codersdk"
7576
"github.com/coder/coder/v2/codersdk/agentsdk"
7677
"github.com/coder/coder/v2/codersdk/drpc"
@@ -334,6 +335,12 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can
334335
if options.WorkspaceUsageTrackerTick == nil {
335336
options.WorkspaceUsageTrackerTick = make(chan time.Time, 1) // buffering just in case
336337
}
338+
// Close is called by API.Close()
339+
wuTracker := workspaceusage.New(
340+
options.Database,
341+
workspaceusage.WithLogger(options.Logger.Named("workspace_usage_tracker")),
342+
workspaceusage.WithTickFlush(options.WorkspaceUsageTrackerTick, options.WorkspaceUsageTrackerFlush),
343+
)
337344

338345
var mutex sync.RWMutex
339346
var handler http.Handler
@@ -488,6 +495,7 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can
488495
AllowWorkspaceRenames: options.AllowWorkspaceRenames,
489496
NewTicker: options.NewTicker,
490497
DatabaseRolluper: options.DatabaseRolluper,
498+
WorkspaceUsageTracker: wuTracker,
491499
}
492500
}
493501

coderd/workspaceagents.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
"github.com/coder/coder/v2/coderd/httpmw"
3737
"github.com/coder/coder/v2/coderd/prometheusmetrics"
3838
"github.com/coder/coder/v2/coderd/rbac/policy"
39-
"github.com/coder/coder/v2/coderd/workspaceapps"
39+
"github.com/coder/coder/v2/coderd/schedule"
4040
"github.com/coder/coder/v2/codersdk"
4141
"github.com/coder/coder/v2/codersdk/agentsdk"
4242
"github.com/coder/coder/v2/codersdk/workspacesdk"
@@ -1167,6 +1167,35 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
11671167
slog.F("payload", req),
11681168
)
11691169

1170+
if req.ConnectionCount > 0 {
1171+
var nextAutostart time.Time
1172+
if workspace.AutostartSchedule.String != "" {
1173+
templateSchedule, err := (*(api.TemplateScheduleStore.Load())).Get(ctx, api.Database, workspace.TemplateID)
1174+
// If the template schedule fails to load, just default to bumping without the next transition and log it.
1175+
if err != nil {
1176+
// There's nothing we can do if the query was canceled, the
1177+
// client most likely went away so we just return an internal
1178+
// server error.
1179+
if database.IsQueryCanceledError(err) {
1180+
httpapi.InternalServerError(rw, err)
1181+
return
1182+
}
1183+
api.Logger.Error(ctx, "failed to load template schedule bumping activity, defaulting to bumping by 60min",
1184+
slog.F("workspace_id", workspace.ID),
1185+
slog.F("template_id", workspace.TemplateID),
1186+
slog.Error(err),
1187+
)
1188+
} else {
1189+
next, allowed := schedule.NextAutostart(time.Now(), workspace.AutostartSchedule.String, templateSchedule)
1190+
if allowed {
1191+
nextAutostart = next
1192+
}
1193+
}
1194+
}
1195+
agentapi.ActivityBumpWorkspace(ctx, api.Logger.Named("activity_bump"), api.Database, workspace.ID, nextAutostart)
1196+
}
1197+
1198+
now := dbtime.Now()
11701199
protoStats := &agentproto.Stats{
11711200
ConnectionsByProto: req.ConnectionsByProto,
11721201
ConnectionCount: req.ConnectionCount,
@@ -1213,6 +1242,19 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
12131242
}
12141243
return nil
12151244
})
1245+
if req.SessionCount() > 0 {
1246+
errGroup.Go(func() error {
1247+
// nolint:gocritic // (#13146) Will be moved soon as part of refactor.
1248+
err := api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
1249+
ID: workspace.ID,
1250+
LastUsedAt: now,
1251+
})
1252+
if err != nil {
1253+
return xerrors.Errorf("can't update workspace LastUsedAt: %w", err)
1254+
}
1255+
return nil
1256+
})
1257+
}
12161258
if api.Options.UpdateAgentMetrics != nil {
12171259
errGroup.Go(func() error {
12181260
user, err := api.Database.GetUserByID(ctx, workspace.OwnerID)
@@ -1235,13 +1277,6 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
12351277
return
12361278
}
12371279

1238-
// Flushing the stats collector will update last_used_at,
1239-
// dealine for the workspace, and will publish a workspace update event.
1240-
api.statsCollector.CollectAndFlush(ctx, workspaceapps.StatsReport{
1241-
WorkspaceID: workspace.ID,
1242-
// TODO: fill out
1243-
})
1244-
12451280
httpapi.Write(ctx, rw, http.StatusOK, agentsdk.StatsResponse{
12461281
ReportInterval: api.AgentStatsRefreshInterval,
12471282
})

coderd/workspaces.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/coder/coder/v2/coderd/searchquery"
3030
"github.com/coder/coder/v2/coderd/telemetry"
3131
"github.com/coder/coder/v2/coderd/util/ptr"
32-
"github.com/coder/coder/v2/coderd/workspaceapps"
3332
"github.com/coder/coder/v2/coderd/wsbuilder"
3433
"github.com/coder/coder/v2/codersdk"
3534
"github.com/coder/coder/v2/codersdk/agentsdk"
@@ -1116,10 +1115,7 @@ func (api *API) postWorkspaceUsage(rw http.ResponseWriter, r *http.Request) {
11161115
return
11171116
}
11181117

1119-
api.statsCollector.CollectAndFlush(r.Context(), workspaceapps.StatsReport{
1120-
WorkspaceID: workspace.ID,
1121-
})
1122-
1118+
api.workspaceUsageTracker.Add(workspace.ID)
11231119
rw.WriteHeader(http.StatusNoContent)
11241120
}
11251121

0 commit comments

Comments
 (0)