| Conditions | 4 |
| Total Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
| 1 | # -*- coding: utf-8 -*- |
||
| 32 | def per_second(self, name, val, ts): |
||
| 33 | """Records and returns the rate of change of a metric in units/second. |
||
| 34 | |||
| 35 | Assumes values will be < MAX_CNT and detects and corrects overflow. |
||
| 36 | |||
| 37 | :param name: The name of a metric |
||
| 38 | :type name: str |
||
| 39 | :param val: The value of a metric |
||
| 40 | :type val: int or float |
||
| 41 | :param max: The maximum possible value for val |
||
| 42 | :type val: int or float |
||
| 43 | :rtype: int or float |
||
| 44 | """ |
||
| 45 | ret = type(val)(0) |
||
| 46 | if name not in self.metrics: |
||
| 47 | self.metrics[name] = (val, ts) |
||
| 48 | else: |
||
| 49 | # get the previous value |
||
| 50 | pval, ptime = self.metrics[name] |
||
| 51 | self.metrics[name] = (val, ts) |
||
| 52 | # check for counter wrap |
||
| 53 | if val < pval: |
||
| 54 | pval = pval - MAX_CNT |
||
| 55 | dval = val - pval |
||
| 56 | dtime = ts - ptime |
||
| 57 | if dtime > 0: |
||
| 58 | ret = float(dval) / float(dtime) |
||
| 59 | else: |
||
| 60 | ret = 0 |
||
| 61 | return ret |
||
| 62 |