-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_ui.py
105 lines (83 loc) · 2.77 KB
/
web_ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import sqlite3
import toml
from flask import Flask, render_template, redirect
from utils import get_valid_directory
scheduler = None
config_path = get_valid_directory()
CONFIG_FILE = os.path.join(str(config_path), "config.toml")
def get_scheduler_instance():
global scheduler
if scheduler is None:
from scheduler import scheduler
return scheduler
app = Flask(__name__)
CONFIG = toml.load(CONFIG_FILE)
DB_PATH = CONFIG["settings"]["db_path"]
@app.route("/")
def index():
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# Fetch jobs and their latest logs
cursor.execute("SELECT DISTINCT job_id FROM job_execution_logs")
jobs = []
for row in cursor.fetchall():
job_id = row[0]
cursor.execute(
"""
SELECT timestamp, exit_code, execution_time
FROM job_execution_logs
WHERE job_id = ?
ORDER BY timestamp DESC
LIMIT 1
""",
(job_id,),
)
log = cursor.fetchone()
last_execution = log[0] if log else "N/A"
last_exit_code = log[1] if log else "N/A"
last_execution_time = log[2] if log else "N/A"
# Fetch the next execution time from APScheduler
apscheduler_job = get_scheduler_instance().get_job(job_id)
next_execution = apscheduler_job.next_run_time if apscheduler_job else "N/A"
jobs.append({
"id": job_id,
"last_execution": last_execution,
"last_exit_code": last_exit_code,
"last_execution_time": last_execution_time,
"next_execution": next_execution,
"condition": CONFIG["jobs"].get(job_id, {}).get("condition", "N/A"),
})
conn.close()
return render_template("index.html", jobs=jobs)
@app.route("/job/<job_id>")
def job_details(job_id):
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# Fetch execution logs for the selected job
cursor.execute(
"""
SELECT timestamp, exit_code, execution_time
FROM job_execution_logs
WHERE job_id = ?
ORDER BY timestamp DESC
""",
(job_id,),
)
logs = cursor.fetchall()
conn.close()
return render_template("job_details.html", job_id=job_id, logs=logs)
@app.route("/delete_logs/<job_id>")
def delete_logs(job_id):
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("DELETE FROM job_execution_logs WHERE job_id = ?", (job_id,))
conn.commit()
conn.close()
return redirect("/")
if __name__ == "__main__":
# Read host and port from config file
host = CONFIG["web_server"].get("host", "127.0.0.1")
port = CONFIG["web_server"].get("port", 5000)
# Run the Flask app
app.run(host=host, port=port, debug=False)