|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, cast |
| 4 | + |
| 5 | +from sift.calculated_channels.v2.calculated_channels_pb2 import ( |
| 6 | + CalculatedChannelAbstractChannelReference, |
| 7 | +) |
| 8 | +from sift.exports.v1.exports_pb2 import ( |
| 9 | + AssetsAndTimeRange, |
| 10 | + CalculatedChannelConfig, |
| 11 | + ExportDataRequest, |
| 12 | + ExportDataResponse, |
| 13 | + ExportOptions, |
| 14 | + GetDownloadUrlRequest, |
| 15 | + GetDownloadUrlResponse, |
| 16 | + RunsAndTimeRange, |
| 17 | + TimeRange, |
| 18 | +) |
| 19 | +from sift.exports.v1.exports_pb2_grpc import ExportServiceStub |
| 20 | + |
| 21 | +from sift_client._internal.low_level_wrappers.base import LowLevelClientBase |
| 22 | +from sift_client._internal.util.timestamp import to_pb_timestamp |
| 23 | +from sift_client.sift_types.calculated_channel import CalculatedChannel, CalculatedChannelCreate |
| 24 | +from sift_client.transport import WithGrpcClient |
| 25 | + |
| 26 | +if TYPE_CHECKING: |
| 27 | + from datetime import datetime |
| 28 | + |
| 29 | + from sift_client.sift_types.export import ExportOutputFormat |
| 30 | + from sift_client.transport.grpc_transport import GrpcClient |
| 31 | + |
| 32 | + |
| 33 | +def _build_calc_channel_configs( |
| 34 | + calculated_channels: list[CalculatedChannel | CalculatedChannelCreate] | None, |
| 35 | +) -> list[CalculatedChannelConfig]: |
| 36 | + """Convert high-level calculated channel objects to proto CalculatedChannelConfig messages.""" |
| 37 | + if not calculated_channels: |
| 38 | + return [] |
| 39 | + configs = [] |
| 40 | + for cc in calculated_channels: |
| 41 | + if isinstance(cc, CalculatedChannelCreate): |
| 42 | + refs = cc.expression_channel_references or [] |
| 43 | + else: |
| 44 | + refs = cc.channel_references |
| 45 | + configs.append( |
| 46 | + CalculatedChannelConfig( |
| 47 | + name=cc.name, |
| 48 | + expression=cc.expression or "", |
| 49 | + channel_references=[ |
| 50 | + CalculatedChannelAbstractChannelReference( |
| 51 | + channel_reference=ref.channel_reference, |
| 52 | + channel_identifier=ref.channel_identifier, |
| 53 | + ) |
| 54 | + for ref in refs |
| 55 | + ], |
| 56 | + units=cc.units, |
| 57 | + ) |
| 58 | + ) |
| 59 | + return configs |
| 60 | + |
| 61 | + |
| 62 | +class ExportsLowLevelClient(LowLevelClientBase, WithGrpcClient): |
| 63 | + """Low-level client for the DataExportAPI. |
| 64 | +
|
| 65 | + This class provides a thin wrapper around the autogenerated gRPC bindings for the DataExportAPI. |
| 66 | + """ |
| 67 | + |
| 68 | + def __init__(self, grpc_client: GrpcClient): |
| 69 | + """Initialize the ExportsLowLevelClient. |
| 70 | +
|
| 71 | + Args: |
| 72 | + grpc_client: The gRPC client to use for making API calls. |
| 73 | + """ |
| 74 | + super().__init__(grpc_client) |
| 75 | + |
| 76 | + async def export_data( |
| 77 | + self, |
| 78 | + *, |
| 79 | + output_format: ExportOutputFormat, |
| 80 | + run_ids: list[str] | None = None, |
| 81 | + asset_ids: list[str] | None = None, |
| 82 | + start_time: datetime | None = None, |
| 83 | + stop_time: datetime | None = None, |
| 84 | + channel_ids: list[str] | None = None, |
| 85 | + calculated_channels: list[CalculatedChannel | CalculatedChannelCreate] | None = None, |
| 86 | + simplify_channel_names: bool = False, |
| 87 | + combine_runs: bool = False, |
| 88 | + split_export_by_asset: bool = False, |
| 89 | + split_export_by_run: bool = False, |
| 90 | + ) -> str: |
| 91 | + """Initiate a data export. |
| 92 | +
|
| 93 | + Builds the ExportDataRequest proto and makes the gRPC call. |
| 94 | + Sets whichever time_selection oneof fields are provided |
| 95 | + (run_ids, asset_ids, or time range); the server validates |
| 96 | + the request. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + The job ID for the background export. |
| 100 | + """ |
| 101 | + request = ExportDataRequest( |
| 102 | + output_format=output_format.value, |
| 103 | + export_options=ExportOptions( |
| 104 | + use_legacy_format=False, |
| 105 | + simplify_channel_names=simplify_channel_names, |
| 106 | + combine_runs=combine_runs, |
| 107 | + split_export_by_asset=split_export_by_asset, |
| 108 | + split_export_by_run=split_export_by_run, |
| 109 | + ), |
| 110 | + channel_ids=channel_ids or [], |
| 111 | + calculated_channel_configs=_build_calc_channel_configs(calculated_channels), |
| 112 | + ) |
| 113 | + |
| 114 | + if run_ids is not None: |
| 115 | + runs_and_time_range = RunsAndTimeRange(run_ids=run_ids) |
| 116 | + if start_time: |
| 117 | + runs_and_time_range.start_time.CopyFrom(to_pb_timestamp(start_time)) |
| 118 | + if stop_time: |
| 119 | + runs_and_time_range.stop_time.CopyFrom(to_pb_timestamp(stop_time)) |
| 120 | + request.runs_and_time_range.CopyFrom(runs_and_time_range) |
| 121 | + |
| 122 | + if asset_ids is not None: |
| 123 | + assets_and_time_range = AssetsAndTimeRange(asset_ids=asset_ids) |
| 124 | + if start_time: |
| 125 | + assets_and_time_range.start_time.CopyFrom(to_pb_timestamp(start_time)) |
| 126 | + if stop_time: |
| 127 | + assets_and_time_range.stop_time.CopyFrom(to_pb_timestamp(stop_time)) |
| 128 | + request.assets_and_time_range.CopyFrom(assets_and_time_range) |
| 129 | + |
| 130 | + if run_ids is None and asset_ids is None: |
| 131 | + time_range = TimeRange() |
| 132 | + if start_time: |
| 133 | + time_range.start_time.CopyFrom(to_pb_timestamp(start_time)) |
| 134 | + if stop_time: |
| 135 | + time_range.stop_time.CopyFrom(to_pb_timestamp(stop_time)) |
| 136 | + request.time_range.CopyFrom(time_range) |
| 137 | + |
| 138 | + response = await self._grpc_client.get_stub(ExportServiceStub).ExportData(request) |
| 139 | + response = cast("ExportDataResponse", response) |
| 140 | + return response.job_id |
| 141 | + |
| 142 | + async def get_download_url(self, job_id: str) -> str: |
| 143 | + """Get the download URL for a background export job. |
| 144 | +
|
| 145 | + Args: |
| 146 | + job_id: The job ID returned from export_data. |
| 147 | +
|
| 148 | + Returns: |
| 149 | + The presigned URL to download the exported zip file. |
| 150 | + """ |
| 151 | + request = GetDownloadUrlRequest(job_id=job_id) |
| 152 | + response = await self._grpc_client.get_stub(ExportServiceStub).GetDownloadUrl(request) |
| 153 | + response = cast("GetDownloadUrlResponse", response) |
| 154 | + return response.presigned_url |
0 commit comments