Skip to content

Commit 9cdb662

Browse files
authored
[BUGFIX] gateway filter was not applied (#289)
This fixes the gateway filter not being applied correctly in the statistics tab. The application of the gateway filter did not know about the nodeData and could not add it when calling the filter. Instead, we now define the nodeData dependent filter for selectedGatewayIP at runtime.
2 parents c1c3687 + 8bfd496 commit 9cdb662

File tree

1 file changed

+27
-35
lines changed

1 file changed

+27
-35
lines changed

lib/proportions.ts

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ type Modifier = (value: any, ctx?: ObjectsLinksAndNodes) => string;
1717

1818
type MappingEntry = {
1919
keys: string[];
20-
modifier?: Modifier;
20+
nodeValueModifier?: Modifier;
2121
};
2222

2323
const statusFieldMapping: Record<string, MappingEntry> = {
2424
"node.status": {
2525
keys: ["is_online"],
26-
modifier: function (d: any) {
26+
nodeValueModifier: function (d: any) {
2727
return d ? "online" : "offline";
2828
},
2929
},
@@ -35,7 +35,7 @@ const statusFieldMapping: Record<string, MappingEntry> = {
3535
},
3636
"node.deprecationStatus": {
3737
keys: ["model"],
38-
modifier: function (d: any) {
38+
nodeValueModifier: function (d: any) {
3939
if (window.config.deprecated && d && window.config.deprecated.includes(d)) return _.t("deprecation");
4040
if (window.config.eol && d && window.config.eol.includes(d)) return _.t("eol");
4141
return _.t("no");
@@ -46,13 +46,13 @@ const statusFieldMapping: Record<string, MappingEntry> = {
4646
},
4747
"node.visible": {
4848
keys: ["location"],
49-
modifier: function (d: any) {
49+
nodeValueModifier: function (d: any) {
5050
return d && d.longitude && d.latitude ? _.t("yes") : _.t("no");
5151
},
5252
},
5353
"node.update": {
5454
keys: ["autoupdater"],
55-
modifier: function (d: any) {
55+
nodeValueModifier: function (d: any) {
5656
if (d.enabled) {
5757
return d.branch;
5858
}
@@ -61,27 +61,13 @@ const statusFieldMapping: Record<string, MappingEntry> = {
6161
},
6262
"node.selectedGatewayIPv4": {
6363
keys: ["gateway"],
64-
modifier: function (nodeid: string | null, data: ObjectsLinksAndNodes) {
65-
let gateway = data.nodeDict[nodeid];
66-
if (gateway) {
67-
return gateway.hostname;
68-
}
69-
return null;
70-
},
7164
},
7265
"node.selectedGatewayIPv6": {
7366
keys: ["gateway6"],
74-
modifier: function (nodeid: string | null, data: ObjectsLinksAndNodes) {
75-
let gateway = data.nodeDict[nodeid];
76-
if (gateway) {
77-
return gateway.hostname;
78-
}
79-
return null;
80-
},
8167
},
8268
"node.domain": {
8369
keys: ["domain"],
84-
modifier: function (d: any) {
70+
nodeValueModifier: function (d: any) {
8571
if (window.config.domainNames) {
8672
window.config.domainNames.some(function (t) {
8773
if (d === t.domain) {
@@ -116,15 +102,15 @@ export const Proportions = function (filterManager: ReturnType<typeof DataDistri
116102
return String(s).replace(/\s+/g, " ").trim();
117103
}
118104

119-
function count(nodes: Node[], keys: string[], f?: (k: any, ctx?: any) => any, ctx?: any) {
105+
function count(nodes: Node[], keys: string[], nodeValueModifier?: (k: any, ctx?: any) => any, ctx?: any) {
120106
const counts = new Map<any, number>();
121107

122108
nodes.forEach(function (node) {
123109
// pass shallow copy of keys to dictGet
124110
let dictKey = helper.dictGet(node, keys.slice(0));
125111

126-
if (f !== undefined) {
127-
dictKey = f(dictKey, ctx);
112+
if (nodeValueModifier !== undefined) {
113+
dictKey = nodeValueModifier(dictKey, ctx);
128114
}
129115

130116
if (dictKey === null) return;
@@ -134,7 +120,7 @@ export const Proportions = function (filterManager: ReturnType<typeof DataDistri
134120

135121
const result: any[] = [];
136122
counts.forEach(function (countValue, k) {
137-
result.push([k, countValue, keys, f]);
123+
result.push([k, countValue, keys, nodeValueModifier]);
138124
});
139125

140126
return result;
@@ -230,21 +216,29 @@ export const Proportions = function (filterManager: ReturnType<typeof DataDistri
230216
let nodes = data.nodes.all;
231217
time = data.timestamp;
232218

233-
// helper to fetch mapping entries from statusFieldMapping
234-
function mapping(name: string) {
235-
return (statusFieldMapping as any)[name] || { keys: [], modifier: undefined };
219+
function gatewayNameFromNodeId(nodeid: string | null) {
220+
let gateway = data.nodeDict[nodeid];
221+
if (gateway) {
222+
return gateway.hostname;
223+
}
224+
return null;
236225
}
237226

227+
// set nodeValueModifier for selectedGatewayIP filter later
228+
// so that access to data.nodeDict can be encapsulated
229+
statusFieldMapping["node.selectedGatewayIPv4"].nodeValueModifier = gatewayNameFromNodeId;
230+
statusFieldMapping["node.selectedGatewayIPv6"].nodeValueModifier = gatewayNameFromNodeId;
231+
238232
function sortVersionCountAndName(a, b) {
239233
// descending by count
240234
if (b[1] !== a[1]) {
241235
return b[1] - a[1];
242236
}
243237
return compare(a[0], b[0]);
244238
}
245-
function processMapping(name: string, sorter?: (a: any, b: any) => number, ctx?: any) {
246-
const m = mapping(name);
247-
const arr = count(nodes, m.keys, m.modifier, ctx);
239+
function processMapping(name: string, sorter?: (a: any, b: any) => number) {
240+
const m = statusFieldMapping[name];
241+
const arr = count(nodes, m.keys, m.nodeValueModifier, data);
248242
const sorted = sorter
249243
? arr.sort(sorter)
250244
: arr.sort(function (a, b) {
@@ -261,12 +255,10 @@ export const Proportions = function (filterManager: ReturnType<typeof DataDistri
261255
processMapping("node.hardware");
262256
processMapping("node.visible");
263257
processMapping("node.update");
264-
processMapping("node.selectedGatewayIPv4", undefined, data);
265-
processMapping("node.selectedGatewayIPv6", undefined, data);
258+
processMapping("node.selectedGatewayIPv4");
259+
processMapping("node.selectedGatewayIPv6");
266260
processMapping("node.domain");
267261

268-
// tables filled above via processMapping
269-
270262
if (!appliedUrlFilters) {
271263
applyFiltersFromHash();
272264
}
@@ -292,7 +284,7 @@ export const Proportions = function (filterManager: ReturnType<typeof DataDistri
292284
encodedValue = encodedValue.slice(1);
293285
}
294286

295-
let filter = GenericNodeFilter(param, mapping.keys, normalizeKey(encodedValue), mapping.modifier);
287+
let filter = GenericNodeFilter(param, mapping.keys, normalizeKey(encodedValue), mapping.nodeValueModifier);
296288
if (negate) {
297289
filter.setNegate(true);
298290
}

0 commit comments

Comments
 (0)