discord-bot/src/lib/prometheus.ts
rainofdestiny 3ac39d03ce
Some checks failed
CI/CD / typecheck (push) Failing after 46s
CI/CD / docker (push) Has been skipped
CI/CD / deploy (push) Has been skipped
refactor: rewrite in TypeScript (discord.js v14)
- Proper modular structure: src/commands/ + src/lib/
- Autocomplete for /logs and /restart (live container names)
- All metrics queries run in parallel (Promise.all)
- Multi-stage Docker build (builder + production)
- TypeScript type check as CI job before docker build

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-26 05:42:51 +07:00

19 lines
695 B
TypeScript

const BASE = process.env.PROMETHEUS_URL ?? 'http://prometheus:9090';
interface PromResponse {
status: string;
data: { result: Array<{ value: [number, string] }> };
}
/** Run an instant query and return the scalar value, or null on error. */
export async function scalar(q: string): Promise<number | null> {
try {
const url = `${BASE}/api/v1/query?query=${encodeURIComponent(q)}`;
const res = await fetch(url, { signal: AbortSignal.timeout(6_000) });
const json = await res.json() as PromResponse;
if (json.status === 'success' && json.data.result.length > 0) {
return parseFloat(json.data.result[0].value[1]);
}
} catch { /* unreachable */ }
return null;
}