diff --git a/coderd/workspaceagents.go b/coderd/workspaceagents.go index 11c9279e36245..9f046490bc5eb 100644 --- a/coderd/workspaceagents.go +++ b/coderd/workspaceagents.go @@ -1702,16 +1702,18 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques } return nil }) - errGroup.Go(func() error { - err := api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{ - ID: workspace.ID, - LastUsedAt: now, + if req.SessionCount() > 0 { + errGroup.Go(func() error { + err := api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{ + ID: workspace.ID, + LastUsedAt: now, + }) + if err != nil { + return xerrors.Errorf("can't update workspace LastUsedAt: %w", err) + } + return nil }) - if err != nil { - return xerrors.Errorf("can't update workspace LastUsedAt: %w", err) - } - return nil - }) + } if api.Options.UpdateAgentMetrics != nil { errGroup.Go(func() error { user, err := api.Database.GetUserByID(ctx, workspace.OwnerID) diff --git a/coderd/workspaceagents_test.go b/coderd/workspaceagents_test.go index e0c4c7f45ee4c..e590922e9018b 100644 --- a/coderd/workspaceagents_test.go +++ b/coderd/workspaceagents_test.go @@ -856,6 +856,32 @@ func TestWorkspaceAgentReportStats(t *testing.T) { agentClient.SetSessionToken(authToken) _, err := agentClient.PostStats(context.Background(), &agentsdk.Stats{ + ConnectionsByProto: map[string]int64{"TCP": 1}, + // Set connection count to 1 but all session counts to zero to + // assert we aren't updating last_used_at for a connections that may + // be spawned passively by the dashboard. + ConnectionCount: 1, + RxPackets: 1, + RxBytes: 1, + TxPackets: 1, + TxBytes: 1, + SessionCountVSCode: 0, + SessionCountJetBrains: 0, + SessionCountReconnectingPTY: 0, + SessionCountSSH: 0, + ConnectionMedianLatencyMS: 10, + }) + require.NoError(t, err) + + newWorkspace, err := client.Workspace(context.Background(), ws.ID) + require.NoError(t, err) + + assert.True(t, + newWorkspace.LastUsedAt.Equal(ws.LastUsedAt), + "%s and %s should not differ", newWorkspace.LastUsedAt, ws.LastUsedAt, + ) + + _, err = agentClient.PostStats(context.Background(), &agentsdk.Stats{ ConnectionsByProto: map[string]int64{"TCP": 1}, ConnectionCount: 1, RxPackets: 1, @@ -863,14 +889,14 @@ func TestWorkspaceAgentReportStats(t *testing.T) { TxPackets: 1, TxBytes: 1, SessionCountVSCode: 1, - SessionCountJetBrains: 1, - SessionCountReconnectingPTY: 1, - SessionCountSSH: 1, + SessionCountJetBrains: 0, + SessionCountReconnectingPTY: 0, + SessionCountSSH: 0, ConnectionMedianLatencyMS: 10, }) require.NoError(t, err) - newWorkspace, err := client.Workspace(context.Background(), ws.ID) + newWorkspace, err = client.Workspace(context.Background(), ws.ID) require.NoError(t, err) assert.True(t, diff --git a/codersdk/agentsdk/agentsdk.go b/codersdk/agentsdk/agentsdk.go index 59e4bc9d785b1..bba1f247b295a 100644 --- a/codersdk/agentsdk/agentsdk.go +++ b/codersdk/agentsdk/agentsdk.go @@ -573,6 +573,10 @@ type Stats struct { Metrics []AgentMetric `json:"metrics"` } +func (s Stats) SessionCount() int64 { + return s.SessionCountVSCode + s.SessionCountJetBrains + s.SessionCountReconnectingPTY + s.SessionCountSSH +} + type AgentMetricType string const (