Skip to content

Commit

Permalink
[FIX] Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
quersa committed Aug 1, 2024
1 parent f2aeee9 commit 191f9bf
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 15 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file.


## [0.11.3] - 2024-08-01

### Added

- Added 'get_batches' method, a feature to retrieve a group of batches based on 'BatchFilters'

## [0.11.2] - 2024-07-31

### Added
Expand Down
2 changes: 1 addition & 1 deletion pasqal_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def get_batches(
Raises:
BatchFetchingError: If fetching batches call failed.
ValueError: If `filters` is not an instance of JobFilters
ValueError: If `filters` is not an instance of BatchFilters
or if `pagination_params` is not an instance of PaginationParams.
"""

Expand Down
2 changes: 1 addition & 1 deletion pasqal_cloud/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.


__version__ = "0.11.2"
__version__ = "0.11.3"
7 changes: 1 addition & 6 deletions pasqal_cloud/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@
)
from pasqal_cloud.endpoints import Auth0Conf, Endpoints
from pasqal_cloud.utils.filters import (
BatchFilters,
CancelJobFilters,
JobFilters,
PaginationParams,
RebatchFilters,
)
from pasqal_cloud.utils.jsend import JobResult, JSendPayload
from pasqal_cloud.utils.models import (
BatchFilters,
JobFilters,
PaginationParams,
RebatchFilters,
)
from pasqal_cloud.utils.retry import retry_http_error

TIMEOUT = 30 # client http requests timeout after 30s
Expand Down
66 changes: 66 additions & 0 deletions pasqal_cloud/utils/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ class BatchFilters(BaseModel):
end_date: Optional[datetime] = None
queue_priority: Optional[Union[List[QueuePriority], QueuePriority]] = None

@staticmethod
def convert_to_list(value: Any) -> Any:
if not value:
return None
if isinstance(value, list):
return value
return [value]

@staticmethod
def convert_str_to_uuid(value: Any) -> Any:
if isinstance(value, str):
try:
return UUID(value)
except ValueError:
raise ValueError(f"Cannot convert {value} to UUID.")
return value

@field_validator(
"id",
"project_id",
Expand Down Expand Up @@ -183,6 +200,55 @@ def str_to_uuid_validator(cls, values: Dict[str, Any]) -> Dict[str, UUID]:
return values


class JobFilters(BaseJobFilters):
"""
Class to provide filters for querying jobs.
Setting a value for any attribute of this class will add a filter to the query.
When using several filters at the same time, the API will return elements who pass
all filters at the same time:
- If the filter value is a single element, the API will return jobs whose
attribute matches the provided value.
- If the filter value is a list, the API will return jobs whose value for
this attribute is contained in that list.
Attributes:
id: Filter by job IDs.
project_id: Filter by project IDs.
user_id: Filter by user IDs.
batch_id: Filter by batch IDs.
status: Filter by job statuses.
min_runs: Minimum number of runs.
max_runs: Maximum number of runs.
errors: Filter by presence of errors.
start_date: Retry jobs created at or after this datetime.
end_date: Retry jobs created at or before this datetime.
"""

project_id: Optional[Union[List[Union[UUID, str]], Union[UUID, str]]] = None
user_id: Optional[Union[List[str], str]] = None
batch_id: Optional[Union[List[Union[UUID, str]], Union[UUID, str]]] = None
status: Optional[Union[List[JobStatus], JobStatus]] = None
errors: Optional[bool] = None

@field_validator("id", "project_id", "user_id", "batch_id", "status", mode="before")
@classmethod
def single_item_to_list_validator(cls, values: Dict[str, Any]) -> Any:
return cls.convert_to_list(values)

@field_validator("id", "project_id", "batch_id", mode="after")
@classmethod
def str_to_uuid_validator(cls, values: Dict[str, Any]) -> Dict[str, UUID]:
for item in values:
cls.convert_str_to_uuid(item)
return values

@field_serializer("status", check_fields=False)
def status_enum_to_string(self, job_statuses: List[JobStatus]) -> List[str]:
return [job_status.value for job_status in job_statuses]


class PaginationParams(BaseModel):
"""
Class providing parameters for paginating query results.
Expand Down
10 changes: 3 additions & 7 deletions tests/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,6 @@ def test_rebatch_raises_value_error_on_invalid_filters(self):
BatchFilters(status=BatchStatus.DONE),
# List of statuses
BatchFilters(status=[BatchStatus.DONE, BatchStatus.PENDING]),
# Minimum runs
BatchFilters(min_runs=10),
# Maximum runs
BatchFilters(max_runs=20),
# Complete flag
BatchFilters(complete=True),
# Start date
Expand All @@ -677,7 +673,7 @@ def test_get_batches_with_filters_success(
):
"""
As a user using the SDK with proper credentials,
I can get a list of jobs with specific filters.
I can get a list of batches with specific filters.
The resulting request will retrieve the jobs that match the filters.
"""
response = self.sdk.get_batches(filters=filters)
Expand Down Expand Up @@ -714,7 +710,7 @@ def test_get_batches_with_pagination_success(
):
"""
As a user using the SDK with proper credentials,
I can get a list of jobs with pagination parameters.
I can get a list of batches with pagination parameters.
The resulting request will retrieve the jobs that match
the pagination parameters.
"""
Expand Down Expand Up @@ -745,7 +741,7 @@ def test_get_batches_sdk_error(
"""
As a user using the SDK with proper credentials,
if my request for getting jobs returns a status code different from 200,
I am faced with the JobFetchingError.
I am faced with the BatchFetchingError.
"""
with pytest.raises(BatchFetchingError):
_ = self.sdk.get_batches()
Expand Down

0 comments on commit 191f9bf

Please sign in to comment.