Total Complexity | 4 |
Total Lines | 27 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
1 | # -*- coding: utf-8 -*- |
||
15 | class Differential(object): |
||
16 | """Computes the rate of change of a set of metrics.""" |
||
17 | |||
18 | def __init__(self): |
||
19 | self.metrics = {} |
||
20 | |||
21 | def per_second(self, name, val, ts): |
||
22 | """Records and returns the rate of change of a metric in units/second. |
||
23 | |||
24 | :param name: The name of a metric |
||
25 | :type name: str |
||
26 | :param val: The value of a metric |
||
27 | :type val: int or float |
||
28 | :rtype: int or float |
||
29 | """ |
||
30 | ret = type(val)(0) |
||
31 | if name not in self.metrics: |
||
32 | self.metrics[name] = (val, ts) |
||
33 | else: |
||
34 | pval, ptime = self.metrics[name] |
||
35 | dval = val - pval |
||
36 | dtime = ts - ptime |
||
37 | if dtime > 0: |
||
38 | ret = dval / dtime |
||
39 | else: |
||
40 | ret = 0 |
||
41 | return ret |
||
42 |