feat(plugins): Enhance plugin counts handling with fail-open support and improved comments

This commit is contained in:
Jokob @NetAlertX
2026-03-27 10:59:22 +00:00
parent 77369c3ce8
commit b18cf98266

View File

@@ -274,8 +274,9 @@ function genericSaveData (id) {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
pluginDefinitions = [] pluginDefinitions = []
// Global counts map, populated before tabs are rendered // Global counts map, populated before tabs are rendered.
let pluginCounts = {}; // null = counts unavailable (fail-open: show all plugins)
let pluginCounts = null;
async function getData() { async function getData() {
try { try {
@@ -285,7 +286,8 @@ async function getData() {
const plugins = await fetchJson('plugins.json'); const plugins = await fetchJson('plugins.json');
pluginDefinitions = plugins.data; pluginDefinitions = plugins.data;
// Fetch counts BEFORE rendering tabs so we can skip empty plugins (no flicker) // Fetch counts BEFORE rendering tabs so we can skip empty plugins (no flicker).
// fetchPluginCounts never throws — returns null on failure (fail-open).
const prefixes = pluginDefinitions.filter(p => p.show_ui).map(p => p.unique_prefix); const prefixes = pluginDefinitions.filter(p => p.show_ui).map(p => p.unique_prefix);
pluginCounts = await fetchPluginCounts(prefixes); pluginCounts = await fetchPluginCounts(prefixes);
@@ -355,54 +357,63 @@ function postPluginGraphQL(gqlField, prefix, foreignKey, dtRequest, callback) {
}); });
} }
// Fetch counts for all plugins. Returns { PREFIX: { objects, events, history } }. // Fetch counts for all plugins. Returns { PREFIX: { objects, events, history } }
// or null on failure (fail-open so tabs still render).
// Fast path: static JSON (~1KB) when no MAC filter is active. // Fast path: static JSON (~1KB) when no MAC filter is active.
// Filtered path: batched GraphQL aliases when a foreignKey (MAC) is set. // Filtered path: batched GraphQL aliases when a foreignKey (MAC) is set.
async function fetchPluginCounts(prefixes) { async function fetchPluginCounts(prefixes) {
if (prefixes.length === 0) return {}; if (prefixes.length === 0) return {};
const mac = $("#txtMacFilter").val(); try {
const foreignKey = (mac && mac !== "--") ? mac : null; const mac = $("#txtMacFilter").val();
let counts = {}; const foreignKey = (mac && mac !== "--") ? mac : null;
let counts = {};
if (!foreignKey) { if (!foreignKey) {
// ---- FAST PATH: lightweight pre-computed JSON ---- // ---- FAST PATH: lightweight pre-computed JSON ----
const stats = await fetchJson('table_plugins_stats.json'); const stats = await fetchJson('table_plugins_stats.json');
for (const row of stats.data) { for (const row of stats.data) {
const p = row.tableName; // 'objects' | 'events' | 'history' const p = row.tableName; // 'objects' | 'events' | 'history'
const plugin = row.plugin; const plugin = row.plugin;
if (!counts[plugin]) counts[plugin] = { objects: 0, events: 0, history: 0 }; if (!counts[plugin]) counts[plugin] = { objects: 0, events: 0, history: 0 };
counts[plugin][p] = row.cnt; counts[plugin][p] = row.cnt;
} }
} else { } else {
// ---- FILTERED PATH: GraphQL with foreignKey ---- // ---- FILTERED PATH: GraphQL with foreignKey ----
const apiToken = getSetting("API_TOKEN"); const apiToken = getSetting("API_TOKEN");
const apiBase = getApiBase(); const apiBase = getApiBase();
const fkOpt = `, foreignKey: "${foreignKey}"`; const fkOpt = `, foreignKey: "${foreignKey}"`;
const fragments = prefixes.map(p => [ const fragments = prefixes.map(p => [
`${p}_obj: pluginsObjects(options: {plugin: "${p}", page: 1, limit: 1${fkOpt}}) { dbCount }`, `${p}_obj: pluginsObjects(options: {plugin: "${p}", page: 1, limit: 1${fkOpt}}) { dbCount }`,
`${p}_evt: pluginsEvents(options: {plugin: "${p}", page: 1, limit: 1${fkOpt}}) { dbCount }`, `${p}_evt: pluginsEvents(options: {plugin: "${p}", page: 1, limit: 1${fkOpt}}) { dbCount }`,
`${p}_hist: pluginsHistory(options: {plugin: "${p}", page: 1, limit: 1${fkOpt}}) { dbCount }`, `${p}_hist: pluginsHistory(options: {plugin: "${p}", page: 1, limit: 1${fkOpt}}) { dbCount }`,
].join('\n ')).join('\n '); ].join('\n ')).join('\n ');
const query = `query BadgeCounts {\n ${fragments}\n }`; const query = `query BadgeCounts {\n ${fragments}\n }`;
const response = await $.ajax({ const response = await $.ajax({
method: "POST", method: "POST",
url: `${apiBase}/graphql`, url: `${apiBase}/graphql`,
headers: { "Authorization": `Bearer ${apiToken}`, "Content-Type": "application/json" }, headers: { "Authorization": `Bearer ${apiToken}`, "Content-Type": "application/json" },
data: JSON.stringify({ query }), data: JSON.stringify({ query }),
}); });
if (response.errors) { console.error("[plugins] badge GQL errors:", response.errors); return counts; } if (response.errors) {
for (const p of prefixes) { console.error("[plugins] badge GQL errors:", response.errors);
counts[p] = { return null; // fail-open
objects: response.data[`${p}_obj`]?.dbCount ?? 0, }
events: response.data[`${p}_evt`]?.dbCount ?? 0, for (const p of prefixes) {
history: response.data[`${p}_hist`]?.dbCount ?? 0, counts[p] = {
}; objects: response.data[`${p}_obj`]?.dbCount ?? 0,
events: response.data[`${p}_evt`]?.dbCount ?? 0,
history: response.data[`${p}_hist`]?.dbCount ?? 0,
};
}
} }
return counts;
} catch (err) {
console.error('[plugins] fetchPluginCounts failed (fail-open):', err);
return null;
} }
return counts;
} }
// Apply pre-fetched counts to the DOM badges and hide empty tabs/sub-tabs. // Apply pre-fetched counts to the DOM badges and hide empty tabs/sub-tabs.
@@ -478,17 +489,20 @@ function generateTabs() {
let assignActive = true; let assignActive = true;
// Build list of visible plugins (skip plugins with 0 total count) // When counts are available, skip plugins with 0 total count (no flicker).
// When counts are null (fetch failed), show all show_ui plugins (fail-open).
const countsAvailable = pluginCounts !== null;
const visiblePlugins = pluginDefinitions.filter(pluginObj => { const visiblePlugins = pluginDefinitions.filter(pluginObj => {
if (!pluginObj.show_ui) return false; if (!pluginObj.show_ui) return false;
if (!countsAvailable) return true; // fail-open: show all
const c = pluginCounts[pluginObj.unique_prefix] || { objects: 0, events: 0, history: 0 }; const c = pluginCounts[pluginObj.unique_prefix] || { objects: 0, events: 0, history: 0 };
return (c.objects + c.events + c.history) > 0; return (c.objects + c.events + c.history) > 0;
}); });
// Create tab DOM for visible plugins only — no flicker // Create tab DOM for visible plugins only
visiblePlugins.forEach(pluginObj => { visiblePlugins.forEach(pluginObj => {
const prefix = pluginObj.unique_prefix; const prefix = pluginObj.unique_prefix;
const c = pluginCounts[prefix] || { objects: 0, events: 0, history: 0 }; const c = countsAvailable ? (pluginCounts[prefix] || { objects: 0, events: 0, history: 0 }) : null;
createTabContent(pluginObj, assignActive, c); createTabContent(pluginObj, assignActive, c);
createTabHeader(pluginObj, assignActive, c); createTabHeader(pluginObj, assignActive, c);
assignActive = false; assignActive = false;
@@ -519,9 +533,11 @@ function generateTabs() {
tabContainer: '#tabs-location' tabContainer: '#tabs-location'
}); });
// Apply badge counts to the DOM and hide empty inner sub-tabs // Apply badge counts to the DOM and hide empty inner sub-tabs (only if counts loaded)
const prefixes = visiblePlugins.map(p => p.unique_prefix); if (countsAvailable) {
applyPluginBadges(pluginCounts, prefixes); const prefixes = visiblePlugins.map(p => p.unique_prefix);
applyPluginBadges(pluginCounts, prefixes);
}
hideSpinner() hideSpinner()
} }
@@ -664,17 +680,27 @@ function initializeDataTables(prefix, colDefinitions, pluginObj) {
}); });
} }
// Initialize the Objects table immediately (it is the active/visible sub-tab). // Initialize the DataTable for whichever inner sub-tab is currently active
// Defer Events and History tables until their sub-tab is first shown. // (may not be Objects if autoHideEmptyTabs switched it).
// Defer the remaining sub-tabs until their shown.bs.tab fires.
const [objCfg, evtCfg, histCfg] = tableConfigs; const [objCfg, evtCfg, histCfg] = tableConfigs;
buildDT(objCfg.tableId, objCfg.gqlField, objCfg.countId, objCfg.badgeId); const allCfgs = [
{ cfg: objCfg, href: `#objectsTarget_${prefix}` },
{ cfg: evtCfg, href: `#eventsTarget_${prefix}` },
{ cfg: histCfg, href: `#historyTarget_${prefix}` },
];
$(`a[href="#eventsTarget_${prefix}"]`).one('shown.bs.tab', function() { allCfgs.forEach(({ cfg, href }) => {
buildDT(evtCfg.tableId, evtCfg.gqlField, evtCfg.countId, evtCfg.badgeId); const $subPane = $(href);
}); if ($subPane.hasClass('active') && $subPane.is(':visible')) {
// This sub-tab is the currently active one — initialize immediately
$(`a[href="#historyTarget_${prefix}"]`).one('shown.bs.tab', function() { buildDT(cfg.tableId, cfg.gqlField, cfg.countId, cfg.badgeId);
buildDT(histCfg.tableId, histCfg.gqlField, histCfg.countId, histCfg.badgeId); } else if ($subPane.closest('.tab-pane').length) {
// Defer until shown
$(`a[href="${href}"]`).one('shown.bs.tab', function() {
buildDT(cfg.tableId, cfg.gqlField, cfg.countId, cfg.badgeId);
});
}
}); });
} }