summaryrefslogtreecommitdiff
path: root/src/metrics/statistics.py
blob: 9ad7bf07ac97c0b7b7a1720dcd42fa7500b7bd16 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import copy

class Stats(dict):

    def __init__(self, count=0, total=0.0, min=0.0, max=0.0,
            sum_of_squares=0.0):

        # Attribute names here must not change as this is what
        # New Relic uses. Easier to adopt that convention rather
        # than something slightly different.

        self.count = count
        self.total = total
        self.min = min
        self.max = max
        self.sum_of_squares = sum_of_squares

    def __setattr__(self, name, value):
        self[name] = value

    def __getattr__(self, name):
        return self[name]

    def merge_stats(self, other):
        self.total += other.total
        self.min = self.count and min(self.min, other.min) or other.min
        self.max = max(self.max, other.max)
        self.sum_of_squares += other.sum_of_squares
        self.count += other.count

    def merge_value(self, value):
        self.total += value
        self.min = self.count and min(self.min, value) or value
        self.max = max(self.max, value)
        self.sum_of_squares += value ** 2
        self.count += 1

class Metrics(object):

    def __init__(self):
        self.metrics = {}

    def __iter__(self):
        return iter(self.metrics.items())

    def __len__(self):
        return len(self.metrics)

    def assign_value(self, name, value):
        if isinstance(value, Stats):
            sample = copy.copy(value)
            self.metrics[name] = sample
        else:
            sample = Stats()
            self.metrics[name] = sample
            sample.merge_value(value)

        return sample

    def merge_value(self, name, value):
        sample = self.fetch_stats(name)

        if isinstance(value, Stats):
            sample.merge_stats(value)
        else:
            sample.merge_value(value)

        return sample

    def fetch_stats(self, name):
        sample = self.metrics.get(name)

        if sample is None:
            sample = Stats()
            self.metrics[name] = sample

        return sample

    def merge_metrics(self, metrics):
        for name, stats in metrics:
            self.merge_value(name, stats)

    def assign_metrics(self, metrics):
        for name, stats in metrics:
            self.assign_value(name, stats)

    def clear_metrics(self):
        self.metrics.clear()