diff --git a/AGENTS.md b/AGENTS.md index ab65e99..50a1938 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -21,8 +21,8 @@ This document outlines general practices and expectations for AI agents assistin source .venv/bin/activate pip install -r requirements.txt ``` - The `init.sh` script can create this environment automatically. Always - activate it before running scripts or tests. + The `run-import.sh` script can initialize this environment automatically. + Always activate the virtual environment before running scripts or tests. * Dependency management: Use `requirements.txt` or `pip-tools` * Use standard libraries where feasible (e.g., `sqlite3`, `argparse`, `datetime`) diff --git a/README.md b/README.md index bc2db2d..9c370c9 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,13 @@ python scripts/generate_reports.py monthly ``` Reports are written under the `output/` directory. Each command updates the corresponding `.json` file and produces an HTML dashboard using Chart.js. + +## Importing Logs + +Use the `run-import.sh` script to set up the Python environment if needed and import the latest Nginx log entries into `database/ngxstat.db`. + +```bash +./run-import.sh +``` + +This script is suitable for cron jobs as it creates the virtual environment on first run, installs dependencies and reuses the environment on subsequent runs. diff --git a/init.sh b/init.sh deleted file mode 100755 index d951d8d..0000000 --- a/init.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash -set -e - -echo "[INFO] Creating virtual environment..." -python3 -m venv .venv -source .venv/bin/activate - -echo "[INFO] Installing dependencies..." -pip install --upgrade pip -pip install -r requirements.txt || echo "[WARN] requirements.txt not found, skipping." - -echo "[INFO] Running database setup..." -python scripts/init_db.py diff --git a/run-import.sh b/run-import.sh index d951d8d..22b4b31 100755 --- a/run-import.sh +++ b/run-import.sh @@ -1,13 +1,28 @@ -#!/bin/bash +#!/usr/bin/env bash set -e -echo "[INFO] Creating virtual environment..." -python3 -m venv .venv -source .venv/bin/activate +# Ensure virtual environment exists +if [ ! -d ".venv" ]; then + echo "[INFO] Creating virtual environment..." + python3 -m venv .venv + source .venv/bin/activate + echo "[INFO] Installing dependencies..." + pip install --upgrade pip + if [ -f requirements.txt ]; then + pip install -r requirements.txt + else + echo "[WARN] requirements.txt not found, skipping." + fi +else + echo "[INFO] Activating virtual environment..." + source .venv/bin/activate +fi -echo "[INFO] Installing dependencies..." -pip install --upgrade pip -pip install -r requirements.txt || echo "[WARN] requirements.txt not found, skipping." - -echo "[INFO] Running database setup..." +# Run log import +echo "[INFO] Importing logs..." python scripts/init_db.py + +# Deactivate to keep cron environment clean +if type deactivate >/dev/null 2>&1; then + deactivate +fi