Skip to content

feat: implement deadline bumping from workspace app activity #12916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ func New(options *Options) *API {
options.WorkspaceAppsStatsCollectorOptions.Logger = &named
}
if options.WorkspaceAppsStatsCollectorOptions.Reporter == nil {
options.WorkspaceAppsStatsCollectorOptions.Reporter = workspaceapps.NewStatsDBReporter(options.Database, workspaceapps.DefaultStatsDBReporterBatchSize)
options.WorkspaceAppsStatsCollectorOptions.Reporter = workspaceapps.NewStatsDBReporter(options.Database, options.Logger.Named("stats-reporter"), workspaceapps.DefaultStatsDBReporterBatchSize)
}

api.workspaceAppServer = &workspaceapps.Server{
Expand Down
4 changes: 2 additions & 2 deletions coderd/insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
})
}
}
reporter := workspaceapps.NewStatsDBReporter(db, workspaceapps.DefaultStatsDBReporterBatchSize)
reporter := workspaceapps.NewStatsDBReporter(db, slogtest.Make(t, nil), workspaceapps.DefaultStatsDBReporterBatchSize)
//nolint:gocritic // This is a test.
err = reporter.Report(dbauthz.AsSystemRestricted(ctx), stats)
require.NoError(t, err, "want no error inserting app stats")
Expand Down Expand Up @@ -1631,7 +1631,7 @@ func TestUserActivityInsights_Golden(t *testing.T) {
})
}
}
reporter := workspaceapps.NewStatsDBReporter(db, workspaceapps.DefaultStatsDBReporterBatchSize)
reporter := workspaceapps.NewStatsDBReporter(db, slogtest.Make(t, nil),workspaceapps.DefaultStatsDBReporterBatchSize)
//nolint:gocritic // This is a test.
err = reporter.Report(dbauthz.AsSystemRestricted(ctx), stats)
require.NoError(t, err, "want no error inserting app stats")
Expand Down
2 changes: 1 addition & 1 deletion coderd/prometheusmetrics/insights/metricscollector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestCollectInsights(t *testing.T) {
require.NoError(t, err, "unable to post fake stats")

// Fake app usage
reporter := workspaceapps.NewStatsDBReporter(db, workspaceapps.DefaultStatsDBReporterBatchSize)
reporter := workspaceapps.NewStatsDBReporter(db, slogtest.Make(t, nil), workspaceapps.DefaultStatsDBReporterBatchSize)
refTime := time.Now().Add(-3 * time.Minute).Truncate(time.Minute)
//nolint:gocritic // This is a test.
err = reporter.Report(dbauthz.AsSystemRestricted(context.Background()), []workspaceapps.StatsReport{
Expand Down
18 changes: 17 additions & 1 deletion coderd/workspaceapps/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"golang.org/x/xerrors"

"cdr.dev/slog"
"github.com/coder/coder/v2/coderd/agentapi"

"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
Expand Down Expand Up @@ -60,13 +61,15 @@ var _ StatsReporter = (*StatsDBReporter)(nil)
// StatsDBReporter writes workspace app StatsReports to the database.
type StatsDBReporter struct {
db database.Store
logger slog.Logger
batchSize int
}

// NewStatsDBReporter returns a new StatsDBReporter.
func NewStatsDBReporter(db database.Store, batchSize int) *StatsDBReporter {
func NewStatsDBReporter(db database.Store, logger slog.Logger, batchSize int) *StatsDBReporter {
return &StatsDBReporter{
db: db,
logger: logger,
batchSize: batchSize,
}
}
Expand Down Expand Up @@ -139,6 +142,19 @@ func (r *StatsDBReporter) Report(ctx context.Context, stats []StatsReport) error
return err
}

// This is very inefficient to loop over all workspaces with activity.
// The 'ActivityBumpWorkspace' sql query is not built to be easily turned
// in a batch.
for _, id := range uniqueIDs {
// Passing in a zero time will default bump the workspace by 1hr.
// The correct behavior is to fetch the template settings, and pass
// in the calculated autostart time. But that requires an extra db
// call per workspace.
//
// This function will log any failures.
agentapi.ActivityBumpWorkspace(ctx, r.logger, r.db, id, time.Time{})
}

return nil
}, nil)
if err != nil {
Expand Down
Loading