Compare commits
36 Commits
446f36fc5b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 7242392233 | |||
| 55e8356b1b | |||
| 8023978307 | |||
| 5617440013 | |||
| 7c5e15af99 | |||
| 89f5c4ef0b | |||
| e7cf9edde9 | |||
| a2b13f3d32 | |||
| 12af4e7bbe | |||
| 4cfd5b2dbe | |||
| 3b87da9292 | |||
| 2ab46f1994 | |||
| afd6383daa | |||
| 8a5ff68f23 | |||
| 05d9ea1104 | |||
| 139be129ee | |||
| e1b0ab4dc5 | |||
| cab5c3b7ec | |||
| 9e4d2e031d | |||
| e355999c53 | |||
| 39b4ee7a00 | |||
| 5ddafd3b98 | |||
| 5a7aa44d6c | |||
| dea5278731 | |||
| 9908683e45 | |||
| 96c8c2d5c8 | |||
| 87aacaa08a | |||
| bb8f3e6c71 | |||
| bf43999d36 | |||
| 3f4dc83929 | |||
| 5b1b43f9ad | |||
| 324f75d100 | |||
| a63e313036 | |||
| 0c580bc9f1 | |||
| 5a42d998d6 | |||
| 7ad601fc14 |
51
app.py
@@ -1,43 +1,20 @@
|
|||||||
# import requests as r
|
from app.flask_app import start_flask, stop_event as flask_stop
|
||||||
from flask import jsonify, render_template, send_file
|
from app.aio_client import start_worker, stop_event as aio_stop
|
||||||
from poll_services import start_async_loop
|
|
||||||
from mem import services, app, db
|
|
||||||
import threading
|
import threading
|
||||||
from flask_migrate import upgrade, stamp
|
import sys
|
||||||
from pathlib import Path
|
import time
|
||||||
|
|
||||||
|
|
||||||
# Init and upgrade
|
|
||||||
with app.app_context():
|
|
||||||
# Check if DB file is missing
|
|
||||||
if not (Path("./instance/app.db").is_file()):
|
|
||||||
with app.app_context():
|
|
||||||
db.create_all()
|
|
||||||
stamp()
|
|
||||||
# Upgrade db if any new migrations exist
|
|
||||||
upgrade()
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
def homepage():
|
|
||||||
return render_template("home.html", services=services)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/status")
|
|
||||||
def status():
|
|
||||||
return jsonify([s.to_dict() for s in services])
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/favicon.svg")
|
|
||||||
def favicon():
|
|
||||||
return send_file("/static/favicon.svg")
|
|
||||||
|
|
||||||
|
|
||||||
# Only run if directly running file
|
# Only run if directly running file
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
threading.Thread(target=start_worker, daemon=True).start()
|
||||||
|
|
||||||
t = threading.Thread(target=start_async_loop, daemon=True)
|
threading.Thread(target=start_flask, daemon=True).start()
|
||||||
t.start()
|
|
||||||
|
|
||||||
# Run flask app
|
# Optional: monitor stop_event in a separate thread
|
||||||
app.run(host="0.0.0.0", port=80, debug=True, use_reloader=False)
|
def monitor_worker():
|
||||||
|
while not aio_stop.is_set() and not flask_stop.is_set():
|
||||||
|
time.sleep(1)
|
||||||
|
print("Worker failed, stopping program...")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
monitor_worker()
|
||||||
|
|||||||
22
app/__init__.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
from flask_migrate import Migrate, upgrade, stamp
|
||||||
|
from .flask_app import app
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
__all__ = ["app"]
|
||||||
|
|
||||||
|
# Create db
|
||||||
|
db = SQLAlchemy(app=app)
|
||||||
|
|
||||||
|
# Set up migration
|
||||||
|
migrations_dir = Path(__file__).parent / "migrations"
|
||||||
|
migration = Migrate(app=app, db=db, directory=str(migrations_dir))
|
||||||
|
|
||||||
|
# Init and upgrade
|
||||||
|
with app.app_context():
|
||||||
|
# Check if DB file is missing
|
||||||
|
if not (Path("./instance/app.db").is_file()):
|
||||||
|
db.create_all()
|
||||||
|
stamp()
|
||||||
|
# Upgrade db if any new migrations exist
|
||||||
|
upgrade()
|
||||||
3
app/aio_client/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from .worker import start_worker, stop_event
|
||||||
|
|
||||||
|
__all__ = ["start_worker", "stop_event"]
|
||||||
47
app/aio_client/client.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
import aiohttp
|
||||||
|
from app.models import service, log
|
||||||
|
from types import SimpleNamespace
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
async def ping(
|
||||||
|
client: aiohttp.ClientSession,
|
||||||
|
s: service,
|
||||||
|
ctx: Optional[SimpleNamespace] = None,
|
||||||
|
) -> int:
|
||||||
|
ctx = ctx or SimpleNamespace()
|
||||||
|
match s.ping_method:
|
||||||
|
case 0:
|
||||||
|
r = await client.head(
|
||||||
|
url=s.url,
|
||||||
|
ssl=True if s.public_access else False,
|
||||||
|
allow_redirects=True,
|
||||||
|
trace_request_ctx=ctx, # type: ignore
|
||||||
|
)
|
||||||
|
case 1:
|
||||||
|
r = await client.get(
|
||||||
|
url=s.url,
|
||||||
|
ssl=True if s.public_access else False,
|
||||||
|
allow_redirects=True,
|
||||||
|
trace_request_ctx=ctx, # type: ignore
|
||||||
|
)
|
||||||
|
case _:
|
||||||
|
raise Exception("UNKNOWN PING METHOD")
|
||||||
|
|
||||||
|
return r.status
|
||||||
|
|
||||||
|
|
||||||
|
async def ping_service(client: aiohttp.ClientSession, s: service) -> log:
|
||||||
|
try:
|
||||||
|
ctx = SimpleNamespace()
|
||||||
|
status = await ping(client=client, s=s, ctx=ctx)
|
||||||
|
|
||||||
|
if status == 200:
|
||||||
|
return log(service_id=s.id, ping=int(ctx.duration_ms))
|
||||||
|
else:
|
||||||
|
return log(service_id=s.id, ping=None)
|
||||||
|
except aiohttp.ConnectionTimeoutError:
|
||||||
|
return log(service_id=s.id, ping=None, timeout=True)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return log(service_id=s.id, ping=None)
|
||||||
91
app/aio_client/worker.py
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
from config import timeout as timeout_
|
||||||
|
import aiohttp
|
||||||
|
import asyncio
|
||||||
|
import time
|
||||||
|
from types import SimpleNamespace
|
||||||
|
from app import db, app
|
||||||
|
from app.models import service
|
||||||
|
from .client import ping_service
|
||||||
|
import threading
|
||||||
|
|
||||||
|
stop_event = threading.Event()
|
||||||
|
|
||||||
|
|
||||||
|
def start_worker():
|
||||||
|
try:
|
||||||
|
# Creates new event loop in new thread
|
||||||
|
loop = asyncio.new_event_loop()
|
||||||
|
|
||||||
|
# Creates new task on new loop
|
||||||
|
loop.create_task(update_services())
|
||||||
|
|
||||||
|
# Schedule loop to run forever
|
||||||
|
loop.run_forever()
|
||||||
|
except Exception as e:
|
||||||
|
print("Worker thread exception:", e)
|
||||||
|
stop_event.set()
|
||||||
|
|
||||||
|
|
||||||
|
def setup_client() -> aiohttp.ClientSession:
|
||||||
|
timeout = aiohttp.client.ClientTimeout(total=timeout_ / 1000)
|
||||||
|
# Each request will get its own context
|
||||||
|
trace_config = aiohttp.TraceConfig()
|
||||||
|
|
||||||
|
async def on_start(
|
||||||
|
session: aiohttp.ClientSession,
|
||||||
|
context: SimpleNamespace,
|
||||||
|
params: aiohttp.TraceRequestStartParams,
|
||||||
|
):
|
||||||
|
ctx = context.trace_request_ctx
|
||||||
|
ctx.start = time.perf_counter() # store per-request
|
||||||
|
|
||||||
|
async def on_end(
|
||||||
|
session: aiohttp.ClientSession,
|
||||||
|
context: SimpleNamespace,
|
||||||
|
params: aiohttp.TraceRequestEndParams,
|
||||||
|
):
|
||||||
|
ctx = context.trace_request_ctx
|
||||||
|
ctx.end = time.perf_counter()
|
||||||
|
ctx.duration_ms = int((ctx.end - ctx.start) * 1000)
|
||||||
|
|
||||||
|
trace_config.on_request_start.append(on_start)
|
||||||
|
trace_config.on_request_end.append(on_end)
|
||||||
|
client = aiohttp.ClientSession(
|
||||||
|
timeout=timeout, auto_decompress=False, trace_configs=[trace_config]
|
||||||
|
)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
async def update_services():
|
||||||
|
try:
|
||||||
|
print("Starting service updates...")
|
||||||
|
# Create new session
|
||||||
|
with app.app_context():
|
||||||
|
WorkerSession = sessionmaker(bind=db.engine)
|
||||||
|
|
||||||
|
client = setup_client()
|
||||||
|
|
||||||
|
# Actual update loop
|
||||||
|
while True:
|
||||||
|
session = WorkerSession()
|
||||||
|
# TODO: Add http processing time var
|
||||||
|
# instead of just adding 1 second
|
||||||
|
sleeptask = asyncio.create_task(asyncio.sleep(timeout_ / 1000 + 1))
|
||||||
|
tasks = [
|
||||||
|
ping_service(client=client, s=s)
|
||||||
|
for s in session.query(service).all()
|
||||||
|
]
|
||||||
|
logs = await asyncio.gather(*tasks)
|
||||||
|
await sleeptask
|
||||||
|
try:
|
||||||
|
session.add_all(logs)
|
||||||
|
session.commit()
|
||||||
|
except Exception as e:
|
||||||
|
session.rollback()
|
||||||
|
raise e
|
||||||
|
finally:
|
||||||
|
session.close()
|
||||||
|
except Exception as e:
|
||||||
|
print("Worker thread exception:", e)
|
||||||
|
stop_event.set()
|
||||||
20
app/flask_app/__init__.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import threading
|
||||||
|
from flask import Flask
|
||||||
|
|
||||||
|
stop_event = threading.Event()
|
||||||
|
|
||||||
|
# Flask app to serve status
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///app.db"
|
||||||
|
|
||||||
|
|
||||||
|
def start_flask() -> None:
|
||||||
|
try:
|
||||||
|
# Run flask app
|
||||||
|
from .routes import bp
|
||||||
|
|
||||||
|
app.register_blueprint(bp)
|
||||||
|
app.run(host="0.0.0.0", port=80, debug=True, use_reloader=False)
|
||||||
|
except Exception as e:
|
||||||
|
print("Worker thread exception:", e)
|
||||||
|
stop_event.set()
|
||||||
101
app/flask_app/routes.py
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
from flask import Blueprint, render_template, abort, jsonify, send_file, json
|
||||||
|
from typing import cast, Optional, Any
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
from config import timeout
|
||||||
|
from ..models import service, log
|
||||||
|
from app import app, db
|
||||||
|
|
||||||
|
bp = Blueprint(
|
||||||
|
"main",
|
||||||
|
"__name__",
|
||||||
|
url_prefix="/",
|
||||||
|
static_folder="static",
|
||||||
|
)
|
||||||
|
|
||||||
|
# TODO: Move util functions to seperate file (utils.py?)
|
||||||
|
|
||||||
|
|
||||||
|
# Prepares log data for chart.js chart
|
||||||
|
def prepare_chart_data(
|
||||||
|
logs: list[log],
|
||||||
|
) -> tuple[list[str], list[Optional[int]]]:
|
||||||
|
if len(logs) <= 0: # Return empty if there are no logs
|
||||||
|
return ([], [])
|
||||||
|
|
||||||
|
x = [logs[0].dateCreatedUTC().isoformat()]
|
||||||
|
y = [logs[0].ping]
|
||||||
|
|
||||||
|
for i in range(1, len(logs)):
|
||||||
|
log1 = logs[i]
|
||||||
|
log2 = logs[i - 1]
|
||||||
|
|
||||||
|
# TODO: add timeout duration to log so this can be used
|
||||||
|
# instead of 1.5 * the current timeout + 1.
|
||||||
|
|
||||||
|
# Check if the gap in points exceeds a threshold
|
||||||
|
if (abs(log1.dateCreatedUTC() - log2.dateCreatedUTC())) > timedelta(
|
||||||
|
milliseconds=1.5 * (timeout + 1000)
|
||||||
|
):
|
||||||
|
x.append(log2.dateCreatedUTC().isoformat())
|
||||||
|
y.append(None)
|
||||||
|
|
||||||
|
x.append(log1.dateCreatedUTC().isoformat())
|
||||||
|
y.append(log1.ping)
|
||||||
|
return (x, y)
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route("/")
|
||||||
|
def homepage():
|
||||||
|
return render_template("home.html")
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route("/chart/<int:id>")
|
||||||
|
def chart(id: int):
|
||||||
|
one_hour_ago = datetime.now(timezone.utc) - timedelta(hours=1)
|
||||||
|
with app.app_context():
|
||||||
|
logs = []
|
||||||
|
s = db.session.query(service).filter_by(id=id).first()
|
||||||
|
if s:
|
||||||
|
logs = cast(
|
||||||
|
list[log],
|
||||||
|
s.logs.filter(log.dateCreated >= one_hour_ago)
|
||||||
|
.order_by(log.dateCreated.desc()) # type: ignore
|
||||||
|
.all(),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return abort(code=403)
|
||||||
|
x, y = prepare_chart_data(logs=logs)
|
||||||
|
|
||||||
|
now = datetime.now(timezone.utc)
|
||||||
|
max_ = now
|
||||||
|
min_ = now - timedelta(hours=1)
|
||||||
|
return render_template(
|
||||||
|
"chart.html",
|
||||||
|
dates=x,
|
||||||
|
values=json.dumps(y),
|
||||||
|
min=min_.isoformat(),
|
||||||
|
max=max_.isoformat(),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route("/api/status")
|
||||||
|
def status():
|
||||||
|
results: list[dict[str, Any]] = []
|
||||||
|
with app.app_context():
|
||||||
|
a = db.session.query(service).all()
|
||||||
|
for s in a:
|
||||||
|
b = cast(
|
||||||
|
Optional[log],
|
||||||
|
s.logs.order_by(
|
||||||
|
log.dateCreated.desc() # type: ignore
|
||||||
|
).first(),
|
||||||
|
)
|
||||||
|
if b:
|
||||||
|
results.append(s.to_dict() | b.to_dict())
|
||||||
|
|
||||||
|
return jsonify(results)
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route("/favicon.svg")
|
||||||
|
def favicon():
|
||||||
|
return send_file("/static/favicon.svg")
|
||||||
|
Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 2.0 KiB |
1
app/flask_app/static/history.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<!-- icon666.com - MILLIONS OF FREE VECTOR ICONS --><svg id="Layer_1" enable-background="new 0 0 24 24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="m18 24h-12c-3.3 0-6-2.7-6-6v-12c0-3.3 2.7-6 6-6h12c3.3 0 6 2.7 6 6v12c0 3.3-2.7 6-6 6z" fill="#00adff" style="fill: rgb(82, 82, 82);"></path><path d="m12 3.8c4.5 0 8.2 3.7 8.2 8.2s-3.7 8.2-8.2 8.2-8.2-3.7-8.2-8.2h1.6c0 3.6 3 6.6 6.6 6.6s6.6-3 6.6-6.6-3-6.6-6.6-6.6c-2.1 0-3.9.9-5 2.4l1.7 1.7h-4.9v-4.9l2 2c1.5-1.7 3.7-2.8 6.2-2.8zm.8 4.1v3.8l2.6 2.6-1.1 1.1-3.1-3.1v-4.4z" fill="#fff"></path></svg>
|
||||||
|
After Width: | Height: | Size: 566 B |
|
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
|
Before Width: | Height: | Size: 191 B After Width: | Height: | Size: 191 B |
|
Before Width: | Height: | Size: 2.4 KiB After Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 680 B After Width: | Height: | Size: 680 B |
|
Before Width: | Height: | Size: 550 B After Width: | Height: | Size: 550 B |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 5.5 KiB After Width: | Height: | Size: 5.5 KiB |
|
Before Width: | Height: | Size: 389 B After Width: | Height: | Size: 389 B |
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 1.0 KiB |
|
Before Width: | Height: | Size: 7.8 KiB After Width: | Height: | Size: 7.8 KiB |
|
Before Width: | Height: | Size: 5.4 KiB After Width: | Height: | Size: 5.4 KiB |
|
Before Width: | Height: | Size: 8.9 KiB After Width: | Height: | Size: 8.9 KiB |
|
Before Width: | Height: | Size: 950 B After Width: | Height: | Size: 950 B |
|
Before Width: | Height: | Size: 642 B After Width: | Height: | Size: 642 B |
66
app/flask_app/templates/chart.html
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en" data-bs-theme="dark">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1" charset=" UTF-8">
|
||||||
|
<title>Simple Line Chart</title>
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet"
|
||||||
|
integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
|
||||||
|
<link rel="icon" type="image/svg+xml" href="/static/favicon.svg">
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns"></script>
|
||||||
|
<script src="https://www.chartjs.org/docs/latest/samples/utils.js"></script>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<canvas id="myChart"></canvas>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
|
||||||
|
const chartDates = ({{ dates | safe }}).map(dt => new Date(dt));
|
||||||
|
const data = {
|
||||||
|
labels: chartDates,
|
||||||
|
datasets: [{
|
||||||
|
label: 'Ping',
|
||||||
|
data: {{ values }},
|
||||||
|
}]
|
||||||
|
};
|
||||||
|
|
||||||
|
const ctx = document.getElementById('myChart').getContext('2d');
|
||||||
|
// Current time in UTC
|
||||||
|
const nowUTC = new Date();
|
||||||
|
|
||||||
|
// One hour ago in UTC
|
||||||
|
const oneDayAgoUTC = new Date(nowUTC.getTime() - 24 * 60 * 60 * 1000);
|
||||||
|
const min = "{{ min }}"
|
||||||
|
const max = "{{ max }}"
|
||||||
|
new Chart(ctx, {
|
||||||
|
type: 'line',
|
||||||
|
data: data,
|
||||||
|
options: {
|
||||||
|
scales: {
|
||||||
|
x: {
|
||||||
|
type: 'time', // Important for datetime axis
|
||||||
|
time: {
|
||||||
|
unit: 'minute',
|
||||||
|
tooltipFormat: 'HH:mm:ss',
|
||||||
|
displayFormats: { hour: 'HH:mm' }
|
||||||
|
},
|
||||||
|
min: min,
|
||||||
|
max: max,
|
||||||
|
ticks: { color: '#ffffff' }, // X-axis labels
|
||||||
|
grid: { color: 'rgba(255, 255, 255, 0.1)' } // X-axis grid lines
|
||||||
|
},
|
||||||
|
y: {
|
||||||
|
ticks: { color: '#ffffff' }, // Y-axis labels
|
||||||
|
grid: { color: 'rgba(255, 255, 255, 0.1)' } // Y-axis grid lines
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</html>
|
||||||
123
app/flask_app/templates/home.html
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en" data-bs-theme="dark">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1" charset=" UTF-8">
|
||||||
|
<title>Dashboard</title>
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet"
|
||||||
|
integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
|
||||||
|
<link rel="icon" type="image/svg+xml" href="/static/favicon.svg">
|
||||||
|
<style>
|
||||||
|
/* Prevent Bootstrap's default 0.6s width tween that causes lag */
|
||||||
|
.progress-bar {
|
||||||
|
transition: none !important;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body class="m-2 bg-light-subtle pt-3">
|
||||||
|
<div class="progress fixed-top mt-1" style="height: auto;">
|
||||||
|
<div id="progressBar" class="progress-bar rounded-pill" role="progressbar" style="width: 100%; margin: 0 auto;">
|
||||||
|
<h5 class="m-0">5s</h5>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div id="main_body" class="d-flex flex-wrap justify-content-center"></div>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const main_body = document.getElementById("main_body");
|
||||||
|
const url = '/api/status';
|
||||||
|
|
||||||
|
const interval = 5000; // 5 seconds between requests
|
||||||
|
const progressBar = document.getElementById('progressBar');
|
||||||
|
const chartURL = "{{ url_for("main.chart", id=0) }}"
|
||||||
|
|
||||||
|
function fetchData() {
|
||||||
|
fetch(url, { cache: 'no-store' })
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
updateWebpage(data)
|
||||||
|
})
|
||||||
|
.catch(error => console.error("Error fetching data", error))
|
||||||
|
.finally(() => {
|
||||||
|
// Animate progress bar over 'interval' before next fetch
|
||||||
|
animateProgress(interval, () => {
|
||||||
|
fetchData(); // next fetch after animation
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function animateProgress(duration, callback) {
|
||||||
|
const start = performance.now();
|
||||||
|
|
||||||
|
function frame(now) {
|
||||||
|
const elapsed = now - start;
|
||||||
|
const ratio = Math.min(elapsed / duration, 1);
|
||||||
|
|
||||||
|
progressBar.style.width = 100 * (1 - ratio) + "%";
|
||||||
|
progressBar.innerHTML = "<h5 class='m-0'>" + (interval * (1 - ratio) / 1000).toFixed(1) + "s</h5>";
|
||||||
|
|
||||||
|
if (ratio < 1) {
|
||||||
|
requestAnimationFrame(frame);
|
||||||
|
} else {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// reset bar and start animation
|
||||||
|
progressBar.style.width = '100%';
|
||||||
|
progressBar.textContent = '100%';
|
||||||
|
requestAnimationFrame(frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateWebpage(services) {
|
||||||
|
main_body.innerHTML = ''; // Clear webpage
|
||||||
|
|
||||||
|
// Build all service divs as a single string
|
||||||
|
main_body.innerHTML = services.map(s => `
|
||||||
|
<div class="service-tile position-relative m-2" style="width: 175px">
|
||||||
|
<a href="${s.url}" class="text-body text-decoration-none bg-body-tertiary d-flex flex-column align-items-center border border-3 ${s.ping ? 'border-success' : 'border-danger'}">
|
||||||
|
<div class="bg-dark w-100">
|
||||||
|
<h4 class="text-center text-truncate m-0 p-1">${s.label}</h4>
|
||||||
|
</div>
|
||||||
|
<div class="position-relative ratio ratio-1x1">
|
||||||
|
<div class="d-flex justify-content-center align-items-center">
|
||||||
|
<img src="{{ url_for('static', filename="icons") }}/${s.service_id - 1}.svg" class="img-fluid w-75">
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
${s.public_access ? `` : `<img src='{{ url_for('static', filename='lock.svg') }}' class='img-fluid position-absolute bottom-0 end-0 w-25'>`}
|
||||||
|
<div class="position-absolute bottom-0 text-body bg-dark bg-opacity-75 px-1 rounded">${s.ping ? s.ping + "ms" : ""}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</a>
|
||||||
|
<a href="${chartURL.replace("0", s.service_id)}" class="overlay position-absolute bottom-0 end-0 m-1" style="width:40px;">
|
||||||
|
<img src="{{ url_for('static', filename='history.svg') }}">
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
`).join(''); // join into a single string
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchData(); // start the loop
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<!-- Add this CSS once (in your stylesheet or in a <style> block) -->
|
||||||
|
<style>
|
||||||
|
/* make overlay invisible and non-interactive by default */
|
||||||
|
.service-tile .overlay {
|
||||||
|
opacity: 0;
|
||||||
|
pointer-events: none;
|
||||||
|
transition: opacity .18s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* reveal overlay on hover (and make it clickable) */
|
||||||
|
.service-tile:hover .overlay {
|
||||||
|
opacity: 1;
|
||||||
|
pointer-events: auto;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"
|
||||||
|
integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI"
|
||||||
|
crossorigin="anonymous"></script>
|
||||||
|
|
||||||
|
</html>
|
||||||
37
app/migrations/versions/3c05315d5b9b_.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: 3c05315d5b9b
|
||||||
|
Revises: f87909a4293b
|
||||||
|
Create Date: 2025-09-05 09:48:08.561045
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = "3c05315d5b9b"
|
||||||
|
down_revision = "f87909a4293b"
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table("log", schema=None) as batch_op:
|
||||||
|
batch_op.add_column(
|
||||||
|
sa.Column(
|
||||||
|
"timeout", sa.Boolean(), nullable=False, server_default="false"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table("log", schema=None) as batch_op:
|
||||||
|
batch_op.drop_column("timeout")
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
35
app/migrations/versions/f04407e8e466_.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: f04407e8e466
|
||||||
|
Revises: d7d380435347
|
||||||
|
Create Date: 2025-09-03 15:40:30.413166
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'f04407e8e466'
|
||||||
|
down_revision = 'd7d380435347'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table('service',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('url', sa.String(), nullable=False),
|
||||||
|
sa.Column('label', sa.String(length=15), nullable=False),
|
||||||
|
sa.Column('public_access', sa.Boolean(), nullable=False),
|
||||||
|
sa.Column('ping_method', sa.Integer(), nullable=False),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_table('service')
|
||||||
|
# ### end Alembic commands ###
|
||||||
50
app/migrations/versions/f87909a4293b_.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: f87909a4293b
|
||||||
|
Revises: f04407e8e466
|
||||||
|
Create Date: 2025-09-03 16:36:14.608372
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = "f87909a4293b"
|
||||||
|
down_revision = "f04407e8e466"
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.execute("DROP TABLE IF EXISTS _alembic_tmp_log")
|
||||||
|
op.execute("DELETE FROM log")
|
||||||
|
|
||||||
|
with op.batch_alter_table("log", schema=None) as batch_op:
|
||||||
|
batch_op.add_column(
|
||||||
|
sa.Column(
|
||||||
|
"service_id", sa.Integer(), nullable=False, server_default="0"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
batch_op.add_column(sa.Column("ping", sa.Integer(), nullable=True))
|
||||||
|
batch_op.create_index(
|
||||||
|
batch_op.f("ix_log_dateCreated"), ["dateCreated"], unique=False
|
||||||
|
)
|
||||||
|
batch_op.create_foreign_key(
|
||||||
|
"fk_log2service", "service", ["service_id"], ["id"]
|
||||||
|
)
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table("log", schema=None) as batch_op:
|
||||||
|
batch_op.drop_constraint("fk_log2service", type_="foreignkey")
|
||||||
|
batch_op.drop_index(batch_op.f("ix_log_dateCreated"))
|
||||||
|
batch_op.drop_column("ping")
|
||||||
|
batch_op.drop_column("service_id")
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
70
app/models.py
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
from app import db
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from validators import url as is_url
|
||||||
|
from typing import Any, Optional
|
||||||
|
|
||||||
|
|
||||||
|
class log(db.Model):
|
||||||
|
id: int = db.Column(db.Integer, primary_key=True) # TODO: Switch to UUID
|
||||||
|
dateCreated: datetime = db.Column(db.DateTime, nullable=False, index=True)
|
||||||
|
service_id: int = db.Column(
|
||||||
|
db.Integer,
|
||||||
|
db.ForeignKey("service.id"),
|
||||||
|
nullable=False,
|
||||||
|
)
|
||||||
|
ping: Optional[int] = db.Column(db.Integer, nullable=True)
|
||||||
|
timeout: bool = db.Column(db.Boolean, nullable=False)
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, service_id: int, ping: Optional[int], timeout: bool = False
|
||||||
|
):
|
||||||
|
super().__init__()
|
||||||
|
self.service_id = service_id
|
||||||
|
self.ping = ping
|
||||||
|
|
||||||
|
self.dateCreated = datetime.now(timezone.utc)
|
||||||
|
self.timeout = timeout
|
||||||
|
|
||||||
|
def to_dict(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"log_id": self.id,
|
||||||
|
"service_id": self.service_id,
|
||||||
|
"ping": self.ping,
|
||||||
|
"dateCreated": self.dateCreatedUTC(),
|
||||||
|
"timeout": self.timeout,
|
||||||
|
}
|
||||||
|
|
||||||
|
def dateCreatedUTC(self) -> datetime:
|
||||||
|
return self.dateCreated.replace(tzinfo=timezone.utc)
|
||||||
|
|
||||||
|
|
||||||
|
class service(db.Model):
|
||||||
|
id: int = db.Column(db.Integer, primary_key=True) # TODO: Switch to UUID
|
||||||
|
url: str = db.Column(db.String, nullable=False)
|
||||||
|
label: str = db.Column(db.String(15), nullable=False)
|
||||||
|
public_access: bool = db.Column(db.Boolean, nullable=False)
|
||||||
|
ping_method: int = db.Column(db.Integer, nullable=False)
|
||||||
|
|
||||||
|
logs = db.relationship("log", lazy="dynamic")
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, url: str, label: str, public_access: bool, ping_method: int
|
||||||
|
):
|
||||||
|
if not is_url(url):
|
||||||
|
raise Exception("URL IS NOT A VALID URL")
|
||||||
|
if len(label) > 15:
|
||||||
|
raise Exception("LABEL EXCEEDS MAXIMUM LENGTH (15)")
|
||||||
|
super().__init__()
|
||||||
|
self.url = url
|
||||||
|
self.label = label
|
||||||
|
self.public_access = public_access
|
||||||
|
self.ping_method = ping_method
|
||||||
|
|
||||||
|
def to_dict(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"url": self.url,
|
||||||
|
"public_access": self.public_access,
|
||||||
|
"label": self.label,
|
||||||
|
"service_id": self.id,
|
||||||
|
"ping_method": self.ping_method,
|
||||||
|
}
|
||||||
95
app/routes.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
from flask import Blueprint, render_template, abort, jsonify, send_file, json
|
||||||
|
from typing import cast, Optional, Any
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
from config import timeout
|
||||||
|
from .models import service, log
|
||||||
|
from app import app, db
|
||||||
|
|
||||||
|
bp = Blueprint(
|
||||||
|
"main",
|
||||||
|
"__name__",
|
||||||
|
url_prefix="/",
|
||||||
|
static_folder="static",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Prepares log data for chart.js chart
|
||||||
|
def prepare_chart_data(
|
||||||
|
logs: list[log],
|
||||||
|
) -> tuple[list[str], list[Optional[int]]]:
|
||||||
|
if len(logs) <= 0: # Return empty if there are no logs
|
||||||
|
return ([], [])
|
||||||
|
|
||||||
|
x = [logs[0].dateCreatedUTC().isoformat()]
|
||||||
|
y = [logs[0].ping]
|
||||||
|
|
||||||
|
for i in range(1, len(logs)):
|
||||||
|
log1 = logs[i]
|
||||||
|
log2 = logs[i - 1]
|
||||||
|
|
||||||
|
# Check if the gap in points exceeds a threshold
|
||||||
|
if (abs(log1.dateCreatedUTC() - log2.dateCreatedUTC())) > timedelta(
|
||||||
|
milliseconds=1.5 * (timeout + 1000)
|
||||||
|
):
|
||||||
|
x.append(log2.dateCreatedUTC().isoformat())
|
||||||
|
y.append(None)
|
||||||
|
|
||||||
|
x.append(log1.dateCreatedUTC().isoformat())
|
||||||
|
y.append(log1.ping)
|
||||||
|
return (x, y)
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route("/")
|
||||||
|
def homepage():
|
||||||
|
return render_template("home.html")
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route("/chart/<int:id>")
|
||||||
|
def chart(id: int):
|
||||||
|
with app.app_context():
|
||||||
|
logs = []
|
||||||
|
s = db.session.query(service).filter_by(id=id).first()
|
||||||
|
if s:
|
||||||
|
logs = cast(
|
||||||
|
list[log],
|
||||||
|
s.logs.order_by(log.dateCreated.desc()) # type: ignore
|
||||||
|
.limit(300)
|
||||||
|
.all(),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return abort(code=403)
|
||||||
|
x, y = prepare_chart_data(logs=logs)
|
||||||
|
|
||||||
|
now = datetime.now(timezone.utc)
|
||||||
|
max_ = now
|
||||||
|
min_ = now - timedelta(hours=1)
|
||||||
|
return render_template(
|
||||||
|
"chart.html",
|
||||||
|
dates=x,
|
||||||
|
values=json.dumps(y),
|
||||||
|
min=min_.isoformat(),
|
||||||
|
max=max_.isoformat(),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route("/api/status")
|
||||||
|
def status():
|
||||||
|
results: list[dict[str, Any]] = []
|
||||||
|
with app.app_context():
|
||||||
|
a = db.session.query(service).all()
|
||||||
|
for s in a:
|
||||||
|
b = cast(
|
||||||
|
Optional[log],
|
||||||
|
s.logs.order_by(
|
||||||
|
log.dateCreated.desc() # type: ignore
|
||||||
|
).first(),
|
||||||
|
)
|
||||||
|
if b:
|
||||||
|
results.append(s.to_dict() | b.to_dict())
|
||||||
|
|
||||||
|
return jsonify(results)
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route("/favicon.svg")
|
||||||
|
def favicon():
|
||||||
|
return send_file("/static/favicon.svg")
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
from typing import Any, Optional
|
|
||||||
from flask import Flask
|
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
|
||||||
from flask_migrate import Migrate
|
|
||||||
|
|
||||||
|
|
||||||
class service:
|
|
||||||
id: int
|
|
||||||
url: str
|
|
||||||
status: Optional[int]
|
|
||||||
online: bool
|
|
||||||
public: bool
|
|
||||||
error: Optional[str]
|
|
||||||
ping: Optional[int]
|
|
||||||
icon_filetype: str
|
|
||||||
ping_type: int
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
id: int,
|
|
||||||
url: str = "",
|
|
||||||
label: str = "",
|
|
||||||
public: bool = True,
|
|
||||||
icon_filetype: str = "svg",
|
|
||||||
ping_type: int = 0,
|
|
||||||
):
|
|
||||||
self.id = id
|
|
||||||
self.url = url
|
|
||||||
self.public = public
|
|
||||||
self.label = label
|
|
||||||
self.ping_type = ping_type
|
|
||||||
|
|
||||||
self.online = False
|
|
||||||
self.status = None
|
|
||||||
self.error = None
|
|
||||||
self.ping = None
|
|
||||||
self.icon_filetype = icon_filetype
|
|
||||||
|
|
||||||
def to_dict(self) -> dict[str, Any]:
|
|
||||||
return {
|
|
||||||
"url": self.url,
|
|
||||||
"status": self.status,
|
|
||||||
"public": self.public,
|
|
||||||
"online": self.online,
|
|
||||||
"error": self.error,
|
|
||||||
"ping": self.ping,
|
|
||||||
"label": self.label,
|
|
||||||
"icon_filetype": self.icon_filetype,
|
|
||||||
"id": self.id,
|
|
||||||
"ping_type": self.ping_type,
|
|
||||||
}
|
|
||||||
|
|
||||||
def set_status(self, status: Optional[int]):
|
|
||||||
self.status = status
|
|
||||||
|
|
||||||
def set_online(self, b: bool):
|
|
||||||
self.online = b
|
|
||||||
|
|
||||||
def set_error(self, s: Optional[str]):
|
|
||||||
self.error = s
|
|
||||||
|
|
||||||
def set_ping(self, n: Optional[int]):
|
|
||||||
self.ping = n
|
|
||||||
|
|
||||||
|
|
||||||
services: list[service] = [
|
|
||||||
service(0, "https://git.ihatemen.uk/", "Gitea"),
|
|
||||||
service(1, "https://plex.ihatemen.uk/", "Plex", ping_type=1),
|
|
||||||
service(2, "https://truenas.local/", "TrueNAS", False),
|
|
||||||
service(3, "https://cloud.ihatemen.uk/", "NextCloud"),
|
|
||||||
service(4, "https://request.ihatemen.uk/", "Overseerr"),
|
|
||||||
service(5, "https://id.ihatemen.uk/", "PocketID"),
|
|
||||||
service(6, "https://tautulli.local/", "Tautulli", False),
|
|
||||||
service(
|
|
||||||
7, "https://transmission.local/", "Transmission", False, ping_type=1
|
|
||||||
),
|
|
||||||
service(8, "https://vault.ihatemen.uk/", "Vault Warden"),
|
|
||||||
service(9, "https://nginx.local/", "Nginx (NPM)", False),
|
|
||||||
service(10, "https://app.ihatemen.uk/", "Kcal Counter"),
|
|
||||||
service(
|
|
||||||
id=11, url="https://unifi.local/", label="Unifi Server", public=False
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|
|
||||||
# Flask app to serve status
|
|
||||||
app = Flask(__name__)
|
|
||||||
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///app.db"
|
|
||||||
|
|
||||||
db = SQLAlchemy(app=app)
|
|
||||||
migration = Migrate(app=app, db=db)
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en" data-bs-theme="dark">
|
|
||||||
|
|
||||||
<head>
|
|
||||||
<meta ame="viewport" content="width=device-width, initial-scale=1" charset=" UTF-8">
|
|
||||||
<title>Dashboard</title>
|
|
||||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet"
|
|
||||||
integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
|
|
||||||
<link rel="icon" type="image/svg+xml" href="/static/favicon.svg">
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body id="main_body" class="m-2 bg-light-subtle d-flex flex-wrap justify-content-center">
|
|
||||||
</body>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
const main_body = document.getElementById("main_body");
|
|
||||||
const url = '/api/status';
|
|
||||||
|
|
||||||
function fetchData() {
|
|
||||||
fetch(url, { cache: 'no-store' })
|
|
||||||
.then(response => response.json())
|
|
||||||
.then(data => {
|
|
||||||
console.log(data)
|
|
||||||
updateWebpage(data)
|
|
||||||
})
|
|
||||||
.catch(error => console.error("Error fetching data", error))
|
|
||||||
.finally(() => {
|
|
||||||
setTimeout(fetchData, 5000); // schedule next request after 5 seconds
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateWebpage(services) {
|
|
||||||
main_body.innerHTML = ''; // Clear webpage
|
|
||||||
|
|
||||||
// Build all service divs as a single string
|
|
||||||
main_body.innerHTML = services.map(s => `
|
|
||||||
<a href="${s.url}" class="d-block text-body text-decoration-none m-2 border border-3 ${s.online ? 'border-success' : 'border-danger'}" style="width: 200px">
|
|
||||||
<div class="bg-body-tertiary d-flex flex-column align-items-center">
|
|
||||||
<div class="bg-dark w-100">
|
|
||||||
<h4 class="text-center text-truncate m-0 p-1" style="font-size: 1.9rem;">${s.label}</h4>
|
|
||||||
</div>
|
|
||||||
<div class="position-relative ratio ratio-1x1">
|
|
||||||
<div class="d-flex justify-content-center align-items-center">
|
|
||||||
<img src="static/icons/${s.id}.${s.icon_filetype}" class="img-fluid w-75">
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
${s.public ? `` : `<img src='static/lock.svg' class='img-fluid position-absolute bottom-0 end-0 w-25'>`}
|
|
||||||
<div class="position-absolute bottom-0 text-body bg-dark bg-opacity-75 px-1 rounded">${s.online ? s.ping + "ms" : ""}</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</a>
|
|
||||||
`).join(''); // join into a single string
|
|
||||||
}
|
|
||||||
|
|
||||||
fetchData(); // start the loop
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"
|
|
||||||
integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI"
|
|
||||||
crossorigin="anonymous"></script>
|
|
||||||
|
|
||||||
</html>
|
|
||||||
12
models.py
@@ -1,12 +0,0 @@
|
|||||||
from mem import db
|
|
||||||
from datetime import datetime, timezone
|
|
||||||
|
|
||||||
|
|
||||||
class log(db.Model):
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
|
||||||
dateCreated = db.Column(db.DateTime, nullable=False)
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
self.dateCreated = datetime.now(timezone.utc)
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
from mem import services, service, db, app
|
|
||||||
import httpx
|
|
||||||
import asyncio
|
|
||||||
import time
|
|
||||||
from models import log
|
|
||||||
from sqlalchemy.orm import sessionmaker
|
|
||||||
|
|
||||||
|
|
||||||
async def check_service(client: httpx.AsyncClient, s: service) -> log:
|
|
||||||
try:
|
|
||||||
before = time.perf_counter()
|
|
||||||
match s.ping_type:
|
|
||||||
case 0:
|
|
||||||
r = await client.head(
|
|
||||||
url=s.url,
|
|
||||||
follow_redirects=True,
|
|
||||||
timeout=1,
|
|
||||||
)
|
|
||||||
case 1:
|
|
||||||
r = await client.get(
|
|
||||||
url=s.url,
|
|
||||||
follow_redirects=True,
|
|
||||||
timeout=1,
|
|
||||||
)
|
|
||||||
case _:
|
|
||||||
raise httpx.HTTPError("Unknown ping type")
|
|
||||||
after = time.perf_counter()
|
|
||||||
s.set_error(None)
|
|
||||||
s.set_online(r.status_code == 200)
|
|
||||||
s.set_status(r.status_code)
|
|
||||||
s.set_ping(int((after - before) * 1000))
|
|
||||||
except httpx.HTTPError as e:
|
|
||||||
s.set_error(str(e))
|
|
||||||
s.set_online(False)
|
|
||||||
s.set_status(None)
|
|
||||||
s.set_ping(None)
|
|
||||||
return log()
|
|
||||||
|
|
||||||
|
|
||||||
def start_async_loop():
|
|
||||||
loop = asyncio.new_event_loop()
|
|
||||||
asyncio.set_event_loop(loop)
|
|
||||||
asyncio.run_coroutine_threadsafe(update_services(loop=loop), loop=loop)
|
|
||||||
loop.run_forever()
|
|
||||||
|
|
||||||
|
|
||||||
async def update_services(loop: asyncio.AbstractEventLoop):
|
|
||||||
print("Starting service updates...")
|
|
||||||
with app.app_context():
|
|
||||||
WorkerSession = sessionmaker(bind=db.engine)
|
|
||||||
async with (
|
|
||||||
httpx.AsyncClient() as public_client,
|
|
||||||
httpx.AsyncClient(verify=False) as local_client,
|
|
||||||
):
|
|
||||||
while True:
|
|
||||||
session = WorkerSession()
|
|
||||||
tasks = [
|
|
||||||
check_service(public_client if s.public else local_client, s)
|
|
||||||
for s in services
|
|
||||||
]
|
|
||||||
logs = await asyncio.gather(*tasks)
|
|
||||||
try:
|
|
||||||
session.add_all(logs)
|
|
||||||
session.commit()
|
|
||||||
except Exception as e:
|
|
||||||
session.rollback()
|
|
||||||
raise e
|
|
||||||
finally:
|
|
||||||
session.close()
|
|
||||||
await asyncio.sleep(2)
|
|
||||||
@@ -1,22 +1,25 @@
|
|||||||
|
aiohappyeyeballs==2.6.1
|
||||||
|
aiohttp==3.12.15
|
||||||
|
aiosignal==1.4.0
|
||||||
alembic==1.16.5
|
alembic==1.16.5
|
||||||
anyio==4.10.0
|
attrs==25.3.0
|
||||||
blinker==1.9.0
|
blinker==1.9.0
|
||||||
certifi==2025.8.3
|
|
||||||
click==8.2.1
|
click==8.2.1
|
||||||
colorama==0.4.6
|
colorama==0.4.6
|
||||||
Flask==3.1.2
|
Flask==3.1.2
|
||||||
Flask-Migrate==4.1.0
|
Flask-Migrate==4.1.0
|
||||||
Flask-SQLAlchemy==3.1.1
|
Flask-SQLAlchemy==3.1.1
|
||||||
|
frozenlist==1.7.0
|
||||||
greenlet==3.2.4
|
greenlet==3.2.4
|
||||||
h11==0.16.0
|
|
||||||
httpcore==1.0.9
|
|
||||||
httpx==0.28.1
|
|
||||||
idna==3.10
|
idna==3.10
|
||||||
itsdangerous==2.2.0
|
itsdangerous==2.2.0
|
||||||
Jinja2==3.1.6
|
Jinja2==3.1.6
|
||||||
Mako==1.3.10
|
Mako==1.3.10
|
||||||
MarkupSafe==3.0.2
|
MarkupSafe==3.0.2
|
||||||
sniffio==1.3.1
|
multidict==6.6.4
|
||||||
|
propcache==0.3.2
|
||||||
SQLAlchemy==2.0.43
|
SQLAlchemy==2.0.43
|
||||||
typing_extensions==4.15.0
|
typing_extensions==4.15.0
|
||||||
|
validators==0.35.0
|
||||||
Werkzeug==3.1.3
|
Werkzeug==3.1.3
|
||||||
|
yarl==1.20.1
|
||||||
|
|||||||