-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathelement.py
More file actions
177 lines (144 loc) · 4.88 KB
/
element.py
File metadata and controls
177 lines (144 loc) · 4.88 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import re
import datetime
from .attribute import Attribute
from .metric import Metric
from .sample import Sample
from .tag import Tag
from .relation import Relation
from .util import to_ms_timestamp_int
class Element(object):
"""
An entity that represents the host that the agent runs on
:param ElementType: Type of the Element
:type ElementType: string
"""
def __init__(self, ElementType='SERVER', location=None):
self.type = ElementType
self.tags = []
self.attributes = []
self.relations = []
self.metrics = []
self.samples = []
self.id = None
self.name = None
self._metrics = {}
if location is not None:
self.location = location
def _sanitize(self, s):
"""
Sanitize the name of a metric to remove unwanted chars
"""
return re.sub('[^a-zA-Z0-9\\._-]', '_', s)
def merge_metrics(self):
"""
Merge metrics in the internal _metrics dict to metrics list
and delete the internal _metrics
"""
self.metrics.extend(self._metrics.values())
del self._metrics
def add_attribute(self, name, value):
"""
:param name: Name of the attribute
:type name: string
:param value: Value of the attribute
:type value: string
"""
self.attributes.append(Attribute(name, value))
def add_relation(self, fqn):
"""
:param fqn: FQN of the other Element
:type name: string
"""
self.relations.append(Relation(fqn))
def add_tag(self, name, value):
"""
:param name: Name of the tag
:type name: string
:param value: Value of the tag
:type value: string
"""
self.tags.append(Tag(name, value))
def add_sample(self,
metricId,
timestamp,
value,
metricType=None,
host=None,
sparseDataStrategy='None',
unit='',
tags=None,
min=None,
max=None,
avg=None,
sum=None,
cnt=None,
ts_is_ms=False):
"""
:param metricId: Metric FQN
:type metricId: string
:param timestamp: Timestamp for the sample
:type timestamp: int
:param value: Value of the sample
:type value: float
:param metricType: Metric Type
:type metricType: string
:param host: Element FQN
:type host: string
:param sparseDataStrategy: Sparse data strategy
:type sparseDataStrategy: string
:param unit: Metric Unit type
:type unit: string
:param tags: List of dicts
:type tags: list
:param min: Minimum of the sample
:type min: float
:param max: Maximum of the sample
:type max: float
:param avg: Average of the sample
:type avg: float
:param sum: Sum of the sample
:type sum: float
:param cnt: Count of the sample
:type cnt: float
:param ts_is_ms: Is the timestamp in milliseconds
:type ts_is_ms: bool
"""
if self.id is None and host is not None:
self.id = host
if self.name is None and host is not None:
self.name = host
if tags is not None:
Tags = []
for i in tags:
for k in i:
Tags.append(Tag(k, i[k]))
else:
Tags = None
metricIdSan = self._sanitize(metricId)
if not hasattr(self, "_metrics"):
setattr(self, "_metrics", {})
if self._metrics.get(metricIdSan) is None:
self._metrics[metricIdSan] = Metric(metricIdSan,
metricType,
sparseDataStrategy,
unit,
Tags)
if timestamp is None:
ts = to_ms_timestamp_int(datetime.datetime.utcnow())
else:
if ts_is_ms:
ts = int(timestamp)
else:
ts = int(timestamp * 1000)
self.samples.append(Sample(metricIdSan,
ts,
value,
min,
max,
avg,
sum,
cnt))
def clear_samples(self):
self.metrics = []
setattr(self, "_metrics", {})
self.samples = []