Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add activity bump to StatsDBReporter.Report
  • Loading branch information
f0ssel committed May 21, 2024
commit 64a5aaddc37ef26b75b5ba89dc4c6d365382cd40
42 changes: 21 additions & 21 deletions coderd/agentapi/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,27 @@ func (a *StatsAPI) UpdateStats(ctx context.Context, req *agentproto.UpdateStatsR
)

now := a.now()
if req.Stats.ConnectionCount > 0 {
var nextAutostart time.Time
if workspace.AutostartSchedule.String != "" {
templateSchedule, err := (*(a.TemplateScheduleStore.Load())).Get(ctx, a.Database, workspace.TemplateID)
// If the template schedule fails to load, just default to bumping
// without the next transition and log it.
if err != nil {
a.Log.Error(ctx, "failed to load template schedule bumping activity, defaulting to bumping by 60min",
slog.F("workspace_id", workspace.ID),
slog.F("template_id", workspace.TemplateID),
slog.Error(err),
)
} else {
next, allowed := schedule.NextAutostart(now, workspace.AutostartSchedule.String, templateSchedule)
if allowed {
nextAutostart = next
}
}
}
ActivityBumpWorkspace(ctx, a.Log.Named("activity_bump"), a.Database, workspace.ID, nextAutostart)
}
// if req.Stats.ConnectionCount > 0 {
// var nextAutostart time.Time
// if workspace.AutostartSchedule.String != "" {
// templateSchedule, err := (*(a.TemplateScheduleStore.Load())).Get(ctx, a.Database, workspace.TemplateID)
// // If the template schedule fails to load, just default to bumping
// // without the next transition and log it.
// if err != nil {
// a.Log.Error(ctx, "failed to load template schedule bumping activity, defaulting to bumping by 60min",
// slog.F("workspace_id", workspace.ID),
// slog.F("template_id", workspace.TemplateID),
// slog.Error(err),
// )
// } else {
// next, allowed := schedule.NextAutostart(now, workspace.AutostartSchedule.String, templateSchedule)
// if allowed {
// nextAutostart = next
// }
// }
// }
// ActivityBumpWorkspace(ctx, a.Log.Named("activity_bump"), a.Database, workspace.ID, nextAutostart)
// }

var errGroup errgroup.Group
errGroup.Go(func() error {
Expand Down
49 changes: 47 additions & 2 deletions coderd/workspaceapps/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package workspaceapps
import (
"context"
"sync"
"sync/atomic"
"time"

"github.com/google/uuid"
"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"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/schedule"
"github.com/coder/coder/v2/coderd/util/slice"
)

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

// StatsDBReporter writes workspace app StatsReports to the database.
type StatsDBReporter struct {
db database.Store
batchSize int
db database.Store
logger slog.Logger
templateScheduleStore *atomic.Pointer[schedule.TemplateScheduleStore]
batchSize int
}

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

workspaces, err := tx.GetWorkspaces(ctx, database.GetWorkspacesParams{
WorkspaceIds: uniqueIDs,
})
if err != nil {
return xerrors.Errorf("getting workspaces: %w", err)
}

// TODO: This probably needs batching to handle larger deployments
for _, workspace := range workspaces {
var nextAutostart time.Time
if workspace.AutostartSchedule.String != "" {
templateSchedule, err := (*(r.templateScheduleStore.Load())).Get(ctx, r.db, workspace.TemplateID)
// If the template schedule fails to load, just default to bumping
// without the next transition and log it.
if err != nil {
r.logger.Error(ctx, "failed to load template schedule bumping activity, defaulting to bumping by 60min",
slog.F("workspace_id", workspace.ID),
slog.F("template_id", workspace.TemplateID),
slog.Error(err),
)
} else {
next, allowed := schedule.NextAutostart(dbtime.Now(), workspace.AutostartSchedule.String, templateSchedule)
if allowed {
nextAutostart = next
}
}
}
agentapi.ActivityBumpWorkspace(ctx, r.logger.Named("activity_bump"), r.db, workspace.ID, nextAutostart)
}

return nil
}, nil)
if err != nil {
Expand Down Expand Up @@ -252,6 +287,16 @@ func (sc *StatsCollector) Collect(report StatsReport) {
sc.opts.Logger.Debug(sc.ctx, "collected workspace app stats", slog.F("report", report))
}

func (sc *StatsCollector) CollectAndFlush(ctx context.Context, report StatsReport) error {
sc.Collect(report)
err := sc.flush(ctx)
if err != nil {
return xerrors.Errorf("flushing collector: %w", err)
}

return nil
}

// rollup performs stats rollup for sessions that fall within the
// configured rollup window. For sessions longer than the window,
// we report them individually.
Expand Down