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
Feature/realm insights (#511)
vlichtenberg74a11a3fec50
diff --git a/ui/app/manager/src/pages/page-insights.ts b/ui/app/manager/src/pages/page-insights.tsindex 3e4599e66..eaa682697 100644--- a/ui/app/manager/src/pages/page-insights.ts+++ b/ui/app/manager/src/pages/page-insights.ts@@ -1,11 +1,14 @@import {css, html, TemplateResult} from "lit";-import {customElement, property} from "lit/decorators.js";+import {customElement, property, query} from "lit/decorators.js";import "@openremote/or-data-viewer";-import {DataViewerConfig} from "@openremote/or-data-viewer";+import {DataViewerConfig, OrDataViewer} from "@openremote/or-data-viewer";import {Page, PageProvider} from "@openremote/or-app";import {AppStateKeyed} from "@openremote/or-app";import {EnhancedStore} from "@reduxjs/toolkit";import i18next from "i18next";+import {createSelector} from "reselect";+import { manager } from "@openremote/core";+import {OrAssetTree} from "@openremote/or-asset-tree";export interface PageInsightsConfig {dataViewer?: DataViewerConfig@@ -86,25 +89,44 @@ class PageInsights<S extends AppStateKeyed> extends Page<S> {@property()protected _assetId;+ @query("#data-viewer")+ protected _dataviewer!: OrDataViewer;++ protected _realmSelector = (state: S) => state.app.realm || manager.displayRealm;+get name(): string {return "insights";}+ protected getRealmState = createSelector(+ [this._realmSelector],+ async () => {+ if (this._dataviewer) this._dataviewer.refresh();+ }+ )+constructor(store: EnhancedStore<S>) {super(store);}+ public connectedCallback() {+ super.connectedCallback();+ // this.realm = this.getState().app.realm;+ }+protected render(): TemplateResult | void {return html`<div id="wrapper"><div id="title"><or-icon icon="chart-areaspline"></or-icon>${i18next.t("insights")}</div>- <or-data-viewer .config="${this.config?.dataViewer}"></or-data-viewer>+ <or-data-viewer id="data-viewer" .config="${this.config?.dataViewer}"></or-data-viewer></div>`;}- public stateChanged(state: S) {+ stateChanged(state: S) {+ // State is only utilised for initial loading+ this.getRealmState(state); // Order is important here!}}diff --git a/ui/app/manager/src/pages/page-roles.ts b/ui/app/manager/src/pages/page-roles.tsindex 50eb10cf4..b89a79c59 100644--- a/ui/app/manager/src/pages/page-roles.ts+++ b/ui/app/manager/src/pages/page-roles.ts@@ -247,6 +247,10 @@ class PageRoles<S extends AppStateKeyed> extends Page<S> {this.realm = this.getState().app.realm;}+ protected getState(): S {+ return this._store.getState();+ }+protected async getRoles() {const roleResponse = await manager.rest.api.UserResource.getRoles(this.realm);this._compositeRoles = [...roleResponse.data.filter(role => role.composite)];diff --git a/ui/component/or-chart/src/index.ts b/ui/component/or-chart/src/index.tsindex f9b52fe5f..795ddbf85 100644--- a/ui/component/or-chart/src/index.ts+++ b/ui/component/or-chart/src/index.ts@@ -84,6 +84,7 @@ export interface ChartConfig {export interface OrChartConfig {chart?: ChartConfig;+ realm?: string;views: {[name: string]: {[panelName: string]: ChartViewConfig}};@@ -354,6 +355,9 @@ export class OrChart extends translate(i18next)(LitElement) {@property({type: Object})public config?: OrChartConfig;+ @property({type: String})+ public realm?: string;+@property()public panelName?: string;@@ -398,10 +402,16 @@ export class OrChart extends translate(i18next)(LitElement) {this.loadSettings();}- updated(changedProperties: PropertyValues) {+ async updated(changedProperties: PropertyValues) {super.updated(changedProperties);- const reloadData = changedProperties.has("period") || changedProperties.has("compareTimestamp") || changedProperties.has("timestamp") || changedProperties.has("assetAttributes");+ if (changedProperties.has("realm")) {+ this.assets = [];+ await this.loadSettings();+ }++ const reloadData = changedProperties.has("period") || changedProperties.has("compareTimestamp")+ || changedProperties.has("timestamp") || changedProperties.has("assetAttributes") || changedProperties.has("realm");if (reloadData) {this._data = undefined;@@ -522,7 +532,7 @@ export class OrChart extends translate(i18next)(LitElement) {const disabled = this._loading;const endDateInputType = this.getInputType();return html`- <div id="container">+ <div id="container"><div id="chart-container"><canvas id="chart"></canvas></div>@@ -674,6 +684,10 @@ export class OrChart extends translate(i18next)(LitElement) {this.timestamp = moment().set('minute', 0).toDate();this.compareTimestamp = undefined;+ if (!this.realm) {+ this.realm = manager.getRealm();+ }+const configStr = await manager.console.retrieveData("OrChartConfig");if (!configStr || !this.panelName) {@@ -681,16 +695,21 @@ export class OrChart extends translate(i18next)(LitElement) {}const viewSelector = window.location.hash;+ let allConfigs: OrChartConfig[] = [];let config: OrChartConfig;-+try {- config = JSON.parse(configStr) as OrChartConfig;+ allConfigs = JSON.parse(configStr);+ if (!Array.isArray(allConfigs)) {+ manager.console.storeData("OrChartConfig", JSON.stringify([allConfigs]));+ }+ config = allConfigs.find(e => e.realm === this.realm) as OrChartConfig;} catch (e) {console.error("Failed to load chart config", e);manager.console.storeData("OrChartConfig", null);return;}-+const view = config && config.views ? config.views[viewSelector][this.panelName] : undefined;if (!view) {@@ -700,7 +719,8 @@ export class OrChart extends translate(i18next)(LitElement) {if (!view.attributeRefs) {// Old/invalid config format remove itdelete config.views[viewSelector][this.panelName];- manager.console.storeData("OrChartConfig", JSON.stringify(config));+ const cleanData = [...allConfigs.filter(e => e.realm !== this.realm), config];+ manager.console.storeData("OrChartConfig", JSON.stringify(cleanData));return;}@@ -725,7 +745,9 @@ export class OrChart extends translate(i18next)(LitElement) {const response = await manager.rest.api.AssetResource.queryAssets(query);const assets = response.data || [];view.attributeRefs = view.attributeRefs.filter((attrRef) => !!assets.find((asset) => asset.id === attrRef.id && asset.attributes && asset.attributes.hasOwnProperty(attrRef.name!)));- manager.console.storeData("OrChartConfig", JSON.stringify(config));++ allConfigs = [...allConfigs.filter(e => e.realm !== this.realm), config];+ manager.console.storeData("OrChartConfig", JSON.stringify(allConfigs));this.assets = assets.filter((asset) => view.attributeRefs!.find((attrRef) => attrRef.id === asset.id));} catch (e) {console.error("Failed to get assets requested in settings", e);@@ -754,11 +776,13 @@ export class OrChart extends translate(i18next)(LitElement) {const viewSelector = window.location.hash;const configStr = await manager.console.retrieveData("OrChartConfig");+ let allConfigs: OrChartConfig[] = [];let config: OrChartConfig | undefined;-+if (configStr) {try {- config = JSON.parse(configStr) as OrChartConfig;+ allConfigs = JSON.parse(configStr);+ config = allConfigs.find(e => e.realm === this.realm);} catch (e) {console.error("Failed to load chart config", e);}@@ -766,16 +790,17 @@ export class OrChart extends translate(i18next)(LitElement) {if (!config) {config = {+ realm: this.realm,views: {- [viewSelector]: {- }+ [viewSelector]: {}}- }+ }}-+if (!this.assets || !this.assetAttributes || this.assets.length === 0 || this.assetAttributes.length === 0) {delete config.views[viewSelector][this.panelName];} else {+ config.realm = this.realm;config.views[viewSelector][this.panelName] = {attributeRefs: this.assetAttributes.map(([index, attr]) => {const asset = this.assets[index];@@ -786,7 +811,8 @@ export class OrChart extends translate(i18next)(LitElement) {};}- manager.console.storeData("OrChartConfig", JSON.stringify(config));+ allConfigs = [...allConfigs.filter(e => e.realm !== this.realm), config];+ manager.console.storeData("OrChartConfig", JSON.stringify(allConfigs));}_openDialog() {diff --git a/ui/component/or-data-viewer/src/index.ts b/ui/component/or-data-viewer/src/index.tsindex 350653ab8..da45339ea 100644--- a/ui/component/or-data-viewer/src/index.ts+++ b/ui/component/or-data-viewer/src/index.ts@@ -11,6 +11,7 @@ import i18next from "i18next";import {styleMap} from "lit/directives/style-map";import {classMap} from "lit/directives/class-map";import "@openremote/or-attribute-card";+import manager from "@openremote/core";export type PanelType = "chart" | "kpi";@@ -155,6 +156,7 @@ export class OrDataViewer extends translate(i18next)(LitElement) {}}+ @property()public config?: DataViewerConfig;@property({type: Array, attribute: false})@@ -163,6 +165,9 @@ export class OrDataViewer extends translate(i18next)(LitElement) {@property()protected _loading: boolean = false;+ @property()+ public realm?: string;+protected _resizeHandler = () => {OrDataViewer.generateGrid(this.shadowRoot)};@@ -186,7 +191,12 @@ export class OrDataViewer extends translate(i18next)(LitElement) {await this.updateComplete;}+ public refresh() {+ this.realm = window.sessionStorage.getItem('realm') || manager.getRealm();+ }+public getPanel(name: string, panelConfig: PanelConfig) {+const content = this.getPanelContent(name, panelConfig);if (!content) {@@ -215,10 +225,12 @@ export class OrDataViewer extends translate(i18next)(LitElement) {return;}+ this.realm = window.sessionStorage.getItem('realm') || manager.getRealm();+let content: TemplateResult | undefined;if (panelConfig && panelConfig.type === "chart") {- content = html`<or-chart id="chart" panelName="${panelName}" .config="${this.config.chartConfig}"></or-chart>`;+ content = html`<or-chart id="chart" panelName="${panelName}" .config="${this.config.chartConfig}" .realm="${this.realm}"></or-chart>`;}if (panelConfig && panelConfig.type === "kpi") {@@ -253,6 +265,10 @@ export class OrDataViewer extends translate(i18next)(LitElement) {protected updated(_changedProperties: PropertyValues) {super.updated(_changedProperties);+ if (_changedProperties.has("realm")) {+ this.refresh();+ }+this.onCompleted().then(() => {onRenderComplete.startCallbacks().then(() => {OrDataViewer.generateGrid(this.shadowRoot);