From 5ddafd3b984db39fb61327e276626dbb046b1432 Mon Sep 17 00:00:00 2001 From: Stef Date: Wed, 3 Sep 2025 18:28:08 +0200 Subject: [PATCH] First big step in moving towards DB storage instead of memory storage --- app.py | 19 ++++++++++++++++--- mem/__init__.py | 30 +----------------------------- mem/templates/home.html | 8 ++++---- models.py | 12 ++++++++++-- poll_services.py | 23 ++++++----------------- 5 files changed, 37 insertions(+), 55 deletions(-) diff --git a/app.py b/app.py index 423457c..93418cd 100644 --- a/app.py +++ b/app.py @@ -5,8 +5,8 @@ from mem import services, app, db import threading from flask_migrate import upgrade, stamp from pathlib import Path -from models import service - +from models import service, log +from typing import Any, Optional, cast # Init and upgrade with app.app_context(): @@ -39,7 +39,20 @@ def homepage(): @app.route("/api/status") def status(): - return jsonify([s.to_dict() for s in services]) + 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) @app.route("/favicon.svg") diff --git a/mem/__init__.py b/mem/__init__.py index 2fa4fbb..1b8e67b 100644 --- a/mem/__init__.py +++ b/mem/__init__.py @@ -1,4 +1,4 @@ -from typing import Any, Optional +from typing import Any from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate @@ -7,12 +7,8 @@ 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__( @@ -21,7 +17,6 @@ class service: url: str = "", label: str = "", public: bool = True, - icon_filetype: str = "svg", ping_type: int = 0, ): self.id = id @@ -30,38 +25,15 @@ class service: 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"), diff --git a/mem/templates/home.html b/mem/templates/home.html index 7d6af6d..fc1d027 100644 --- a/mem/templates/home.html +++ b/mem/templates/home.html @@ -75,18 +75,18 @@ // Build all service divs as a single string main_body.innerHTML = services.map(s => ` - +

${s.label}

- +
- ${s.public ? `` : ``} -
${s.online ? s.ping + "ms" : ""}
+ ${s.public_access ? `` : ``} +
${s.ping ? s.ping + "ms" : ""}
diff --git a/models.py b/models.py index 7b76ddc..727d49a 100644 --- a/models.py +++ b/models.py @@ -21,6 +21,14 @@ class log(db.Model): self.dateCreated = datetime.now(timezone.utc) + def to_dict(self) -> dict[str, Any]: + return { + "log_id": self.id, + "service_id": self.service_id, + "ping": self.ping, + "dateCreated": self.dateCreated, + } + class service(db.Model): id: int = db.Column(db.Integer, primary_key=True) # TODO: Switch to UUID @@ -29,7 +37,7 @@ class service(db.Model): public_access: bool = db.Column(db.Boolean, nullable=False) ping_method: int = db.Column(db.Integer, nullable=False) - logs = db.relationship("log") + logs = db.relationship("log", lazy="dynamic") def __init__( self, url: str, label: str, public_access: bool, ping_method: int @@ -49,6 +57,6 @@ class service(db.Model): "url": self.url, "public_access": self.public_access, "label": self.label, - "id": self.id, + "service_id": self.id, "ping_method": self.ping_method, } diff --git a/poll_services.py b/poll_services.py index d5b0309..b66e092 100644 --- a/poll_services.py +++ b/poll_services.py @@ -28,25 +28,14 @@ async def check_service(client: aiohttp.ClientSession, s: service) -> log: case _: raise Exception("UNKNOWN PING TYPE") after = time.perf_counter() - s.set_error(None) - s.set_online(r.status == 200) - s.set_status(r.status) - if r.status != 200: - s.set_ping(None) + if r.status == 200: + return log(service_id=s.id + 1, ping=int((after - before) * 1000)) else: - s.set_ping(int((after - before) * 1000)) + return log(service_id=s.id + 1, ping=None) except aiohttp.ConnectionTimeoutError: - s.set_error("Connection Timeout") - s.set_online(False) - s.set_status(None) - s.set_ping(None) - except Exception as e: - print(type(e)) - s.set_error(str(e)) - s.set_online(False) - s.set_status(None) - s.set_ping(None) - return log(service_id=s.id, ping=s.ping) + return log(service_id=s.id + 1, ping=None) + except Exception: + return log(service_id=s.id + 1, ping=None) def start_async_loop():