2026-05-21 13:34:33 +09:00
|
|
|
import getServiceWidget from "utils/config/service-helpers";
|
2026-05-21 11:59:09 +09:00
|
|
|
import { formatApiCall } from "utils/proxy/api-helpers";
|
2026-05-21 13:34:33 +09:00
|
|
|
import { httpProxy } from "utils/proxy/http";
|
2026-05-21 11:59:09 +09:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
}
|