Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- Dockerfile.txt +26 -0
- app.py +298 -0
- requirements.txt +4 -0
- static/app.js +143 -0
- static/index.html +75 -0
- static/style.css +82 -0
Dockerfile.txt
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 4 |
+
PYTHONUNBUFFERED=1 \
|
| 5 |
+
PIP_NO_CACHE_DIR=1 \
|
| 6 |
+
HF_HUB_DISABLE_TELEMETRY=1
|
| 7 |
+
|
| 8 |
+
WORKDIR /code
|
| 9 |
+
|
| 10 |
+
# System deps
|
| 11 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 12 |
+
ca-certificates curl && rm -rf /var/lib/apt/lists/*
|
| 13 |
+
|
| 14 |
+
# App deps
|
| 15 |
+
COPY requirements.txt .
|
| 16 |
+
RUN pip install -r requirements.txt
|
| 17 |
+
|
| 18 |
+
# App
|
| 19 |
+
COPY app.py ./app.py
|
| 20 |
+
COPY static ./static
|
| 21 |
+
|
| 22 |
+
# Create persistent data dir (HF Spaces persists /data)
|
| 23 |
+
RUN mkdir -p /data
|
| 24 |
+
|
| 25 |
+
EXPOSE 7860
|
| 26 |
+
CMD ["python", "-u", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, json, sqlite3, asyncio, time
|
| 2 |
+
from datetime import datetime, timedelta, timezone
|
| 3 |
+
from threading import Lock
|
| 4 |
+
|
| 5 |
+
import httpx
|
| 6 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 7 |
+
from fastapi.responses import FileResponse, JSONResponse
|
| 8 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
+
from starlette.staticfiles import StaticFiles
|
| 10 |
+
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
| 11 |
+
from pydantic import BaseModel, HttpUrl
|
| 12 |
+
|
| 13 |
+
# --------------------------
|
| 14 |
+
# Config & Paths
|
| 15 |
+
# --------------------------
|
| 16 |
+
PORT = int(os.getenv("PORT", "7860"))
|
| 17 |
+
DATA_DIR = "/data" if os.path.isdir("/data") else "."
|
| 18 |
+
DB_PATH = os.path.join(DATA_DIR, "uptime.db")
|
| 19 |
+
DEFAULT_SITES = [
|
| 20 |
+
{"url": "https://huggingface.co", "name": "Hugging Face"},
|
| 21 |
+
{"url": "https://www.google.com", "name": "Google"},
|
| 22 |
+
{"url": "https://www.bbc.com", "name": "BBC"},
|
| 23 |
+
]
|
| 24 |
+
CHECK_INTERVAL_SECONDS = 300 # 5 minutes
|
| 25 |
+
HTTP_TIMEOUT_SECONDS = 10
|
| 26 |
+
|
| 27 |
+
# --------------------------
|
| 28 |
+
# DB setup
|
| 29 |
+
# --------------------------
|
| 30 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 31 |
+
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
| 32 |
+
conn.row_factory = sqlite3.Row
|
| 33 |
+
db_lock = Lock()
|
| 34 |
+
|
| 35 |
+
def init_db():
|
| 36 |
+
with db_lock:
|
| 37 |
+
c = conn.cursor()
|
| 38 |
+
c.execute("""
|
| 39 |
+
CREATE TABLE IF NOT EXISTS sites (
|
| 40 |
+
url TEXT PRIMARY KEY,
|
| 41 |
+
name TEXT NOT NULL,
|
| 42 |
+
method TEXT DEFAULT 'GET',
|
| 43 |
+
expected_min INT DEFAULT 200,
|
| 44 |
+
expected_max INT DEFAULT 399,
|
| 45 |
+
created_at TEXT DEFAULT (datetime('now'))
|
| 46 |
+
);
|
| 47 |
+
""")
|
| 48 |
+
c.execute("""
|
| 49 |
+
CREATE TABLE IF NOT EXISTS checks (
|
| 50 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 51 |
+
ts TEXT NOT NULL,
|
| 52 |
+
url TEXT NOT NULL,
|
| 53 |
+
ok INT NOT NULL,
|
| 54 |
+
status_code INT,
|
| 55 |
+
ms REAL,
|
| 56 |
+
FOREIGN KEY (url) REFERENCES sites(url)
|
| 57 |
+
);
|
| 58 |
+
""")
|
| 59 |
+
c.execute("""
|
| 60 |
+
CREATE TABLE IF NOT EXISTS incidents (
|
| 61 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 62 |
+
url TEXT NOT NULL,
|
| 63 |
+
start_ts TEXT NOT NULL,
|
| 64 |
+
end_ts TEXT,
|
| 65 |
+
FOREIGN KEY (url) REFERENCES sites(url)
|
| 66 |
+
);
|
| 67 |
+
""")
|
| 68 |
+
conn.commit()
|
| 69 |
+
|
| 70 |
+
# Seed a few demo sites if empty
|
| 71 |
+
existing = c.execute("SELECT COUNT(*) AS n FROM sites").fetchone()["n"]
|
| 72 |
+
if existing == 0:
|
| 73 |
+
for s in DEFAULT_SITES:
|
| 74 |
+
try:
|
| 75 |
+
c.execute("INSERT OR IGNORE INTO sites(url, name) VALUES (?, ?)", (s["url"], s["name"]))
|
| 76 |
+
except sqlite3.Error:
|
| 77 |
+
pass
|
| 78 |
+
conn.commit()
|
| 79 |
+
|
| 80 |
+
def list_sites():
|
| 81 |
+
with db_lock:
|
| 82 |
+
rows = conn.execute("SELECT url, name, method, expected_min, expected_max FROM sites ORDER BY url").fetchall()
|
| 83 |
+
return [dict(r) for r in rows]
|
| 84 |
+
|
| 85 |
+
def get_last_check(url: str):
|
| 86 |
+
with db_lock:
|
| 87 |
+
r = conn.execute(
|
| 88 |
+
"SELECT ts, ok, status_code, ms FROM checks WHERE url=? ORDER BY id DESC LIMIT 1", (url,)
|
| 89 |
+
).fetchone()
|
| 90 |
+
return dict(r) if r else None
|
| 91 |
+
|
| 92 |
+
def get_prev_check(url: str):
|
| 93 |
+
with db_lock:
|
| 94 |
+
r = conn.execute(
|
| 95 |
+
"SELECT ts, ok, status_code, ms FROM checks WHERE url=? ORDER BY id DESC LIMIT 1 OFFSET 1", (url,)
|
| 96 |
+
).fetchone()
|
| 97 |
+
return dict(r) if r else None
|
| 98 |
+
|
| 99 |
+
def record_check(url: str, ok: int, status_code: int | None, ms: float | None, ts_iso: str):
|
| 100 |
+
with db_lock:
|
| 101 |
+
conn.execute("INSERT INTO checks(ts, url, ok, status_code, ms) VALUES (?,?,?,?,?)",
|
| 102 |
+
(ts_iso, url, ok, status_code, ms))
|
| 103 |
+
conn.commit()
|
| 104 |
+
|
| 105 |
+
def open_incident_if_needed(url: str, now_iso: str, prev_ok: int | None, current_ok: int):
|
| 106 |
+
with db_lock:
|
| 107 |
+
# If transitioning from UP->DOWN, open a new incident
|
| 108 |
+
if (prev_ok == 1 or prev_ok is None) and current_ok == 0:
|
| 109 |
+
conn.execute("INSERT INTO incidents(url, start_ts) VALUES (?,?)", (url, now_iso))
|
| 110 |
+
conn.commit()
|
| 111 |
+
# If transitioning from DOWN->UP, close the latest open incident
|
| 112 |
+
elif prev_ok == 0 and current_ok == 1:
|
| 113 |
+
conn.execute(
|
| 114 |
+
"UPDATE incidents SET end_ts=? WHERE url=? AND end_ts IS NULL",
|
| 115 |
+
(now_iso, url)
|
| 116 |
+
)
|
| 117 |
+
conn.commit()
|
| 118 |
+
|
| 119 |
+
def incidents_for(url: str, limit: int = 50):
|
| 120 |
+
with db_lock:
|
| 121 |
+
rows = conn.execute(
|
| 122 |
+
"SELECT id, url, start_ts, end_ts FROM incidents WHERE url=? ORDER BY id DESC LIMIT ?",
|
| 123 |
+
(url, limit)
|
| 124 |
+
).fetchall()
|
| 125 |
+
return [dict(r) for r in rows]
|
| 126 |
+
|
| 127 |
+
def uptime_ratio(url: str, since: datetime):
|
| 128 |
+
with db_lock:
|
| 129 |
+
rows = conn.execute(
|
| 130 |
+
"SELECT ok FROM checks WHERE url=? AND ts >= ?",
|
| 131 |
+
(url, since.replace(tzinfo=timezone.utc).isoformat())
|
| 132 |
+
).fetchall()
|
| 133 |
+
if not rows:
|
| 134 |
+
return None
|
| 135 |
+
total = len(rows)
|
| 136 |
+
ups = sum(1 for r in rows if r["ok"] == 1)
|
| 137 |
+
return round(100.0 * ups / total, 2)
|
| 138 |
+
|
| 139 |
+
# --------------------------
|
| 140 |
+
# HTTP checker
|
| 141 |
+
# --------------------------
|
| 142 |
+
async def check_one(client: httpx.AsyncClient, site: dict):
|
| 143 |
+
url = site["url"]
|
| 144 |
+
expected_min = int(site.get("expected_min", 200))
|
| 145 |
+
expected_max = int(site.get("expected_max", 399))
|
| 146 |
+
|
| 147 |
+
t0 = time.perf_counter()
|
| 148 |
+
code = None
|
| 149 |
+
ok = 0
|
| 150 |
+
try:
|
| 151 |
+
# Use GET with follow_redirects to treat 3xx as success if within range
|
| 152 |
+
resp = await client.get(url, follow_redirects=True, timeout=HTTP_TIMEOUT_SECONDS)
|
| 153 |
+
code = resp.status_code
|
| 154 |
+
ok = 1 if (expected_min <= code <= expected_max) else 0
|
| 155 |
+
except Exception:
|
| 156 |
+
ok = 0
|
| 157 |
+
ms = round((time.perf_counter() - t0) * 1000.0, 2)
|
| 158 |
+
now_iso = datetime.now(timezone.utc).isoformat()
|
| 159 |
+
|
| 160 |
+
prev = get_last_check(url)
|
| 161 |
+
prev_ok = prev["ok"] if prev else None
|
| 162 |
+
|
| 163 |
+
record_check(url, ok, code, ms, now_iso)
|
| 164 |
+
open_incident_if_needed(url, now_iso, prev_ok, ok)
|
| 165 |
+
|
| 166 |
+
return {
|
| 167 |
+
"url": url,
|
| 168 |
+
"ok": ok,
|
| 169 |
+
"status_code": code,
|
| 170 |
+
"ms": ms,
|
| 171 |
+
"ts": now_iso
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
async def run_checks():
|
| 175 |
+
sites = list_sites()
|
| 176 |
+
if not sites:
|
| 177 |
+
return []
|
| 178 |
+
limits = httpx.Limits(max_connections=20, max_keepalive_connections=10)
|
| 179 |
+
async with httpx.AsyncClient(limits=limits) as client:
|
| 180 |
+
tasks = [check_one(client, s) for s in sites]
|
| 181 |
+
return await asyncio.gather(*tasks)
|
| 182 |
+
|
| 183 |
+
# --------------------------
|
| 184 |
+
# API & Scheduler
|
| 185 |
+
# --------------------------
|
| 186 |
+
app = FastAPI(title="HF Uptime Monitor", version="1.0.0")
|
| 187 |
+
|
| 188 |
+
# Simple CORS for same-origin front-end
|
| 189 |
+
app.add_middleware(
|
| 190 |
+
CORSMiddleware,
|
| 191 |
+
allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
class SiteIn(BaseModel):
|
| 195 |
+
url: HttpUrl
|
| 196 |
+
name: str
|
| 197 |
+
|
| 198 |
+
@app.on_event("startup")
|
| 199 |
+
async def on_startup():
|
| 200 |
+
init_db()
|
| 201 |
+
# Kick an immediate check on boot (don't await, run in background)
|
| 202 |
+
asyncio.create_task(run_checks())
|
| 203 |
+
|
| 204 |
+
# Start APScheduler
|
| 205 |
+
scheduler = AsyncIOScheduler()
|
| 206 |
+
scheduler.add_job(run_checks, "interval", seconds=CHECK_INTERVAL_SECONDS, next_run_time=datetime.now(timezone.utc))
|
| 207 |
+
scheduler.start()
|
| 208 |
+
app.state.scheduler = scheduler
|
| 209 |
+
|
| 210 |
+
@app.on_event("shutdown")
|
| 211 |
+
async def on_shutdown():
|
| 212 |
+
sched = getattr(app.state, "scheduler", None)
|
| 213 |
+
if sched:
|
| 214 |
+
sched.shutdown(wait=False)
|
| 215 |
+
|
| 216 |
+
# Static files
|
| 217 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 218 |
+
|
| 219 |
+
@app.get("/")
|
| 220 |
+
def root():
|
| 221 |
+
return FileResponse("static/index.html")
|
| 222 |
+
|
| 223 |
+
@app.get("/api/sites")
|
| 224 |
+
def api_sites():
|
| 225 |
+
return JSONResponse(list_sites())
|
| 226 |
+
|
| 227 |
+
@app.post("/api/sites")
|
| 228 |
+
def api_add_site(site: SiteIn):
|
| 229 |
+
url_s = str(site.url) # <- cast from HttpUrl to str
|
| 230 |
+
name_s = (site.name or "").strip() or url_s
|
| 231 |
+
with db_lock:
|
| 232 |
+
try:
|
| 233 |
+
conn.execute(
|
| 234 |
+
"INSERT OR IGNORE INTO sites(url, name) VALUES (?, ?)",
|
| 235 |
+
(url_s, name_s)
|
| 236 |
+
)
|
| 237 |
+
conn.commit()
|
| 238 |
+
except sqlite3.Error as e:
|
| 239 |
+
# bubble up the real DB message so the UI can show it
|
| 240 |
+
raise HTTPException(400, f"DB error: {e}")
|
| 241 |
+
return {"ok": True}
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
@app.delete("/api/sites")
|
| 245 |
+
def api_delete_site(url: str):
|
| 246 |
+
with db_lock:
|
| 247 |
+
conn.execute("DELETE FROM sites WHERE url=?", (url,))
|
| 248 |
+
conn.execute("DELETE FROM checks WHERE url=?", (url,))
|
| 249 |
+
conn.execute("DELETE FROM incidents WHERE url=?", (url,))
|
| 250 |
+
conn.commit()
|
| 251 |
+
return {"ok": True}
|
| 252 |
+
|
| 253 |
+
@app.post("/api/check-now")
|
| 254 |
+
async def api_check_now():
|
| 255 |
+
results = await run_checks()
|
| 256 |
+
return results
|
| 257 |
+
|
| 258 |
+
@app.get("/api/status")
|
| 259 |
+
def api_status():
|
| 260 |
+
sites = list_sites()
|
| 261 |
+
now = datetime.now(timezone.utc)
|
| 262 |
+
since_24h = now - timedelta(hours=24)
|
| 263 |
+
since_7d = now - timedelta(days=7)
|
| 264 |
+
|
| 265 |
+
out = []
|
| 266 |
+
for s in sites:
|
| 267 |
+
last = get_last_check(s["url"])
|
| 268 |
+
u24 = uptime_ratio(s["url"], since_24h)
|
| 269 |
+
u7 = uptime_ratio(s["url"], since_7d)
|
| 270 |
+
open_incident = False
|
| 271 |
+
with db_lock:
|
| 272 |
+
open_inc = conn.execute(
|
| 273 |
+
"SELECT 1 FROM incidents WHERE url=? AND end_ts IS NULL LIMIT 1",
|
| 274 |
+
(s["url"],)
|
| 275 |
+
).fetchone()
|
| 276 |
+
open_incident = bool(open_inc)
|
| 277 |
+
|
| 278 |
+
out.append({
|
| 279 |
+
"url": s["url"],
|
| 280 |
+
"name": s["name"],
|
| 281 |
+
"last": last, # {ts, ok, status_code, ms} or None
|
| 282 |
+
"uptime24h": u24, # float or None
|
| 283 |
+
"uptime7d": u7, # float or None
|
| 284 |
+
"open_incident": open_incident
|
| 285 |
+
})
|
| 286 |
+
return out
|
| 287 |
+
|
| 288 |
+
@app.get("/api/incidents")
|
| 289 |
+
def api_incidents(url: str):
|
| 290 |
+
return incidents_for(url, limit=100)
|
| 291 |
+
|
| 292 |
+
@app.get("/api/ping")
|
| 293 |
+
def api_ping():
|
| 294 |
+
return {"message": "pong"}
|
| 295 |
+
|
| 296 |
+
if __name__ == "__main__":
|
| 297 |
+
import uvicorn
|
| 298 |
+
uvicorn.run(app, host="0.0.0.0", port=PORT)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.112.2
|
| 2 |
+
uvicorn[standard]==0.30.6
|
| 3 |
+
httpx==0.27.2
|
| 4 |
+
apscheduler==3.10.4
|
static/app.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const tbody = document.getElementById("statusTbody");
|
| 2 |
+
const lastRefresh = document.getElementById("lastRefresh");
|
| 3 |
+
const checkNowBtn = document.getElementById("checkNowBtn");
|
| 4 |
+
const addSiteBtn = document.getElementById("addSiteBtn");
|
| 5 |
+
const addDialog = document.getElementById("addDialog");
|
| 6 |
+
const addForm = document.getElementById("addForm");
|
| 7 |
+
const cancelAdd = document.getElementById("cancelAdd");
|
| 8 |
+
const siteName = document.getElementById("siteName");
|
| 9 |
+
const siteUrl = document.getElementById("siteUrl");
|
| 10 |
+
|
| 11 |
+
const incidentPane = document.getElementById("incidentPane");
|
| 12 |
+
const incidentTitle = document.getElementById("incidentTitle");
|
| 13 |
+
const incidentsList = document.getElementById("incidentsList");
|
| 14 |
+
const closeIncidents = document.getElementById("closeIncidents");
|
| 15 |
+
|
| 16 |
+
function fmtTs(s){
|
| 17 |
+
if(!s) return "—";
|
| 18 |
+
const d = new Date(s);
|
| 19 |
+
return d.toLocaleString();
|
| 20 |
+
}
|
| 21 |
+
function fmtPct(v){
|
| 22 |
+
if(v === null || v === undefined) return "—";
|
| 23 |
+
return `${v.toFixed ? v.toFixed(2) : v}%`;
|
| 24 |
+
}
|
| 25 |
+
function dot(ok){
|
| 26 |
+
return `<span class="dot ${ok ? 'green':'red'}" title="${ok?'UP':'DOWN'}"></span>`;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
async function fetchStatus(){
|
| 30 |
+
const res = await fetch("/api/status");
|
| 31 |
+
const data = await res.json();
|
| 32 |
+
tbody.innerHTML = "";
|
| 33 |
+
data.forEach(item => {
|
| 34 |
+
const last = item.last || {};
|
| 35 |
+
const tr = document.createElement("tr");
|
| 36 |
+
tr.innerHTML = `
|
| 37 |
+
<td>${dot(last.ok)}</td>
|
| 38 |
+
<td>${item.name}</td>
|
| 39 |
+
<td><a class="url" href="${item.url}" target="_blank" rel="noopener">${item.url}</a></td>
|
| 40 |
+
<td>${fmtTs(last.ts)}</td>
|
| 41 |
+
<td>${last.ms ?? "—"}</td>
|
| 42 |
+
<td>${last.status_code ?? "—"}</td>
|
| 43 |
+
<td><span class="badge">${fmtPct(item.uptime24h)}</span></td>
|
| 44 |
+
<td><span class="badge">${fmtPct(item.uptime7d)}</span></td>
|
| 45 |
+
<td class="row-actions">
|
| 46 |
+
<button class="ghost" data-action="incidents" data-url="${item.url}" data-name="${item.name}">Incidents</button>
|
| 47 |
+
<button class="ghost" data-action="delete" data-url="${item.url}">Delete</button>
|
| 48 |
+
</td>
|
| 49 |
+
`;
|
| 50 |
+
tbody.appendChild(tr);
|
| 51 |
+
});
|
| 52 |
+
lastRefresh.textContent = `Last refresh: ${new Date().toLocaleTimeString()}`;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
async function checkNow(){
|
| 56 |
+
checkNowBtn.disabled = true;
|
| 57 |
+
try{
|
| 58 |
+
await fetch("/api/check-now", {method:"POST"});
|
| 59 |
+
await fetchStatus();
|
| 60 |
+
} finally {
|
| 61 |
+
checkNowBtn.disabled = false;
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
function openAdd(){
|
| 66 |
+
siteName.value = "";
|
| 67 |
+
siteUrl.value = "";
|
| 68 |
+
addDialog.showModal();
|
| 69 |
+
}
|
| 70 |
+
function closeAdd(){
|
| 71 |
+
addDialog.close();
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
addForm.addEventListener("submit", async (e) => {
|
| 75 |
+
e.preventDefault();
|
| 76 |
+
const body = { name: siteName.value || siteUrl.value, url: siteUrl.value };
|
| 77 |
+
if(!body.url) return;
|
| 78 |
+
const res = await fetch("/api/sites", {
|
| 79 |
+
method:"POST",
|
| 80 |
+
headers: { "Content-Type":"application/json" },
|
| 81 |
+
body: JSON.stringify(body)
|
| 82 |
+
});
|
| 83 |
+
if (res.ok){
|
| 84 |
+
closeAdd();
|
| 85 |
+
await fetchStatus();
|
| 86 |
+
} else {
|
| 87 |
+
const msg = await res.text(); // <-- show backend detail
|
| 88 |
+
alert("Failed to add site:\n" + msg);
|
| 89 |
+
}
|
| 90 |
+
});
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
cancelAdd.addEventListener("click", (e)=>{ e.preventDefault(); closeAdd(); });
|
| 94 |
+
|
| 95 |
+
tbody.addEventListener("click", async (e) => {
|
| 96 |
+
const btn = e.target.closest("button");
|
| 97 |
+
if(!btn) return;
|
| 98 |
+
const action = btn.dataset.action;
|
| 99 |
+
const url = btn.dataset.url;
|
| 100 |
+
if(action === "delete"){
|
| 101 |
+
if(confirm(`Delete monitor for:\n${url}?`)){
|
| 102 |
+
await fetch(`/api/sites?url=${encodeURIComponent(url)}`, { method: "DELETE" });
|
| 103 |
+
await fetchStatus();
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
if(action === "incidents"){
|
| 107 |
+
await loadIncidents(url, btn.dataset.name || url);
|
| 108 |
+
}
|
| 109 |
+
});
|
| 110 |
+
|
| 111 |
+
async function loadIncidents(url, name){
|
| 112 |
+
const res = await fetch(`/api/incidents?url=${encodeURIComponent(url)}`);
|
| 113 |
+
const data = await res.json();
|
| 114 |
+
incidentPane.classList.remove("hidden");
|
| 115 |
+
incidentTitle.textContent = `Incidents — ${name}`;
|
| 116 |
+
if(!data.length){
|
| 117 |
+
incidentsList.innerHTML = `<div class="muted" style="padding:8px 2px">No incidents recorded.</div>`;
|
| 118 |
+
return;
|
| 119 |
+
}
|
| 120 |
+
incidentsList.innerHTML = "";
|
| 121 |
+
data.forEach(x => {
|
| 122 |
+
const end = x.end_ts ? new Date(x.end_ts) : null;
|
| 123 |
+
const start = new Date(x.start_ts);
|
| 124 |
+
const durationMin = end ? Math.max(0, Math.round((end - start)/60000)) : null;
|
| 125 |
+
const div = document.createElement("div");
|
| 126 |
+
div.className = "incident";
|
| 127 |
+
div.innerHTML = `
|
| 128 |
+
<div>
|
| 129 |
+
<div><strong class="down">DOWN</strong> ${start.toLocaleString()}</div>
|
| 130 |
+
${end ? `<div><strong class="ok">UP</strong> ${end.toLocaleString()}</div>` : `<div class="muted">ongoing...</div>`}
|
| 131 |
+
</div>
|
| 132 |
+
<div class="muted">${durationMin !== null ? durationMin + " min" : ""}</div>
|
| 133 |
+
`;
|
| 134 |
+
incidentsList.appendChild(div);
|
| 135 |
+
});
|
| 136 |
+
}
|
| 137 |
+
closeIncidents.addEventListener("click", ()=> incidentPane.classList.add("hidden"));
|
| 138 |
+
|
| 139 |
+
checkNowBtn.addEventListener("click", checkNow);
|
| 140 |
+
addSiteBtn.addEventListener("click", openAdd);
|
| 141 |
+
|
| 142 |
+
fetchStatus();
|
| 143 |
+
setInterval(fetchStatus, 30000);
|
static/index.html
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8"/>
|
| 5 |
+
<meta name="viewport" content="width=device-width,initial-scale=1"/>
|
| 6 |
+
<title>Uptime Monitor</title>
|
| 7 |
+
<link rel="stylesheet" href="/static/style.css"/>
|
| 8 |
+
</head>
|
| 9 |
+
<body>
|
| 10 |
+
<header>
|
| 11 |
+
<div class="brand">
|
| 12 |
+
<div class="logo-dot"></div>
|
| 13 |
+
<div class="title">Uptime Monitor</div>
|
| 14 |
+
</div>
|
| 15 |
+
<div class="actions">
|
| 16 |
+
<button id="checkNowBtn">Check Now</button>
|
| 17 |
+
<button id="addSiteBtn">+ Add Site</button>
|
| 18 |
+
</div>
|
| 19 |
+
</header>
|
| 20 |
+
|
| 21 |
+
<main>
|
| 22 |
+
<section class="card">
|
| 23 |
+
<div class="card-head">
|
| 24 |
+
<h2>Monitors</h2>
|
| 25 |
+
<span id="lastRefresh" class="muted"></span>
|
| 26 |
+
</div>
|
| 27 |
+
<div class="table-wrap">
|
| 28 |
+
<table id="statusTable">
|
| 29 |
+
<thead>
|
| 30 |
+
<tr>
|
| 31 |
+
<th>Status</th>
|
| 32 |
+
<th>Name</th>
|
| 33 |
+
<th>URL</th>
|
| 34 |
+
<th>Last Check</th>
|
| 35 |
+
<th>Resp (ms)</th>
|
| 36 |
+
<th>Code</th>
|
| 37 |
+
<th>Uptime 24h</th>
|
| 38 |
+
<th>Uptime 7d</th>
|
| 39 |
+
<th></th>
|
| 40 |
+
</tr>
|
| 41 |
+
</thead>
|
| 42 |
+
<tbody id="statusTbody"></tbody>
|
| 43 |
+
</table>
|
| 44 |
+
</div>
|
| 45 |
+
</section>
|
| 46 |
+
|
| 47 |
+
<section id="incidentPane" class="card hidden">
|
| 48 |
+
<div class="card-head">
|
| 49 |
+
<h2 id="incidentTitle">Incidents</h2>
|
| 50 |
+
<button id="closeIncidents" class="ghost">Close</button>
|
| 51 |
+
</div>
|
| 52 |
+
<div id="incidentsList" class="incidents"></div>
|
| 53 |
+
</section>
|
| 54 |
+
</main>
|
| 55 |
+
|
| 56 |
+
<!-- Add site modal -->
|
| 57 |
+
<dialog id="addDialog">
|
| 58 |
+
<form method="dialog" id="addForm" class="dialog-card">
|
| 59 |
+
<h3>Add Site</h3>
|
| 60 |
+
<label>Display Name
|
| 61 |
+
<input type="text" id="siteName" placeholder="e.g., My API"/>
|
| 62 |
+
</label>
|
| 63 |
+
<label>URL
|
| 64 |
+
<input type="url" id="siteUrl" placeholder="https://example.com" required/>
|
| 65 |
+
</label>
|
| 66 |
+
<div class="row">
|
| 67 |
+
<button type="submit" id="saveSite">Save</button>
|
| 68 |
+
<button id="cancelAdd" class="ghost">Cancel</button>
|
| 69 |
+
</div>
|
| 70 |
+
</form>
|
| 71 |
+
</dialog>
|
| 72 |
+
|
| 73 |
+
<script src="/static/app.js"></script>
|
| 74 |
+
</body>
|
| 75 |
+
</html>
|
static/style.css
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
:root{
|
| 2 |
+
--bg:#0b1220; --panel:#121a2e; --card:#0f1730; --text:#e6edf7; --muted:#9fb0cf;
|
| 3 |
+
--green:#18c37e; --red:#ff6363; --accent:#3a8dde; --ring: rgba(58,141,222,.35);
|
| 4 |
+
--radius:16px; --shadow: 0 10px 28px rgba(2,8,23,.35);
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
*{box-sizing:border-box}
|
| 8 |
+
html,body{height:100%}
|
| 9 |
+
body{
|
| 10 |
+
margin:0; font: 15px/1.45 ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial;
|
| 11 |
+
color:var(--text); background:linear-gradient(180deg,#0b1220,#0b1220 60%, #0d1530);
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
header{
|
| 15 |
+
display:flex; align-items:center; justify-content:space-between;
|
| 16 |
+
padding:16px 22px; position:sticky; top:0; background:#0b1220cc; backdrop-filter:saturate(120%) blur(6px);
|
| 17 |
+
border-bottom:1px solid #1b2542;
|
| 18 |
+
}
|
| 19 |
+
.brand{display:flex; align-items:center; gap:12px;}
|
| 20 |
+
.logo-dot{width:14px; height:14px; border-radius:50%; background:linear-gradient(135deg,var(--accent),#6ea8ff); box-shadow:0 0 20px #2e6fd6;}
|
| 21 |
+
.title{font-weight:700; letter-spacing:.3px}
|
| 22 |
+
|
| 23 |
+
.actions button{
|
| 24 |
+
background:var(--accent); color:white; border:none; padding:10px 14px; border-radius:12px; cursor:pointer;
|
| 25 |
+
box-shadow:var(--shadow); margin-left:8px; font-weight:600;
|
| 26 |
+
}
|
| 27 |
+
.actions button#addSiteBtn{background:#243254; border:1px solid #2d3c63}
|
| 28 |
+
|
| 29 |
+
main{max-width:1100px; margin:26px auto; padding:0 16px; display:grid; gap:18px}
|
| 30 |
+
.card{
|
| 31 |
+
background:var(--panel); border-radius:var(--radius); box-shadow:var(--shadow); border:1px solid #1b2542;
|
| 32 |
+
}
|
| 33 |
+
.card-head{display:flex; align-items:center; justify-content:space-between; padding:16px 18px; border-bottom:1px solid #1b2542}
|
| 34 |
+
.muted{color:var(--muted); font-size:13px}
|
| 35 |
+
.table-wrap{overflow:auto}
|
| 36 |
+
table{width:100%; border-collapse:collapse}
|
| 37 |
+
th, td{padding:12px 14px; border-bottom:1px solid #1b2542; text-align:left}
|
| 38 |
+
th{color:var(--muted); font-weight:600; background: #0f1730}
|
| 39 |
+
tbody tr:hover{background:#0f1730}
|
| 40 |
+
|
| 41 |
+
.dot{
|
| 42 |
+
width:12px; height:12px; border-radius:50%;
|
| 43 |
+
box-shadow:0 0 0 3px #0b1220, 0 0 16px rgba(0,0,0,.25);
|
| 44 |
+
display:inline-block;
|
| 45 |
+
}
|
| 46 |
+
.dot.green{background:var(--green)}
|
| 47 |
+
.dot.red{background:var(--red)}
|
| 48 |
+
|
| 49 |
+
a.url{color:#a9c6ff; text-decoration:none}
|
| 50 |
+
a.url:hover{text-decoration:underline}
|
| 51 |
+
|
| 52 |
+
.badge{padding:4px 8px; border-radius:999px; font-size:12px; border:1px solid #263257; color:#c9d7ff; background:#102143}
|
| 53 |
+
|
| 54 |
+
td .row-actions{display:flex; gap:8px}
|
| 55 |
+
button.ghost{
|
| 56 |
+
background:transparent; border:1px solid #27355f; color:#c9d7ff; padding:8px 12px; border-radius:12px; cursor:pointer;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
.hidden{display:none}
|
| 60 |
+
|
| 61 |
+
.incidents{padding:10px 16px}
|
| 62 |
+
.incident{
|
| 63 |
+
display:flex; align-items:center; justify-content:space-between;
|
| 64 |
+
background:#0f1730; border:1px solid #1b2542; border-radius:12px; padding:10px 12px; margin-bottom:10px;
|
| 65 |
+
}
|
| 66 |
+
.incident .down{color:#ff9f9f}
|
| 67 |
+
.incident .ok{color:#9fffc7}
|
| 68 |
+
|
| 69 |
+
dialog{
|
| 70 |
+
border:none; border-radius:18px; padding:0; background:#111a31; color:var(--text); box-shadow: var(--shadow);
|
| 71 |
+
}
|
| 72 |
+
.dialog-card{padding:18px; width:360px}
|
| 73 |
+
.dialog-card h3{margin:0 0 10px}
|
| 74 |
+
.dialog-card label{display:block; margin:10px 0}
|
| 75 |
+
.dialog-card input{
|
| 76 |
+
width:100%; padding:10px 12px; border-radius:12px; border:1px solid #283663; background:#0f1730; color:var(--text);
|
| 77 |
+
}
|
| 78 |
+
.dialog-card .row{display:flex; gap:10px; margin-top:14px}
|
| 79 |
+
.dialog-card button{
|
| 80 |
+
background:var(--accent); color:white; border:none; padding:10px 14px; border-radius:12px; cursor:pointer; font-weight:600;
|
| 81 |
+
}
|
| 82 |
+
.dialog-card button.ghost{background:transparent; border:1px solid #27355f; color:#c9d7ff}
|