3434"""
3535TODO: Search datapoints with tags.. tag datapoints.
3636TODO: Allow changing instance's tenant?
37- TODO: Authentication when it's done..
37+ TODO: Authentication
3838TODO: Remove HawkularMetricsConnectionError and use HawkularMetricsError only?
3939TODO: HWKMETRICS-110 (fetching a single definition)
40- TODO: Tag queries, stats queries
40+ TODO: Stats queries
4141"""
4242
4343class MetricType :
@@ -50,6 +50,8 @@ class MetricType:
5050 def short (metric_type ):
5151 if metric_type is MetricType .Gauge :
5252 return 'gauge'
53+ elif metric_type is MetricType .Counter :
54+ return 'counter'
5355 else :
5456 return 'availability'
5557
@@ -132,6 +134,10 @@ def _get_metrics_tags_url(self, metrics_url):
132134
133135 def _get_tenants_url (self ):
134136 return self ._get_base_url () + 'tenants'
137+
138+ @staticmethod
139+ def _transform_tags (** tags ):
140+ return ',' .join ("%s:%s" % (key ,val ) for (key ,val ) in tags .items ())
135141
136142 def _http (self , url , method , data = None ):
137143 res = None
@@ -281,16 +287,38 @@ def query_single_availability(self, metric_id, **search_options):
281287 """
282288 return self .query_metric (MetricType .Availability , metric_id , ** search_options )
283289
284- def query_definitions (self , query_type ):
290+ def query_definitions (self , metric_type = None , id_filter = None , ** tags ):
291+ """
292+ Query available metric definitions. Available query options are id_filter for id filtering and tags (dict) as tag based query.
293+ Tags filtering is required for the id filtering to work.
294+ """
295+ if id is not None and tags is None :
296+ raise HawkularMetricsError ('Tags query is required when id filter is used' )
297+
298+ params = {}
299+
300+ if metric_type is not None :
301+ params = { 'type' : MetricType .short (metric_type ) }
302+
303+ if len (tags ) > 0 :
304+ params ['tags' ] = self ._transform_tags (** tags )
305+
306+ return self ._get (self ._get_url ('metrics' ), ** params )
307+
308+ def query_tagvalues (self , metric_type = None , ** tags ):
285309 """
286- Query available metric definitions.
310+ Query for possible tag values.
287311 """
288- definition_url = self ._get_url ('metrics' ) + '?type=' + MetricType .short (query_type )
289- return self ._get (definition_url )
312+ tagql = self ._transform_tags (** tags )
313+
314+ if metric_type is None :
315+ metric_type = 'metrics'
316+
317+ return self ._get (self ._get_metrics_tags_url (self ._get_url (metric_type )) + '/{}' .format (tagql ))
290318
291319 def create_metric_definition (self , metric_type , metric_id , ** tags ):
292320 """
293- Create metric definition with custom definition. **options should be a set of tags, such as
321+ Create metric definition with custom definition. **tags should be a set of tags, such as
294322 units, env ..
295323
296324 Use methods create_gauge_definition and create_availability_definition to avoid using
@@ -334,7 +362,6 @@ def query_metric_tags(self, metric_type, metric_id):
334362 """
335363 definition = self ._get (self ._get_metrics_tags_url (self ._get_metrics_single_url (metric_type , metric_id )))
336364 return definition
337- # return definition.get('tags', {})
338365
339366 def update_metric_tags (self , metric_type , metric_id , ** tags ):
340367 """
@@ -346,7 +373,7 @@ def delete_metric_tags(self, metric_type, metric_id, **deleted_tags):
346373 """
347374 Delete one or more tags from the metric definition. The tag values must match what's stored on the server.
348375 """
349- tags = ',' . join ( "%s:%s" % ( key , val ) for ( key , val ) in deleted_tags . items () )
376+ tags = self . _transform_tags ( ** deleted_tags )
350377 tags_url = self ._get_metrics_tags_url (self ._get_metrics_single_url (metric_type , metric_id )) + '/{0}' .format (tags )
351378
352379 self ._delete (tags_url )
0 commit comments