def test_get_metrics_excludes_abstract_metrics(): class AbstractMetric(metrics.Metric): class Meta: abstract = True class ConcreteMetric(AbstractMetric): class Meta: app_label = "anotherapp" assert metrics.Metric not in registry.get_metrics() assert AbstractMetric not in registry.get_metrics() assert ConcreteMetric in registry.get_metrics()
def handle(self, *args, **options): style = color_style() connection = options["connection"] if options["app_label"]: if options["app_label"] not in registry.all_metrics: raise CommandError("No metrics found for app '{}'".format( options["app_label"])) app_labels = [options["app_label"]] else: app_labels = registry.all_metrics.keys() if connection: self.stdout.write("Using connection: '{}'".format(connection)) for app_label in app_labels: self.stdout.write( "Syncing metrics for app: '{}'".format(app_label), style.MIGRATE_HEADING) metrics = registry.get_metrics(app_label=app_label) for metric in metrics: metric_name = style.METRIC(metric.__name__) template_name = metric._template_name template = style.ES_TEMPLATE(metric._template) self.stdout.write( " Syncing {metric_name} -> {template_name} ({template})". format(**locals())) metric.sync_index_template(using=connection) self.stdout.write("Synchronized metrics.", style.SUCCESS)
def test_get_metrics(): class AnotherMetric(metrics.Metric): class Meta: app_label = "anotherapp" assert DummyMetric in registry.get_metrics() assert AnotherMetric in registry.get_metrics() assert DummyMetric in registry.get_metrics(app_label="dummyapp") assert AnotherMetric not in registry.get_metrics(app_label="dummyapp") with pytest.raises(LookupError) as excinfo: registry.get_metrics("notanapp") assert "No metrics found in app with label 'notanapp'." in excinfo.value.args[0]
def handle(self, *args, **options): style = color_style() if options["app_label"]: if options["app_label"] not in registry.all_metrics: raise CommandError("No metrics found for app '{}'".format( options["app_label"])) app_labels = [options["app_label"]] else: app_labels = registry.all_metrics.keys() for app_label in app_labels: self.stdout.write("Metrics for '{}':".format(app_label), style.MIGRATE_HEADING) metrics = registry.get_metrics(app_label=app_label) for metric in metrics: metric_name = style.METRIC(metric.__name__) template_name = metric._template_name template = style.ES_TEMPLATE(metric._template) self.stdout.write( " {metric_name} -> {template_name} ({template})".format( **locals()))
def handle(self, *args, **options): # Avoid elasticsearch requests from getting logged logging.getLogger("elasticsearch").setLevel(logging.CRITICAL) style = color_style() connection = options["connection"] if options["app_label"]: if options["app_label"] not in registry.all_metrics: raise CommandError("No metrics found for app '{}'".format( options["app_label"])) app_labels = [options["app_label"]] else: app_labels = registry.all_metrics.keys() out_of_sync_count = 0 self.stdout.write("Checking for outdated index templates...") for app_label in app_labels: metrics = registry.get_metrics(app_label=app_label) for metric in metrics: try: metric.check_index_template(using=connection) except ( exceptions.IndexTemplateNotFoundError, exceptions.IndexTemplateOutOfSyncError, ) as error: self.stdout.write(" " + error.args[0]) out_of_sync_count += 1 if out_of_sync_count: self.stdout.write( "{} index template(s) out of sync.".format(out_of_sync_count), style.ERROR, ) cmd = colorize("python manage.py sync_metrics", opts=("bold", )) self.stdout.write("Run {cmd} to synchronize.".format(cmd=cmd)) sys.exit(1) else: self.stdout.write("All metrics in sync.", style.SUCCESS)
def get_metrics(self): return { each.__name__: each for each in metrics_registry.get_metrics() }
def test_without_args(run_mgmt_command, mock_sync_index_template): out, err = run_mgmt_command(Command, ["sync_metrics"]) assert mock_sync_index_template.call_count == len(registry.get_metrics()) assert "Synchronized metrics." in out
def test_exits_with_success(run_mgmt_command, mock_check_index_template): mock_check_index_template.return_value = True run_mgmt_command(Command, ["check_metrics"]) assert mock_check_index_template.call_count == len(registry.get_metrics())
def get_metrics(self): return {each.__name__: each for each in metrics_registry.get_metrics()}