Skip to content

feat: order workspaces by running first #7656

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 25, 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
More fixes
  • Loading branch information
mtojek committed May 25, 2023
commit eee8c982baa150ba11e377ec70e48a4893fc1d1f
16 changes: 10 additions & 6 deletions coderd/database/dbfake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"reflect"
"regexp"
Expand Down Expand Up @@ -1340,22 +1341,25 @@ func (q *fakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.

for _, w := range workspaces {
build, err := q.getLatestWorkspaceBuildByWorkspaceIDNoLock(ctx, w.ID)
if err != nil {
if err == nil {
preloadedWorkspaceBuilds[w.ID] = build
} else if !errors.Is(err, sql.ErrNoRows) {
return nil, xerrors.Errorf("get latest build: %w", err)
}
preloadedWorkspaceBuilds[w.ID] = build

job, err := q.getProvisionerJobByIDNoLock(ctx, build.JobID)
if err != nil {
if err == nil {
preloadedProvisionerJobs[w.ID] = job
} else if !errors.Is(err, sql.ErrNoRows) {
return nil, xerrors.Errorf("get provisioner job: %w", err)
}
preloadedProvisionerJobs[w.ID] = job

user, err := q.getUserByIDNoLock(w.OwnerID)
if err != nil {
if err == nil {
preloadedUsers[w.ID] = user
} else if !errors.Is(err, sql.ErrNoRows) {
return nil, xerrors.Errorf("get user: %w", err)
}
preloadedUsers[w.ID] = user
}

sort.Slice(workspaces, func(i, j int) bool {
Expand Down
23 changes: 15 additions & 8 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,13 @@ func TestWorkspacesSortOrder(t *testing.T) {
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, firstUser.OrganizationID, version.ID)

// Workspace 1 should be running
// c-workspace should be running
workspace1 := coderdtest.CreateWorkspace(t, client, firstUser.OrganizationID, template.ID, func(ctr *codersdk.CreateWorkspaceRequest) {
ctr.Name = "c-workspace"
})
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace1.LatestBuild.ID)

// Workspace 2 should be stopped
// b-workspace should be stopped
workspace2 := coderdtest.CreateWorkspace(t, client, firstUser.OrganizationID, template.ID, func(ctr *codersdk.CreateWorkspaceRequest) {
ctr.Name = "b-workspace"
})
Expand All @@ -222,27 +222,34 @@ func TestWorkspacesSortOrder(t *testing.T) {
build2 := coderdtest.CreateWorkspaceBuild(t, client, workspace2, database.WorkspaceTransitionStop)
coderdtest.AwaitWorkspaceBuildJob(t, client, build2.ID)

// Workspace 3 should be running
// a-workspace should be running
workspace3 := coderdtest.CreateWorkspace(t, client, firstUser.OrganizationID, template.ID, func(ctr *codersdk.CreateWorkspaceRequest) {
ctr.Name = "a-workspace"
})
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace3.LatestBuild.ID)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

workspacesResponse, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{})
require.NoError(t, err, "(first) fetch workspaces")
workspaces := workspacesResponse.Workspaces

expected := []string{
workspace3.Name,
workspace1.Name,
workspace2.Name,
}

var actual []string
for _, w := range workspaces {
actual = append(actual, w.Name)
}

// the correct sorting order is:
// 1. Running workspaces
// 2. Sort by usernames
// 3. Sort by workspace names
require.Equal(t, 3, workspacesResponse.Count)
require.Equal(t, workspace3.Name, workspaces[0].Name)
require.Equal(t, workspace1.Name, workspaces[1].Name)
require.Equal(t, workspace2.Name, workspaces[2].Name)
require.Equal(t, expected, actual)
}

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