-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add data-review/batch-initiate endpoint #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4985dd7
feat: add data-review/batch-initiate endpoint
mariobuikhuizen af0efd7
rename to integrations and remove argument from toplevel call
mariobuikhuizen b2c0257
don't recreate timedelta
mariobuikhuizen 83c15fb
address review issues
mariobuikhuizen ad81efd
Merge branch 'main' into feat_data_review
mariobuikhuizen bdbae96
Merge branch 'main' into feat_data_review
mariobuikhuizen 19a0cec
add URL to DataReview class
mariobuikhuizen 2152d93
Merge branch 'main' into feat_data_review
mariobuikhuizen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass, field | ||
from datetime import datetime, timedelta | ||
from time import sleep | ||
from typing import Protocol, Sequence | ||
|
||
from typing_extensions import Self | ||
|
||
from nominal._api.scout_service_api import scout_checks_api, scout_datareview_api, scout_integrations_api | ||
from nominal.core import checklist | ||
from nominal.core._clientsbunch import HasAuthHeader | ||
from nominal.core._utils import HasRid | ||
from nominal.ts import IntegralNanosecondsUTC, _SecondsNanos | ||
|
||
|
||
@dataclass(frozen=True) | ||
class DataReview(HasRid): | ||
rid: str | ||
run_rid: str | ||
checklist_rid: str | ||
checklist_commit: str | ||
completed: bool | ||
|
||
_clients: _Clients = field(repr=False) | ||
|
||
class _Clients(HasAuthHeader, Protocol): | ||
@property | ||
def datareview(self) -> scout_datareview_api.DataReviewService: ... | ||
|
||
@classmethod | ||
def _from_conjure(cls, clients: _Clients, data_review: scout_datareview_api.DataReview) -> Self: | ||
executing_states = [ | ||
check.automatic_check.state._pending_execution or check.automatic_check.state._executing | ||
for check in data_review.checklist.checks | ||
if check.automatic_check | ||
] | ||
completed = not any(executing_states) | ||
return cls( | ||
rid=data_review.rid, | ||
run_rid=data_review.run_rid, | ||
checklist_rid=data_review.checklist.checklist.rid, | ||
checklist_commit=data_review.checklist.checklist.commit, | ||
completed=completed, | ||
_clients=clients, | ||
) | ||
|
||
def get_violations(self) -> Sequence[CheckViolation]: | ||
"""Retrieves the list of check violations for the data review.""" | ||
response = self._clients.datareview.get_check_alerts_for_data_review(self._clients.auth_header, self.rid) | ||
return [CheckViolation._from_conjure(alert) for alert in response] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class CheckViolation: | ||
rid: str | ||
check_rid: str | ||
name: str | ||
start: IntegralNanosecondsUTC | ||
end: IntegralNanosecondsUTC | None | ||
priority: checklist.Priority | None | ||
|
||
@classmethod | ||
def _from_conjure(cls, check_alert: scout_datareview_api.CheckAlert) -> CheckViolation: | ||
return cls( | ||
rid=check_alert.rid, | ||
check_rid=check_alert.check_rid, | ||
name=check_alert.name, | ||
start=_SecondsNanos.from_api(check_alert.start).to_nanoseconds(), | ||
end=_SecondsNanos.from_api(check_alert.end).to_nanoseconds() if check_alert.end is not None else None, | ||
priority=checklist._conjure_priority_to_priority(check_alert.priority) | ||
if check_alert.priority is not scout_checks_api.Priority.UNKNOWN | ||
else None, | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class DataReviewBatchBuilder: | ||
integration_rids: list[str] | ||
_requests: list[scout_datareview_api.CreateDataReviewRequest] | ||
mariobuikhuizen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_clients: DataReview._Clients = field(repr=False) | ||
|
||
def add_integration(self, integration_rid: str) -> DataReviewBatchBuilder: | ||
self.integration_rids.append(integration_rid) | ||
return self | ||
|
||
def add_request(self, run_rid: str, checklist_rid: str, commit: str) -> DataReviewBatchBuilder: | ||
self._requests.append(scout_datareview_api.CreateDataReviewRequest(checklist_rid, run_rid, commit)) | ||
return self | ||
|
||
def initiate( | ||
self, wait_for_completion: bool = True, wait_timeout: timedelta = timedelta(minutes=1) | ||
) -> Sequence[DataReview]: | ||
"""Initiates a batch data review process. | ||
|
||
Args: | ||
wait_for_completion (bool): If True, waits for the data review process to complete before returning. | ||
Default is True. | ||
wait_timeout (timedelta): The maximum time to wait for the data review process to complete. | ||
Default is 1 minute. | ||
|
||
Raises: | ||
TimeoutError: If the data review process does not complete before the wait_timeout. | ||
""" | ||
request = scout_datareview_api.BatchInitiateDataReviewRequest( | ||
notification_configurations=[ | ||
scout_integrations_api.NotificationConfiguration(c) for c in self.integration_rids | ||
], | ||
requests=self._requests, | ||
) | ||
response = self._clients.datareview.batch_initiate(self._clients.auth_header, request) | ||
|
||
if not wait_for_completion: | ||
return [ | ||
DataReview._from_conjure(self._clients, self._clients.datareview.get(self._clients.auth_header, rid)) | ||
for rid in response.rids | ||
] | ||
|
||
started = datetime.now() | ||
mariobuikhuizen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
completed_review_rids = [] | ||
completed_reviews = [] | ||
while datetime.now() - started <= timedelta(seconds=wait_timeout.total_seconds()): | ||
mariobuikhuizen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sleep(2) | ||
for rid in response.rids: | ||
if rid not in completed_review_rids: | ||
review_response = self._clients.datareview.get(self._clients.auth_header, rid) | ||
review = DataReview._from_conjure(self._clients, review_response) | ||
if review.completed: | ||
completed_review_rids.append(rid) | ||
completed_reviews.append(review) | ||
if len(completed_reviews) == len(response.rids): | ||
return completed_reviews | ||
|
||
raise TimeoutError(f"Data review initiation did not complete before wait_timeout. Review rids: {response.rids}") | ||
mariobuikhuizen marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.