Add suggest_cache command and tests

This commit is contained in:
Jordan Wages 2025-07-19 02:07:23 -05:00
commit 7f996fe123
2 changed files with 169 additions and 0 deletions

View file

@ -143,5 +143,59 @@ def check_missing_domains(json_output: bool = typer.Option(False, "--json", help
typer.echo(d)
@app.command("suggest-cache")
def suggest_cache(
threshold: int = typer.Option(
10, help="Minimum number of MISS entries to report"
),
json_output: bool = typer.Option(False, "--json", help="Output results as JSON"),
) -> None:
"""Suggest domain/path pairs that could benefit from caching.
Paths with at least ``threshold`` ``MISS`` entries are shown for domains
whose server blocks lack a ``proxy_cache`` directive.
"""
# Discover domains without explicit proxy_cache
paths = nginx_config.discover_configs()
servers = nginx_config.parse_servers(paths)
no_cache: Set[str] = set()
for server in servers:
if "proxy_cache" in server:
continue
for name in server.get("server_name", "").split():
if name:
no_cache.add(name)
conn = _connect()
cur = conn.cursor()
cur.execute(
"""
SELECT host,
substr(request, instr(request, ' ')+1,
instr(request, ' HTTP') - instr(request, ' ') - 1) AS path,
COUNT(*) AS miss_count
FROM logs
WHERE cache_status = 'MISS'
GROUP BY host, path
HAVING miss_count >= ?
ORDER BY miss_count DESC
""",
(threshold,),
)
rows = [r for r in cur.fetchall() if r[0] in no_cache]
conn.close()
if json_output:
result = [
{"host": host, "path": path, "misses": count} for host, path, count in rows
]
typer.echo(json.dumps(result))
else:
for host, path, count in rows:
typer.echo(f"{host} {path} {count}")
if __name__ == "__main__":
app()