Skip to content

Commit

Permalink
Continue working
Browse files Browse the repository at this point in the history
  • Loading branch information
peytondmurray committed Nov 15, 2024
1 parent 642056a commit 2edfff6
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 79 deletions.
12 changes: 10 additions & 2 deletions conda-store-server/conda_store_server/_internal/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class Environment(BaseModel):
id: int
namespace: Namespace
name: str
current_build_id: int
current_build_id: Optional[int]
current_build: Optional[Build]

description: Optional[str]
Expand Down Expand Up @@ -648,6 +648,14 @@ class APIPaginatedResponse(APIResponse):
count: int


class APICursorPaginatedResponse(BaseModel):
data: Optional[Any]
status: APIStatus
message: Optional[str]
cursor: Optional[str]
count: int


class APIAckResponse(BaseModel):
status: APIStatus
message: Optional[str]
Expand Down Expand Up @@ -712,7 +720,7 @@ class APIDeleteNamespaceRole(BaseModel):


# GET /api/v1/environment
class APIListEnvironment(APIPaginatedResponse):
class APIListEnvironment(APICursorPaginatedResponse):
data: List[Environment]


Expand Down
81 changes: 35 additions & 46 deletions conda-store-server/conda_store_server/_internal/server/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,32 @@
Permissions,
)
from conda_store_server._internal.server import dependencies
from conda_store_server._internal.server.views.pagination import Cursor, paginate
from conda_store_server._internal.server.views.pagination import (
Cursor,
CursorPaginatedArgs,
OrderingMetadata,
paginate,
)
from conda_store_server.server.auth import Authentication


def get_cursor(encoded_cursor: Optional[str] = None) -> Cursor:
return Cursor.load(encoded_cursor)


def get_cursor_paginated_args(
order: Optional[str] = None,
limit: Optional[int] = None,
sort_by: List[str] = Query([]),
server=Depends(dependencies.get_server),
) -> CursorPaginatedArgs:
return CursorPaginatedArgs(
limit=server.max_page_size if limit is None else limit,
order=order,
sort_by=sort_by,
)


class PaginatedArgs(TypedDict):
"""Dictionary type holding information about paginated requests."""

Expand Down Expand Up @@ -639,7 +657,7 @@ async def api_list_environments(
auth: Authentication = Depends(dependencies.get_auth),
conda_store: app.CondaStore = Depends(dependencies.get_conda_store),
entity: AuthenticationToken = Depends(dependencies.get_entity),
paginated_args: PaginatedArgs = Depends(get_paginated_args),
paginated_args: CursorPaginatedArgs = Depends(get_cursor_paginated_args),
cursor: Cursor = Depends(get_cursor),
artifact: Optional[schema.BuildArtifactType] = None,
jwt: Optional[str] = None,
Expand All @@ -648,7 +666,7 @@ async def api_list_environments(
packages: Optional[List[str]] = Query([]),
search: Optional[str] = None,
status: Optional[schema.BuildStatus] = None,
):
) -> schema.APIListEnvironment:
"""Retrieve a list of environments.
Parameters
Expand All @@ -659,7 +677,7 @@ async def api_list_environments(
the request
entity : AuthenticationToken
Token of the user making the request
paginated_args : PaginatedArgs
paginated_args : CursorPaginatedArgs
Arguments for controlling pagination of the response
conda_store : app.CondaStore
The running conda store application
Expand All @@ -683,7 +701,7 @@ async def api_list_environments(
Returns
-------
Dict
schema.APIListEnvironment
Paginated JSON response containing the requested environments. Results are sorted by each
envrionment's build's scheduled_on time to ensure all results are returned when iterating
over pages in systems where the number of environments is changing while results are being
Expand Down Expand Up @@ -716,51 +734,22 @@ async def api_list_environments(
role_bindings=auth.entity_bindings(entity),
)

valid_sort_by = {
"namespace": orm.Namespace.name,
"name": orm.Environment.name,
}

# sorts = get_sorts(
# order=paginated_args["order"],
# sort_by=paginated_args["sort_by"],
# allowed_sort_bys=valid_sort_by,
# default_sort_by=["namespace", "name"],
# default_order="asc",
# )

# query = (
# query
# .filter(
# or_(
# orm.Namespace.name > cursor.order_by['namespace'],
# orm.Namespace.name == cursor.order_by['namespace']
# )
# )
# .order_by(
# *sorts,
# orm.Environment.id.asc()
# )
# )

return paginate(
paginated, next_cursor = paginate(
query=query,
ordering_metadata=OrderingMetadata(
order_names=["namespace", "name"],
column_names=["namespace.name", "name"],
),
cursor=cursor,
sort_by=paginated_args["sort_by"],
valid_sort_by=valid_sort_by,
order_by=paginated_args["sort_by"],
limit=paginated_args["limit"],
)

return paginated_api_response(
query,
paginated_args,
schema.Environment,
exclude={"current_build"},
allowed_sort_bys={
"namespace": orm.Namespace.name,
"name": orm.Environment.name,
},
default_sort_by=["namespace", "name"],
default_order="asc",
return schema.APIListEnvironment(
data=paginated,
status="ok",
cursor=None if next_cursor is None else next_cursor.dump(),
count=1000,
)


Expand Down
Loading

0 comments on commit 2edfff6

Please sign in to comment.