This commit is contained in:
Mateusz Gruszczyński committed 2026-11-14 22:54:43 +01:00
1 parent e2c8099d9c
commit 5e57e48c6a
4 files changed
+43 -21

No files matched your search

+29 -9
View File
@@ -190,10 +190,17 @@ window.rustProxyCacheRules = (() => {
return targets || (envelope?.config?.vhosts || []).map(vhost => ({ id: vhost.id, hosts: vhost.frontend?.hosts || [], docker: false }));
}
const targetTitle = target => target.docker ? `${target.hosts?.[0] || target.id} (Docker)` : target.id;
let hits = {};
async function loadHits() {
if (!authenticated) return;
try { hits = (await api("/api/cache/rules/stats")).hits || {}; } catch { hits = {}; }
let hits = {}, hitsRequest = null, hitsFetchedAt = 0;
const HITS_REFRESH_MS = 15_000;
function loadHits() {
if (!authenticated) return Promise.resolve();
if (hitsRequest) return hitsRequest;
if (Date.now() - hitsFetchedAt < HITS_REFRESH_MS) return Promise.resolve();
hitsRequest = api("/api/cache/rules/stats")
.then(data => { hits = data.hits || {}; })
.catch(() => { hits = {}; })
.finally(() => { hitsFetchedAt = Date.now(); hitsRequest = null; });
return hitsRequest;
}
function select(values, current, label) {
const node = el("select"); for (const [value, text] of values) { const option = el("option", text); option.value = value; node.append(option); } node.value = current; if (label) node.setAttribute("aria-label", label); return node;
@@ -242,8 +249,10 @@ window.rustProxyCacheRules = (() => {
for (const isolated of [tester.node, importBox]) for (const type of ["input", "change"]) isolated.addEventListener(type, event => event.stopPropagation());
root.addEventListener("keydown", event => { if (event.key === "Enter" && event.target.matches?.("input:not([type=checkbox])")) event.preventDefault(); });
function taken() { return new Set(rules.map(rule => rule.id)); }
void loadTargets();
document.addEventListener("rustproxy-cache-targets", () => { if (root.isConnected && scope === "global" && open.size) render(); });
if (scope === "global") {
void loadTargets();
document.addEventListener("rustproxy-cache-targets", () => { if (root.isConnected && open.size) render(); });
}
function renderNotices() {
notices.replaceChildren();
if (scope !== "vhost") return;
@@ -252,6 +261,12 @@ window.rustProxyCacheRules = (() => {
if (globals.length) notices.append(el("p", t("cacheRulesGlobalApply").replace("{count}", globals.length).replace("{names}", globals.slice(0, 6).map(rule => rule.name || rule.id).join(", ") + (globals.length > 6 ? "…" : "")), "fine"));
for (const note of conflictsFor(rules, globals)) notices.append(el("p", note, "fine cache-rules-notice"));
}
function updateHitCounts() {
for (const node of list.querySelectorAll(".cache-rule-hits")) {
const count = hits[node.dataset.hitKey];
node.textContent = count == null ? "" : t("cacheRulesHits").replace("{count}", fmt(count));
}
}
function render() {
list.replaceChildren(...rules.map((rule, index) => ruleCard(rule, index)));
empty.hidden = rules.length > 0;
@@ -276,6 +291,7 @@ window.rustProxyCacheRules = (() => {
title.addEventListener("click", () => { if (open.has(rule.id)) open.delete(rule.id); else open.add(rule.id); render(); });
const hitKey = scope === "vhost" ? `vhost:${vhostId}:${rule.id}` : `global:${rule.id}`;
const hitCount = el("span", hits[hitKey] != null ? t("cacheRulesHits").replace("{count}", fmt(hits[hitKey])) : "", "fine cache-rule-hits");
hitCount.dataset.hitKey = hitKey;
const actions = el("span", undefined, "cache-rule-actions");
const up = el("button", "↑", "quiet"); up.type = "button"; up.disabled = index === 0; up.setAttribute("aria-label", t("cacheRulesMoveUp")); up.addEventListener("click", () => move(index, index - 1));
const down = el("button", "↓", "quiet"); down.type = "button"; down.disabled = index === rules.length - 1; down.setAttribute("aria-label", t("cacheRulesMoveDown")); down.addEventListener("click", () => move(index, index + 1));
@@ -392,7 +408,7 @@ window.rustProxyCacheRules = (() => {
const method = select(["GET", "HEAD", "POST"].map(value => [value, value]), "GET", t("cacheRuleFieldMethod"));
const url = textInput("", scope === "vhost" ? "/produkt?id=7&utm_source=fb" : "https://shop.example.com/produkt?id=7&utm_source=fb", "mono");
const vhostSelect = select([["", t("cacheRulesTesterNoVhost")], ...targetList().map(target => [target.id, targetTitle(target)])], vhostId, t("cacheRulesTesterVhost"));
document.addEventListener("rustproxy-cache-targets", () => {
if (scope === "global") document.addEventListener("rustproxy-cache-targets", () => {
const current = vhostSelect.value;
vhostSelect.replaceChildren(...[["", t("cacheRulesTesterNoVhost")], ...targetList().map(target => [target.id, targetTitle(target)])].map(([value, text]) => { const option = el("option", text); option.value = value; return option; }));
vhostSelect.value = [...vhostSelect.options].some(option => option.value === current) ? current : "";
@@ -473,9 +489,13 @@ window.rustProxyCacheRules = (() => {
}
return rules.map(rule => clean(rule, scope === "global"));
}
function refreshHits() {
if (!rules.length) return Promise.resolve();
return loadHits().then(updateHitCounts);
}
render();
void loadHits().then(render);
return { node: root, read, ignoreGlobal: () => ignoreGlobal, set(next) { rules = (next || []).map(normalize); open = new Set(); render(); }, refreshHits: () => loadHits().then(render) };
if (options.autoLoadHits !== false) void refreshHits();
return { node: root, read, ignoreGlobal: () => ignoreGlobal, set(next) { rules = (next || []).map(normalize); open = new Set(); render(); }, refreshHits };
}
return { create, exportVcl, TEMPLATES };
})();
+3 -2
View File
@@ -405,7 +405,7 @@
data.token = reply.token;
}
if (!name.value.trim() && data.name) { name.value = data.name; fire(name); }
endpoint.value = data.endpoint; token.value = data.token; fingerprint.value = data.fingerprint; [endpoint, token, fingerprint].forEach(fire);
endpoint.value = data.endpoint; token.value = data.token; fingerprint.value = data.fingerprint;[endpoint, token, fingerprint].forEach(fire);
provisionInput.value = ""; provisionStatus.textContent = t("dockerProvisionApplied").replace("{endpoint}", data.endpoint); provisionStatus.classList.remove("is-error");
updateSecurityBadge(row); setSettingsDirty("docker-agents-form");
} catch (error) { provisionStatus.textContent = error.message; provisionStatus.classList.add("is-error"); }
@@ -683,9 +683,10 @@
const ttl = input(assignment?.cache_default_ttl_seconds ?? "", "number", "docker-cache-ttl"); ttl.min = 0; ttl.max = 31536000; ttl.placeholder = String(envelope?.config?.cache?.default_ttl_seconds ?? "");
grid.append(labeled("dockerCacheTtl", ttl, t("dockerCacheTtlHelp")));
const vhostId = poolRoutes.find(item => item.agent_id === agent.id && item.service_key === key)?.pool_id || "";
const rules = window.rustProxyCacheRules?.create?.({ scope: "vhost", vhostId, rules: assignment?.cache_rules || [], ignoreGlobal: assignment?.cache_ignore_global_rules === true, ignoreId: "" });
const rules = window.rustProxyCacheRules?.create?.({ scope: "vhost", vhostId, rules: assignment?.cache_rules || [], ignoreGlobal: assignment?.cache_ignore_global_rules === true, ignoreId: "", autoLoadHits: false });
if (rules?.node) { rules.node.classList.add("docker-cache-rules"); details._rules = rules; }
details.append(grid); if (rules?.node) details.append(rules.node);
details.addEventListener("toggle", () => { if (details.open && !details.hidden) void rules?.refreshHits?.(); });
const sync = () => { details.hidden = !enabled.checked; }; enabled.addEventListener("change", sync); sync();
return details;
}