Skip to content
Merged
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
Prev Previous commit
Next Next commit
Add API
  • Loading branch information
ammario committed Sep 1, 2022
commit eeebe0ca0bdf33f9d0701abd9b26ab60a2a759b4
10 changes: 10 additions & 0 deletions coderd/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,10 @@ func TestTemplateDAUs(t *testing.T) {
Entries: []codersdk.DAUEntry{},
}, daus, "no DAUs when stats are empty")

workspaces, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{})
require.NoError(t, err)
assert.Zero(t, workspaces[0].LastUsedAt)

conn, err := client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, opts)
require.NoError(t, err)
defer func() {
Expand Down Expand Up @@ -641,4 +645,10 @@ func TestTemplateDAUs(t *testing.T) {
testutil.WaitShort, testutil.IntervalFast,
"got %+v != %+v", daus, want,
)

workspaces, err = client.Workspaces(ctx, codersdk.WorkspaceFilter{})
require.NoError(t, err)
assert.WithinDuration(t,
time.Now(), workspaces[0].LastUsedAt, time.Minute,
)
}
20 changes: 17 additions & 3 deletions coderd/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,18 +830,20 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
// We will see duplicate reports when on idle connections
// (e.g. web terminal left open) or when there are no connections at
// all.
var insert = !reflect.DeepEqual(lastReport, rep)
// We also don't want to update the workspace last used at on duplicate
// reports.
var updateDB = !reflect.DeepEqual(lastReport, rep)

api.Logger.Debug(ctx, "read stats report",
slog.F("interval", api.AgentStatsRefreshInterval),
slog.F("agent", workspaceAgent.ID),
slog.F("resource", resource.ID),
slog.F("workspace", workspace.ID),
slog.F("insert", insert),
slog.F("update_db", updateDB),
slog.F("payload", rep),
)

if insert {
if updateDB {
lastReport = rep

_, err = api.Database.InsertAgentStat(ctx, database.InsertAgentStatParams{
Expand All @@ -860,6 +862,18 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
})
return
}

err = api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
ID: build.WorkspaceID,
LastUsedAt: time.Now(),
})
if err != nil {
httpapi.Write(rw, http.StatusBadRequest, codersdk.Response{
Message: "Failed to update workspace last used at.",
Detail: err.Error(),
})
return
}
}

select {
Expand Down
1 change: 1 addition & 0 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ func convertWorkspace(
Name: workspace.Name,
AutostartSchedule: autostartSchedule,
TTLMillis: ttlMillis,
LastUsedAt: workspace.LastUsedAt,
}
}

Expand Down
1 change: 1 addition & 0 deletions codersdk/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Workspace struct {
Name string `json:"name"`
AutostartSchedule *string `json:"autostart_schedule,omitempty"`
TTLMillis *int64 `json:"ttl_ms,omitempty"`
LastUsedAt time.Time `json:"last_used_at"`
}

// CreateWorkspaceBuildRequest provides options to update the latest workspace build.
Expand Down