-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
103 lines (85 loc) · 2.78 KB
/
utils.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import sys
import network
import math
from typing import Union, List, Tuple, Dict, TYPE_CHECKING
INF = float("inf")
MINUS_INF = float("-inf")
NaN = float("NaN")
wlan_status_code = {
network.STAT_IDLE: 'Idle',
network.STAT_CONNECTING: 'Connecting',
network.STAT_WRONG_PASSWORD: 'Wrong Password',
network.STAT_NO_AP_FOUND: 'No AP Found',
network.STAT_GOT_IP: 'Connected'
}
def prom_metric_str(
name: str, help: str, values: List[Tuple[Dict, Union[int, float]]],
metric_type: str = 'gauge'
) -> str:
"""
Generate and return a Prometheus client-compatible exposition string for
a given metric, which has one or more values.
:param name: Prometheus metric name
:param help: help/description string for the metric
:param values: Current values for the metric, as a list of 2-tuples where
the first item is a dictionary of labels and the second items is the
integer or float value.
:param metric_type: Metric type, i.e. gauge
"""
s = '# HELP ' + name + ' ' + help + '\n' + \
'# TYPE ' + name + ' ' + metric_type + '\n'
labels: Dict
value: Union[int, float]
for labels, value in values:
s += name + '{' + ','.join([
f'{k}="{v}"' for k, v in sorted(labels.items())
]) + '} ' + floatToGoString(value) + '\n'
return s
def time_to_unix_time(t: Union[int, float]) -> int:
"""
Return a timestamp in integer seconds since January 1, 1970.
"""
if sys.platform in ['esp32', 'esp8266']:
# 946684800.0 is 2000-01-01 00:00:00 UTC which is used as the
# epoch on ESP systems
return int(t) + 946684800
else:
return int(t)
def floatToGoString(d) -> str:
if d == 1:
return '1.0'
if d == 0:
return '0.0'
# from https://github.com/prometheus/client_python/blob/master/prometheus_client/utils.py#L8
d = float(d)
if d == INF:
return '+Inf'
elif d == MINUS_INF:
return '-Inf'
elif math.isnan(d):
return 'NaN'
else:
s = repr(d)
dot = s.find('.')
# Go switches to exponents sooner than Python.
# We only need to care about positive values for le/quantile.
if d > 0 and dot > 6:
mantissa = f'{s[0]}.{s[1:dot]}{s[dot + 1:]}'.rstrip('0.')
return f'{mantissa}e+0{dot - 1}'
return s
class Logger:
"""Stand-in for a real logging library"""
def debug(self, *args):
if len(args) == 0:
print(args[0])
return
print(args[0] % args[1:])
def info(self, *args):
return self.debug(*args)
def warning(self, *args):
return self.debug(*args)
def error(self, *args):
return self.debug(*args)
def critical(self, *args):
return self.debug(*args)
logger = Logger()