Completed
Push — master ( ee7b96...42a2db )
by Kenny
01:20
created

Differential   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 27
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 2 1
A per_second() 0 21 3
1
# -*- coding: utf-8 -*-
2
"""
3
.. moduleauthor:: Kenny Freeman <[email protected]>
4
5
"""
6
7
__author__ = 'Kenny Freeman'
8
__email__ = '[email protected]'
9
__version__ = '0.3.3'
10
__license__     = "ISCL"
11
__docformat__   = 'reStructuredText'
12
13
import time
14
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