Skip to content

Commit

Permalink
Merge branch 'main' into refactor-max-statements
Browse files Browse the repository at this point in the history
  • Loading branch information
jcjones committed Mar 1, 2024
2 parents b9693ed + 65a56d7 commit af93096
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
17 changes: 6 additions & 11 deletions partitionmanager/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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}")
Expand Down
42 changes: 39 additions & 3 deletions partitionmanager/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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"])
Expand All @@ -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):
Expand Down
4 changes: 0 additions & 4 deletions wheel2deb.yml

This file was deleted.

0 comments on commit af93096

Please sign in to comment.