Skip to content

Commit dc818d7

Browse files
committed
remove workspace tracker
1 parent 0a8d85c commit dc818d7

File tree

6 files changed

+17
-527
lines changed

6 files changed

+17
-527
lines changed

coderd/coderd.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ 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"
7271
"github.com/coder/coder/v2/codersdk"
7372
"github.com/coder/coder/v2/codersdk/drpc"
7473
"github.com/coder/coder/v2/codersdk/healthsdk"
@@ -204,8 +203,6 @@ type Options struct {
204203
// DatabaseRolluper rolls up template usage stats from raw agent and app
205204
// stats. This is used to provide insights in the WebUI.
206205
DatabaseRolluper *dbrollup.Rolluper
207-
// WorkspaceUsageTracker tracks workspace usage by the CLI.
208-
WorkspaceUsageTracker *workspaceusage.Tracker
209206
}
210207

211208
// @title Coder API
@@ -382,12 +379,6 @@ func New(options *Options) *API {
382379
options.DatabaseRolluper = dbrollup.New(options.Logger.Named("dbrollup"), options.Database)
383380
}
384381

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

@@ -432,8 +423,7 @@ func New(options *Options) *API {
432423
options.Database,
433424
options.Pubsub,
434425
),
435-
dbRolluper: options.DatabaseRolluper,
436-
workspaceUsageTracker: options.WorkspaceUsageTracker,
426+
dbRolluper: options.DatabaseRolluper,
437427
}
438428

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

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

12801271
Acquirer *provisionerdserver.Acquirer
12811272
// dbRolluper rolls up template usage stats from raw agent and app
12821273
// stats. This is used to provide insights in the WebUI.
1283-
dbRolluper *dbrollup.Rolluper
1284-
workspaceUsageTracker *workspaceusage.Tracker
1274+
dbRolluper *dbrollup.Rolluper
12851275
}
12861276

12871277
// Close waits for all WebSocket connections to drain before returning.
@@ -1320,7 +1310,6 @@ func (api *API) Close() error {
13201310
_ = (*coordinator).Close()
13211311
}
13221312
_ = api.agentProvider.Close()
1323-
api.workspaceUsageTracker.Close()
13241313
return nil
13251314
}
13261315

coderd/coderdtest/coderdtest.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ 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"
7574
"github.com/coder/coder/v2/codersdk"
7675
"github.com/coder/coder/v2/codersdk/agentsdk"
7776
"github.com/coder/coder/v2/codersdk/drpc"
@@ -335,12 +334,6 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can
335334
if options.WorkspaceUsageTrackerTick == nil {
336335
options.WorkspaceUsageTrackerTick = make(chan time.Time, 1) // buffering just in case
337336
}
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-
)
344337

345338
var mutex sync.RWMutex
346339
var handler http.Handler
@@ -495,7 +488,6 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can
495488
AllowWorkspaceRenames: options.AllowWorkspaceRenames,
496489
NewTicker: options.NewTicker,
497490
DatabaseRolluper: options.DatabaseRolluper,
498-
WorkspaceUsageTracker: wuTracker,
499491
}
500492
}
501493

coderd/workspaceagents.go

Lines changed: 8 additions & 43 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/schedule"
39+
"github.com/coder/coder/v2/coderd/workspaceapps"
4040
"github.com/coder/coder/v2/codersdk"
4141
"github.com/coder/coder/v2/codersdk/agentsdk"
4242
"github.com/coder/coder/v2/codersdk/workspacesdk"
@@ -1167,35 +1167,6 @@ 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()
11991170
protoStats := &agentproto.Stats{
12001171
ConnectionsByProto: req.ConnectionsByProto,
12011172
ConnectionCount: req.ConnectionCount,
@@ -1242,19 +1213,6 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
12421213
}
12431214
return nil
12441215
})
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-
}
12581216
if api.Options.UpdateAgentMetrics != nil {
12591217
errGroup.Go(func() error {
12601218
user, err := api.Database.GetUserByID(ctx, workspace.OwnerID)
@@ -1277,6 +1235,13 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
12771235
return
12781236
}
12791237

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+
12801245
httpapi.Write(ctx, rw, http.StatusOK, agentsdk.StatsResponse{
12811246
ReportInterval: api.AgentStatsRefreshInterval,
12821247
})

coderd/workspaces.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ 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"
3233
"github.com/coder/coder/v2/coderd/wsbuilder"
3334
"github.com/coder/coder/v2/codersdk"
3435
"github.com/coder/coder/v2/codersdk/agentsdk"
@@ -1115,7 +1116,10 @@ func (api *API) postWorkspaceUsage(rw http.ResponseWriter, r *http.Request) {
11151116
return
11161117
}
11171118

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy