Skip to content

Commit

Permalink
Add sort, order, and cursor options to workspace search methods (#182)
Browse files Browse the repository at this point in the history
* Add sort, order, and cursor options to workspace search methods

* Add `Beaker.workspace.iter()`
  • Loading branch information
epwalsh authored Dec 9, 2022
1 parent 6b543ba commit f3645de
Show file tree
Hide file tree
Showing 24 changed files with 391 additions and 105 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use patch releases for compatibility fixes instead.

## Unreleased

### Added

- Added `cursor`, `sort_by` and ordering options to `Beaker.workspace.*` search methods.

## [v1.12.1](https://github.com/allenai/beaker-py/releases/tag/v1.12.1) - 2022-12-08

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion beaker/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Config:
The address of the Beaker server.
"""

default_org: Optional[str] = None
default_org: Optional[str] = "ai2"
"""
Default Beaker organization to use.
"""
Expand Down
7 changes: 6 additions & 1 deletion beaker/data_model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
logger = logging.getLogger("beaker")


__all__ = ["BaseModel", "MappedSequence"]
__all__ = ["BaseModel", "MappedSequence", "StrEnum"]


BUG_REPORT_URL = (
Expand Down Expand Up @@ -144,3 +144,8 @@ def keys(self):

def values(self):
return self._mapping.values()


class StrEnum(str, Enum):
def __str__(self) -> str:
return self.value
5 changes: 2 additions & 3 deletions beaker/data_model/cluster.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
from datetime import datetime
from enum import Enum
from typing import Optional, Tuple

from pydantic import validator

from .base import BaseModel
from .base import BaseModel, StrEnum
from .node import NodeResources, NodeUtilization

__all__ = ["ClusterStatus", "Cluster", "ClusterUtilization", "ClusterSpec", "ClusterPatch"]


class ClusterStatus(str, Enum):
class ClusterStatus(StrEnum):
"""
Current status of a cluster.
"""
Expand Down
10 changes: 9 additions & 1 deletion beaker/data_model/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pydantic import validator

from .account import Account
from .base import BaseModel
from .base import BaseModel, StrEnum
from .workspace import WorkspaceRef

__all__ = [
Expand All @@ -18,6 +18,7 @@
"DatasetsPage",
"DatasetSpec",
"DatasetPatch",
"DatasetSort",
]


Expand Down Expand Up @@ -168,3 +169,10 @@ class DatasetPatch(BaseModel):
name: Optional[str] = None
description: Optional[str] = None
commit: Optional[bool] = None


class DatasetSort(StrEnum):
created = "created"
author = "author"
dataset_name = "name"
dataset_name_or_description = "nameOrDescription"
11 changes: 9 additions & 2 deletions beaker/data_model/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from pydantic import Field

from .account import Account
from .base import BaseModel, MappedSequence
from .base import BaseModel, MappedSequence, StrEnum
from .job import Job
from .workspace import WorkspaceRef

__all__ = ["Experiment", "Task", "Tasks", "ExperimentsPage", "ExperimentPatch"]
__all__ = ["Experiment", "Task", "Tasks", "ExperimentsPage", "ExperimentPatch", "ExperimentSort"]


class Experiment(BaseModel):
Expand Down Expand Up @@ -70,3 +70,10 @@ class ExperimentsPage(BaseModel):
class ExperimentPatch(BaseModel):
name: Optional[str] = None
description: Optional[str] = None


class ExperimentSort(StrEnum):
created = "created"
author = "author"
experiment_name = "name"
experiment_name_or_description = "nameOrDescription"
9 changes: 4 additions & 5 deletions beaker/data_model/experiment_spec.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from enum import Enum
from typing import Any, Dict, List, Optional

from pydantic import Field, root_validator, validator

from ..aliases import PathOrStr
from ..exceptions import *
from .base import BaseModel
from .base import BaseModel, StrEnum

__all__ = [
"ImageSource",
Expand Down Expand Up @@ -238,7 +237,7 @@ class TaskResources(BaseModel, frozen=False):
"""


class Priority(str, Enum):
class Priority(StrEnum):
urgent = "urgent"
high = "high"
normal = "normal"
Expand Down Expand Up @@ -305,7 +304,7 @@ class TaskSpec(BaseModel, frozen=False):
Context describes how and where this task should run.
"""

constraints: Optional[Dict[str, List[str]]]
constraints: Optional[Dict[str, List[str]]] = None
"""
Each task can have many constraints. And each constraint can have many values.
Constraints are rules that change where a task is executed,
Expand Down Expand Up @@ -598,7 +597,7 @@ def with_constraint(self, **kwargs: List[str]) -> "TaskSpec":
)


class SpecVersion(str, Enum):
class SpecVersion(StrEnum):
v2 = "v2"
v2_alpha = "v2-alpha"

Expand Down
31 changes: 26 additions & 5 deletions beaker/data_model/group.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from datetime import datetime
from enum import Enum
from typing import List, Optional
from typing import List, Optional, Tuple

from .account import Account
from .base import BaseModel
from .base import BaseModel, StrEnum
from .workspace import WorkspaceRef

__all__ = ["Group", "GroupSpec", "GroupParameterType", "GroupParameter", "GroupPatch"]
__all__ = [
"Group",
"GroupSpec",
"GroupParameterType",
"GroupParameter",
"GroupPatch",
"GroupsPage",
"GroupSort",
]


class Group(BaseModel):
Expand All @@ -31,7 +38,7 @@ class GroupSpec(BaseModel):
experiments: Optional[List[str]] = None


class GroupParameterType(str, Enum):
class GroupParameterType(StrEnum):
metric = "metric"
env = "env"

Expand All @@ -47,3 +54,17 @@ class GroupPatch(BaseModel):
add_experiments: Optional[List[str]] = None
remove_experiments: Optional[List[str]] = None
parameters: Optional[List[GroupParameter]] = None


class GroupsPage(BaseModel):
data: Tuple[Group, ...]
next_cursor: Optional[str] = None
next: Optional[str] = None


class GroupSort(StrEnum):
created = "created"
modified = "modified"
author = "author"
group_name = "name"
group_name_or_description = "nameOrDescription"
14 changes: 10 additions & 4 deletions beaker/data_model/image.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from datetime import datetime
from enum import Enum
from typing import Optional, Tuple

from pydantic import validator

from .account import Account
from .base import BaseModel
from .base import BaseModel, StrEnum
from .workspace import WorkspaceRef

__all__ = [
Expand All @@ -20,6 +19,7 @@
"DockerLayerDownloadState",
"ImageSpec",
"ImagePatch",
"ImageSort",
]


Expand Down Expand Up @@ -73,15 +73,15 @@ class DockerLayerProgress(BaseModel):
total: Optional[int] = None


class DockerLayerUploadStatus(str, Enum):
class DockerLayerUploadStatus(StrEnum):
preparing = "preparing"
waiting = "waiting"
pushing = "pushing"
pushed = "pushed"
already_exists = "layer already exists"


class DockerLayerDownloadStatus(str, Enum):
class DockerLayerDownloadStatus(StrEnum):
waiting = "waiting"
downloading = "downloading"
download_complete = "download complete"
Expand Down Expand Up @@ -122,3 +122,9 @@ class ImagePatch(BaseModel):
name: Optional[str] = None
description: Optional[str] = None
commit: Optional[bool] = None


class ImageSort(StrEnum):
created = "created"
author = "author"
image_name = "name"
8 changes: 4 additions & 4 deletions beaker/data_model/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pydantic import Field, validator

from .account import Account
from .base import BaseModel
from .base import BaseModel, StrEnum
from .experiment_spec import DataMount, EnvVar, ImageSource, Priority, TaskSpec

__all__ = [
Expand All @@ -25,7 +25,7 @@
]


class CurrentJobStatus(str, Enum):
class CurrentJobStatus(StrEnum):
"""
The status of a job.
"""
Expand All @@ -41,7 +41,7 @@ class CurrentJobStatus(str, Enum):
preempted = "preempted"


class CanceledCode(int, Enum):
class CanceledCode(Enum):
not_set = 0
system_preemption = 1
user_preemption = 2
Expand Down Expand Up @@ -125,7 +125,7 @@ class JobExecution(BaseModel):
workspace: Optional[str] = None


class JobKind(str, Enum):
class JobKind(StrEnum):
"""
The kind of job.
"""
Expand Down
5 changes: 2 additions & 3 deletions beaker/data_model/organization.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from datetime import datetime
from enum import Enum
from typing import Optional

from .account import Account
from .base import BaseModel
from .base import BaseModel, StrEnum

__all__ = ["Organization", "OrganizationRole", "OrganizationMember"]

Expand All @@ -17,7 +16,7 @@ class Organization(BaseModel):
pronouns: Optional[str] = None


class OrganizationRole(str, Enum):
class OrganizationRole(StrEnum):
admin = "admin"
member = "member"

Expand Down
12 changes: 9 additions & 3 deletions beaker/data_model/workspace.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from datetime import datetime
from enum import Enum
from typing import Dict, List, Optional, Tuple

from .account import Account
from .base import BaseModel
from .base import BaseModel, StrEnum

__all__ = [
"WorkspaceSize",
Expand All @@ -17,6 +16,7 @@
"WorkspacePatch",
"WorkspacePermissionsPatch",
"WorkspaceClearResult",
"WorkspaceSort",
]


Expand Down Expand Up @@ -64,7 +64,7 @@ class WorkspaceTransferSpec(BaseModel):
ids: List[str]


class Permission(str, Enum):
class Permission(StrEnum):
"""
Workspace permission levels.
"""
Expand Down Expand Up @@ -101,3 +101,9 @@ class WorkspaceClearResult(BaseModel):
images_deleted: int = 0
datasets_deleted: int = 0
secrets_deleted: int = 0


class WorkspaceSort(StrEnum):
created = "created"
modified = "modified"
workspace_name = "name"
1 change: 1 addition & 0 deletions beaker/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"TaskStoppedError",
"JobFailedError",
"JobTimeoutError",
"ExperimentSpecError",
]


Expand Down
15 changes: 15 additions & 0 deletions beaker/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,18 @@
from .secret import SecretClient
from .service_client import ServiceClient
from .workspace import WorkspaceClient

__all__ = [
"AccountClient",
"ClusterClient",
"DatasetClient",
"ExperimentClient",
"GroupClient",
"ImageClient",
"JobClient",
"NodeClient",
"OrganizationClient",
"SecretClient",
"ServiceClient",
"WorkspaceClient",
]
13 changes: 11 additions & 2 deletions beaker/services/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
import os
from datetime import datetime
from pathlib import Path
from typing import TYPE_CHECKING, Deque, Dict, Generator, Optional, Tuple, Union
from typing import (
TYPE_CHECKING,
ClassVar,
Deque,
Dict,
Generator,
Optional,
Tuple,
Union,
)

from ..aliases import PathOrStr
from ..data_model import *
Expand All @@ -27,7 +36,7 @@ class DatasetClient(ServiceClient):
HEADER_LAST_MODIFIED = "Last-Modified"
HEADER_CONTENT_LENGTH = "Content-Length"

REQUEST_SIZE_LIMIT = 32 * 1024 * 1024
REQUEST_SIZE_LIMIT: ClassVar[int] = 32 * 1024 * 1024

def get(self, dataset: str) -> Dataset:
"""
Expand Down
Loading

0 comments on commit f3645de

Please sign in to comment.