Skip to content

Commit

Permalink
WIP - make multiple tag values be queried in one query statement - #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan committed Jan 17, 2017
1 parent 6c14fbe commit b470fcd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
34 changes: 25 additions & 9 deletions influxgraph/classes/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ def _get_all_template_values(self, paths):
query_data = deque()
path_measurements = {}
for (_filter, template, default_tags, separator) in self.graphite_templates:
# One influx query statement per template
if not paths:
break
_measurements, _tags, _fields, matched_paths = \
Expand All @@ -383,28 +384,42 @@ def _get_all_template_values(self, paths):
return query_data, path_measurements

def _gen_query(self, _measurements, _tags, _fields, retention):
# import ipdb; ipdb.set_trace()
measurements = ', '.join(
('"%s"."%s"' % (retention, measure,) for measure in _measurements)) \
if retention \
else ', '.join(('"%s"' % (measure,) for measure in _measurements))
tag_sets = [[""""%s" = '%s'""" % (tag, tag_val,)
for tag_val in _tags[tag]]
for tag in _tags] \
if _tags else None
tag_pairs = itertools.product(*tag_sets) if tag_sets else None
tags = [" AND ".join(t) for t in tag_pairs] if tag_pairs else None
groupings = deque()
tags = deque()
for tag in _tags:
if len(_tags[tag]) > 1:
groupings.append(tag)
continue
tags.append(""""%s" = '%s'""" % (tag, _tags[tag],))
# tag_sets = [[""""%s" = '%s'""" % (tag, tag_val,)
# for tag_val in _tags[tag]]
# for tag in _tags] \
# if _tags else None
####
# tags = [' AND '.join(['(%s)' % ' OR '.join(
# [""""%s" = '%s'""" % (tag, tag_val,) for tag_val in _tags[tag]])
# for tag in _tags])] if _tags else None
####
# tag_pairs = itertools.product(*tag_sets) if tag_sets else None
# tags = [" AND ".join(t) for t in tag_pairs] if tag_pairs else None
fields = _fields if _fields else ['value']
return measurements, tags, fields
return measurements, tags, fields, groupings

def _gen_query_values_from_templates(self, paths, retention):
_query_data, path_measurements = self._get_all_template_values(paths)
if len(_query_data) == 0:
return
query_data = deque()
for (_measurements, _tags, _fields) in _query_data:
measurements, tags, fields = self._gen_query(
measurements, tags, fields, groupings = self._gen_query(
_measurements, _tags, _fields, retention)
query_data.append((measurements, tags, fields),)
query_data.append((measurements, tags, fields, groupings),)
# import ipdb; ipdb.set_trace()
return query_data, path_measurements

def _gen_query_values(self, paths, retention):
Expand Down Expand Up @@ -491,6 +506,7 @@ def fetch_multi(self, nodes, start_time, end_time):
logger.debug("Calling influxdb multi fetch with query - %s", query)
data = self.client.query(query, params=_INFLUXDB_CLIENT_PARAMS)
logger.debug('fetch_multi() - Retrieved %d result set(s)', len(data))
# import ipdb; ipdb.set_trace()
data = read_influxdb_values(data, paths, path_measurements)
timer.stop()
# Graphite API requires that data contain keys for
Expand Down
1 change: 1 addition & 0 deletions tests/test_influxdb_templates_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ def test_template_multi_tags_multi_templ_multi_nodes_no_fields(self):
int(self.end_time.strftime("%s")))
self.assertTrue(data[cpu_metric_nodes[0].path][-1] == idle_data['value'],
msg="Got incorrect data from multi-tag query")
# import ipdb; ipdb.set_trace()

def test_template_multi_tags_multi_templ_multi_nodes(self):
self.client.drop_database(self.db_name)
Expand Down

0 comments on commit b470fcd

Please sign in to comment.