382825085f
Nové SQL migrace pro alert_rules a alert_events v migrations.py (line 165). Nová ruční DB vrstva v alerting.py (line 7). Nové stránky a akce v routes/alerting.py (line 288):Alert pravidla, detail, vytvoření, úprava, smazání povolit/zakázat pravidlo Alert eventy a detail eventu s payload_json odkazy z job_id na detail úlohy detail pravidla s posledními eventy admin-only editor .sh alert skriptů v /opt/appfactory/workspace/appfactory-tools/alerts audit eventy podle zadání Menu Provoz → Alerting v layout.py (line 23). Router je zaregistrovaný v main.py (line 8).
203 lines
7.6 KiB
Python
203 lines
7.6 KiB
Python
from app.db.database import get_connection
|
|
|
|
|
|
def run_migrations():
|
|
con = get_connection()
|
|
|
|
con.execute(
|
|
"""
|
|
CREATE TABLE IF NOT EXISTS jobs (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
type TEXT NOT NULL,
|
|
target_type TEXT NOT NULL,
|
|
target_id TEXT NOT NULL,
|
|
payload_json TEXT NOT NULL DEFAULT '{}',
|
|
status TEXT NOT NULL DEFAULT 'queued',
|
|
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
started_at TEXT,
|
|
finished_at TEXT,
|
|
created_by_user_id INTEGER,
|
|
created_by_username TEXT,
|
|
created_by_display_name TEXT,
|
|
source TEXT NOT NULL DEFAULT 'portal',
|
|
worker_id TEXT,
|
|
result_json TEXT,
|
|
error_text TEXT
|
|
)
|
|
"""
|
|
)
|
|
con.execute(
|
|
"""
|
|
CREATE TABLE IF NOT EXISTS job_logs (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
job_id INTEGER NOT NULL,
|
|
stream TEXT NOT NULL,
|
|
message TEXT NOT NULL,
|
|
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY(job_id) REFERENCES jobs(id)
|
|
)
|
|
"""
|
|
)
|
|
con.execute(
|
|
"""
|
|
CREATE TABLE IF NOT EXISTS workers (
|
|
id TEXT PRIMARY KEY,
|
|
status TEXT NOT NULL DEFAULT 'online',
|
|
started_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
last_seen_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
current_job_id INTEGER,
|
|
metadata_json TEXT
|
|
)
|
|
"""
|
|
)
|
|
|
|
con.execute("CREATE INDEX IF NOT EXISTS idx_jobs_status_created ON jobs(status, created_at)")
|
|
con.execute("CREATE INDEX IF NOT EXISTS idx_jobs_target_status ON jobs(target_type, target_id, status)")
|
|
con.execute("CREATE INDEX IF NOT EXISTS idx_job_logs_job_id ON job_logs(job_id, id)")
|
|
con.execute("CREATE INDEX IF NOT EXISTS idx_workers_last_seen ON workers(last_seen_at)")
|
|
|
|
try:
|
|
con.execute("ALTER TABLE jobs ADD COLUMN created_by_display_name TEXT")
|
|
except Exception:
|
|
pass
|
|
|
|
try:
|
|
con.execute("ALTER TABLE deployments ADD COLUMN job_id INTEGER")
|
|
except Exception:
|
|
pass
|
|
|
|
for statement in (
|
|
"ALTER TABLE apps ADD COLUMN description TEXT",
|
|
"ALTER TABLE apps ADD COLUMN owner TEXT",
|
|
"ALTER TABLE apps ADD COLUMN template TEXT",
|
|
"ALTER TABLE apps ADD COLUMN runtime TEXT",
|
|
"ALTER TABLE apps ADD COLUMN repository_url TEXT",
|
|
"ALTER TABLE apps ADD COLUMN repository_name TEXT",
|
|
"ALTER TABLE apps ADD COLUMN default_branch TEXT",
|
|
"ALTER TABLE apps ADD COLUMN domain TEXT",
|
|
"ALTER TABLE apps ADD COLUMN health_url TEXT",
|
|
"ALTER TABLE apps ADD COLUMN container_port INTEGER",
|
|
"ALTER TABLE apps ADD COLUMN is_public INTEGER NOT NULL DEFAULT 0",
|
|
"ALTER TABLE apps ADD COLUMN is_enabled INTEGER NOT NULL DEFAULT 1",
|
|
):
|
|
try:
|
|
con.execute(statement)
|
|
except Exception:
|
|
pass
|
|
|
|
con.execute(
|
|
"""
|
|
CREATE TABLE IF NOT EXISTS app_templates (
|
|
id TEXT PRIMARY KEY,
|
|
name TEXT NOT NULL,
|
|
runtime TEXT,
|
|
language TEXT,
|
|
description TEXT,
|
|
default_port INTEGER,
|
|
default_health_path TEXT,
|
|
create_script TEXT,
|
|
deploy_script TEXT,
|
|
is_enabled INTEGER NOT NULL DEFAULT 1,
|
|
create_enabled INTEGER NOT NULL DEFAULT 0,
|
|
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
"""
|
|
)
|
|
con.execute(
|
|
"""
|
|
CREATE TABLE IF NOT EXISTS app_variables (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
app_id TEXT NOT NULL,
|
|
"key" TEXT NOT NULL,
|
|
value TEXT,
|
|
is_secret INTEGER NOT NULL DEFAULT 0,
|
|
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY(app_id) REFERENCES apps(id)
|
|
)
|
|
"""
|
|
)
|
|
con.execute(
|
|
"""
|
|
CREATE TABLE IF NOT EXISTS service_incidents (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
service_id TEXT NOT NULL,
|
|
status TEXT NOT NULL,
|
|
started_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
ended_at TEXT,
|
|
duration_seconds INTEGER,
|
|
title TEXT,
|
|
description TEXT
|
|
)
|
|
"""
|
|
)
|
|
for statement in (
|
|
"ALTER TABLE app_templates ADD COLUMN id TEXT",
|
|
"ALTER TABLE app_templates ADD COLUMN name TEXT",
|
|
"ALTER TABLE app_templates ADD COLUMN runtime TEXT",
|
|
"ALTER TABLE app_templates ADD COLUMN language TEXT",
|
|
"ALTER TABLE app_templates ADD COLUMN description TEXT",
|
|
"ALTER TABLE app_templates ADD COLUMN default_port INTEGER",
|
|
"ALTER TABLE app_templates ADD COLUMN default_health_path TEXT",
|
|
"ALTER TABLE app_templates ADD COLUMN create_script TEXT",
|
|
"ALTER TABLE app_templates ADD COLUMN deploy_script TEXT",
|
|
"ALTER TABLE app_templates ADD COLUMN is_enabled INTEGER NOT NULL DEFAULT 1",
|
|
"ALTER TABLE app_templates ADD COLUMN create_enabled INTEGER NOT NULL DEFAULT 0",
|
|
"ALTER TABLE app_templates ADD COLUMN created_at TEXT",
|
|
"ALTER TABLE app_templates ADD COLUMN updated_at TEXT",
|
|
"ALTER TABLE app_variables ADD COLUMN created_at TEXT",
|
|
"ALTER TABLE app_variables ADD COLUMN updated_at TEXT",
|
|
):
|
|
try:
|
|
con.execute(statement)
|
|
except Exception:
|
|
pass
|
|
try:
|
|
con.execute("UPDATE app_templates SET id = name WHERE id IS NULL OR id = ''")
|
|
except Exception:
|
|
pass
|
|
con.execute("CREATE INDEX IF NOT EXISTS idx_app_variables_app_id ON app_variables(app_id)")
|
|
con.execute("CREATE INDEX IF NOT EXISTS idx_service_incidents_status_started ON service_incidents(status, started_at)")
|
|
con.execute("CREATE INDEX IF NOT EXISTS idx_service_incidents_service_started ON service_incidents(service_id, started_at)")
|
|
con.execute(
|
|
"""
|
|
CREATE TABLE IF NOT EXISTS alert_rules (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
description TEXT,
|
|
event_type TEXT NOT NULL,
|
|
service_id TEXT,
|
|
script_name TEXT NOT NULL,
|
|
is_enabled INTEGER NOT NULL DEFAULT 1,
|
|
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
"""
|
|
)
|
|
con.execute(
|
|
"""
|
|
CREATE TABLE IF NOT EXISTS alert_events (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
rule_id INTEGER,
|
|
event_type TEXT NOT NULL,
|
|
service_id TEXT,
|
|
incident_id INTEGER,
|
|
status TEXT,
|
|
job_id INTEGER,
|
|
error_text TEXT,
|
|
payload_json TEXT,
|
|
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
processed_at TEXT
|
|
)
|
|
"""
|
|
)
|
|
con.execute("CREATE INDEX IF NOT EXISTS idx_alert_rules_event_enabled ON alert_rules(event_type, is_enabled)")
|
|
con.execute("CREATE INDEX IF NOT EXISTS idx_alert_rules_service ON alert_rules(service_id)")
|
|
con.execute("CREATE INDEX IF NOT EXISTS idx_alert_events_rule_created ON alert_events(rule_id, created_at)")
|
|
con.execute("CREATE INDEX IF NOT EXISTS idx_alert_events_created ON alert_events(created_at)")
|
|
con.execute("CREATE INDEX IF NOT EXISTS idx_alert_events_job_id ON alert_events(job_id)")
|
|
|
|
con.commit()
|
|
con.close()
|