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