Skip to content

feat: add deleted_at field to workspace model #7475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 11, 2023
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
22 changes: 21 additions & 1 deletion coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,10 @@ func convertWorkspace(
autostartSchedule = &workspace.AutostartSchedule.String
}

ttlMillis := convertWorkspaceTTLMillis(workspace.Ttl)
var (
ttlMillis = convertWorkspaceTTLMillis(workspace.Ttl)
impendingDeletion = calculateImpendingDeletion(workspace, template)
)
return codersdk.Workspace{
ID: workspace.ID,
CreatedAt: workspace.CreatedAt,
Expand All @@ -1188,6 +1191,7 @@ func convertWorkspace(
AutostartSchedule: autostartSchedule,
TTLMillis: ttlMillis,
LastUsedAt: workspace.LastUsedAt,
ImpendingDeletion: impendingDeletion,
}
}

Expand All @@ -1200,6 +1204,22 @@ func convertWorkspaceTTLMillis(i sql.NullInt64) *int64 {
return &millis
}

// Calculate the time of the upcoming working deletion, if applicable; otherwise, return an empty time.Time struct.
// Workspaces may have impending deletions if InactivityTTL feature is turned on and the workspace is inactive.
func calculateImpendingDeletion(workspace database.Workspace, template database.Template) time.Time {
var (
year, month, day = time.Now().Date()
beginningOfToday = time.Date(year, month, day, 0, 0, 0, 0, time.Now().Location())
)
// If InactivityTTL is turned off (set to 0), if the workspace has already been deleted,
// or if the workspace was used sometime within the last day, there is no impending deletion
if template.InactivityTTL == 0 || workspace.Deleted || workspace.LastUsedAt.After(beginningOfToday) {
return time.Time{}
}

return workspace.LastUsedAt.Add(time.Duration(template.InactivityTTL) * time.Nanosecond)
}

func validWorkspaceTTLMillis(millis *int64, templateDefault, templateMax time.Duration) (sql.NullInt64, error) {
if templateDefault == 0 && templateMax != 0 || (templateMax > 0 && templateDefault > templateMax) {
templateDefault = templateMax
Expand Down
1 change: 1 addition & 0 deletions codersdk/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Workspace struct {
AutostartSchedule *string `json:"autostart_schedule,omitempty"`
TTLMillis *int64 `json:"ttl_ms,omitempty"`
LastUsedAt time.Time `json:"last_used_at" format:"date-time"`
ImpendingDeletion time.Time `json:"impending_deletion" format:"date-time"`
}

type WorkspacesRequest struct {
Expand Down
1 change: 1 addition & 0 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,7 @@ export interface Workspace {
readonly autostart_schedule?: string
readonly ttl_ms?: number
readonly last_used_at: string
readonly impending_deletion: string
}

// From codersdk/workspaceagents.go
Expand Down
1 change: 1 addition & 0 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ export const MockWorkspace: TypesGen.Workspace = {
ttl_ms: 2 * 60 * 60 * 1000,
latest_build: MockWorkspaceBuild,
last_used_at: "2022-05-16T15:29:10.302441433Z",
impending_deletion: "0001-01-01T00:00:00Z",
}

export const MockStoppedWorkspace: TypesGen.Workspace = {
Expand Down