From 48e0afc0cfd96842389b2d63b368a0f1496a7717 Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Mon, 20 Nov 2023 22:49:57 +0000 Subject: [PATCH 1/2] fix: only update last_used_at when connection count > 0 --- coderd/workspaceagents.go | 14 ++++++++------ coderd/workspaceagents_test.go | 27 ++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/coderd/workspaceagents.go b/coderd/workspaceagents.go index 11c9279e36245..38b11c24e80e7 100644 --- a/coderd/workspaceagents.go +++ b/coderd/workspaceagents.go @@ -1703,12 +1703,14 @@ 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 err != nil { - return xerrors.Errorf("can't update workspace LastUsedAt: %w", err) + if req.ConnectionCount > 0 { + 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 }) diff --git a/coderd/workspaceagents_test.go b/coderd/workspaceagents_test.go index e0c4c7f45ee4c..6b4be2d6ea46c 100644 --- a/coderd/workspaceagents_test.go +++ b/coderd/workspaceagents_test.go @@ -856,6 +856,31 @@ 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 0 to assert we aren't updating + // last_used_at. + ConnectionCount: 0, + RxPackets: 1, + RxBytes: 1, + TxPackets: 1, + TxBytes: 1, + SessionCountVSCode: 1, + SessionCountJetBrains: 1, + SessionCountReconnectingPTY: 1, + SessionCountSSH: 1, + 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, @@ -870,7 +895,7 @@ func TestWorkspaceAgentReportStats(t *testing.T) { }) 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, From 8db43d4a208cbeb0d255dbb907fb7b0dff250c50 Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Tue, 21 Nov 2023 23:25:32 +0000 Subject: [PATCH 2/2] use session count --- coderd/workspaceagents.go | 10 +++++----- coderd/workspaceagents_test.go | 21 +++++++++++---------- codersdk/agentsdk/agentsdk.go | 4 ++++ 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/coderd/workspaceagents.go b/coderd/workspaceagents.go index 38b11c24e80e7..9f046490bc5eb 100644 --- a/coderd/workspaceagents.go +++ b/coderd/workspaceagents.go @@ -1702,8 +1702,8 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques } return nil }) - errGroup.Go(func() error { - if req.ConnectionCount > 0 { + if req.SessionCount() > 0 { + errGroup.Go(func() error { err := api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{ ID: workspace.ID, LastUsedAt: now, @@ -1711,9 +1711,9 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques if err != nil { return xerrors.Errorf("can't update workspace LastUsedAt: %w", err) } - } - return nil - }) + 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 6b4be2d6ea46c..e590922e9018b 100644 --- a/coderd/workspaceagents_test.go +++ b/coderd/workspaceagents_test.go @@ -857,17 +857,18 @@ func TestWorkspaceAgentReportStats(t *testing.T) { _, err := agentClient.PostStats(context.Background(), &agentsdk.Stats{ ConnectionsByProto: map[string]int64{"TCP": 1}, - // Set connection count to 0 to assert we aren't updating - // last_used_at. - ConnectionCount: 0, + // 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: 1, - SessionCountJetBrains: 1, - SessionCountReconnectingPTY: 1, - SessionCountSSH: 1, + SessionCountVSCode: 0, + SessionCountJetBrains: 0, + SessionCountReconnectingPTY: 0, + SessionCountSSH: 0, ConnectionMedianLatencyMS: 10, }) require.NoError(t, err) @@ -888,9 +889,9 @@ 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) 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 (