homepage-blocky-widget/src/widgets/blocky/proxy.js

34 lines
1.1 KiB
JavaScript
Raw Normal View History

import getServiceWidget from "utils/config/service-helpers";
import { formatApiCall } from "utils/proxy/api-helpers";
import { httpProxy } from "utils/proxy/http";
function parseMetric(text, name) {
const re = new RegExp(`^${name}(?:{[^}]*})?\\s+([0-9.e+\\-]+)`, "m");
const m = text.match(re);
return m ? parseFloat(m[1]) : 0;
}
export default async function blockyProxyHandler(req, res) {
const { group, service } = req.query;
const widget = await getServiceWidget(group, service);
const url = formatApiCall("{url}/metrics", widget);
const [status, , data] = await httpProxy(url);
if (status !== 200) {
return res.status(status).json({ error: `HTTP ${status}` });
}
const text = data.toString();
const hits = parseMetric(text, "blocky_cache_hits_total");
const misses = parseMetric(text, "blocky_cache_misses_total");
const total = hits + misses;
return res.json({
enabled: parseMetric(text, "blocky_blocking_enabled") === 1,
denylistEntries: parseMetric(text, "blocky_denylist_cache_entries"),
totalQueries: total,
cacheHitRate: total > 0 ? (hits / total) * 100 : 0,
});
}