-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python(feature): downloading telemetry (#72)
- Loading branch information
1 parent
25e2733
commit 79a8daf
Showing
23 changed files
with
2,008 additions
and
8 deletions.
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,18 @@ | ||
""" | ||
Utilities to interact with APIs that have a CEL-based interface. | ||
""" | ||
|
||
from typing import Iterable | ||
|
||
|
||
def cel_in(field: str, values: Iterable[str]) -> str: | ||
""" | ||
Produces a list membership CEL expression. Example: | ||
```python | ||
> print(cel_in("name", ["foo", "bar"])) | ||
name in ["foo", "bar"] | ||
``` | ||
""" | ||
items = ",".join([f'"{val}"' for val in values]) | ||
return f"{field} in [{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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from typing import Optional | ||
|
||
|
||
def channel_fqn(name: str, component: Optional[str]) -> str: | ||
return name if component is None or len(component) == 0 else f"{component}.{name}" |
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,9 @@ | ||
from datetime import datetime | ||
|
||
from google.protobuf.timestamp_pb2 import Timestamp | ||
|
||
|
||
def to_pb_timestamp(timestamp: datetime) -> Timestamp: | ||
timestamp_pb = Timestamp() | ||
timestamp_pb.FromDatetime(timestamp) | ||
return timestamp_pb |
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,48 @@ | ||
from datetime import datetime, timezone | ||
from typing import Union, cast | ||
|
||
import pandas as pd | ||
from google.protobuf.timestamp_pb2 import Timestamp as TimestampPb | ||
|
||
|
||
def to_timestamp_nanos(arg: Union[TimestampPb, pd.Timestamp, datetime, str, int]) -> pd.Timestamp: | ||
""" | ||
Converts a variety of time-types to a pandas timestamp which supports nano-second precision. | ||
""" | ||
|
||
if isinstance(arg, pd.Timestamp): | ||
return arg | ||
elif isinstance(arg, TimestampPb): | ||
seconds = arg.seconds | ||
nanos = arg.nanos | ||
|
||
dt = datetime.fromtimestamp(seconds, tz=timezone.utc) | ||
ts = pd.Timestamp(dt) | ||
|
||
return cast(pd.Timestamp, ts + pd.Timedelta(nanos, unit="ns")) | ||
|
||
elif isinstance(arg, int): | ||
dt = datetime.fromtimestamp(arg, tz=timezone.utc) | ||
return cast(pd.Timestamp, pd.Timestamp(dt)) | ||
|
||
else: | ||
return cast(pd.Timestamp, pd.Timestamp(arg)) | ||
|
||
|
||
def to_timestamp_pb(arg: Union[datetime, str, int]) -> TimestampPb: | ||
""" | ||
Mainly used for testing at the moment. If using this for non-testing purposes | ||
should probably make this more robust and support nano-second precision. | ||
""" | ||
|
||
ts = TimestampPb() | ||
|
||
if isinstance(arg, datetime): | ||
ts.FromDatetime(arg) | ||
return ts | ||
elif isinstance(arg, int): | ||
ts.FromDatetime(datetime.fromtimestamp(arg)) | ||
return ts | ||
else: | ||
ts.FromDatetime(datetime.fromisoformat(arg)) | ||
return ts |
Oops, something went wrong.