mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add paginated API endpoint for groups (#27603)
backend-only changes from #27271; see that PR for summary of changes + implementation details
This commit is contained in:
@@ -377,6 +377,24 @@ func Group(row database.GetGroupsRow, members []database.GroupMember, totalMembe
|
||||
}
|
||||
}
|
||||
|
||||
// PaginatedGroup converts a group row into the slim summary returned by the
|
||||
// paginated groups endpoint, which omits the member roster and carries only
|
||||
// the total member count.
|
||||
func PaginatedGroup(row database.GetGroupsRow, totalMemberCount int) codersdk.PaginatedGroup {
|
||||
return codersdk.PaginatedGroup{
|
||||
ID: row.Group.ID,
|
||||
Name: row.Group.Name,
|
||||
DisplayName: row.Group.DisplayName,
|
||||
OrganizationID: row.Group.OrganizationID,
|
||||
AvatarURL: row.Group.AvatarURL,
|
||||
TotalMemberCount: totalMemberCount,
|
||||
QuotaAllowance: int(row.Group.QuotaAllowance),
|
||||
Source: codersdk.GroupSource(row.Group.Source),
|
||||
OrganizationName: row.OrganizationName,
|
||||
OrganizationDisplayName: row.OrganizationDisplayName,
|
||||
}
|
||||
}
|
||||
|
||||
func TemplateInsightsParameters(parameterRows []database.GetTemplateParameterInsightsRow) ([]codersdk.TemplateParameterUsage, error) {
|
||||
// Use a stable sort, similarly to how we would sort in the query, note that
|
||||
// we don't sort in the query because order varies depending on the table
|
||||
|
||||
@@ -3901,6 +3901,17 @@ func (q *querier) GetGroups(ctx context.Context, arg database.GetGroupsParams) (
|
||||
return fetchWithPostFilter(q.auth, policy.ActionRead, q.db.GetGroups)(ctx, arg)
|
||||
}
|
||||
|
||||
func (q *querier) GetGroupsByOrganizationIDPaginated(ctx context.Context, arg database.GetGroupsByOrganizationIDPaginatedParams) ([]database.GetGroupsByOrganizationIDPaginatedRow, error) {
|
||||
// Required to have permission to read all groups in the organization. This
|
||||
// mirrors PaginatedOrganizationMembers: a single org-wide read check with no
|
||||
// per-row post-filter, so that SQL LIMIT/OFFSET and COUNT(*) OVER() stay
|
||||
// consistent across pages.
|
||||
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceGroup.InOrg(arg.OrganizationID)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return q.db.GetGroupsByOrganizationIDPaginated(ctx, arg)
|
||||
}
|
||||
|
||||
func (q *querier) GetHealthSettings(ctx context.Context) (string, error) {
|
||||
// No authz checks
|
||||
return q.db.GetHealthSettings(ctx)
|
||||
|
||||
@@ -2353,6 +2353,19 @@ func (s *MethodTestSuite) TestOrganization() {
|
||||
|
||||
check.Args(arg).Asserts(mem, policy.ActionRead)
|
||||
}))
|
||||
s.Run("GetGroupsByOrganizationIDPaginated", s.Mocked(func(dbm *dbmock.MockStore, faker *gofakeit.Faker, check *expects) {
|
||||
o := testutil.Fake(s.T(), faker, database.Organization{})
|
||||
g := testutil.Fake(s.T(), faker, database.Group{OrganizationID: o.ID})
|
||||
arg := database.GetGroupsByOrganizationIDPaginatedParams{OrganizationID: o.ID, LimitOpt: 0}
|
||||
rows := []database.GetGroupsByOrganizationIDPaginatedRow{{
|
||||
Group: g,
|
||||
OrganizationName: o.Name,
|
||||
OrganizationDisplayName: o.DisplayName,
|
||||
Count: 1,
|
||||
}}
|
||||
dbm.EXPECT().GetGroupsByOrganizationIDPaginated(gomock.Any(), arg).Return(rows, nil).AnyTimes()
|
||||
check.Args(arg).Asserts(rbac.ResourceGroup.InOrg(o.ID), policy.ActionRead).Returns(rows)
|
||||
}))
|
||||
s.Run("PaginatedOrganizationMembers", s.Mocked(func(dbm *dbmock.MockStore, faker *gofakeit.Faker, check *expects) {
|
||||
o := testutil.Fake(s.T(), faker, database.Organization{})
|
||||
u := testutil.Fake(s.T(), faker, database.User{})
|
||||
|
||||
+8
@@ -2153,6 +2153,14 @@ func (m queryMetricsStore) GetGroups(ctx context.Context, arg database.GetGroups
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m queryMetricsStore) GetGroupsByOrganizationIDPaginated(ctx context.Context, arg database.GetGroupsByOrganizationIDPaginatedParams) ([]database.GetGroupsByOrganizationIDPaginatedRow, error) {
|
||||
start := time.Now()
|
||||
r0, r1 := m.s.GetGroupsByOrganizationIDPaginated(ctx, arg)
|
||||
m.queryLatencies.WithLabelValues("GetGroupsByOrganizationIDPaginated").Observe(time.Since(start).Seconds())
|
||||
m.queryCounts.WithLabelValues(httpmw.ExtractHTTPRoute(ctx), httpmw.ExtractHTTPMethod(ctx), "GetGroupsByOrganizationIDPaginated").Inc()
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m queryMetricsStore) GetHealthSettings(ctx context.Context) (string, error) {
|
||||
start := time.Now()
|
||||
r0, r1 := m.s.GetHealthSettings(ctx)
|
||||
|
||||
Generated
+15
@@ -3990,6 +3990,21 @@ func (mr *MockStoreMockRecorder) GetGroups(ctx, arg any) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetGroups", reflect.TypeOf((*MockStore)(nil).GetGroups), ctx, arg)
|
||||
}
|
||||
|
||||
// GetGroupsByOrganizationIDPaginated mocks base method.
|
||||
func (m *MockStore) GetGroupsByOrganizationIDPaginated(ctx context.Context, arg database.GetGroupsByOrganizationIDPaginatedParams) ([]database.GetGroupsByOrganizationIDPaginatedRow, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetGroupsByOrganizationIDPaginated", ctx, arg)
|
||||
ret0, _ := ret[0].([]database.GetGroupsByOrganizationIDPaginatedRow)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// GetGroupsByOrganizationIDPaginated indicates an expected call of GetGroupsByOrganizationIDPaginated.
|
||||
func (mr *MockStoreMockRecorder) GetGroupsByOrganizationIDPaginated(ctx, arg any) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetGroupsByOrganizationIDPaginated", reflect.TypeOf((*MockStore)(nil).GetGroupsByOrganizationIDPaginated), ctx, arg)
|
||||
}
|
||||
|
||||
// GetHealthSettings mocks base method.
|
||||
func (m *MockStore) GetHealthSettings(ctx context.Context) (string, error) {
|
||||
m.ctrl.T.Helper()
|
||||
|
||||
Generated
+1
@@ -624,6 +624,7 @@ type sqlcQuerier interface {
|
||||
GetGroupMembersCountByGroupIDs(ctx context.Context, arg GetGroupMembersCountByGroupIDsParams) ([]GetGroupMembersCountByGroupIDsRow, error)
|
||||
// A limit of 0 means "no limit".
|
||||
GetGroups(ctx context.Context, arg GetGroupsParams) ([]GetGroupsRow, error)
|
||||
GetGroupsByOrganizationIDPaginated(ctx context.Context, arg GetGroupsByOrganizationIDPaginatedParams) ([]GetGroupsByOrganizationIDPaginatedRow, error)
|
||||
GetHealthSettings(ctx context.Context) (string, error)
|
||||
// Returns the highest group AI budget across the groups the user belongs to,
|
||||
// breaking ties by the earliest organization membership. Implements the
|
||||
|
||||
Generated
+95
@@ -15125,6 +15125,101 @@ func (q *sqlQuerier) GetGroups(ctx context.Context, arg GetGroupsParams) ([]GetG
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getGroupsByOrganizationIDPaginated = `-- name: GetGroupsByOrganizationIDPaginated :many
|
||||
SELECT
|
||||
groups.id, groups.name, groups.organization_id, groups.avatar_url, groups.quota_allowance, groups.display_name, groups.source, groups.chat_spend_limit_micros,
|
||||
organizations.name AS organization_name,
|
||||
organizations.display_name AS organization_display_name,
|
||||
COUNT(*) OVER() AS count
|
||||
FROM
|
||||
groups
|
||||
INNER JOIN
|
||||
organizations ON groups.organization_id = organizations.id
|
||||
WHERE
|
||||
true
|
||||
AND groups.organization_id = $1
|
||||
-- Keyset pagination cursor. When @after_id is set, return only groups
|
||||
-- ordered after it, matching the ORDER BY (LOWER(name), id) below. This
|
||||
-- lets callers page without duplicated or skipped rows even if groups are
|
||||
-- inserted or deleted between page requests.
|
||||
AND CASE
|
||||
WHEN $2 :: uuid != '00000000-0000-0000-0000-000000000000' :: uuid THEN
|
||||
(LOWER(groups.name), groups.id) > (
|
||||
SELECT LOWER(name), id FROM groups WHERE id = $2
|
||||
)
|
||||
ELSE true
|
||||
END
|
||||
-- Filter by group name or display name (substring, case-insensitive).
|
||||
AND CASE WHEN $3 :: text != '' THEN (
|
||||
groups.name ILIKE concat('%', $3, '%')
|
||||
OR groups.display_name ILIKE concat('%', $3, '%')
|
||||
)
|
||||
ELSE true
|
||||
END
|
||||
ORDER BY
|
||||
-- Deterministic and consistent ordering of all groups. This is to ensure consistent pagination.
|
||||
LOWER(groups.name) ASC, groups.id ASC OFFSET $4
|
||||
LIMIT
|
||||
-- A null limit means "no limit", so 0 means return all
|
||||
NULLIF($5 :: int, 0)
|
||||
`
|
||||
|
||||
type GetGroupsByOrganizationIDPaginatedParams struct {
|
||||
OrganizationID uuid.UUID `db:"organization_id" json:"organization_id"`
|
||||
AfterID uuid.UUID `db:"after_id" json:"after_id"`
|
||||
Search string `db:"search" json:"search"`
|
||||
OffsetOpt int32 `db:"offset_opt" json:"offset_opt"`
|
||||
LimitOpt int32 `db:"limit_opt" json:"limit_opt"`
|
||||
}
|
||||
|
||||
type GetGroupsByOrganizationIDPaginatedRow struct {
|
||||
Group Group `db:"group" json:"group"`
|
||||
OrganizationName string `db:"organization_name" json:"organization_name"`
|
||||
OrganizationDisplayName string `db:"organization_display_name" json:"organization_display_name"`
|
||||
Count int64 `db:"count" json:"count"`
|
||||
}
|
||||
|
||||
func (q *sqlQuerier) GetGroupsByOrganizationIDPaginated(ctx context.Context, arg GetGroupsByOrganizationIDPaginatedParams) ([]GetGroupsByOrganizationIDPaginatedRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getGroupsByOrganizationIDPaginated,
|
||||
arg.OrganizationID,
|
||||
arg.AfterID,
|
||||
arg.Search,
|
||||
arg.OffsetOpt,
|
||||
arg.LimitOpt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetGroupsByOrganizationIDPaginatedRow
|
||||
for rows.Next() {
|
||||
var i GetGroupsByOrganizationIDPaginatedRow
|
||||
if err := rows.Scan(
|
||||
&i.Group.ID,
|
||||
&i.Group.Name,
|
||||
&i.Group.OrganizationID,
|
||||
&i.Group.AvatarURL,
|
||||
&i.Group.QuotaAllowance,
|
||||
&i.Group.DisplayName,
|
||||
&i.Group.Source,
|
||||
&i.Group.ChatSpendLimitMicros,
|
||||
&i.OrganizationName,
|
||||
&i.OrganizationDisplayName,
|
||||
&i.Count,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const insertAllUsersGroup = `-- name: InsertAllUsersGroup :one
|
||||
INSERT INTO groups (
|
||||
id,
|
||||
|
||||
@@ -91,6 +91,44 @@ WHERE
|
||||
LIMIT NULLIF(@limit_opt :: int, 0)
|
||||
;
|
||||
|
||||
-- name: GetGroupsByOrganizationIDPaginated :many
|
||||
SELECT
|
||||
sqlc.embed(groups),
|
||||
organizations.name AS organization_name,
|
||||
organizations.display_name AS organization_display_name,
|
||||
COUNT(*) OVER() AS count
|
||||
FROM
|
||||
groups
|
||||
INNER JOIN
|
||||
organizations ON groups.organization_id = organizations.id
|
||||
WHERE
|
||||
true
|
||||
AND groups.organization_id = @organization_id
|
||||
-- Keyset pagination cursor. When @after_id is set, return only groups
|
||||
-- ordered after it, matching the ORDER BY (LOWER(name), id) below. This
|
||||
-- lets callers page without duplicated or skipped rows even if groups are
|
||||
-- inserted or deleted between page requests.
|
||||
AND CASE
|
||||
WHEN @after_id :: uuid != '00000000-0000-0000-0000-000000000000' :: uuid THEN
|
||||
(LOWER(groups.name), groups.id) > (
|
||||
SELECT LOWER(name), id FROM groups WHERE id = @after_id
|
||||
)
|
||||
ELSE true
|
||||
END
|
||||
-- Filter by group name or display name (substring, case-insensitive).
|
||||
AND CASE WHEN @search :: text != '' THEN (
|
||||
groups.name ILIKE concat('%', @search, '%')
|
||||
OR groups.display_name ILIKE concat('%', @search, '%')
|
||||
)
|
||||
ELSE true
|
||||
END
|
||||
ORDER BY
|
||||
-- Deterministic and consistent ordering of all groups. This is to ensure consistent pagination.
|
||||
LOWER(groups.name) ASC, groups.id ASC OFFSET @offset_opt
|
||||
LIMIT
|
||||
-- A null limit means "no limit", so 0 means return all
|
||||
NULLIF(@limit_opt :: int, 0);
|
||||
|
||||
-- name: InsertGroup :one
|
||||
INSERT INTO groups (
|
||||
id,
|
||||
|
||||
Reference in New Issue
Block a user