-
Notifications
You must be signed in to change notification settings - Fork 63
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
Fetch runs table without Bravado deserialization #1573
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
433fd07
Initial refactor
Raalsky 857090d
Tests added
Raalsky d2c3e3b
List comprehension
Raalsky 2025819
No bravado deserialization
Raalsky 767d291
Changelog updated
Raalsky 23b09f0
Auto review
Raalsky 8376cb6
Use generators
Raalsky ad93d1d
Refactor
Raalsky 9b9e300
Tests fixed
Raalsky 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 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 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,108 @@ | ||
# | ||
# Copyright (c) 2023, Neptune Labs Sp. z o.o. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
__all__ = ["get_single_page", "iter_over_pages", "to_leaderboard_entry"] | ||
|
||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
Callable, | ||
Dict, | ||
Generator, | ||
Iterable, | ||
List, | ||
Optional, | ||
) | ||
|
||
from bravado.client import construct_request # type: ignore | ||
from bravado.config import RequestConfig # type: ignore | ||
|
||
from neptune.internal.backends.api_model import ( | ||
AttributeType, | ||
AttributeWithProperties, | ||
LeaderboardEntry, | ||
) | ||
from neptune.internal.backends.hosted_client import DEFAULT_REQUEST_KWARGS | ||
|
||
if TYPE_CHECKING: | ||
from neptune.internal.backends.swagger_client_wrapper import SwaggerClientWrapper | ||
from neptune.internal.id_formats import UniqueId | ||
|
||
|
||
SUPPORTED_ATTRIBUTE_TYPES = {item.value for item in AttributeType} | ||
|
||
|
||
def get_single_page( | ||
*, | ||
client: "SwaggerClientWrapper", | ||
project_id: "UniqueId", | ||
query_params: Dict[str, Any], | ||
attributes_filter: Dict[str, Any], | ||
limit: int, | ||
offset: int, | ||
types: Optional[Iterable[str]] = None, | ||
) -> List[Any]: | ||
params = { | ||
"projectIdentifier": project_id, | ||
"type": types, | ||
"params": { | ||
**query_params, | ||
**attributes_filter, | ||
"pagination": {"limit": limit, "offset": offset}, | ||
}, | ||
} | ||
|
||
request_options = DEFAULT_REQUEST_KWARGS.get("_request_options", {}) | ||
request_config = RequestConfig(request_options, True) | ||
request_params = construct_request(client.api.searchLeaderboardEntries, request_options, **params) | ||
|
||
http_client = client.swagger_spec.http_client | ||
|
||
result = ( | ||
http_client.request(request_params, operation=None, request_config=request_config) | ||
.response() | ||
.incoming_response.json() | ||
) | ||
|
||
return list(result.get("entries", [])) | ||
|
||
|
||
def to_leaderboard_entry(*, entry: Dict[str, Any]) -> LeaderboardEntry: | ||
return LeaderboardEntry( | ||
id=entry["experimentId"], | ||
attributes=[ | ||
AttributeWithProperties( | ||
path=attr["name"], | ||
type=AttributeType(attr["type"]), | ||
properties=attr.__getitem__(f"{attr['type']}Properties"), | ||
) | ||
for attr in entry["attributes"] | ||
if attr["type"] in SUPPORTED_ATTRIBUTE_TYPES | ||
], | ||
) | ||
|
||
|
||
def iter_over_pages( | ||
*, iter_once: Callable[..., List[Any]], step: int, max_server_offset: int = 10000 | ||
) -> Generator[Any, None, None]: | ||
previous_items = None | ||
num_of_collected_items = 0 | ||
|
||
while (previous_items is None or len(previous_items) >= step) and num_of_collected_items < max_server_offset: | ||
previous_items = iter_once( | ||
limit=min(step, max_server_offset - num_of_collected_items), offset=num_of_collected_items | ||
) | ||
num_of_collected_items += len(previous_items) | ||
yield from previous_items |
This file contains 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 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.
What do you think not pass callable here and always call
get_single_page
inside? And rename it to get leaderboard_entries?To make it not so general
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.
Maybe move
max_server_offset
to const?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.
Sure, I agree with your suggestion but as we're going to reimplement the pagination (move from offset + limit to
starting_after
) it's probably not worth it right now as we're going to get rid ofmax_server_offset
.I'm not sure about getting rid of callable here as this potentially abstracts the whole pagination scheme and simplifies a testing setup. I'll try to remember this suggestion to address that with the next Pull Request.