Skip to content

feat(workspaces): change sorting order of the workspace list #7594

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 6 commits into from
May 22, 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
Prev Previous commit
Next Next commit
split slice into its own function and test it
  • Loading branch information
rodrimaia committed May 19, 2023
commit da9de66faa1de8fc89e7db778879fac8c8059a27
15 changes: 10 additions & 5 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,9 +1147,16 @@ func convertWorkspaces(workspaces []database.Workspace, data workspaceData) ([]c
&owner,
))
}
sort.Slice(apiWorkspaces, func(i, j int) bool {
iw := apiWorkspaces[i]
jw := apiWorkspaces[j]

sortWorkspaces(apiWorkspaces)

return apiWorkspaces, nil
}

func sortWorkspaces(workspaces []codersdk.Workspace) {
sort.Slice(workspaces, func(i, j int) bool {
iw := workspaces[i]
jw := workspaces[j]

if iw.LatestBuild.Status == codersdk.WorkspaceStatusRunning && jw.LatestBuild.Status != codersdk.WorkspaceStatusRunning {
return true
Expand All @@ -1168,8 +1175,6 @@ func convertWorkspaces(workspaces []database.Workspace, data workspaceData) ([]c
}
return iw.LastUsedAt.After(jw.LastUsedAt)
})

return apiWorkspaces, nil
}

func convertWorkspace(
Expand Down
106 changes: 106 additions & 0 deletions coderd/workspaces_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package coderd

import (
"fmt"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/coder/coder/coderd/database"
Expand Down Expand Up @@ -80,3 +83,106 @@ func Test_calculateDeletingAt(t *testing.T) {
})
}
}

func TestSortWorkspaces(t *testing.T) {
t.Parallel()

t.Run("Running First", func(t *testing.T) {
t.Parallel()
workspaces := []codersdk.Workspace{
WorkspaceFactory(t, "test-workspace-sort-1", uuid.New(), "user1", codersdk.WorkspaceStatusPending),
WorkspaceFactory(t, "test-workspace-sort-2", uuid.New(), "user2", codersdk.WorkspaceStatusRunning),
WorkspaceFactory(t, "test-workspace-sort-3", uuid.New(), "user2", codersdk.WorkspaceStatusRunning),
WorkspaceFactory(t, "test-workspace-sort-4", uuid.New(), "user1", codersdk.WorkspaceStatusPending),
}

sortWorkspaces(workspaces)

assert.Equal(t, workspaces[0].LatestBuild.Status, codersdk.WorkspaceStatusRunning)
assert.Equal(t, workspaces[1].LatestBuild.Status, codersdk.WorkspaceStatusRunning)
assert.Equal(t, workspaces[2].LatestBuild.Status, codersdk.WorkspaceStatusPending)
assert.Equal(t, workspaces[3].LatestBuild.Status, codersdk.WorkspaceStatusPending)
})

t.Run("Then sort by owner Name", func(t *testing.T) {
t.Parallel()
workspaces := []codersdk.Workspace{
WorkspaceFactory(t, "test-workspace-sort-1", uuid.New(), "userZ", codersdk.WorkspaceStatusRunning),
WorkspaceFactory(t, "test-workspace-sort-2", uuid.New(), "userA", codersdk.WorkspaceStatusRunning),
WorkspaceFactory(t, "test-workspace-sort-3", uuid.New(), "userB", codersdk.WorkspaceStatusRunning),
}

sortWorkspaces(workspaces)

t.Log("uuid ", uuid.New().String())
t.Log("uuid ", uuid.New().String())

assert.Equal(t, "userA", workspaces[0].OwnerName)
assert.Equal(t, "userB", workspaces[1].OwnerName)
assert.Equal(t, "userZ", workspaces[2].OwnerName)
})

t.Run("Then sort by last used at (recent first)", func(t *testing.T) {
t.Parallel()
var workspaces []codersdk.Workspace

useruuid := uuid.New()

for i := 0; i < 4; i++ {
workspaces = append(workspaces, WorkspaceFactory(t, fmt.Sprintf("test-workspace-sort-%d", i+1), useruuid, "user2", codersdk.WorkspaceStatusRunning))
}

sortWorkspaces(workspaces)

// in this case, the last used at is the creation time
assert.Equal(t, workspaces[0].Name, "test-workspace-sort-4")
assert.Equal(t, workspaces[1].Name, "test-workspace-sort-3")
assert.Equal(t, workspaces[2].Name, "test-workspace-sort-2")
assert.Equal(t, workspaces[3].Name, "test-workspace-sort-1")
})
}

func WorkspaceFactory(t *testing.T, name string, ownerID uuid.UUID, ownerName string, status codersdk.WorkspaceStatus) codersdk.Workspace {
t.Helper()
return codersdk.Workspace{
ID: uuid.New(),
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
OwnerID: ownerID,
OwnerName: ownerName,
OrganizationID: [16]byte{},
TemplateID: [16]byte{},
TemplateName: name,
TemplateDisplayName: name,
TemplateIcon: "",
TemplateAllowUserCancelWorkspaceJobs: false,
LatestBuild: codersdk.WorkspaceBuild{
ID: uuid.New(),
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
WorkspaceID: [16]byte{},
WorkspaceName: name,
WorkspaceOwnerID: [16]byte{},
WorkspaceOwnerName: ownerName,
TemplateVersionID: [16]byte{},
TemplateVersionName: name,
BuildNumber: 0,
Transition: "",
InitiatorID: [16]byte{},
InitiatorUsername: name,
Job: codersdk.ProvisionerJob{},
Reason: "",
Resources: []codersdk.WorkspaceResource{},
Deadline: codersdk.NullTime{},
MaxDeadline: codersdk.NullTime{},
Status: status,
DailyCost: 0,
},
Outdated: false,
Name: name,
AutostartSchedule: new(string),
TTLMillis: new(int64),
LastUsedAt: time.Now(),
DeletingAt: &time.Time{},
}
}
4 changes: 4 additions & 0 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ func TestWorkspacesSortOrder(t *testing.T) {
require.Equal(t, codersdk.WorkspaceStatusStopped, workspacesResponse.Workspaces[2].LatestBuild.Status, "The stopped one should be last")

require.Equal(t, workspace3.ID, workspacesResponse.Workspaces[0].ID, "If both are running, and have the same owner, sort by last used (in this case, the last one created)")
<<<<<<< Updated upstream
=======

>>>>>>> Stashed changes
}

func TestPostWorkspacesByOrganization(t *testing.T) {
Expand Down