-
Notifications
You must be signed in to change notification settings - Fork 1
Dev/minimal flow #16
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
Dev/minimal flow #16
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6cf6715
Added minimal Run classes (#6)
Raalsky b85f1c5
Added `OperationsQueue` component (#7)
Raalsky 2faf3a5
Logging metadata (#8)
Raalsky 4c91b15
Run creation and basic data synchronization (#9)
Raalsky 67f63cb
Added support for env variables for project and api token (#11)
Raalsky 6e4ada2
Splitting metadata into multiple messages on `log` (#12)
Raalsky d8098f9
Added ErrorsMonitor and ErrorsQueue (#13)
Raalsky e35876c
Added support for family parameter (#14)
Raalsky cceddab
Code review
Raalsky 04a7dce
Code review 2
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 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 |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
pre-commit | ||
pytest | ||
pytest-timeout | ||
freezegun |
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,120 @@ | ||
from __future__ import annotations | ||
|
||
__all__ = ("MessageBuilder",) | ||
|
||
from datetime import datetime | ||
|
||
from google.protobuf.timestamp_pb2 import Timestamp | ||
from neptune_api.proto.neptune_pb.ingest.v1.common_pb2 import ( | ||
SET_OPERATION, | ||
ModifySet, | ||
Step, | ||
StringSet, | ||
UpdateRunSnapshot, | ||
Value, | ||
) | ||
from neptune_api.proto.neptune_pb.ingest.v1.pub.ingest_pb2 import RunOperation | ||
|
||
|
||
class MessageBuilder: | ||
def __init__( | ||
self, | ||
*, | ||
project: str, | ||
run_id: str, | ||
step: int | float | None, | ||
timestamp: datetime, | ||
fields: dict[str, float | bool | int | str | datetime | list | set], | ||
metrics: dict[str, float], | ||
add_tags: dict[str, list[str] | set[str]], | ||
remove_tags: dict[str, list[str] | set[str]], | ||
): | ||
self._step = None if step is None else make_step(number=step) | ||
self._timestamp = datetime_to_proto(timestamp) | ||
self._fields = fields | ||
self._metrics = metrics | ||
self._add_tags = add_tags | ||
self._remove_tags = remove_tags | ||
self._project = project | ||
self._run_id = run_id | ||
|
||
self._was_produced: bool = False | ||
|
||
def __iter__(self) -> MessageBuilder: | ||
return self | ||
|
||
def __next__(self) -> RunOperation: | ||
if not self._was_produced: | ||
self._was_produced = True | ||
|
||
modify_sets = {key: mod_tags(add=add) for key, add in self._add_tags.items()} | ||
modify_sets.update({key: mod_tags(remove=remove) for key, remove in self._remove_tags.items()}) | ||
update = UpdateRunSnapshot( | ||
step=self._step, | ||
timestamp=self._timestamp, | ||
assign={key: make_value(value) for key, value in self._fields.items()}, | ||
append={key: make_value(value) for key, value in self._metrics.items()}, | ||
modify_sets=modify_sets, | ||
) | ||
|
||
return RunOperation(project=self._project, run_id=self._run_id, update=update) | ||
|
||
raise StopIteration | ||
|
||
|
||
def mod_tags(add: list[str] | set[str] | None = None, remove: list[str] | set[str] | None = None) -> ModifySet: | ||
mod_set = ModifySet() | ||
if add is not None: | ||
for tag in add: | ||
mod_set.string.values[tag] = SET_OPERATION.ADD | ||
if remove is not None: | ||
for tag in remove: | ||
mod_set.string.values[tag] = SET_OPERATION.REMOVE | ||
return mod_set | ||
|
||
|
||
def make_value(value: Value | float | str | int | bool | datetime | list[str] | set[str]) -> Value: | ||
if isinstance(value, Value): | ||
return value | ||
if isinstance(value, float): | ||
return Value(float64=value) | ||
elif isinstance(value, bool): | ||
return Value(bool=value) | ||
elif isinstance(value, int): | ||
return Value(int64=value) | ||
elif isinstance(value, str): | ||
return Value(string=value) | ||
elif isinstance(value, datetime): | ||
return Value(timestamp=datetime_to_proto(value)) | ||
elif isinstance(value, (list, set)): | ||
fv = Value(string_set=StringSet(values=value)) | ||
return fv | ||
else: | ||
raise ValueError(f"Unsupported ingest field value type: {type(value)}") | ||
|
||
|
||
def datetime_to_proto(dt: datetime) -> Timestamp: | ||
dt_ts = dt.timestamp() | ||
return Timestamp(seconds=int(dt_ts), nanos=int((dt_ts % 1) * 1e9)) | ||
|
||
|
||
def make_step(number: float | int, raise_on_step_precision_loss: bool = False) -> Step: | ||
""" | ||
Converts a number to protobuf Step value. Example: | ||
>>> assert make_step(7.654321, True) == Step(whole=7, micro=654321) | ||
Args: | ||
number: step expressed as number | ||
raise_on_step_precision_loss: inform converter whether it should silently drop precision and | ||
round down to 6 decimal places or raise an error. | ||
|
||
Returns: Step protobuf used in Neptune API. | ||
""" | ||
m = int(1e6) | ||
micro: int = int(number * m) | ||
if raise_on_step_precision_loss and number * m - micro != 0: | ||
raise ValueError(f"step must not use more than 6-decimal points, got: {number}") | ||
|
||
whole = micro // m | ||
micro = micro % m | ||
|
||
return Step(whole=whole, micro=micro) |
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.
Or just have OpeartionsQueue own the lock? It doesn't seem Run is using it, apart from passing to the OperationsQueue constructor.
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.
In the following PRs we're making sure that no one is putting any new operations once we're operating on SyncProcess so this cannot be a part of
OperationsQueue