From 8ea0ff877c087dc9adcaf3db984777aa09f0abf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Bompard?= Date: Fri, 21 Apr 2023 14:56:02 +0200 Subject: [PATCH] Add a CLI to get the stored build durations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Aurélien Bompard --- fmn/cache/base.py | 4 ++-- fmn/cache/cli.py | 12 ++++++++++++ tests/cache/test_cli.py | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/fmn/cache/base.py b/fmn/cache/base.py index bcde1b242..1aecd035d 100644 --- a/fmn/cache/base.py +++ b/fmn/cache/base.py @@ -52,12 +52,12 @@ def __init__(self): async def compute_value(self, db: "AsyncSession"): log.debug(f"Building the {self.name} cache") before = monotonic() - now = datetime.utcnow().isoformat() + now = datetime.utcnow().replace(microsecond=0).isoformat() value = await self._compute_value(db=db) after = monotonic() duration = after - before log.debug(f"Built the {self.name} cache in %.2f seconds", duration) - await cache.set(key=f"duration:{self.name}:{now}", value=duration, expire="31d") + await cache.set(key=f"duration:{self.name}:{now}", value=int(duration), expire="31d") return value async def _compute_value(self, db: "AsyncSession"): diff --git a/fmn/cache/cli.py b/fmn/cache/cli.py index e56439091..6e6a4062d 100644 --- a/fmn/cache/cli.py +++ b/fmn/cache/cli.py @@ -91,3 +91,15 @@ async def _doit(): click.echo(f"The {cache_value.name} cache is recent enough.") asyncio.run(_doit()) + + +@cache_cmd.command("get-build-durations") +def get_build_durations(): + async def _do_it(): + configure_cache() + async for key in cache.scan("duration:*"): + duration = await cache.get(key) + _, name, when = key.split(":", 2) + click.echo(f"Built {name} on {when} in {duration:.02f}s") + + asyncio.run(_do_it()) diff --git a/tests/cache/test_cli.py b/tests/cache/test_cli.py index 6780d7ae9..09c78957e 100644 --- a/tests/cache/test_cli.py +++ b/tests/cache/test_cli.py @@ -116,3 +116,21 @@ async def _set_cache(): assert result.exit_code == 0, result.output assert result.output == "Deleted lock for foo\nDeleted lock for bar\nCache locks deleted.\n" + + +@pytest.mark.cashews_cache(enabled=True) +def test_get_build_durations(mocker, cli_runner): + async def _set_cache(): + await cache.set("duration:foo:2022-01-01T01:01:01", value=42.42424242) + await cache.set("duration:bar:2023-02-01T01:01:01", value=12.345678) + + mocker.patch("fmn.cache.cli.configure_cache") + asyncio.run(_set_cache()) + result = cli_runner.invoke(cli, ["cache", "get-build-durations"]) + + assert result.exit_code == 0, result.output + expected_msg = ( + "Built foo on 2022-01-01T01:01:01 in 42.42s\n" + "Built bar on 2023-02-01T01:01:01 in 12.35s\n" + ) + assert result.output == expected_msg