mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: order workspaces by running first (#7656)
* wip * use updated sql * wip * Implement sorting in databasefake.go * More fixes * sql fmt --------- Co-authored-by: Marcin Tojek <marcin@coder.com>
This commit is contained in:
co-authored by
Marcin Tojek
parent
96a2e63809
commit
d9299caa12
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
@@ -1329,6 +1330,63 @@ func (q *fakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.
|
||||
workspaces = append(workspaces, workspace)
|
||||
}
|
||||
|
||||
// Sort workspaces (ORDER BY)
|
||||
isRunning := func(build database.WorkspaceBuild, job database.ProvisionerJob) bool {
|
||||
return job.CompletedAt.Valid && !job.CanceledAt.Valid && !job.Error.Valid && build.Transition == database.WorkspaceTransitionStart
|
||||
}
|
||||
|
||||
preloadedWorkspaceBuilds := map[uuid.UUID]database.WorkspaceBuild{}
|
||||
preloadedProvisionerJobs := map[uuid.UUID]database.ProvisionerJob{}
|
||||
preloadedUsers := map[uuid.UUID]database.User{}
|
||||
|
||||
for _, w := range workspaces {
|
||||
build, err := q.getLatestWorkspaceBuildByWorkspaceIDNoLock(ctx, w.ID)
|
||||
if err == nil {
|
||||
preloadedWorkspaceBuilds[w.ID] = build
|
||||
} else if !errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, xerrors.Errorf("get latest build: %w", err)
|
||||
}
|
||||
|
||||
job, err := q.getProvisionerJobByIDNoLock(ctx, build.JobID)
|
||||
if err == nil {
|
||||
preloadedProvisionerJobs[w.ID] = job
|
||||
} else if !errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, xerrors.Errorf("get provisioner job: %w", err)
|
||||
}
|
||||
|
||||
user, err := q.getUserByIDNoLock(w.OwnerID)
|
||||
if err == nil {
|
||||
preloadedUsers[w.ID] = user
|
||||
} else if !errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, xerrors.Errorf("get user: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(workspaces, func(i, j int) bool {
|
||||
w1 := workspaces[i]
|
||||
w2 := workspaces[j]
|
||||
|
||||
// Order by: running first
|
||||
w1IsRunning := isRunning(preloadedWorkspaceBuilds[w1.ID], preloadedProvisionerJobs[w1.ID])
|
||||
w2IsRunning := isRunning(preloadedWorkspaceBuilds[w2.ID], preloadedProvisionerJobs[w2.ID])
|
||||
|
||||
if w1IsRunning && !w2IsRunning {
|
||||
return true
|
||||
}
|
||||
|
||||
if !w1IsRunning && w2IsRunning {
|
||||
return false
|
||||
}
|
||||
|
||||
// Order by: usernames
|
||||
if w1.ID != w2.ID {
|
||||
return sort.StringsAreSorted([]string{preloadedUsers[w1.ID].Username, preloadedUsers[w2.ID].Username})
|
||||
}
|
||||
|
||||
// Order by: workspace names
|
||||
return sort.StringsAreSorted([]string{w1.Name, w2.Name})
|
||||
})
|
||||
|
||||
beforePageCount := len(workspaces)
|
||||
|
||||
if arg.Offset > 0 {
|
||||
|
||||
@@ -8172,7 +8172,11 @@ const getWorkspaces = `-- name: GetWorkspaces :many
|
||||
SELECT
|
||||
workspaces.id, workspaces.created_at, workspaces.updated_at, workspaces.owner_id, workspaces.organization_id, workspaces.template_id, workspaces.deleted, workspaces.name, workspaces.autostart_schedule, workspaces.ttl, workspaces.last_used_at, COUNT(*) OVER () as count
|
||||
FROM
|
||||
workspaces
|
||||
workspaces
|
||||
JOIN
|
||||
users
|
||||
ON
|
||||
workspaces.owner_id = users.id
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT
|
||||
workspace_builds.transition,
|
||||
@@ -8333,7 +8337,12 @@ WHERE
|
||||
-- Authorize Filter clause will be injected below in GetAuthorizedWorkspaces
|
||||
-- @authorize_filter
|
||||
ORDER BY
|
||||
last_used_at DESC
|
||||
(latest_build.completed_at IS NOT NULL AND
|
||||
latest_build.canceled_at IS NULL AND
|
||||
latest_build.error IS NULL AND
|
||||
latest_build.transition = 'start'::workspace_transition) DESC,
|
||||
LOWER(users.username) ASC,
|
||||
LOWER(name) ASC
|
||||
LIMIT
|
||||
CASE
|
||||
WHEN $11 :: integer > 0 THEN
|
||||
|
||||
@@ -77,7 +77,11 @@ WHERE
|
||||
SELECT
|
||||
workspaces.*, COUNT(*) OVER () as count
|
||||
FROM
|
||||
workspaces
|
||||
workspaces
|
||||
JOIN
|
||||
users
|
||||
ON
|
||||
workspaces.owner_id = users.id
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT
|
||||
workspace_builds.transition,
|
||||
@@ -238,7 +242,12 @@ WHERE
|
||||
-- Authorize Filter clause will be injected below in GetAuthorizedWorkspaces
|
||||
-- @authorize_filter
|
||||
ORDER BY
|
||||
last_used_at DESC
|
||||
(latest_build.completed_at IS NOT NULL AND
|
||||
latest_build.canceled_at IS NULL AND
|
||||
latest_build.error IS NULL AND
|
||||
latest_build.transition = 'start'::workspace_transition) DESC,
|
||||
LOWER(users.username) ASC,
|
||||
LOWER(name) ASC
|
||||
LIMIT
|
||||
CASE
|
||||
WHEN @limit_ :: integer > 0 THEN
|
||||
|
||||
Reference in New Issue
Block a user