def _search_metrics_from_components(self, glob, components): search = self._create_search_query().source('name') # Handle glob with globstar(s). globstars = components.count(bg_glob.Globstar()) if globstars: name_regexp = "\\.".join([_parse_regexp_component(c) for c in components]) return True, search.filter('regexp', **{"name": name_regexp}) # TODO (t.chataigner) Handle fully defined prefix (like a.b.c.*.*.*) # with a wildcard on name. # Handle fully defined glob. if self.__glob_parser.is_fully_defined(components): return False, search.filter( 'term', **{"name": bg_accessor.sanitize_metric_name(glob)}) # Handle all other use cases. for i, c in enumerate(components): if len(c) == 1: filter_type, value = parse_simple_component(c) else: filter_type, value = parse_complex_component(c) if filter_type: search = search.filter(filter_type, **{"p%d" % i: value}) return False, search
def delete_metric(self, name): name = bg_accessor.sanitize_metric_name(name) query = self._create_search_query() \ .filter('term', name=name) log.debug(json.dumps(query.to_dict())) query.delete()
def get_metric(self, metric_name): """See the real Accessor for a description.""" super(_ElasticSearchAccessor, self).get_metric(metric_name) metric_name = bg_accessor.sanitize_metric_name(metric_name) document = self.__get_document(metric_name) if document is None: return None return self._document_to_metric(document)
def touch_metric(self, metric): """See the real Accessor for a description.""" super(_ElasticSearchAccessor, self).touch_metric(metric) metric_name = bg_accessor.sanitize_metric_name(metric.name) document = self.__get_document(metric_name) if not document.updated_on: delta = self.__updated_on_ttl_sec + 1 else: updated_on_timestamp = ttls.str_to_timestamp(document.updated_on) delta = int(time.time()) - int(updated_on_timestamp) if delta >= self.__updated_on_ttl_sec: self.__touch_document(document)
def update_metric(self, name, updated_metadata): """See bg_accessor.Accessor.""" super(_ElasticSearchAccessor, self).update_metric(name, updated_metadata) name = bg_accessor.sanitize_metric_name(name) metric = self.get_metric(name) if metric is None: raise InvalidArgumentError("Unknown metric '%s'" % name) updated_metric = self.make_metric( name, updated_metadata, created_on=metric.created_on, updated_on=datetime.datetime.now(), read_on=metric.read_on ) self.create_metric(updated_metric)
def document_from_metric(metric): """Creates an ElasticSearch document from a Metric.""" config = metric.metadata.as_string_dict() components = _components_from_name(metric.name) name = bg_accessor.sanitize_metric_name(metric.name) data = { "depth": len(components) - 1, "name": name, } for i, component in enumerate(components): data["p%d" % i] = component data.update({ "uuid": metric.id, "created_on": datetime.datetime.now(), "updated_on": datetime.datetime.now(), "read_on": None, "config": config, }) return data
def test_sanitize_metric_name_should_handle_empty_value(self): self.assertEqual("", bg_accessor.sanitize_metric_name("")) self.assertEqual("", bg_accessor.sanitize_metric_name(".")) self.assertEqual("", bg_accessor.sanitize_metric_name(".."))
def test_sanitize_metric_name_should_handle_None_value(self): self.assertEqual(None, bg_accessor.sanitize_metric_name(None))
def test_sanitize_metric_name_should_trim_heading_dots(self): self.assertEqual("foo.bar", bg_accessor.sanitize_metric_name(".foo.bar")) self.assertEqual("foo.bar", bg_accessor.sanitize_metric_name("..foo.bar"))
def test_sanitize_metric_name_should_remove_multiple_dots(self): self.assertEqual("foo.bar.baz", bg_accessor.sanitize_metric_name("foo.bar..baz")) self.assertEqual("foo.bar.baz", bg_accessor.sanitize_metric_name("foo.bar...baz")) self.assertEqual("foo.bar.baz", bg_accessor.sanitize_metric_name("foo..bar..baz"))