summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/diskprediction_cloud/common/__init__.py
blob: ce5131b8f2b906a749717e7b6be1b41930d6596d (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
from __future__ import absolute_import
import errno
from functools import wraps
import os
import signal


DP_MGR_STAT_OK = 'OK'
DP_MGR_STAT_WARNING = 'WARNING'
DP_MGR_STAT_FAILED = 'FAILED'
DP_MGR_STAT_DISABLED = 'DISABLED'
DP_MGR_STAT_ENABLED = 'ENABLED'


class DummyResonse:
    def __init__(self):
        self.resp_json = dict()
        self.content = 'DummyResponse'
        self.status_code = 404

    def json(self):
        return self.resp_json

    def __str__(self):
        return '{}'.format({'resp': self.resp_json, 'content': self.content, 'status_code': self.status_code})


class TimeoutError(Exception):
    pass


def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
    def decorator(func):
        def _handle_timeout(signum, frame):
            raise TimeoutError(error_message)

        def wrapper(*args, **kwargs):
            if hasattr(args[0], '_timeout') is not None:
                seconds = args[0]._timeout
            signal.signal(signal.SIGALRM, _handle_timeout)
            signal.alarm(seconds)
            try:
                result = func(*args, **kwargs)
            finally:
                signal.alarm(0)
            return result

        return wraps(func)(wrapper)

    return decorator


def get_human_readable(size, precision=2):
    suffixes = ['B', 'KB', 'MB', 'GB', 'TB']
    suffix_index = 0
    while size > 1000 and suffix_index < 4:
        # increment the index of the suffix
        suffix_index += 1
        # apply the division
        size = size/1000.0
    return '%.*d %s' % (precision, size, suffixes[suffix_index])