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 466ac3b commit 642056a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
27 changes: 15 additions & 12 deletions conda-store-server/conda_store_server/_internal/server/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from conda_store_server.server.auth import Authentication


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


class PaginatedArgs(TypedDict):
Expand Down Expand Up @@ -716,16 +716,18 @@ async def api_list_environments(
role_bindings=auth.entity_bindings(entity),
)

sorts = get_sorts(
order=paginated_args["order"],
sort_by=paginated_args["sort_by"],
allowed_sort_bys={
"namespace": orm.Namespace.name,
"name": orm.Environment.name,
},
default_sort_by=["namespace", "name"],
default_order="asc",
)
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
Expand All @@ -745,6 +747,7 @@ async def api_list_environments(
query=query,
cursor=cursor,
sort_by=paginated_args["sort_by"],
valid_sort_by=valid_sort_by,
)

return paginated_api_response(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
import base64
from typing import Dict, List, Optional

import pydantic
from sqlalchemy import tuple_
from sqlalchemy.orm import Query as SqlQuery


class Cursor(pydantic.BaseModel):
last_id: Optional[int] = 1
last_id: int | None = 1

# List of names of attributes to order by, and the last value of the ordered attribute
# {
# 'namespace': 'foo',
# 'environment': 'bar',
# }
last_value: Optional[Dict[str, str]] = {}
last_value: dict[str, str] | None = {}

def dump(self):
return base64.b64encode(self.model_dump_json())

@classmethod
def load(cls, data: str):
def load(cls, data: str | None = None):
if data is None:
return cls()
return cls.from_json(base64.b64decode(data))


def paginate(
query: SqlQuery,
cursor: Cursor,
sort_by: List[str],
valid_sort_by: Dict[str, object],
sort_by: list[str] | None = None,
valid_sort_by: dict[str, object] | None = None,
) -> SqlQuery:
"""Paginate the query using the cursor and the requested sort_bys.
Expand All @@ -44,11 +45,19 @@ def paginate(
cursor : Cursor
Cursor object containing information about the last item
on the previous page
sort_by : List[str]
sort_by : list[str] | None
List of sort_by query parameters
valid_sort_by : Dict[str, object]
valid_sort_by : dict[str, object] | None
Mapping between query parameter names and the orm object they apply to
"""
breakpoint()

if sort_by is None:
sort_by = []

if valid_sort_by is None:
valid_sort_by = {}

objects = []
last_values = []
for obj in sort_by:
Expand All @@ -57,4 +66,4 @@ def paginate(

return query.filter(
tuple_(*objects, object.id) > (*last_values, cursor.last_id)
).order_by(sorts)
) # .order_by(sorts)
9 changes: 4 additions & 5 deletions conda-store-server/conda_store_server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,7 @@ def list_environments(
Query
Sqlalchemy query containing the requested environments
"""
query = (
db.query(orm.Environment)
.join(orm.Environment.namespace)
.join(orm.Environment.current_build)
)
query = db.query(orm.Environment).join(orm.Environment.namespace)

if namespace:
query = query.filter(orm.Namespace.name == namespace)
Expand All @@ -346,6 +342,9 @@ def list_environments(
if not show_soft_deleted:
query = query.filter(orm.Environment.deleted_on == null())

if status or artifact or packages:
query = query.join(orm.Environment.current_build)

if status:
query = query.filter(orm.Build.status == status)

Expand Down

0 comments on commit 642056a

Please sign in to comment.