|
21 | 21 | import collections |
22 | 22 | import base64 |
23 | 23 | import ssl |
| 24 | +from datetime import datetime, timedelta |
24 | 25 |
|
25 | 26 | try: |
26 | 27 | import simplejson as json |
@@ -58,6 +59,8 @@ class HawkularMetricsClient(HawkularBaseClient): |
58 | 59 | """ |
59 | 60 | Internal methods |
60 | 61 | """ |
| 62 | + epoch = datetime.utcfromtimestamp(0) |
| 63 | + |
61 | 64 | def _get_url(self, metric_type=None): |
62 | 65 | if metric_type is None: |
63 | 66 | metric_type = MetricType._Metrics |
@@ -139,32 +142,70 @@ def push(self, metric_type, metric_id, value, timestamp=None): |
139 | 142 | :param metric_type: MetricType to be matched (required) |
140 | 143 | :param metric_id: Exact string matching metric id |
141 | 144 | :param value: Datapoint value (depending on the MetricType) |
142 | | - :param timestamp: Timestamp of the datapoint. If left empty, uses current client time. |
| 145 | + :param timestamp: Timestamp of the datapoint. If left empty, uses current client time. Can be milliseconds since epoch or datetime instance |
143 | 146 | """ |
| 147 | + if type(timestamp) is datetime: |
| 148 | + timestamp = datetime_to_time_millis(timestamp) |
| 149 | + |
144 | 150 | item = create_metric(metric_type, metric_id, create_datapoint(value, timestamp)) |
145 | 151 | self.put(item) |
146 | 152 |
|
147 | | - def query_metric(self, metric_type, metric_id, **query_options): |
| 153 | + def query_metric(self, metric_type, metric_id, start=None, end=None, **query_options): |
148 | 154 | """ |
149 | 155 | Query for metrics datapoints from the server. |
150 | 156 |
|
151 | 157 | :param metric_type: MetricType to be matched (required) |
152 | 158 | :param metric_id: Exact string matching metric id |
| 159 | + :param start: Milliseconds since epoch or datetime instance |
| 160 | + :param end: Milliseconds since epoch or datetime instance |
153 | 161 | :param query_options: For possible query_options, see the Hawkular-Metrics documentation. |
154 | 162 | """ |
| 163 | + if start is not None: |
| 164 | + if type(start) is datetime: |
| 165 | + query_options['start'] = datetime_to_time_millis(start) |
| 166 | + else: |
| 167 | + query_options['start'] = start |
| 168 | + |
| 169 | + if end is not None: |
| 170 | + if type(end) is datetime: |
| 171 | + query_options['end'] = datetime_to_time_millis(end) |
| 172 | + else: |
| 173 | + query_options['end'] = end |
| 174 | + |
155 | 175 | return self._get( |
156 | 176 | self._get_metrics_raw_url( |
157 | 177 | self._get_metrics_single_url(metric_type, metric_id)), |
158 | 178 | **query_options) |
159 | 179 |
|
160 | | - def query_metric_stats(self, metric_type, metric_id, **query_options): |
| 180 | + def query_metric_stats(self, metric_type, metric_id, start=None, end=None, bucketDuration=None, **query_options): |
161 | 181 | """ |
162 | 182 | Query for metric aggregates from the server. This is called buckets in the Hawkular-Metrics documentation. |
163 | 183 |
|
164 | 184 | :param metric_type: MetricType to be matched (required) |
165 | 185 | :param metric_id: Exact string matching metric id |
| 186 | + :param start: Milliseconds since epoch or datetime instance |
| 187 | + :param end: Milliseconds since epoch or datetime instance |
| 188 | + :param bucketDuration: The timedelta or duration of buckets. Can be a string presentation or timedelta object |
166 | 189 | :param query_options: For possible query_options, see the Hawkular-Metrics documentation. |
167 | 190 | """ |
| 191 | + if start is not None: |
| 192 | + if type(start) is datetime: |
| 193 | + query_options['start'] = datetime_to_time_millis(start) |
| 194 | + else: |
| 195 | + query_options['start'] = start |
| 196 | + |
| 197 | + if end is not None: |
| 198 | + if type(end) is datetime: |
| 199 | + query_options['end'] = datetime_to_time_millis(end) |
| 200 | + else: |
| 201 | + query_options['end'] = end |
| 202 | + |
| 203 | + if bucketDuration is not None: |
| 204 | + if type(bucketDuration) is timedelta: |
| 205 | + query_options['bucketDuration'] = timedelta_to_duration(bucketDuration) |
| 206 | + else: |
| 207 | + query_options['bucketDuration'] = bucketDuration |
| 208 | + |
168 | 209 | return self._get( |
169 | 210 | self._get_metrics_stats_url( |
170 | 211 | self._get_metrics_single_url(metric_type, metric_id)), |
@@ -312,17 +353,26 @@ def time_millis(): |
312 | 353 | """ |
313 | 354 | return int(round(time.time() * 1000)) |
314 | 355 |
|
| 356 | +def timedelta_to_duration(td): |
| 357 | + return '{}s'.format(int(td.total_seconds())) |
| 358 | + |
| 359 | +def datetime_to_time_millis(dt): |
| 360 | + return '{:.0f}'.format((dt - HawkularMetricsClient.epoch).total_seconds() * 1000) |
| 361 | + |
315 | 362 | def create_datapoint(value, timestamp=None, **tags): |
316 | 363 | """ |
317 | 364 | Creates a single datapoint dict with a value, timestamp and tags. |
318 | 365 |
|
319 | 366 | :param value: Value of the datapoint. Type depends on the id's MetricType |
320 | | - :param timestamp: Optional timestamp of the datapoint. Uses client current time if not set. Millisecond accuracy |
| 367 | + :param timestamp: Optional timestamp of the datapoint. Uses client current time if not set. Millisecond accuracy. Can be datetime instance also. |
321 | 368 | :param tags: Optional datapoint tags. Not to be confused with metric definition tags |
322 | 369 | """ |
323 | 370 | if timestamp is None: |
324 | 371 | timestamp = time_millis() |
325 | 372 |
|
| 373 | + if type(timestamp) is datetime: |
| 374 | + timestamp = datetime_to_time_millis(timestamp) |
| 375 | + |
326 | 376 | item = { 'timestamp': timestamp, |
327 | 377 | 'value': value } |
328 | 378 |
|
|
0 commit comments