65 lines
2.4 KiB
Python
65 lines
2.4 KiB
Python
"""queries/logs.py — rpa_logs"""
|
|
|
|
from .db import query, count, e, dt, badge
|
|
|
|
|
|
def get_logs(db_path: str) -> list:
|
|
return query(db_path,
|
|
"SELECT l.*, COALESCE(p.process_name, 'unknown') AS process_name "
|
|
"FROM rpa_logs l "
|
|
"LEFT JOIN rpa_process p ON p.id = l.rpa_process_id "
|
|
"ORDER BY l.date_log DESC LIMIT 500")
|
|
|
|
|
|
def get_last_run_ids(db_path: str, n: int = 20) -> list:
|
|
"""Returns the last N distinct rpa_process_id values with process_name and start_run date, DESC."""
|
|
return query(db_path,
|
|
"SELECT l.rpa_process_id, COALESCE(p.process_name, 'unknown') AS process_name, "
|
|
"COALESCE(p.start_run, '') AS start_run "
|
|
"FROM rpa_logs l "
|
|
"LEFT JOIN rpa_process p ON p.id = l.rpa_process_id "
|
|
"WHERE l.rpa_process_id IS NOT NULL "
|
|
"GROUP BY l.rpa_process_id ORDER BY l.rpa_process_id DESC LIMIT ?", (n,))
|
|
|
|
|
|
def get_logs_by_run(db_path: str, run_id: int) -> list:
|
|
return query(db_path,
|
|
"SELECT l.*, COALESCE(p.process_name, 'unknown') AS process_name "
|
|
"FROM rpa_logs l "
|
|
"LEFT JOIN rpa_process p ON p.id = l.rpa_process_id "
|
|
"WHERE l.rpa_process_id=? ORDER BY l.date_log DESC", (run_id,))
|
|
|
|
|
|
def get_stats(db_path: str) -> dict:
|
|
return {
|
|
'log_errors': count(db_path, "SELECT COUNT(*) as n FROM rpa_logs WHERE type='error'"),
|
|
'log_warnings': count(db_path, "SELECT COUNT(*) as n FROM rpa_logs WHERE type='warning'"),
|
|
}
|
|
|
|
|
|
def render_table(rows: list) -> str:
|
|
if not rows:
|
|
return '<p class="empty">Nessun log trovato.</p>'
|
|
|
|
def _badge(tipo):
|
|
m = {'log': ('log', 'Log'), 'warning': ('warn', 'Warning'), 'error': ('err', 'Error')}
|
|
cls, lbl = m.get(tipo, ('', ''))
|
|
return badge(cls, lbl) if cls else e(tipo)
|
|
|
|
trs = ''.join(
|
|
f'<tr data-type="{e(r.get("type", ""))}"'
|
|
f' data-process-name="{e(r.get("process_name", "unknown"))}"'
|
|
f' data-date="{str(r.get("date_log") or "")[:10]}">'
|
|
f'<td><span class="proc-name-badge">{e(r.get("process_name", "—"))}</span> <span class="run-id">#{e(r.get("rpa_process_id"))}</span></td>'
|
|
f'<td>{dt(r.get("date_log"))}</td>'
|
|
f'<td>{_badge(r.get("type"))}</td>'
|
|
f'<td class="nc">{e(r.get("log"))}</td>'
|
|
f'</tr>'
|
|
for r in rows
|
|
)
|
|
return (
|
|
'<table><thead><tr>'
|
|
'<th>Processo</th><th>Data</th><th>Tipo</th><th>Messaggio</th>'
|
|
f'</tr></thead><tbody>{trs}</tbody></table>'
|
|
)
|