33
44"""Types for the Reporting API client."""
55
6- import math
76from collections .abc import Iterable , Iterator
87from dataclasses import dataclass
98from datetime import datetime , timezone
@@ -70,11 +69,13 @@ def is_empty(self) -> bool:
7069 return True
7170 return False
7271
72+ # pylint: disable=too-many-locals
73+ # pylint: disable=too-many-branches
7374 def __iter__ (self ) -> Iterator [MetricSample ]:
7475 """Get generator that iterates over all values in the batch.
7576
76- Note: So far only `SimpleMetricSample ` in the `MetricSampleVariant`
77- message is supported.
77+ `SimpleMetricSample` and `AggregatedMetricSample ` in the
78+ `MetricSampleVariant` message is supported.
7879
7980
8081 Yields:
@@ -93,12 +94,26 @@ def __iter__(self) -> Iterator[MetricSample]:
9394 for sample in getattr (item , "metric_samples" , []):
9495 ts = sample .sampled_at .ToDatetime ().replace (tzinfo = timezone .utc )
9596 met = Metric .from_proto (sample .metric ).name
96- value = (
97- sample .value .simple_metric .value
98- if sample .value .HasField ("simple_metric" )
99- else math .nan
100- )
101- yield MetricSample (ts , mid , cid , met , value )
97+
98+ # Handle simple_metric
99+ if sample .value .HasField ("simple_metric" ):
100+ value = sample .value .simple_metric .value
101+ yield MetricSample (ts , mid , cid , met , value )
102+
103+ # Handle aggregated_metric
104+ if sample .value .HasField ("aggregated_metric" ):
105+ agg = sample .value .aggregated_metric
106+ # Average value
107+ yield MetricSample (ts , mid , cid , f"{ met } _avg" , agg .avg_value )
108+ # Min value if present
109+ if agg .HasField ("min_value" ):
110+ yield MetricSample (ts , mid , cid , f"{ met } _min" , agg .min_value )
111+ # Max value if present
112+ if agg .HasField ("max_value" ):
113+ yield MetricSample (ts , mid , cid , f"{ met } _max" , agg .max_value )
114+ # Optionally yield individual raw values
115+ for i , raw in enumerate (agg .raw_values ):
116+ yield MetricSample (ts , mid , cid , f"{ met } _raw_{ i } " , raw )
102117
103118 if self .has_bounds :
104119 for i , bound in enumerate (sample .bounds ):
0 commit comments