100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
UI bugfix for asset pagination in Rules UI not working anymore (#2875)
Martin Peetersad50b5df3f88
diff --git a/ui/app/manager/test/rules.test.ts b/ui/app/manager/test/rules.test.tsindex e1cf94532..0592dcff9 100644--- a/ui/app/manager/test/rules.test.ts+++ b/ui/app/manager/test/rules.test.ts@@ -150,7 +150,7 @@ test("Create a When-Then rule for an asset with a trigger and action", async ({p* @then The When-Then rule should appear in the rule list*/test("Create a When-Then rule by searching for an asset", async ({page, manager, shared}) => {- const multiplier = 100;+ const multiplier = 200; // Using a multiplier above 100, which is the default querying limitconst aLotOfAssets = generateALotOfAssets(multiplier);await manager.setup("smartcity", {assets: aLotOfAssets});await manager.goToRealmStartPage("smartcity");diff --git a/ui/component/or-rules/package.json b/ui/component/or-rules/package.jsonindex 4f7f25d79..ec46cdd9e 100644--- a/ui/component/or-rules/package.json+++ b/ui/component/or-rules/package.json@@ -39,6 +39,7 @@"iso-639-1": "^3.1.3","linqts": "^1.12.6","lit": "^3.3.1",+ "lodash.debounce": "^4.0.8","moment": "^2.29.4","resize-observer": "^1.0.3","shortid": "^2.2.15"diff --git a/ui/component/or-rules/src/json-viewer/or-rule-action-attribute.ts b/ui/component/or-rules/src/json-viewer/or-rule-action-attribute.tsindex 814cdfc4c..e60baba07 100644--- a/ui/component/or-rules/src/json-viewer/or-rule-action-attribute.ts+++ b/ui/component/or-rules/src/json-viewer/or-rule-action-attribute.ts@@ -3,11 +3,13 @@ import {customElement, property, state} from "lit/decorators.js";import {getAssetIdsFromQuery, getAssetTypeFromQuery, RulesConfig} from "../index";import {Asset,+ AssetModelUtil,+ AssetQuery,+ AssetQueryMatch,AssetTypeInfo,RuleActionUpdateAttribute,RuleActionWriteAttribute,- WellknownValueTypes,- AssetModelUtil, AssetQuery+ WellknownValueTypes} from "@openremote/model";import {Util} from "@openremote/core";import "@openremote/or-attribute-input";@@ -17,7 +19,12 @@ import {OrRulesJsonRuleChangedEvent} from "./or-rule-json-viewer";import {OrAttributeInputChangedEvent} from "@openremote/or-attribute-input";import {ifDefined} from "lit/directives/if-defined.js";import {when} from "lit/directives/when.js";-import {OrVaadinComboBox} from "@openremote/or-vaadin-components/or-vaadin-combo-box";+import {+ ComboBoxDataProviderCallback,+ ComboBoxDataProviderParams,+ OrVaadinComboBox+} from "@openremote/or-vaadin-components/or-vaadin-combo-box";+import debounce from "lodash.debounce";// language=CSSconst style = css`@@ -89,6 +96,19 @@ export class OrRuleActionAttribute extends translate(i18next)(LitElement) {return query ? getAssetTypeFromQuery(query) : undefined;}+ protected _assetDataProvider = debounce((params: ComboBoxDataProviderParams, callback: ComboBoxDataProviderCallback<{value: any, label: string}>) => {+ const query = this.action.target?.assets ?? this.action?.target?.matchedAssets;+ const assetType = getAssetTypeFromQuery(query);+ const ids = getAssetIdsFromQuery(query);+ const idValue = ids && ids.length > 0 ? ids[0] : "*";+ console.debug(`Searching for ${assetType} assets with filter '${params.filter}'...`);+ this.loadAssets(assetType!, params.filter, idValue).finally(() => {+ const assets = this._cache?.assets ?? [];+ const filtered = assets.filter(a => a.name?.toLowerCase().includes(params.filter.toLowerCase()));+ callback(filtered.map(a => ({value: a.id, label: a.name!})), filtered.length);+ })+ }, 500);+protected render() {if (!this.action.target) {@@ -103,18 +123,18 @@ export class OrRuleActionAttribute extends translate(i18next)(LitElement) {const query = this.action.target.assets ? this.action.target.assets : this.action.target.matchedAssets ? this.action.target.matchedAssets : undefined;const assetDescriptor = this.assetInfos ? this.assetInfos.find(assetTypeInfo => assetTypeInfo.assetDescriptor!.name === assetType) : undefined;+ const assetIds = getAssetIdsFromQuery(query);if (!assetDescriptor) {return html``;}if (!this._cache && !this._loading) {- this.loadAssets(assetType);+ this.loadAssets(assetType, undefined, assetIds?.[0]);}// TODO: Add multiselect support- const ids = getAssetIdsFromQuery(query);- const idValue = ids && ids.length > 0 ? ids[0] : "*";+ const idValue = assetIds?.[0] ?? "*";const idOptions: {value: any, label: string}[] = [{value: "*", label: i18next.t("matched")}];@@ -164,11 +184,19 @@ export class OrRuleActionAttribute extends translate(i18next)(LitElement) {}return html`- <or-vaadin-combo-box id="matchSelect" class="min-width"- .items=${idOptions} value=${idValue} ?readonly=${this.readonly}- @change=${(ev: Event) => this._assetId = (ev.currentTarget as OrVaadinComboBox).value}>- <or-translate slot="label" value="asset"></or-translate>- </or-vaadin-combo-box>+ ${when(idOptions.length >= 100, () => html`+ <or-vaadin-combo-box id="matchSelect" class="min-width" value=${idValue} ?readonly=${this.readonly}+ .filteredItems=${idOptions} .dataProvider=${this._assetDataProvider}+ @change=${(ev: Event) => this._assetId = (ev.currentTarget as OrVaadinComboBox).value}>+ <or-translate slot="label" value="asset"></or-translate>+ </or-vaadin-combo-box>+ `, () => html`+ <or-vaadin-combo-box id="matchSelect" class="min-width"+ .items=${idOptions} value=${idValue} ?readonly=${this.readonly}+ @change=${(ev: Event) => this._assetId = (ev.currentTarget as OrVaadinComboBox).value}>+ <or-translate slot="label" value="asset"></or-translate>+ </or-vaadin-combo-box>+ `)}${when(attributes.length > 0, () => html`<or-vaadin-combo-box id="attributeSelect" class="min-width" ?readonly=${this.readonly}.items=${attributes} value=${this.action.attributeName}@@ -242,7 +270,7 @@ export class OrRuleActionAttribute extends translate(i18next)(LitElement) {const query: AssetQuery = { limit: 100 };if (search) {query.names ??= [];- query.names.push({ predicateType: "string", value: search });+ query.names.push({ predicateType: "string", match: AssetQueryMatch.CONTAINS, value: search });}// If the cache contains assets from the same query, don't send HTTP request againconst isQueryCached = this._cache?.query && Util.objectsEqual(this._cache.query, query, true);diff --git a/ui/component/or-rules/src/json-viewer/or-rule-asset-query.ts b/ui/component/or-rules/src/json-viewer/or-rule-asset-query.tsindex 6e7c794a3..a3c985f48 100644--- a/ui/component/or-rules/src/json-viewer/or-rule-asset-query.ts+++ b/ui/component/or-rules/src/json-viewer/or-rule-asset-query.ts@@ -3,6 +3,8 @@ import {customElement, property, state} from "lit/decorators.js";import {Asset,AssetDescriptor,+ AssetModelUtil,+ AssetQuery,AssetQueryMatch,AssetQueryOperator as AQO,AssetTypeInfo,@@ -15,8 +17,7 @@ import {ValueDescriptor,ValuePredicateUnion,WellknownMetaItems,- WellknownValueTypes,- AssetModelUtil, AssetQuery+ WellknownValueTypes} from "@openremote/model";import {AssetQueryOperator, getAssetIdsFromQuery, getAssetTypeFromQuery, RulesConfig} from "../index";import "@openremote/or-attribute-input";@@ -26,12 +27,17 @@ import {buttonStyle} from "../style";import {OrRulesJsonRuleChangedEvent} from "./or-rule-json-viewer";import {OrAttributeInputChangedEvent} from "@openremote/or-attribute-input";import "./modals/or-rule-radial-modal";-import { ifDefined } from "lit/directives/if-defined.js";+import {ifDefined} from "lit/directives/if-defined.js";import {when} from "lit/directives/when.js";import moment from "moment";-import {OrVaadinComboBox} from "@openremote/or-vaadin-components/or-vaadin-combo-box";+import {+ ComboBoxDataProviderCallback,+ ComboBoxDataProviderParams,+ OrVaadinComboBox+} from "@openremote/or-vaadin-components/or-vaadin-combo-box";import {OrVaadinNumberField} from "@openremote/or-vaadin-components/or-vaadin-number-field";import {InputType} from "@openremote/or-vaadin-components/util";+import debounce from "lodash.debounce";// language=CSSconst style = css`@@ -415,9 +421,22 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {}}+ protected _assetDataProvider = debounce((params: ComboBoxDataProviderParams, callback: ComboBoxDataProviderCallback<{value: any, label: string}>) => {+ const assetType = getAssetTypeFromQuery(this.query);+ const ids = getAssetIdsFromQuery(this.query);+ const idValue = ids && ids.length > 0 ? ids[0] : "*";+ console.debug(`Searching for ${assetType} assets with filter '${params.filter}'...`);+ this.loadAssets(assetType!, params.filter, idValue).finally(() => {+ const assets = this._cache?.assets ?? [];+ const filtered = assets.filter(a => a.name?.toLowerCase().includes(params.filter.toLowerCase()));+ callback(filtered.map(a => ({value: a.id, label: a.name!})), filtered.length);+ })+ }, 500);+protected render() {const assetType = getAssetTypeFromQuery(this.query);+ const assetIds = getAssetIdsFromQuery(this.query);if (!assetType) {return html`<span class="invalidLabel">${i18next.t("errorOccurred")}</span>`;@@ -430,7 +449,7 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {}if (!this._cache && !this._loading) {- this.loadAssets(assetType);+ this.loadAssets(assetType, undefined, assetIds?.[0]);}if (!this.query.attributes) {@@ -444,8 +463,7 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {const showRemoveAttribute = !this.readonly && this.query.attributes && this.query.attributes.items && this.query.attributes.items.length > 1;// TODO: Add multiselect support- const ids = getAssetIdsFromQuery(this.query);- const idValue = ids && ids.length > 0 ? ids[0] : "*";+ const idValue = assetIds?.[0] ?? "*";const idOptions: {value: any, label: string}[] = [{value: "*", label: i18next.t("anyOfThisType")}];@@ -456,8 +474,6 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {// If >= 100 assets: only display if in line with search inputconst assets: Asset[] = this._cache ? this._cache.assets : [];- let searchProvider: (search?: string) => Promise<[any, string][]>;-return html`<div class="attribute-group">@@ -483,10 +499,18 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {};return html`- <or-vaadin-combo-box id="idSelect" class="min-width" ?readonly=${this.readonly} .items=${idOptions} value=${idValue}- @change=${(ev: Event) => this._assetId = (ev.currentTarget as OrVaadinComboBox).value}>- <or-translate slot="label" value="asset"></or-translate>- </or-vaadin-combo-box>+ ${when(idOptions.length >= 100, () => html`+ <or-vaadin-combo-box id="idSelect" class="min-width" ?readonly=${this.readonly} value=${idValue}+ .filteredItems=${idOptions} .dataProvider=${this._assetDataProvider}+ @change=${(ev: Event) => this._assetId = (ev.currentTarget as OrVaadinComboBox).value}>+ <or-translate slot="label" value="asset"></or-translate>+ </or-vaadin-combo-box>+ `, () => html`+ <or-vaadin-combo-box id="idSelect" class="min-width" ?readonly=${this.readonly} value=${idValue} .items=${idOptions}+ @change=${(ev: Event) => this._assetId = (ev.currentTarget as OrVaadinComboBox).value}>+ <or-translate slot="label" value="asset"></or-translate>+ </or-vaadin-combo-box>+ `)}<div class="attributes">${this.query.attributes && this.query.attributes.items ? this.query.attributes.items.map((attributePredicate, index) => {return html`@@ -955,7 +979,7 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {const query: AssetQuery = { limit: 100 };if (search) {query.names ??= [];- query.names.push({ predicateType: "string", value: search });+ query.names.push({ predicateType: "string", match: AssetQueryMatch.CONTAINS, value: search });}// If the cache contains assets from the same query, don't send HTTP request againconst isQueryCached = this._cache?.query && Util.objectsEqual(this._cache.query, query, true);diff --git a/ui/component/or-rules/src/json-viewer/or-rule-json-viewer.ts b/ui/component/or-rules/src/json-viewer/or-rule-json-viewer.tsindex 9cfe0b8db..05339f8dc 100644--- a/ui/component/or-rules/src/json-viewer/or-rule-json-viewer.ts+++ b/ui/component/or-rules/src/json-viewer/or-rule-json-viewer.ts@@ -219,7 +219,7 @@ export class OrRuleJsonViewer extends translate(i18next)(LitElement) implements// loadAssets function that also tracks what promises/fetches are active.// If so, await for those to finish to prevent multiple API requests.protected async loadAssets(type: string, query?: AssetQuery): Promise<Asset[] | undefined> {- if (this._activeAssetPromises.has(type)) {+ if (this._activeAssetPromises.has(type) && !query?.ids) {const data = await (this._activeAssetPromises.get(type)); // await for the already existing fetchreturn data.assets;} else {@@ -230,9 +230,9 @@ export class OrRuleJsonViewer extends translate(i18next)(LitElement) implementsname: this._ruleset.type == "realm" ? this._ruleset.realm : undefined};const promise = getAssetsByType(type, query);- this._activeAssetPromises.set(type, promise);+ if(!query.ids) this._activeAssetPromises.set(type, promise);const data = await promise;- this._activeAssetPromises.delete(type);+ if(!query.ids) this._activeAssetPromises.delete(type);return data.assets;}}diff --git a/ui/component/or-vaadin-components/src/or-vaadin-combo-box.ts b/ui/component/or-vaadin-components/src/or-vaadin-combo-box.tsindex be04eb7a9..e35241374 100644--- a/ui/component/or-vaadin-components/src/or-vaadin-combo-box.ts+++ b/ui/component/or-vaadin-components/src/or-vaadin-combo-box.ts@@ -28,6 +28,7 @@ import {PropertyValues, type LitElement} from "lit";* https://vaadin.com/docs/latest/components/combo-box/*/export {comboBoxRenderer, ComboBoxLitRenderer} from "@vaadin/combo-box/lit";+export {ComboBoxDataProviderCallback, ComboBoxDataProviderParams} from "@vaadin/combo-box";@customElement("or-vaadin-combo-box")export class OrVaadinComboBox extends (ComboBox as new () => ComboBox & LitElement) implements OrVaadinComponent {diff --git a/ui/component/or-vaadin-components/src/or-vaadin-multi-select-combo-box.ts b/ui/component/or-vaadin-components/src/or-vaadin-multi-select-combo-box.tsindex 1a906b413..32c4ac193 100644--- a/ui/component/or-vaadin-components/src/or-vaadin-multi-select-combo-box.ts+++ b/ui/component/or-vaadin-components/src/or-vaadin-multi-select-combo-box.ts@@ -20,7 +20,7 @@import { customElement } from "lit/decorators.js";import { MultiSelectComboBox } from "@vaadin/multi-select-combo-box";import { OrVaadinComponent } from "./util";-import { LitElement, PropertyValues } from "lit";+import { LitElement } from "lit";@customElement("or-vaadin-multi-select-combo-box")export class OrVaadinMultiSelectComboBox extends (MultiSelectComboBox as new () => MultiSelectComboBox & LitElement) implements OrVaadinComponent {diff --git a/yarn.lock b/yarn.lockindex d5a06c3d3..dbc9f5561 100644--- a/yarn.lock+++ b/yarn.lock@@ -2235,6 +2235,7 @@ __metadata:iso-639-1: "npm:^3.1.3"linqts: "npm:^1.12.6"lit: "npm:^3.3.1"+ lodash.debounce: "npm:^4.0.8"moment: "npm:^2.29.4"resize-observer: "npm:^1.0.3"shortid: "npm:^2.2.15"