|
| 1 | +import argparse |
| 2 | +import datetime |
| 3 | +import json |
| 4 | + |
| 5 | +from enapter import cli, http |
| 6 | + |
| 7 | + |
| 8 | +class TelemetryTimeseriesCommand(cli.Command): |
| 9 | + |
| 10 | + @staticmethod |
| 11 | + def register(parent: cli.Subparsers) -> None: |
| 12 | + parser = parent.add_parser( |
| 13 | + "timeseries", formatter_class=argparse.ArgumentDefaultsHelpFormatter |
| 14 | + ) |
| 15 | + parser.add_argument( |
| 16 | + "-f", |
| 17 | + "--time-from", |
| 18 | + required=True, |
| 19 | + help="Start time for the telemetry data (ISO 8601 format)", |
| 20 | + ) |
| 21 | + parser.add_argument( |
| 22 | + "-t", "--time-to", help="End time for the telemetry data (ISO 8601 format)" |
| 23 | + ) |
| 24 | + parser.add_argument( |
| 25 | + "-s", |
| 26 | + "--shape", |
| 27 | + choices=["w", "wide", "l", "long"], |
| 28 | + default="long", |
| 29 | + help="Shape of the output data", |
| 30 | + ) |
| 31 | + parser.add_argument( |
| 32 | + "-g", |
| 33 | + "--granularity", |
| 34 | + type=int, |
| 35 | + default=60 * 60, |
| 36 | + help="Granularity of the telemetry data in seconds", |
| 37 | + ) |
| 38 | + parser.add_argument( |
| 39 | + "selectors", |
| 40 | + metavar="device:attr1,attr2,...,attrN", |
| 41 | + nargs="+", |
| 42 | + help="Device ID or slug followed by a colon and a comma-separated list of attribute names to fetch the telemetry for", |
| 43 | + ) |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + async def run(args: argparse.Namespace) -> None: |
| 47 | + attributes_by_device = {} |
| 48 | + for selector in args.selectors: |
| 49 | + device, attributes = selector.split(":", 1) |
| 50 | + attributes_by_device[device] = attributes.split(",") |
| 51 | + async with http.api.Client(http.api.Config.from_env()) as client: |
| 52 | + time_to = ( |
| 53 | + datetime.datetime.fromisoformat(args.time_to) |
| 54 | + if args.time_to is not None |
| 55 | + else datetime.datetime.now(datetime.timezone.utc) |
| 56 | + ) |
| 57 | + time_from = ( |
| 58 | + datetime.datetime.fromisoformat(args.time_from) |
| 59 | + if args.time_from is not None |
| 60 | + else time_to - datetime.timedelta(minutes=10) |
| 61 | + ) |
| 62 | + match args.shape: |
| 63 | + case "l" | "long": |
| 64 | + await TelemetryTimeseriesCommand._handle_long_timeseries( |
| 65 | + args, client, time_from, time_to, attributes_by_device |
| 66 | + ) |
| 67 | + case "w" | "wide": |
| 68 | + await TelemetryTimeseriesCommand._handle_wide_timeseries( |
| 69 | + args, client, time_from, time_to, attributes_by_device |
| 70 | + ) |
| 71 | + case _: |
| 72 | + raise NotImplementedError(args.shape) |
| 73 | + |
| 74 | + @staticmethod |
| 75 | + async def _handle_long_timeseries( |
| 76 | + args: argparse.Namespace, |
| 77 | + client: http.api.Client, |
| 78 | + time_from: datetime.datetime, |
| 79 | + time_to: datetime.datetime, |
| 80 | + attributes_by_device: dict[str, list[str]], |
| 81 | + ) -> None: |
| 82 | + async with client.telemetry.long_timeseries( |
| 83 | + from_=time_from, |
| 84 | + to=time_to, |
| 85 | + granularity=args.granularity, |
| 86 | + selectors=[ |
| 87 | + http.api.telemetry.Selector(device=device, attributes=attributes) |
| 88 | + for device, attributes in attributes_by_device.items() |
| 89 | + ], |
| 90 | + ) as stream: |
| 91 | + async for row in stream: |
| 92 | + print(json.dumps(row.to_dto())) |
| 93 | + |
| 94 | + @staticmethod |
| 95 | + async def _handle_wide_timeseries( |
| 96 | + args: argparse.Namespace, |
| 97 | + client: http.api.Client, |
| 98 | + time_from: datetime.datetime, |
| 99 | + time_to: datetime.datetime, |
| 100 | + attributes_by_device: dict[str, list[str]], |
| 101 | + ) -> None: |
| 102 | + wide_timeseries = await client.telemetry.wide_timeseries( |
| 103 | + from_=time_from, |
| 104 | + to=time_to, |
| 105 | + granularity=args.granularity, |
| 106 | + selectors=[ |
| 107 | + http.api.telemetry.Selector(device=device, attributes=attributes) |
| 108 | + for device, attributes in attributes_by_device.items() |
| 109 | + ], |
| 110 | + ) |
| 111 | + print(json.dumps(wide_timeseries.to_dto())) |
0 commit comments