Skip to content

feat: paginate workspaces page #4647

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 44 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
770e473
Start - still needs api call changes
presleyp Oct 14, 2022
13256ce
Some xservice changes
presleyp Oct 14, 2022
76071b9
Finish adding count to xservice
presleyp Oct 14, 2022
bdb0614
Mock out api call on frontend
presleyp Oct 14, 2022
86caa80
Handle errors
presleyp Oct 14, 2022
9317155
Doctor getWorkspaces
presleyp Oct 14, 2022
2a8c1b3
Add types, start writing count function
presleyp Oct 14, 2022
f501786
Hook up route
presleyp Oct 14, 2022
fbcfa36
Use empty page struct
presleyp Oct 17, 2022
939dcdc
Write interface and database fake
presleyp Oct 17, 2022
1b142b4
SQL query
presleyp Oct 17, 2022
ea9f240
Fix params type
presleyp Oct 17, 2022
09791c7
Missed a spot
presleyp Oct 17, 2022
32168a5
Space after alert banner
presleyp Oct 18, 2022
4d8e565
Fix model queries
presleyp Oct 18, 2022
5eea639
Unpack query correctly
presleyp Oct 18, 2022
ef7f59d
Fix filter-page interaction
presleyp Oct 18, 2022
eae13a2
Make mobile friendly
presleyp Oct 18, 2022
b1ab93f
Format
presleyp Oct 18, 2022
fdf74aa
Test backend
presleyp Oct 18, 2022
7b6e822
Fix key
presleyp Oct 18, 2022
644f305
Delete unnecessary conditional
presleyp Oct 18, 2022
8502b05
Add test helpers
presleyp Oct 19, 2022
d838789
Use limit constant
presleyp Oct 19, 2022
2efe49b
Show widget with no count
presleyp Oct 19, 2022
950ac50
Add test
presleyp Oct 19, 2022
296281d
Format
presleyp Oct 19, 2022
e091841
Merge branch 'main' into paginate-ws/presleyp
presleyp Oct 19, 2022
78c231d
make gen from garretts workspace idk why
f0ssel Oct 19, 2022
fc5df6c
fix authorize test'
f0ssel Oct 19, 2022
bb2f0f3
Hide widget with 0 records
presleyp Oct 19, 2022
6174b4f
Fix tests
presleyp Oct 19, 2022
3ab3505
Format
presleyp Oct 19, 2022
a20827c
Fix types generated
presleyp Oct 19, 2022
c06765b
Fix story
presleyp Oct 19, 2022
6bd9683
Add alert banner story
presleyp Oct 19, 2022
de2ed63
Format
presleyp Oct 19, 2022
ff8cb81
Fix import
presleyp Oct 19, 2022
e609e5a
Format
presleyp Oct 19, 2022
476019b
Try removing story
presleyp Oct 19, 2022
833c1af
Merge branch 'main' into paginate-ws/presleyp
presleyp Oct 20, 2022
1f62974
Revert "Fix story"
presleyp Oct 20, 2022
e182c19
Add counts to page view story
presleyp Oct 20, 2022
8bd9afa
Revert "Try removing story"
presleyp Oct 20, 2022
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
1 change: 1 addition & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ func New(options *Options) *API {
apiKeyMiddleware,
)
r.Get("/", api.workspaces)
r.Get("/count", api.workspaceCount)
r.Route("/{workspace}", func(r chi.Router) {
r.Use(
httpmw.ExtractWorkspaceParam(options.Database),
Expand Down
150 changes: 150 additions & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,156 @@ func (q *fakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.
return workspaces, nil
}

func (q *fakeQuerier) GetWorkspaceCount(ctx context.Context, arg database.GetWorkspaceCountParams) (int64, error) {
count, err := q.GetAuthorizedWorkspaceCount(ctx, arg, nil)
return count, err
}

//nolint:gocyclo
func (q *fakeQuerier) GetAuthorizedWorkspaceCount(ctx context.Context, arg database.GetWorkspaceCountParams, authorizedFilter rbac.AuthorizeFilter) (int64, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

workspaces := make([]database.Workspace, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably save some code by just converting this from database.GetWorkspaceCountParams to database.GetWorkspaceParams and then calling q.GetAuthorizedWorkspace but completely optional.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this!

for _, workspace := range q.workspaces {
if arg.OwnerID != uuid.Nil && workspace.OwnerID != arg.OwnerID {
continue
}

if arg.OwnerUsername != "" {
owner, err := q.GetUserByID(ctx, workspace.OwnerID)
if err == nil && !strings.EqualFold(arg.OwnerUsername, owner.Username) {
continue
}
}

if arg.TemplateName != "" {
template, err := q.GetTemplateByID(ctx, workspace.TemplateID)
if err == nil && !strings.EqualFold(arg.TemplateName, template.Name) {
continue
}
}

if !arg.Deleted && workspace.Deleted {
continue
}

if arg.Name != "" && !strings.Contains(strings.ToLower(workspace.Name), strings.ToLower(arg.Name)) {
continue
}

if arg.Status != "" {
build, err := q.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspace.ID)
if err != nil {
return 0, xerrors.Errorf("get latest build: %w", err)
}

job, err := q.GetProvisionerJobByID(ctx, build.JobID)
if err != nil {
return 0, xerrors.Errorf("get provisioner job: %w", err)
}

switch arg.Status {
case "pending":
if !job.StartedAt.Valid {
continue
}

case "starting":
if !job.StartedAt.Valid &&
!job.CanceledAt.Valid &&
job.CompletedAt.Valid &&
time.Since(job.UpdatedAt) > 30*time.Second ||
build.Transition != database.WorkspaceTransitionStart {
continue
}

case "running":
if !job.CompletedAt.Valid &&
job.CanceledAt.Valid &&
job.Error.Valid ||
build.Transition != database.WorkspaceTransitionStart {
continue
}

case "stopping":
if !job.StartedAt.Valid &&
!job.CanceledAt.Valid &&
job.CompletedAt.Valid &&
time.Since(job.UpdatedAt) > 30*time.Second ||
build.Transition != database.WorkspaceTransitionStop {
continue
}

case "stopped":
if !job.CompletedAt.Valid &&
job.CanceledAt.Valid &&
job.Error.Valid ||
build.Transition != database.WorkspaceTransitionStop {
continue
}

case "failed":
if (!job.CanceledAt.Valid && !job.Error.Valid) ||
(!job.CompletedAt.Valid && !job.Error.Valid) {
continue
}

case "canceling":
if !job.CanceledAt.Valid && job.CompletedAt.Valid {
continue
}

case "canceled":
if !job.CanceledAt.Valid && !job.CompletedAt.Valid {
continue
}

case "deleted":
if !job.StartedAt.Valid &&
job.CanceledAt.Valid &&
!job.CompletedAt.Valid &&
time.Since(job.UpdatedAt) > 30*time.Second ||
build.Transition != database.WorkspaceTransitionDelete {
continue
}

case "deleting":
if !job.CompletedAt.Valid &&
job.CanceledAt.Valid &&
job.Error.Valid &&
build.Transition != database.WorkspaceTransitionDelete {
continue
}

default:
return 0, xerrors.Errorf("unknown workspace status in filter: %q", arg.Status)
}
}

if len(arg.TemplateIds) > 0 {
match := false
for _, id := range arg.TemplateIds {
if workspace.TemplateID == id {
match = true
break
}
}
if !match {
continue
}
}

// If the filter exists, ensure the object is authorized.
if authorizedFilter != nil && !authorizedFilter.Eval(workspace.RBACObject()) {
continue
}
workspaces = append(workspaces, workspace)
}

return int64(len(workspaces)), nil
}

func (q *fakeQuerier) GetWorkspaceByID(_ context.Context, id uuid.UUID) (database.Workspace, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
Expand Down
21 changes: 21 additions & 0 deletions coderd/database/modelqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func (q *sqlQuerier) GetTemplateGroupRoles(ctx context.Context, id uuid.UUID) ([

type workspaceQuerier interface {
GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspacesParams, authorizedFilter rbac.AuthorizeFilter) ([]Workspace, error)
GetAuthorizedWorkspaceCount(ctx context.Context, arg GetWorkspaceCountParams, authorizedFilter rbac.AuthorizeFilter) (int64, error)
}

// GetAuthorizedWorkspaces returns all workspaces that the user is authorized to access.
Expand Down Expand Up @@ -213,3 +214,23 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
}
return items, nil
}

func (q *sqlQuerier) GetAuthorizedWorkspaceCount(ctx context.Context, arg GetWorkspaceCountParams, authorizedFilter rbac.AuthorizeFilter) (int64, error) {
// In order to properly use ORDER BY, OFFSET, and LIMIT, we need to inject the
// authorizedFilter between the end of the where clause and those statements.
filter := strings.Replace(getWorkspaceCount, "-- @authorize_filter", fmt.Sprintf(" AND %s", authorizedFilter.SQLString(rbac.NoACLConfig())), 1)
// The name comment is for metric tracking
query := fmt.Sprintf("-- name: GetAuthorizedWorkspaceCount :one\n%s", filter)
row := q.db.QueryRowContext(ctx, query,
arg.Deleted,
arg.Status,
arg.OwnerID,
arg.OwnerUsername,
arg.TemplateName,
pq.Array(arg.TemplateIds),
arg.Name,
)
var count int64
err := row.Scan(&count)
return count, err
}
129 changes: 129 additions & 0 deletions coderd/database/queries/workspaces.sql
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,135 @@ OFFSET
@offset_
;

-- this duplicates the filtering in GetWorkspaces
-- name: GetWorkspaceCount :one
Comment on lines +148 to +149
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left an ad-hoc comment on the commit, but I think this is quite bug-prone.

It's not extremely obvious that you have to change this when introducing a new filter type, which could lead to a mismatch with the workspaces and count.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Colin thought this was the best thing for now but we have an issue to clean it up #4604

SELECT
COUNT(*) as count
FROM
workspaces
LEFT JOIN LATERAL (
SELECT
workspace_builds.transition,
provisioner_jobs.started_at,
provisioner_jobs.updated_at,
provisioner_jobs.canceled_at,
provisioner_jobs.completed_at,
provisioner_jobs.error
FROM
workspace_builds
LEFT JOIN
provisioner_jobs
ON
provisioner_jobs.id = workspace_builds.job_id
WHERE
workspace_builds.workspace_id = workspaces.id
ORDER BY
build_number DESC
LIMIT
1
) latest_build ON TRUE
WHERE
-- Optionally include deleted workspaces
workspaces.deleted = @deleted
AND CASE
WHEN @status :: text != '' THEN
CASE
WHEN @status = 'pending' THEN
latest_build.started_at IS NULL
WHEN @status = 'starting' THEN
latest_build.started_at IS NOT NULL AND
latest_build.canceled_at IS NULL AND
latest_build.completed_at IS NULL AND
latest_build.updated_at - INTERVAL '30 seconds' < NOW() AND
latest_build.transition = 'start'::workspace_transition

WHEN @status = 'running' THEN
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

WHEN @status = 'stopping' THEN
latest_build.started_at IS NOT NULL AND
latest_build.canceled_at IS NULL AND
latest_build.completed_at IS NULL AND
latest_build.updated_at - INTERVAL '30 seconds' < NOW() AND
latest_build.transition = 'stop'::workspace_transition

WHEN @status = 'stopped' THEN
latest_build.completed_at IS NOT NULL AND
latest_build.canceled_at IS NULL AND
latest_build.error IS NULL AND
latest_build.transition = 'stop'::workspace_transition

WHEN @status = 'failed' THEN
(latest_build.canceled_at IS NOT NULL AND
latest_build.error IS NOT NULL) OR
(latest_build.completed_at IS NOT NULL AND
latest_build.error IS NOT NULL)

WHEN @status = 'canceling' THEN
latest_build.canceled_at IS NOT NULL AND
latest_build.completed_at IS NULL

WHEN @status = 'canceled' THEN
latest_build.canceled_at IS NOT NULL AND
latest_build.completed_at IS NOT NULL

WHEN @status = 'deleted' THEN
latest_build.started_at IS NOT NULL AND
latest_build.canceled_at IS NULL AND
latest_build.completed_at IS NOT NULL AND
latest_build.updated_at - INTERVAL '30 seconds' < NOW() AND
latest_build.transition = 'delete'::workspace_transition

WHEN @status = 'deleting' THEN
latest_build.completed_at IS NOT NULL AND
latest_build.canceled_at IS NULL AND
latest_build.error IS NULL AND
latest_build.transition = 'delete'::workspace_transition

ELSE
true
END
ELSE true
END
-- Filter by owner_id
AND CASE
WHEN @owner_id :: uuid != '00000000-00000000-00000000-00000000' THEN
owner_id = @owner_id
ELSE true
END
-- Filter by owner_name
AND CASE
WHEN @owner_username :: text != '' THEN
owner_id = (SELECT id FROM users WHERE lower(username) = lower(@owner_username) AND deleted = false)
ELSE true
END
-- Filter by template_name
-- There can be more than 1 template with the same name across organizations.
-- Use the organization filter to restrict to 1 org if needed.
AND CASE
WHEN @template_name :: text != '' THEN
template_id = ANY(SELECT id FROM templates WHERE lower(name) = lower(@template_name) AND deleted = false)
ELSE true
END
-- Filter by template_ids
AND CASE
WHEN array_length(@template_ids :: uuid[], 1) > 0 THEN
template_id = ANY(@template_ids)
ELSE true
END
-- Filter by name, matching on substring
AND CASE
WHEN @name :: text != '' THEN
name ILIKE '%' || @name || '%'
ELSE true
END
-- Authorize Filter clause will be injected below in GetAuthorizedWorkspaceCount
-- @authorize_filter
;

-- name: GetWorkspaceByOwnerIDAndName :one
SELECT
*
Expand Down
52 changes: 52 additions & 0 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,58 @@ func (api *API) workspaces(rw http.ResponseWriter, r *http.Request) {
httpapi.Write(ctx, rw, http.StatusOK, wss)
}

func (api *API) workspaceCount(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
apiKey := httpmw.APIKey(r)

queryStr := r.URL.Query().Get("q")
filter, errs := workspaceSearchQuery(queryStr, codersdk.Pagination{})
if len(errs) > 0 {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid audit search query.",
Validations: errs,
})
return
}

if filter.OwnerUsername == "me" {
filter.OwnerID = apiKey.UserID
filter.OwnerUsername = ""
}

sqlFilter, err := api.HTTPAuth.AuthorizeSQLFilter(r, rbac.ActionRead, rbac.ResourceWorkspace.Type)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error preparing sql filter.",
Detail: err.Error(),
})
return
}

countFilter := database.GetWorkspaceCountParams{
Deleted: filter.Deleted,
OwnerUsername: filter.OwnerUsername,
OwnerID: filter.OwnerID,
Name: filter.Name,
Status: filter.Status,
TemplateIds: filter.TemplateIds,
TemplateName: filter.TemplateName,
}

count, err := api.Database.GetAuthorizedWorkspaceCount(ctx, countFilter, sqlFilter)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching workspace count.",
Detail: err.Error(),
})
return
}

httpapi.Write(ctx, rw, http.StatusOK, codersdk.WorkspaceCountResponse{
Count: count,
})
}

func (api *API) workspaceByOwnerAndName(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
owner := httpmw.UserParam(r)
Expand Down
Loading