fet: reduce LOC by reducing comments

This commit is contained in:
2026-03-28 18:05:29 +04:00
parent 005f635fab
commit 8e554498f0
20 changed files with 2 additions and 246 deletions
-9
View File
@@ -1,13 +1,9 @@
// PingQL Dashboard — shared utilities
// Auth is now cookie-based. No localStorage needed.
const API_BASE = 'https://api.pingql.com';
function logout() {
window.location.href = '/dashboard/logout';
}
// requireAuth is a no-op now — server redirects to /dashboard if not authed
function requireAuth() { return true; }
async function api(path, opts = {}) {
@@ -29,7 +25,6 @@ async function api(path, opts = {}) {
return data;
}
// Format relative time
function formatAgo(ms) {
const s = Math.ceil(ms / 1000) || 1;
if (s < 60) return `${s}s ago`;
@@ -44,7 +39,6 @@ function timeAgo(date) {
return `<span class="timestamp" data-ts="${ts}">${formatAgo(elapsed)}</span>`;
}
// Tick all live timestamps
setInterval(() => {
document.querySelectorAll('.timestamp[data-ts]').forEach(el => {
const elapsed = Date.now() - Number(el.dataset.ts);
@@ -58,9 +52,6 @@ function escapeHtml(str) {
return div.innerHTML;
}
// Subscribe to live ping updates for the whole account via a single SSE stream.
// onPing receives each ping object (includes monitor_id).
// Returns an AbortController — call .abort() to close.
function watchAccount(onPing) {
const ac = new AbortController();
-5
View File
@@ -1,5 +1,3 @@
// PingQL Visual Query Builder
const FIELDS = [
{ name: 'status', label: 'Status Code', type: 'number', operators: ['$eq', '$ne', '$gt', '$gte', '$lt', '$lte', '$in'] },
{ name: 'body', label: 'Response Body', type: 'string', operators: ['$eq', '$ne', '$contains', '$startsWith', '$endsWith', '$regex', '$exists'] },
@@ -61,7 +59,6 @@ class QueryBuilder {
return { [headerField]: { [operator]: parsedVal } };
}
if (operator === '$exists') return { [field]: { '$exists': parsedVal } };
// Simple shorthand for $eq on basic fields
if (operator === '$eq') return { [field]: parsedVal };
return { [field]: { [operator]: parsedVal } };
}
@@ -93,7 +90,6 @@ class QueryBuilder {
return;
}
// Strip $consider before parsing rules
this.consider = query.$consider === 'down' ? 'down' : 'up';
const q = Object.fromEntries(Object.entries(query).filter(([k]) => k !== '$consider'));
@@ -201,7 +197,6 @@ class QueryBuilder {
</div>
`;
// Bind events
this.container.querySelector('#qb-consider').addEventListener('change', (e) => {
this.consider = e.target.value;
this.render();
-1
View File
@@ -76,7 +76,6 @@ export const account = new Elysia({ prefix: "/account" })
await sql`INSERT INTO accounts (key, email_hash) VALUES (${key}, ${emailHash})`;
cookie.pingql_key.set({ value: key, ...COOKIE_OPTS });
// Form submission → redirect to welcome page showing the key
if ((body as any)._form) return redir(`/dashboard/welcome?key=${encodeURIComponent(key)}`);
return { key, email_registered: !!emailHash };
-32
View File
@@ -33,7 +33,6 @@ function latencyChartSSR(pings: any[]): string {
const pad = { top: 8, bottom: 8 };
const cH = h - pad.top - pad.bottom;
// Build ordered list of unique runs, evenly spaced (matches canvas)
const runTimes: Record<string, number[]> = {};
for (const p of data) {
const rid = p.run_id || p.checked_at;
@@ -49,7 +48,6 @@ function latencyChartSSR(pings: any[]): string {
runs.forEach((rid, i) => { runIndex[rid] = i; });
const maxIdx = Math.max(runs.length - 1, 1);
// Group by region
const byRegion: Record<string, any[]> = {};
for (const p of data) {
const key = p.region || '__none__';
@@ -71,7 +69,6 @@ function latencyChartSSR(pings: any[]): string {
return pad.top + cH - ((v - yMin) / yRange) * cH;
}
// Grid lines
let grid = '';
for (let i = 0; i <= 4; i++) {
const y = (pad.top + (cH / 4) * i).toFixed(1);
@@ -152,15 +149,12 @@ const dashDir = resolve(import.meta.dir, "../dashboard");
export const dashboard = new Elysia()
.get("/", () => html("landing", {}))
// Shared assets
.get("/favicon.svg", () => new Response(Bun.file(`${dashDir}/favicon.svg`), { headers: { "content-type": "image/svg+xml", "cache-control": "public, max-age=86400" } }))
.get("/assets/tailwind.css", () => new Response(Bun.file(`${dashDir}/tailwind.css`), { headers: { "cache-control": "public, max-age=31536000, immutable" } }))
.get("/assets/app.css", () => new Response(Bun.file(`${dashDir}/app.css`), { headers: { "cache-control": "public, max-age=31536000, immutable" } }))
.get("/assets/app.js", () => new Response(Bun.file(`${dashDir}/app.js`), { headers: { "cache-control": "public, max-age=31536000, immutable" } }))
// Dashboard-only assets
.get("/dashboard/query-builder.js", () => new Response(Bun.file(`${dashDir}/query-builder.js`), { headers: { "cache-control": "public, max-age=31536000, immutable" } }))
// Login page
.get("/dashboard", async ({ cookie }) => {
const key = cookie?.pingql_key?.value;
if (key) {
@@ -172,21 +166,17 @@ export const dashboard = new Elysia()
return html("login", {});
})
// Logout
.get("/dashboard/logout", ({ cookie }) => {
// Explicitly expire with same domain/path so browser actually clears it
cookie.pingql_key?.set({ value: "", maxAge: 0, path: "/", domain: process.env.COOKIE_DOMAIN ?? ".pingql.com", secure: process.env.NODE_ENV !== "development", sameSite: "lax" });
return redirect("/dashboard");
})
// Welcome page — shows new account key after registration (no-JS flow)
.get("/dashboard/welcome", async ({ cookie, headers, query }) => {
const resolved = await getAccountId(cookie, headers);
if (!resolved?.accountId) return redirect("/dashboard");
return html("welcome", { key: query.key || cookie?.pingql_key?.value || "" });
})
// Home — SSR monitor list
.get("/dashboard/home", async ({ cookie, headers }) => {
const resolved = await getAccountId(cookie, headers);
const accountId = resolved?.accountId ?? null;
@@ -202,7 +192,6 @@ export const dashboard = new Elysia()
ORDER BY m.created_at DESC
`;
// Fetch last 20 pings per monitor for sparklines
const monitorIds = monitors.map((m: any) => m.id);
let pingsMap: Record<string, any[]> = {};
if (monitorIds.length > 0) {
@@ -226,7 +215,6 @@ export const dashboard = new Elysia()
return html("home", { nav: "monitors", monitors: monitorsWithPings, accountId });
})
// Settings — SSR account info
.get("/dashboard/settings", async ({ cookie, headers }) => {
const resolved = await getAccountId(cookie, headers);
const accountId = resolved?.accountId ?? null;
@@ -239,7 +227,6 @@ export const dashboard = new Elysia()
const loginKey = isSubKey ? null : (cookie?.pingql_key?.value ?? null);
const [{ count: monitorCount }] = await sql`SELECT COUNT(*)::int as count FROM monitors WHERE account_id = ${accountId}`;
// Fetch paid + active (non-expired) invoices
let invoices: any[] = [];
try {
invoices = await sql`
@@ -255,7 +242,6 @@ export const dashboard = new Elysia()
return html("settings", { nav: "settings", account: acc, apiKeys, accountId, loginKey, isSubKey, monitorCount, invoices });
})
// Checkout — upgrade plan
.get("/dashboard/checkout", async ({ cookie, headers }) => {
const resolved = await getAccountId(cookie, headers);
if (!resolved?.accountId) return redirect("/dashboard");
@@ -264,10 +250,8 @@ export const dashboard = new Elysia()
const hasLifetime = acc.plan === "lifetime" || stack.some((s: any) => s.plan === "lifetime");
if (acc.plan === "lifetime" && stack.length === 0) return redirect("/dashboard/settings");
// Total spent on paid invoices (for lifetime discount)
const [{ total_spent }] = await sql`SELECT COALESCE(SUM(amount_usd), 0)::numeric as total_spent FROM payments WHERE account_id = ${resolved.accountId} AND status = 'paid'`;
// Fetch coins server-side for no-JS rendering
const payApi = process.env.PAY_API || "https://pay.pingql.com";
let coins: any[] = [];
try {
@@ -279,7 +263,6 @@ export const dashboard = new Elysia()
return html("checkout", { nav: "settings", account: acc, payApi, invoiceId: null, coins, invoice: null, totalSpent: Number(total_spent), hasLifetime });
})
// Existing invoice by ID — SSR the payment status
.get("/dashboard/checkout/:id", async ({ cookie, headers, params }) => {
const resolved = await getAccountId(cookie, headers);
if (!resolved?.accountId) return redirect("/dashboard");
@@ -302,7 +285,6 @@ export const dashboard = new Elysia()
return html("checkout", { nav: "settings", account: acc, payApi, invoiceId: params.id, coins, invoice });
})
// Receipt (proxy to pay service, serves HTML directly)
.get("/dashboard/checkout/:id/receipt", async ({ cookie, headers, params, set }) => {
const resolved = await getAccountId(cookie, headers);
if (!resolved?.accountId) return redirect("/dashboard");
@@ -326,7 +308,6 @@ export const dashboard = new Elysia()
}
})
// Create checkout via form POST (no-JS)
.post("/dashboard/checkout", async ({ cookie, headers, body }) => {
const resolved = await getAccountId(cookie, headers);
if (!resolved?.accountId) return redirect("/dashboard");
@@ -352,7 +333,6 @@ export const dashboard = new Elysia()
return redirect("/dashboard/checkout");
})
// New monitor
.get("/dashboard/monitors/new", async ({ cookie, headers }) => {
const resolved = await getAccountId(cookie, headers);
const accountId = resolved?.accountId ?? null;
@@ -361,7 +341,6 @@ export const dashboard = new Elysia()
return html("new", { nav: "monitors", plan: resolved?.plan || "free" });
})
// Home data endpoint for polling (monitor list change detection)
.get("/dashboard/home/data", async ({ cookie, headers }) => {
const resolved = await getAccountId(cookie, headers);
const accountId = resolved?.accountId ?? null;
@@ -376,7 +355,6 @@ export const dashboard = new Elysia()
});
})
// Monitor detail — SSR with initial data
.get("/dashboard/monitors/:id", async ({ cookie, headers, params }) => {
const resolved = await getAccountId(cookie, headers);
const accountId = resolved?.accountId ?? null;
@@ -396,7 +374,6 @@ export const dashboard = new Elysia()
return html("detail", { nav: "monitors", monitor, pings, plan: resolved?.plan || "free" });
})
// Chart partial endpoint — returns just the latency chart SVG
.get("/dashboard/monitors/:id/chart", async ({ cookie, headers, params }) => {
const resolved = await getAccountId(cookie, headers);
const accountId = resolved?.accountId ?? null;
@@ -418,7 +395,6 @@ export const dashboard = new Elysia()
});
})
// Sparkline partial — returns just the SVG for one monitor
.get("/dashboard/monitors/:id/sparkline", async ({ cookie, headers, params }) => {
const resolved = await getAccountId(cookie, headers);
const accountId = resolved?.accountId ?? null;
@@ -440,9 +416,6 @@ export const dashboard = new Elysia()
});
})
// ── Form-based monitor actions (no-JS support) ─────────────────────
// Create monitor via form POST
.post("/dashboard/monitors/new", async ({ cookie, headers, body, set }) => {
const resolved = await getAccountId(cookie, headers);
if (!resolved?.accountId) return redirect("/dashboard");
@@ -451,7 +424,6 @@ export const dashboard = new Elysia()
const regions = Array.isArray(b.regions) ? b.regions : (b.regions ? [b.regions] : []);
const query = b.query ? (typeof b.query === "string" ? JSON.parse(b.query) : b.query) : undefined;
const requestHeaders: Record<string, string> = {};
// Collect header_key[]/header_value[] pairs
const hKeys = Array.isArray(b.header_key) ? b.header_key : (b.header_key ? [b.header_key] : []);
const hVals = Array.isArray(b.header_value) ? b.header_value : (b.header_value ? [b.header_value] : []);
for (let i = 0; i < hKeys.length; i++) {
@@ -481,7 +453,6 @@ export const dashboard = new Elysia()
return redirect("/dashboard/home");
})
// Edit monitor via form POST
.post("/dashboard/monitors/:id/edit", async ({ cookie, headers, params, body }) => {
const resolved = await getAccountId(cookie, headers);
if (!resolved?.accountId) return redirect("/dashboard");
@@ -519,7 +490,6 @@ export const dashboard = new Elysia()
return redirect(`/dashboard/monitors/${params.id}`);
})
// Delete monitor via form POST
.post("/dashboard/monitors/:id/delete", async ({ cookie, headers, params }) => {
const resolved = await getAccountId(cookie, headers);
if (!resolved?.accountId) return redirect("/dashboard");
@@ -534,7 +504,6 @@ export const dashboard = new Elysia()
return redirect("/dashboard/home");
})
// Toggle monitor via form POST
.post("/dashboard/monitors/:id/toggle", async ({ cookie, headers, params }) => {
const resolved = await getAccountId(cookie, headers);
if (!resolved?.accountId) return redirect("/dashboard");
@@ -549,7 +518,6 @@ export const dashboard = new Elysia()
return redirect(`/dashboard/monitors/${params.id}`);
})
// Docs
.get("/docs", () => html("docs", {}))
.get("/privacy", () => html("privacy", {}))
.get("/terms", () => html("tos", {}));
-6
View File
@@ -14,14 +14,10 @@ export function sparkline(values: number[], width = 120, height = 32, color = '#
import { REGION_COLORS } from "../../../shared/plans";
// Pick the best region: the one with the lowest avg latency across its last 3 pings.
// Only considers regions that have at least one ping in the most recent 3 pings overall,
// so stale regions that haven't reported recently are excluded.
export function pickBestRegion(pings: Array<{latency_ms?: number|null, region?: string|null}>): { region: string, values: number[], latest: number | null } {
const withLatency = pings.filter(p => p.latency_ms != null);
if (!withLatency.length) return { region: '__none__', values: [], latest: null };
// Group all pings by region
const byRegion: Record<string, number[]> = {};
for (const p of withLatency) {
const key = p.region || '__none__';
@@ -29,7 +25,6 @@ export function pickBestRegion(pings: Array<{latency_ms?: number|null, region?:
byRegion[key].push(p.latency_ms!);
}
// Only consider regions that appear in the 3 most recent pings
const recentRegions = new Set(
withLatency.slice(-3).map(p => p.region || '__none__')
);
@@ -47,7 +42,6 @@ export function pickBestRegion(pings: Array<{latency_ms?: number|null, region?:
return { region: bestRegion, values, latest: values.length ? values[values.length - 1] : null };
}
// Given pings with region+latency, pick the best region and render its sparkline.
export function sparklineFromPings(pings: Array<{latency_ms?: number|null, region?: string|null}>, width = 120, height = 32): string {
const { region, values } = pickBestRegion(pings);
if (!values.length) return '';