Place generated marker in output directory

This commit is contained in:
Jordan Wages 2025-08-02 03:12:59 -05:00
commit 2300849fdc
2 changed files with 32 additions and 3 deletions

View file

@ -15,6 +15,7 @@ DB_PATH = Path("database/ngxstat.db")
OUTPUT_DIR = Path("output") OUTPUT_DIR = Path("output")
TEMPLATE_DIR = Path("templates") TEMPLATE_DIR = Path("templates")
REPORT_CONFIG = Path("reports.yml") REPORT_CONFIG = Path("reports.yml")
GENERATED_MARKER = OUTPUT_DIR / "generated.txt"
# Mapping of interval names to SQLite strftime formats. These strings are # Mapping of interval names to SQLite strftime formats. These strings are
# substituted into report queries whenever the special ``{bucket}`` token is # substituted into report queries whenever the special ``{bucket}`` token is
@ -30,6 +31,18 @@ INTERVAL_FORMATS = {
app = typer.Typer(help="Generate aggregated log reports") app = typer.Typer(help="Generate aggregated log reports")
@app.callback()
def _cli_callback(ctx: typer.Context) -> None:
"""Register post-command hook to note generation time."""
def _write_marker() -> None:
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
GENERATED_MARKER.write_text(f"{timestamp}\n")
ctx.call_on_close(_write_marker)
def _get_domains() -> List[str]: def _get_domains() -> List[str]:
"""Return a sorted list of unique domains from the logs table.""" """Return a sorted list of unique domains from the logs table."""
conn = sqlite3.connect(DB_PATH) conn = sqlite3.connect(DB_PATH)

View file

@ -2,8 +2,10 @@ import sqlite3
from pathlib import Path from pathlib import Path
import json import json
import sys import sys
from datetime import datetime
import pytest import pytest
from typer.testing import CliRunner
REPO_ROOT = Path(__file__).resolve().parents[1] REPO_ROOT = Path(__file__).resolve().parents[1]
sys.path.append(str(REPO_ROOT)) sys.path.append(str(REPO_ROOT))
@ -199,9 +201,23 @@ def test_generate_root_index(tmp_path, sample_reports, monkeypatch):
assert '<option value="Global">' not in content assert '<option value="Global">' not in content
assert '<option value="analysis">' not in content assert '<option value="analysis">' not in content
# check for domain options
assert '<option value="foo.com">' in content def test_generated_marker_written(tmp_path, monkeypatch):
assert '<option value="bar.com">' in content out_dir = tmp_path / "output"
monkeypatch.setattr(gr, "OUTPUT_DIR", out_dir)
monkeypatch.setattr(gr, "TEMPLATE_DIR", REPO_ROOT / "templates")
monkeypatch.setattr(gr, "GENERATED_MARKER", out_dir / "generated.txt")
monkeypatch.setattr(gr, "_copy_icons", lambda: None)
(out_dir / "hourly").mkdir(parents=True)
runner = CliRunner()
result = runner.invoke(gr.app, ["index"])
assert result.exit_code == 0, result.output
marker = out_dir / "generated.txt"
assert marker.exists()
content = marker.read_text().strip()
datetime.strptime(content, "%Y-%m-%d %H:%M:%S")
def test_global_reports_once(tmp_path, sample_reports, monkeypatch): def test_global_reports_once(tmp_path, sample_reports, monkeypatch):