Thanks to visit codestin.com
Credit goes to pkg.go.dev

workspacestats

package
v2.21.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2025 License: AGPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFlushInterval = 60 * time.Second

Functions

func ActivityBumpWorkspace

func ActivityBumpWorkspace(ctx context.Context, log slog.Logger, db database.Store, workspaceID uuid.UUID, nextAutostart time.Time)

ActivityBumpWorkspace automatically bumps the workspace's auto-off timer if it is set to expire soon. The deadline will be bumped by 1 hour*. If the bump crosses over an autostart time, the workspace will be bumped by the workspace ttl instead.

If nextAutostart is the zero value or in the past, the workspace will be bumped by 1 hour. It handles the edge case in the example:

  1. Autostart is set to 9am.
  2. User works all day, and leaves a terminal open to the workspace overnight.
  3. The open terminal continually bumps the workspace deadline.
  4. 9am the next day, the activity bump pushes to 10am.
  5. If the user goes inactive for 1 hour during the day, the workspace will now stop, because it has been extended by 1 hour durations. Despite the TTL being set to 8hrs from the autostart time.

So the issue is that when the workspace is bumped across an autostart deadline, we should treat the workspace as being "started" again and extend the deadline by the autostart time + workspace ttl instead.

The issue still remains with build_max_deadline. We need to respect the original maximum deadline, so that will need to be handled separately. A way to avoid this is to configure the max deadline to something that will not span more than 1 day. This will force the workspace to restart and reset the deadline each morning when it autostarts.

func UpdateTemplateWorkspacesLastUsedAt

func UpdateTemplateWorkspacesLastUsedAt(ctx context.Context, db database.Store, templateID uuid.UUID, lastUsedAt time.Time) error

Types

type Batcher added in v2.13.0

type Batcher interface {
	Add(now time.Time, agentID uuid.UUID, templateID uuid.UUID, userID uuid.UUID, workspaceID uuid.UUID, st *agentproto.Stats, usage bool)
}

type BatcherOption added in v2.13.0

type BatcherOption func(b *DBBatcher)

Option is a functional option for configuring a Batcher.

func BatcherWithBatchSize added in v2.13.0

func BatcherWithBatchSize(size int) BatcherOption

BatcherWithBatchSize sets the number of stats to store in a batch.

func BatcherWithInterval added in v2.13.0

func BatcherWithInterval(d time.Duration) BatcherOption

BatcherWithInterval sets the interval for flushes.

func BatcherWithLogger added in v2.13.0

func BatcherWithLogger(log slog.Logger) BatcherOption

BatcherWithLogger sets the logger to use for logging.

func BatcherWithStore added in v2.13.0

func BatcherWithStore(store database.Store) BatcherOption

BatcherWithStore sets the store to use for storing stats.

type DBBatcher added in v2.13.0

type DBBatcher struct {
	// contains filtered or unexported fields
}

DBBatcher holds a buffer of agent stats and periodically flushes them to its configured store.

func NewBatcher added in v2.13.0

func NewBatcher(ctx context.Context, opts ...BatcherOption) (*DBBatcher, func(), error)

NewBatcher creates a new Batcher and starts it.

func (*DBBatcher) Add added in v2.13.0

func (b *DBBatcher) Add(
	now time.Time,
	agentID uuid.UUID,
	templateID uuid.UUID,
	userID uuid.UUID,
	workspaceID uuid.UUID,
	st *agentproto.Stats,
	usage bool,
)

Add adds a stat to the batcher for the given workspace and agent.

type Reporter

type Reporter struct {
	// contains filtered or unexported fields
}

func NewReporter

func NewReporter(opts ReporterOptions) *Reporter

func (*Reporter) Close added in v2.13.0

func (r *Reporter) Close() error

func (*Reporter) ReportAgentStats

func (r *Reporter) ReportAgentStats(ctx context.Context, now time.Time, workspace database.Workspace, workspaceAgent database.WorkspaceAgent, templateName string, stats *agentproto.Stats, usage bool) error

nolint:revive // usage is a control flag while we have the experiment

func (*Reporter) ReportAppStats

func (r *Reporter) ReportAppStats(ctx context.Context, stats []workspaceapps.StatsReport) error

func (*Reporter) TrackUsage added in v2.13.0

func (r *Reporter) TrackUsage(workspaceID uuid.UUID)

type ReporterOptions

type ReporterOptions struct {
	Database              database.Store
	Logger                slog.Logger
	Pubsub                pubsub.Pubsub
	TemplateScheduleStore *atomic.Pointer[schedule.TemplateScheduleStore]
	StatsBatcher          Batcher
	UsageTracker          *UsageTracker
	UpdateAgentMetricsFn  func(ctx context.Context, labels prometheusmetrics.AgentMetricLabels, metrics []*agentproto.Stats_Metric)

	AppStatBatchSize int
}

type Store added in v2.13.0

type Store interface {
	BatchUpdateWorkspaceLastUsedAt(context.Context, database.BatchUpdateWorkspaceLastUsedAtParams) error
}

Store is a subset of database.Store

type TrackerOption added in v2.13.0

type TrackerOption func(*UsageTracker)

func TrackerWithFlushInterval added in v2.13.0

func TrackerWithFlushInterval(d time.Duration) TrackerOption

TrackerWithFlushInterval allows configuring the flush interval of Tracker.

func TrackerWithLogger added in v2.13.0

func TrackerWithLogger(log slog.Logger) TrackerOption

TrackerWithLogger sets the logger to be used by Tracker.

func TrackerWithTickFlush added in v2.13.0

func TrackerWithTickFlush(tickCh <-chan time.Time, flushCh chan int) TrackerOption

TrackerWithTickFlush allows passing two channels: one that reads a time.Time, and one that returns the number of marked workspaces every time Tracker flushes. For testing only and will panic if used outside of tests.

type UpdateTemplateWorkspacesLastUsedAtFunc

type UpdateTemplateWorkspacesLastUsedAtFunc func(ctx context.Context, db database.Store, templateID uuid.UUID, lastUsedAt time.Time) error

type UsageTracker added in v2.13.0

type UsageTracker struct {
	// contains filtered or unexported fields
}

UsageTracker tracks and de-bounces updates to workspace usage activity. It keeps an internal map of workspace IDs that have been used and periodically flushes this to its configured Store.

func NewTracker added in v2.13.0

func NewTracker(s Store, opts ...TrackerOption) *UsageTracker

NewTracker returns a new Tracker. It is the caller's responsibility to call Close().

func (*UsageTracker) Add added in v2.13.0

func (tr *UsageTracker) Add(workspaceID uuid.UUID)

Add marks the workspace with the given ID as having been used recently. Tracker will periodically flush this to its configured Store.

func (*UsageTracker) Close added in v2.13.0

func (tr *UsageTracker) Close() error

Close stops Tracker and returns once Loop has exited. After calling Close(), Loop must not be called.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL