Repository navigation
feat(logs): read project logs #66
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,135 @@ | ||
| """Project runtime log facade.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping | ||
| from typing import TYPE_CHECKING, Protocol, cast | ||
|
|
||
| from ._transport import TransportResponse, invoke, response_payload | ||
| from .models import JSONValue, LogActivityResponse, LogSearchResponse | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .client import VolcanoClient | ||
|
|
||
| _INVALID_PROJECT_ID = "project_id must be a non-empty string" | ||
| _INVALID_LOG_REQUEST = "Log request must be a mapping" | ||
| _INVALID_LOG_RESPONSE = "Expected a complete log response" | ||
|
|
||
|
|
||
| class LogsTransport(Protocol): | ||
| """Transport operations required by the logs facade.""" | ||
|
|
||
| def search_project_logs( | ||
| self, | ||
| *, | ||
| authorization: str, | ||
| project_id: str, | ||
| request: Mapping[str, JSONValue], | ||
| ) -> TransportResponse: | ||
| """Search a project's retained logs.""" | ||
| ... | ||
|
|
||
| def get_project_log_activity( | ||
| self, | ||
| *, | ||
| authorization: str, | ||
| project_id: str, | ||
| request: Mapping[str, JSONValue], | ||
| ) -> TransportResponse: | ||
| """Get bucketed log activity for a project.""" | ||
| ... | ||
|
|
||
|
|
||
| class Logs: | ||
| """Search retained project logs and activity.""" | ||
|
|
||
| def __init__(self, client: VolcanoClient) -> None: | ||
| """Bind log reads to a Volcano client.""" | ||
| self._client = client | ||
|
|
||
| def search( | ||
| self, | ||
| project_id: str, | ||
| request: Mapping[str, JSONValue], | ||
| ) -> LogSearchResponse: | ||
| """Search retained logs for one project resource type.""" | ||
| project_id, request = _log_request(project_id, request) | ||
| transport = cast("LogsTransport", self._client._transport) | ||
| response = invoke( | ||
| transport.search_project_logs, | ||
| authorization=self._client._session_token(), | ||
| project_id=project_id, | ||
| request=request, | ||
| ) | ||
| return _search_response(response_payload(response, 200)) | ||
|
|
||
| def activity( | ||
| self, | ||
| project_id: str, | ||
| request: Mapping[str, JSONValue], | ||
| ) -> LogActivityResponse: | ||
| """Get bucketed activity for one project resource type.""" | ||
| project_id, request = _log_request(project_id, request) | ||
| transport = cast("LogsTransport", self._client._transport) | ||
| response = invoke( | ||
| transport.get_project_log_activity, | ||
| authorization=self._client._session_token(), | ||
| project_id=project_id, | ||
| request=request, | ||
| ) | ||
| return _activity_response(response_payload(response, 200)) | ||
|
|
||
|
|
||
| def _log_request( | ||
| project_id: object, | ||
| request: object, | ||
| ) -> tuple[str, Mapping[str, JSONValue]]: | ||
| if not isinstance(project_id, str) or not project_id.strip(): | ||
| raise ValueError(_INVALID_PROJECT_ID) | ||
| if not isinstance(request, Mapping): | ||
| raise TypeError(_INVALID_LOG_REQUEST) | ||
| return project_id, cast("Mapping[str, JSONValue]", request) | ||
|
|
||
|
|
||
| def _response_values(payload: object) -> Mapping[str, object]: | ||
| if not isinstance(payload, Mapping): | ||
| raise TypeError(_INVALID_LOG_RESPONSE) | ||
| return cast("Mapping[str, object]", payload) | ||
|
|
||
|
|
||
| def _response_data(values: Mapping[str, object]) -> tuple[Mapping[str, JSONValue], ...]: | ||
| raw_data = values.get("data") | ||
| if not isinstance(raw_data, list): | ||
| raise TypeError(_INVALID_LOG_RESPONSE) | ||
| data = cast("list[object]", raw_data) | ||
| if any(not isinstance(item, Mapping) for item in data): | ||
| raise TypeError(_INVALID_LOG_RESPONSE) | ||
| return tuple(cast("Mapping[str, JSONValue]", item) for item in data) | ||
|
|
||
|
|
||
| def _search_response(payload: object) -> LogSearchResponse: | ||
| values = _response_values(payload) | ||
| limit = values.get("limit") | ||
| has_more = values.get("has_more") | ||
| next_cursor = values.get("next_cursor") | ||
| if ( | ||
| not isinstance(limit, int) | ||
| or isinstance(limit, bool) | ||
| or not isinstance(has_more, bool) | ||
| or (next_cursor is not None and not isinstance(next_cursor, str)) | ||
| ): | ||
| raise TypeError(_INVALID_LOG_RESPONSE) | ||
| return LogSearchResponse( | ||
| data=_response_data(values), | ||
| limit=limit, | ||
| has_more=has_more, | ||
| next_cursor=next_cursor, | ||
| ) | ||
|
|
||
|
|
||
| def _activity_response(payload: object) -> LogActivityResponse: | ||
| values = _response_values(payload) | ||
| total = values.get("total") | ||
| if not isinstance(total, int) or isinstance(total, bool): | ||
| raise TypeError(_INVALID_LOG_RESPONSE) | ||
| return LogActivityResponse(data=_response_data(values), total=total) |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a request omits the required
resourcefield,LogSearchRequest.from_dict()raises a bareKeyErrorbefore the raw-body override or HTTP request runs, so Hosting cannot return its 400 response and the facade cannot map it toValidationError; malformed known timestamps or resource IDs similarly leakValueError. The post-fix override preserves unknown fields only for bodies that the generated parser already accepts, and the activity path has the same ordering, so construct the generated URL without parsing the caller body or translate these local failures consistently.Useful? React with 👍 / 👎.