forked from windelbouwman/lognplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregation.py
More file actions
44 lines (38 loc) · 1.45 KB
/
Copy pathaggregation.py
File metadata and controls
44 lines (38 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import operator
from functools import reduce
from ..time import TimeSpan
from .metrics import Metrics
class Aggregation:
def __init__(self, timespan: TimeSpan, metrics: Metrics):
self.timespan = timespan
self.metrics = metrics
@classmethod
def from_sample(cls, sample):
timestamp, value = sample
timespan = TimeSpan(timestamp, timestamp)
metrics = Metrics.from_value(value)
return cls(timespan, metrics)
@staticmethod
def from_samples(samples):
""" Take a bunch of samples, and convert into a single metric. """
assert samples
return reduce(operator.add, map(Aggregation.from_sample, samples))
@classmethod
def from_aggregations(cls, aggregations):
assert aggregations
metrics = []
timespans = []
for aggregation in aggregations:
assert isinstance(aggregation, Aggregation)
metrics.append(aggregation.metrics)
timespans.append(aggregation.timespan)
timespan = TimeSpan.from_timespans(timespans)
metrics = Metrics.from_metrics(metrics)
return cls(timespan, metrics)
def __add__(self, other):
if isinstance(other, Aggregation):
metrics = self.metrics + other.metrics
timespan = TimeSpan.from_timespans([self.timespan, other.timespan])
return Aggregation(timespan, metrics)
else: # pragma: no cover
return NotImplemented