Fix filter by tag

This commit is contained in:
Zaid Abdulla committed 2019-09-23 22:02:20 -07:00
1 parent ed0aa8ae7a
commit 559aa0fd36
4 files changed
+17 -21

No files matched your search

+13
View File
@@ -132,6 +132,11 @@ function queryMac(param, value): Expression {
];
}
function queryTag(tag: string): Expression {
const t = tag.replace(/[^a-zA-Z0-9-]+/g, "_");
return ["IS NOT NULL", ["PARAM", `Tags.${t}`]];
}
export function getTip(resource, label): string {
let tip;
if (resources[resource] && resources[resource][label]) {
@@ -156,6 +161,9 @@ export function getTip(resource, label): string {
case "mac":
tips.push("partial case insensitive MAC address");
break;
case "tag":
tips.push("case sensitive string");
break;
}
}
@@ -190,6 +198,11 @@ export function unpack(resource, label, value): Expression {
if (q) res.push(q);
}
if (type.includes("tag")) {
const q = queryTag(value);
if (q) res.push(q);
}
if (res.length <= 1) return null;
else if (res.length === 2) return res[1];
else return res;
+1 -17
View File
@@ -18,7 +18,7 @@
*/
import m from "mithril";
import { map, stringify } from "../lib/common/expression-parser";
import { stringify } from "../lib/common/expression-parser";
import { or, and, not, evaluate, subset } from "../lib/common/expression";
import memoize from "../lib/common/memoize";
import { QueryOptions, Expression } from "../lib/types";
@@ -299,22 +299,6 @@ function compareFunction(sort: {
}
function findMatches(resourceType, filter, sort, limit): any[] {
// Handle "tag =" and "tag <>" special cases
if (resourceType === "devices") {
filter = map(filter, e => {
if (
Array.isArray(e) &&
Array.isArray(e[1]) &&
e[1][0] === "PARAM" &&
e[1][1] === "tag"
) {
if (e[0] === "=") return ["IS NOT NULL", ["PARAM", `Tags.${e[2]}`]];
else if (e[0] === "<>") return ["IS NULL", ["PARAM", `Tags.${e[2]}`]];
}
return e;
});
}
let value = [];
for (const obj of resources[resourceType].objects.values())
if (evaluate(filter, obj, fulfillTimestamp)) value.push(obj);