Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Fix attribute value filter not working in asset tree (#3078)

Martin Peeters26b1692c69ea
diff --git a/ui/app/manager/test/assettree.test.ts b/ui/app/manager/test/assettree.test.ts
index 030695d92..4b96a0aef 100644
--- a/ui/app/manager/test/assettree.test.ts
+++ b/ui/app/manager/test/assettree.test.ts
@@ -48,23 +48,6 @@ function createBuildingAssets(amount: number, realm = "smartcity"): Asset[] {
}));
}
-function createComplexTree(): Asset[] {
- const [cityAsset1, cityAsset2] = parentAssets;
- const buildingAssets = [cityAsset1, cityAsset2].flatMap((city) =>
- createBuildingAssets(3).map((building) => ({
- ...building,
- parentId: city.id,
- }))
- );
- const batteryAssets = buildingAssets.flatMap((building) =>
- createBatteryAssets(5).map((battery) => ({
- ...battery,
- parentId: building.id,
- }))
- );
- return [cityAsset1, cityAsset2, ...buildingAssets, ...batteryAssets];
-}
-
// Utility function to create parent assets, and apply assets as children
async function applyParentAssets(parentAssets: Asset[], manager: Manager) {
for (const p of parentAssets) {
@@ -458,6 +441,167 @@ test(`Searching for an asset and removing it keeps the tree and viewer in tact`,
await expect(assetTree.getAssetNodes()).toHaveCount(0); // Nothing is visible anymore, since there is nothing matching the "Battery 10" text filter.
});
+/**
+ * @given 5 assets are created in the "smartcity" realm
+ * @and the assets are visible in the tree (a total of 6)
+ * @when the user applies a combination of name, type and value in the filtering menu
+ * @then the asset tree should show the assets that comply with those requirements.
+ */
+test(`Should update asset list correctly when applying filters`, async ({ manager, assetsPage, assetTree }) => {
+ const asset1 = batteryAsset;
+ const asset2 = buildingAsset;
+ const asset3 = {
+ ...asset2,
+ name: "Building (new) 1",
+ attributes: {
+ ...asset2.attributes,
+ isNew: { name: "isNew", type: "boolean" },
+ amount: { name: "amount", type: "number", value: 80 },
+ status: { name: "status", type: "text", value: "Test value" },
+ },
+ };
+ const asset4 = {
+ ...asset2,
+ name: "Building (new) 2",
+ attributes: {
+ ...asset2.attributes,
+ isNew: { name: "isNew", type: "boolean", value: "true" },
+ },
+ };
+ const asset5 = {
+ ...asset2,
+ name: "Building (new) 3",
+ attributes: {
+ ...asset2.attributes,
+ amount: { name: "amount", type: "number", value: 70.5 },
+ },
+ };
+
+ const assets = [asset1, asset2, asset3, asset4, asset5];
+ await manager.setup("smartcity", { assets });
+ await manager.goToRealmStartPage("smartcity");
+ await assetsPage.goto();
+ await expect(assetTree.getAssetNodes()).toHaveCount(6); // 1 battery + 4 buildings + 1 console group
+
+ const filterButton = assetTree.getFilterButton();
+ const filterMenu = assetTree.getFilterMenu();
+ await expect(filterButton).toBeVisible();
+ await expect(filterButton).toHaveRole("button");
+ await expect(filterButton).not.toBeDisabled();
+
+ // Filter by the Building asset type, so only the 4 buildings are visible
+ await filterButton.click();
+ await expect(filterMenu).toBeVisible();
+ const assetTypeCombobox = filterMenu.getByRole("combobox", { name: "Asset type", exact: true });
+ await expect(assetTypeCombobox).toBeVisible();
+ await assetTypeCombobox.click();
+ await filterMenu.getByRole("option", { name: "Building asset" }).click();
+ await filterMenu.getByRole("button", { name: "Filter", exact: true }).click();
+ await expect(filterMenu).not.toBeVisible();
+ expect(await assetTree.getFilterInput().inputValue()).toBe("type:BuildingAsset");
+ await expect(assetTree.getAssetNodes()).toHaveCount(4); // 4 buildings that are left
+ await expect(assetTree.getAssetNodes()).toContainText([asset2.name, asset3.name, asset4.name, asset5.name]);
+
+ const filterCases = [
+ {
+ attribute: "Is New",
+ expectedInput: 'type:BuildingAsset attribute:"Is New"',
+ expectedNames: [asset3.name, asset4.name],
+ },
+ {
+ attribute: "Is New",
+ value: "true",
+ expectedInput: 'type:BuildingAsset "Is New":true',
+ expectedNames: [asset4.name],
+ },
+ {
+ attribute: "status",
+ value: "Test value",
+ expectedInput: 'type:BuildingAsset "status":"Test value"',
+ expectedNames: [asset3.name],
+ },
+ {
+ attribute: "amount",
+ value: "70,5",
+ expectedInput: 'type:BuildingAsset "amount":70,5',
+ expectedNames: [asset5.name],
+ },
+ {
+ attribute: "amount",
+ value: ">=60",
+ expectedInput: 'type:BuildingAsset "amount":>=60',
+ expectedNames: [asset3.name, asset5.name],
+ },
+ ];
+
+ for (const { attribute, value, expectedInput, expectedNames } of filterCases) {
+ await filterButton.click();
+ await expect(filterMenu).toBeVisible();
+ if (attribute) {
+ await filterMenu.getByRole("textbox", { name: "Attribute", exact: true }).fill(attribute);
+ }
+ if (value) {
+ await filterMenu.getByRole("textbox", { name: "Attribute value", exact: true }).fill(value);
+ }
+ await filterMenu.getByRole("button", { name: "Filter", exact: true }).click();
+ await expect(filterMenu).not.toBeVisible();
+ expect(await assetTree.getFilterInput().inputValue()).toBe(expectedInput);
+ await expect(assetTree.getAssetNodes()).toHaveCount(expectedNames.length);
+ await expect(assetTree.getAssetNodes()).toContainText(expectedNames);
+ }
+
+ // Clearing the filter, shows them all again
+ await filterButton.click();
+ await expect(filterMenu).toBeVisible();
+ await filterMenu.getByRole("button", { name: "Clear", exact: true }).click();
+ await filterMenu.getByRole("button", { name: "Filter", exact: true }).click();
+ await expect(filterMenu).not.toBeVisible();
+ await expect(assetTree.getFilterInput()).toBeEmpty();
+ await expect(assetTree.getAssetNodes()).toHaveCount(6); // 1 battery + 4 buildings + 1 console group
+});
+
+/**
+ * @given 2 battery assets are created in the "smartcity" realm
+ * @and the asset tree filter contains "Battery", showing 2 out of 3 nodes
+ * @when the filter text is removed while leaving a single space behind
+ * @then all assets should be shown again
+ * @and the tree should settle, without repeatedly re-fetching the assets
+ */
+test(`Should load the asset tree only once after clearing the filter to whitespace`, async ({
+ page,
+ manager,
+ assetsPage,
+ assetTree,
+}) => {
+ const batteryAssets = createBatteryAssets(2);
+ await manager.setup("smartcity", { assets: batteryAssets });
+ await manager.goToRealmStartPage("smartcity");
+ await assetsPage.goto();
+ await expect(assetTree.getAssetNodes()).toHaveCount(3); // 2 battery assets + 1 console group
+
+ await assetTree.getFilterInput().fill("Battery");
+ await expect(assetTree.getAssetNodes()).toHaveCount(2); // Both battery assets, but no console group
+
+ // Monitor requests to the /count endpoint
+ let countRequests = 0;
+ await page.route("**/asset/count", async (route) => {
+ countRequests++;
+ await route.continue();
+ });
+
+ // Clear the input, and leave a single space behind.
+ await assetTree.getFilterInput().fill(" ");
+ await expect(assetTree.getAssetNodes()).toHaveCount(3); // All assets are visible again
+ expect(countRequests, "Clearing the filter should reload the tree once").toBeLessThanOrEqual(2);
+
+ // The empty filter used to re-enter the clear branch on every load, so the tree kept re-fetching
+ const reloadedAgain = await page
+ .waitForRequest("**/asset/count", { timeout: 1000 })
+ .then(() => true)
+ .catch(() => false);
+ expect(reloadedAgain, "Asset tree kept reloading after the filter was cleared").toBe(false);
+});
+
/**
* @given 4 assets are created in the "master" realm
* @and 2 assets are created in the "smartcity" realm
diff --git a/ui/component/or-asset-tree/src/index.ts b/ui/component/or-asset-tree/src/index.ts
index 43f685982..102499012 100644
--- a/ui/component/or-asset-tree/src/index.ts
+++ b/ui/component/or-asset-tree/src/index.ts
@@ -22,8 +22,8 @@ import { customElement, property, query, state } from "lit/decorators.js";
import type { OrVaadinTextField } from "@openremote/or-vaadin-components/or-vaadin-text-field";
import type { OrVaadinButton } from "@openremote/or-vaadin-components/or-vaadin-button";
import {
- comboBoxRenderer,
type ComboBoxLitRenderer,
+ comboBoxRenderer,
type OrVaadinComboBox,
} from "@openremote/or-vaadin-components/or-vaadin-combo-box";
import { createMenuBarItem, type MenuBarItem } from "@openremote/or-vaadin-components/or-vaadin-menu-bar";
@@ -41,6 +41,7 @@ import {
AssetModelUtil,
type AssetQuery,
AssetQueryMatch,
+ AssetQueryOperator,
AssetQueryOrderBy$Property,
type AssetsEvent,
type AssetTreeEvent,
@@ -52,6 +53,7 @@ import {
LogicGroupOperator,
type SharedEvent,
type StringPredicate,
+ type ValuePredicateUnion,
WellknownAssets,
} from "@openremote/model";
import "@openremote/or-translate";
@@ -181,6 +183,13 @@ enum FilterElementType {
ATTRIBUTE_VALUE,
}
+const NUMBER_OPERATORS: Record<string, AssetQueryOperator> = {
+ ">": AssetQueryOperator.GREATER_THAN,
+ ">=": AssetQueryOperator.GREATER_EQUALS,
+ "<": AssetQueryOperator.LESS_THAN,
+ "<=": AssetQueryOperator.LESS_EQUALS,
+};
+
export type AddEventDetail = {
sourceAsset?: Asset;
asset: Asset;
@@ -431,7 +440,11 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
console.debug("Applying filter to the asset tree:", filter);
this._filter = filter;
if (reflect) {
- this.updateComplete.finally(() => (this._filterInput.value = this.formatFilter(filter)));
+ this.updateComplete.finally(() => {
+ if (this._filter === filter) {
+ this._filterInput.value = this.formatFilter(filter);
+ }
+ });
}
}
@@ -637,7 +650,7 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
this._assetTypes?.find((at: AssetDescriptor) => {
return at.name === this._assetTypeFilter;
}),
- this._assetTypes
+ this._assetTypes?.sort(Util.sortByString(Util.getAssetTypeLabel))
)
)}
<or-vaadin-text-field
@@ -658,8 +671,7 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
style="float: left"
@click=${() => {
// Wipe the current value and hide the clear button
- this._filterInput.toggleAttribute("value", false);
-
+ this._filterInput.clear();
this._attributeTypeFilter?.clear();
this._attributeValueFilter.clear();
this._attributeNameFilter.clear();
@@ -692,7 +704,7 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
)}
${when(
!this._nodes,
- () => html` <span id="loading"><or-translate value="loading"></or-translate></span> `,
+ () => html`<span id="loading"><or-translate value="loading"></or-translate></span> `,
() => html`
${when(
this._nodes!.length === 0 || !this.atLeastOneNodeToBeShown(),
@@ -1187,8 +1199,10 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
if (newFilter.attribute.length > 0 && newFilter.attributeValue.length > 0) {
newFilter.attributeValue.forEach((attributeValue: string, index: number) => {
+ // Values that contain spaces get additional "quotes", so the parser keeps them as a single value
+ const displayValue = attributeValue.includes(" ") ? `"${attributeValue}"` : attributeValue;
handledAttributeForValues.push(newFilter.attribute[index]);
- searchInput += prefix + '"' + newFilter.attribute[index] + '":' + attributeValue;
+ searchInput += prefix + `"${newFilter.attribute[index]}":` + displayValue;
prefix = " ";
});
}
@@ -1237,11 +1251,7 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
}
if (this._attributeNameFilter.value && this._attributeValueFilter.value) {
- let attributeValueValue: string = this._attributeValueFilter.value;
- if (attributeValueValue.includes(" ")) {
- attributeValueValue = '"' + attributeValueValue + '"';
- }
- filter.attributeValue = [attributeValueValue];
+ filter.attributeValue = [this._attributeValueFilter.value];
} else {
filter.attributeValue = [];
}
@@ -1270,15 +1280,19 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
this._doFiltering();
}
+ protected isFilterEmpty(): boolean {
+ return !(
+ this._filter.asset?.length ||
+ this._filter.attribute?.length ||
+ this._filter.assetType?.length ||
+ this._filter.attributeValue?.length
+ );
+ }
+
protected async _doFiltering() {
if (this.isConnected && this._nodes) {
// Clear filter if everything is not set anymore
- if (
- !this._filter.asset?.length &&
- !this._filter.attribute?.length &&
- !this._filter.assetType?.length &&
- !this._filter.attributeValue?.length
- ) {
+ if (this.isFilterEmpty()) {
console.debug("Clearing asset tree filter...");
OrAssetTree._forEachNodeRecursive(this._nodes!, (node) => {
node.notMatchingFilter = false;
@@ -1375,7 +1389,31 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
if (this._filter.attribute.length > 0) {
attributeCond = {
operator: LogicGroupOperator.AND,
- items: this._filter.attribute.map((attributeName: string) => {
+ items: this._filter.attribute.map((attributeName: string, index) => {
+ const value = this._filter?.attributeValue?.[index]?.trim();
+ let valuePredicate: ValuePredicateUnion | undefined;
+ if (value) {
+ const number = value.match(/^([<>]=?|==?)?\s*([-+]?(?:\d+(?:[.,]\d+)?|\.\d+))$/);
+ // String input is number and/or function (for example '>=50')
+ if (number) {
+ valuePredicate = {
+ predicateType: "number",
+ operator: NUMBER_OPERATORS[number[1]] ?? AssetQueryOperator.EQUALS,
+ value: Number(number[2].replace(",", ".")),
+ };
+ // String input is a boolean
+ } else if (value === "true" || value === "false") {
+ valuePredicate = { predicateType: "boolean", value: value === "true" };
+ // Otherwise, it's a string and do a contains check
+ } else {
+ valuePredicate = {
+ predicateType: "string",
+ match: AssetQueryMatch.CONTAINS,
+ value,
+ caseSensitive: false,
+ };
+ }
+ }
return {
name: {
predicateType: "string",
@@ -1383,6 +1421,7 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
value: Util.sentenceCaseToCamelCase(attributeName),
caseSensitive: false,
},
+ value: valuePredicate,
};
}),
};
@@ -1462,112 +1501,10 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
return {
assets: foundAssets,
- matcher: (asset) => {
- let attrValueCheck = true;
-
- if (
- this._filter.attribute.length > 0 &&
- this._filter.attributeValue.length > 0 &&
- foundAssetIds.includes(asset.id!)
- ) {
- const attributeVal: [string, string][] = [];
-
- this._filter.attributeValue.forEach((attrVal: string, index: number) => {
- if (attrVal.length > 0) {
- attributeVal.push([this._filter.attribute[index], attrVal]);
- }
- });
-
- const matchingAsset: Asset | undefined = foundAssets.find((a: Asset) => a.id === asset.id);
-
- if (matchingAsset && matchingAsset.attributes) {
- for (let attributeValIndex = 0; attributeValIndex < attributeVal.length; attributeValIndex++) {
- const currentAttributeVal = attributeVal[attributeValIndex];
-
- let atLeastOneAttributeMatchValue: boolean = false;
- Object.keys(matchingAsset.attributes).forEach((key: string) => {
- const attr: Attribute<any> = matchingAsset!.attributes![key];
-
- // attr.value check to avoid to compare with empty/non existing value
- if (attr.name!.toLowerCase() === currentAttributeVal[0].toLowerCase()) {
- switch (attr.type!) {
- case "number":
- case "integer":
- case "long":
- case "bigInteger":
- case "bigNumber":
- case "positiveInteger":
- case "negativeInteger":
- case "positiveNumber":
- case "negativeNumber": {
- let normalizedValue: string = currentAttributeVal[1]?.replace(",", ".");
- if (!isNaN(Number(normalizedValue))) {
- if ((attr.value ?? 0) === Number(normalizedValue)) {
- atLeastOneAttributeMatchValue = true;
- }
- } else if (/\d/.test(normalizedValue)) {
- if (normalizedValue.endsWith("%")) {
- normalizedValue = normalizedValue?.replace("%", "");
- }
- // If filter starts with a number, append '==' in front of it.
- if (/^[0-9]/.test(normalizedValue)) {
- normalizedValue = "==" + normalizedValue;
- }
- const func = attr.value + normalizedValue.replace(/[a-z]/gi, "");
-
- // Execute the function
- try {
- const resultNumberEval: boolean = eval(func);
- if (resultNumberEval) {
- atLeastOneAttributeMatchValue = true;
- }
- } catch (_ignored) {
- console.warn("Could not process filter on attribute number value;", func);
- }
- }
- break;
- }
- case "boolean": {
- const value: string = currentAttributeVal[1];
- if ((value === "false" || value === "true") && value === (attr.value ?? false).toString()) {
- atLeastOneAttributeMatchValue = true;
- }
- break;
- }
- case "text": {
- if (attr.value) {
- const unparsedValue: string = currentAttributeVal[1];
- const multicharString: string = "*";
-
- let parsedValue: string = unparsedValue.replace(multicharString, ".*");
- parsedValue = parsedValue.replace(/"/g, "");
-
- const valueFromAttribute: string = attr.value as string;
-
- if (valueFromAttribute.toLowerCase().indexOf(parsedValue.toLowerCase()) != -1) {
- atLeastOneAttributeMatchValue = true;
- }
- }
- break;
- }
- }
- }
- });
-
- attrValueCheck = atLeastOneAttributeMatchValue;
- }
- }
- }
-
- return foundAssetIds.includes(asset.id!) && attrValueCheck;
- },
+ matcher: (asset) => foundAssetIds.includes(asset.id!),
};
}
- protected isAnyFilter(): boolean {
- return this._filter.asset !== undefined || this._filter.assetType.length > 0 || this._filter.attribute.length > 0;
- }
-
protected filterTreeNode(
currentNode: UiAssetTreeNode,
matcher: (asset: Asset) => boolean,
@@ -1582,7 +1519,7 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
const childMatches: boolean = childOrDescendantMatches.some((m) => m);
nodeOrDescendantMatches = nodeOrDescendantMatches || childMatches;
- currentNode.expanded = childMatches && currentNode.children.length > 0 && this.isAnyFilter();
+ currentNode.expanded = childMatches && currentNode.children.length > 0 && !this.isFilterEmpty();
currentNode.hidden = !nodeOrDescendantMatches && !parentMatching;
return nodeOrDescendantMatches;
}
@@ -1655,7 +1592,7 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
},
{
actionName: "add",
- content: html` <or-vaadin-button id="add-btn" theme="primary" disabled>
+ content: html`<or-vaadin-button id="add-btn" theme="primary" disabled>
<or-translate value="add"></or-translate>
</or-vaadin-button>`,
action: () => {
@@ -1956,7 +1893,7 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
this.dataProvider(offset, this.queryLimit, parentId).then((assets) => {
this._loading = false;
this._buildTreeNodes(assets);
- if (this._filterInput?.value) {
+ if (!this.isFilterEmpty()) {
this._doFiltering();
}
});
@@ -2028,7 +1965,7 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
} else {
this._buildTreeNodes(newAssets);
}
- if (this._filterInput?.value) {
+ if (!this.isFilterEmpty()) {
this._doFiltering();
}
}) as Promise<AssetTreeEvent>;
@@ -2096,7 +2033,7 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
// In case of filter already active, do not override the actual state of assetTree
this._buildTreeNodes(assets);
- if (this._filterInput?.value) {
+ if (!this.isFilterEmpty()) {
this._doFiltering();
}
this.dispatchEvent(new OrAssetTreeAssetEvent(assetEvent));
@@ -2424,7 +2361,7 @@ export class OrAssetTree extends subscribe(manager)(OrElement) {
>
${
this.checkboxes
- ? html` <span class="mdc-list-item__graphic">
+ ? html`<span class="mdc-list-item__graphic">
${
treeNode.expandable
? html`<div class="mdc-checkbox">
diff --git a/ui/component/or-asset-tree/test/fixtures/index.ts b/ui/component/or-asset-tree/test/fixtures/index.ts
index 55d8814cf..ed70d52d6 100644
--- a/ui/component/or-asset-tree/test/fixtures/index.ts
+++ b/ui/component/or-asset-tree/test/fixtures/index.ts
@@ -28,6 +28,20 @@ export class AssetTree {
return this.page.locator("or-asset-tree #filterInput").getByRole("textbox");
}
+ /**
+ * Returns a locator of the filter button
+ */
+ getFilterButton() {
+ return this.page.locator("or-asset-tree #asset-tree-filter").getByTitle("Open filter");
+ }
+
+ /**
+ * Returns a locator of the filter menu
+ */
+ getFilterMenu() {
+ return this.page.locator("or-asset-tree #asset-tree-filter-setting");
+ }
+
/**
* Returns a locator of all nodes that contain assets
*/