diff --git a/partitionmanager/cli.py b/partitionmanager/cli.py index 2733381..43c48ee 100644 --- a/partitionmanager/cli.py +++ b/partitionmanager/cli.py @@ -119,7 +119,7 @@ def from_yaml_file(self, file): self.dbcmd = partitionmanager.sql.SubprocessDatabaseCommand( data["mariadb"] ) - if not self.tables: # Only load tables froml YAML if not supplied via args + if not self.tables: # Only load tables from YAML if not supplied via args for key in data["tables"]: tab = partitionmanager.types.Table(key) tabledata = data["tables"][key] @@ -179,14 +179,6 @@ def _extract_single_column(row): return row[columns[0]] -def list_tables(conf): - """List all tables for the current database.""" - rows = conf.dbcmd.run("SHOW TABLES;") - table_names = (_extract_single_column(row) for row in rows) - table_objects = (partitionmanager.types.Table(name) for name in table_names) - return list(table_objects) - - def partition_cmd(args): """Runs do_partition on the config that results from the CLI arguments. @@ -379,13 +371,16 @@ def do_partition(conf): return all_results -def do_stats(conf, metrics=partitionmanager.stats.PrometheusMetrics()): +def do_stats(conf, metrics=None): """Populates a metrics object from the tables in the configuration.""" log = logging.getLogger("do_stats") + if not metrics: + metrics = partitionmanager.stats.PrometheusMetrics() + all_results = {} - for table in list_tables(conf): + for table in conf.tables: table_problems = pm_tap.get_table_compatibility_problems(conf.dbcmd, table) if table_problems: log.debug(f"Cannot gather statistics for {table}: {table_problems}") diff --git a/partitionmanager/cli_test.py b/partitionmanager/cli_test.py index 3fd7edc..8ce5ff6 100644 --- a/partitionmanager/cli_test.py +++ b/partitionmanager/cli_test.py @@ -279,13 +279,17 @@ def assert_stats_results(self, results): results["partitioned_yesterday"]["max_partition_delta"].days, 2 ) - def assert_stats_prometheus_outfile(self, prom_file): + def parse_prometheus_outfile(self, prom_file): lines = prom_file.split("\n") metrics = {} for line in lines: if not line.startswith("#") and len(line) > 0: key, value = line.split(" ") metrics[key] = value + return metrics + + def assert_stats_prometheus_outfile(self, prom_file): + metrics = self.parse_prometheus_outfile(prom_file) for table in ["partitioned_last_week", "partitioned_yesterday", "other"]: self.assertIn(f'partition_total{{table="{table}"}}', metrics) @@ -303,7 +307,7 @@ def assert_stats_prometheus_outfile(self, prom_file): def test_stats_cli_flag(self): args = PARSER.parse_args(["--mariadb", str(fake_exec), "stats"]) results = stats_cmd(args) - self.assert_stats_results(results) + assert results == {} def test_stats_yaml(self): with tempfile.NamedTemporaryFile( @@ -314,7 +318,9 @@ def test_stats_yaml(self): mariadb: {str(fake_exec)} prometheus_stats: {stats_outfile.name} tables: - unused: + other: + partitioned_last_week: + partitioned_yesterday: """ insert_into_file(tmpfile, yaml) args = PARSER.parse_args(["--config", tmpfile.name, "stats"]) @@ -324,6 +330,36 @@ def test_stats_yaml(self): self.assert_stats_results(results) self.assert_stats_prometheus_outfile(stats_outfile.read()) + def test_stats_yaml_ignore_unconfigured_tables(self): + with tempfile.NamedTemporaryFile( + mode="w+", encoding="UTF-8" + ) as stats_outfile, tempfile.NamedTemporaryFile() as tmpfile: + yaml = f""" + partitionmanager: + mariadb: {str(fake_exec)} + prometheus_stats: {stats_outfile.name} + tables: + other: + """ + insert_into_file(tmpfile, yaml) + args = PARSER.parse_args(["--config", tmpfile.name, "stats"]) + + results = stats_cmd(args) + + assert list(results.keys()) == ["other"] + + out_data = stats_outfile.read() + + metrics = self.parse_prometheus_outfile(out_data) + assert list(metrics.keys()) == [ + 'partition_total{table="other"}', + 'partition_time_remaining_until_partition_overrun{table="other"}', + 'partition_age_of_retained_partitions{table="other"}', + 'partition_mean_delta_seconds{table="other"}', + 'partition_max_delta_seconds{table="other"}', + "partition_last_run_timestamp{}", + ] + class TestConfig(unittest.TestCase): def test_cli_tables_override_yaml(self): diff --git a/wheel2deb.yml b/wheel2deb.yml deleted file mode 100644 index 2a1ee5a..0000000 --- a/wheel2deb.yml +++ /dev/null @@ -1,4 +0,0 @@ -mariadb-sequential-partition-manager: - maintainer_name: "Let's Encrypt" - maintainer_email: "opensource@letsencrypt.org" - extended_desc: "Manage DB partitions based on sequential IDs"