From 2d784c83ae865ddea03bc1c2376877791bf5ace0 Mon Sep 17 00:00:00 2001 From: Maksym Tsymbarov Date: Thu, 3 Dec 2026 13:59:37 +0100 Subject: [PATCH] Fixed aggregated values in the Entities table and trips on the Route map disappearing when the entities matched by an alias change (#16716) * Added prevention for aggregated data loss and trip marker flicker * Minor improvements * Fixed pr comments * License headers fix * Refetched timeseries history when dynamic entity list changes * Fixed dynamic alias page changes resetting aggregated table columns and rebuilding map trips * Skipped entity query when refetching timeseries after dynamic entity list change --------- Co-authored-by: Vladyslav_Prykhodko --- .../app/core/api/entity-data-subscription.ts | 189 +++++++++++++----- 1 file changed, 143 insertions(+), 46 deletions(-) diff --git a/ui-ngx/src/app/core/api/entity-data-subscription.ts b/ui-ngx/src/app/core/api/entity-data-subscription.ts index 4e959089a0..d055cd14b0 100644 --- a/ui-ngx/src/app/core/api/entity-data-subscription.ts +++ b/ui-ngx/src/app/core/api/entity-data-subscription.ts @@ -47,7 +47,7 @@ import { } from '@shared/models/telemetry/telemetry.models'; import { UtilsService } from '@core/services/utils.service'; import { EntityDataListener, EntityDataLoadResult } from '@core/api/entity-data.service'; -import { deepClone, isDefined, isDefinedAndNotNull, isNumeric, isObject, objectHashCode } from '@core/utils'; +import { deepClone, isDefined, isDefinedAndNotNull, isEmpty, isNumeric, isObject, objectHashCode } from '@core/utils'; import { PageData } from '@shared/models/page/page-data'; import { DataAggregator, onAggregatedData } from '@core/api/data-aggregator'; import { NULL_UUID } from '@shared/models/id/has-uuid'; @@ -142,6 +142,9 @@ export class EntityDataSubscription { private latestTsOffset: number; private dataAggregators: Array; private tsLatestDataAggregators: Array; + private pendingTsHistory = new Set(); + private pendingAggTsHistory = new Set(); + private reconnectPageExpected = false; private dataKeys: {[key: string]: Array | SubscriptionDataKey} = {}; private dataKeysList: SubscriptionDataKey[] = []; private datasourceData: {[index: number]: {[key: string]: DataSetHolder}}; @@ -406,35 +409,10 @@ export class EntityDataSubscription { this.subscriber.reconnect$.subscribe(() => { if (this.started) { const targetCommand = this.entityDataSubscriptionOptions.isPaginatedDataSubscription ? this.dataCommand : this.subsCommand; - if (!this.history && (this.entityDataSubscriptionOptions.type === widgetType.timeseries && this.tsFields.length || - this.aggTsValues.length > 0 && !this.isFloatingTimewindow)) { - const newSubsTw = this.listener.updateRealtimeSubscription(); - this.subsTw = newSubsTw; - if (this.entityDataSubscriptionOptions.type === widgetType.timeseries && this.tsFields.length) { - targetCommand.tsCmd.startTs = this.subsTw.startTs; - targetCommand.tsCmd.timeWindow = this.subsTw.aggregation.timeWindow; - if (typeof this.subsTw.aggregation.interval === 'number') { - targetCommand.tsCmd.interval = this.subsTw.aggregation.interval; - targetCommand.tsCmd.intervalType = IntervalType.MILLISECONDS; - } else { - targetCommand.tsCmd.intervalType = this.subsTw.aggregation.interval; - } - targetCommand.tsCmd.timeZoneId = this.subsTw.timezone; - targetCommand.tsCmd.limit = this.subsTw.aggregation.limit; - targetCommand.tsCmd.agg = this.subsTw.aggregation.type; - targetCommand.tsCmd.fetchLatestPreviousPoint = this.subsTw.aggregation.stateData; - this.dataAggregators.forEach((dataAggregator) => { - dataAggregator.reset(newSubsTw); - }); - } - if (this.aggTsValues.length > 0 && !this.isFloatingTimewindow) { - targetCommand.aggTsCmd.startTs = this.subsTw.startTs; - targetCommand.aggTsCmd.timeWindow = this.subsTw.aggregation.timeWindow; - this.tsLatestDataAggregators.forEach((dataAggregator) => { - dataAggregator.reset(newSubsTw); - }); - } + if (this.entityDataSubscriptionOptions.isPaginatedDataSubscription) { + this.reconnectPageExpected = true; } + this.updateRealtimeCommand(targetCommand, true); if (this.entityDataSubscriptionOptions.type === widgetType.timeseries) { this.subscriber.setTsOffset(this.subsTw.tsOffset); } else { @@ -913,6 +891,13 @@ export class EntityDataSubscription { return; } } + const refetchTimeseries = this.isTimeseriesRefetchRequired(isInitialData, pageData); + this.reconnectPageExpected = false; + const prevPageData = this.pageData; + const prevDatasourceData = this.datasourceData; + if (refetchTimeseries) { + this.updateRealtimeCommand(this.dataCommand, false); + } this.pageData = pageData; if (this.entityDataSubscriptionOptions.isPaginatedDataSubscription) { @@ -920,6 +905,7 @@ export class EntityDataSubscription { } else if (isInitialData) { this.resetData(); } + this.markPendingHistory(refetchTimeseries); for (let dataIndex = 0; dataIndex < pageData.data.length; dataIndex++) { const entityData = pageData.data[dataIndex]; this.processEntityData(entityData, dataIndex, false, @@ -931,6 +917,111 @@ export class EntityDataSubscription { } ); } + if (refetchTimeseries) { + this.restorePreviousData(prevPageData, prevDatasourceData); + } + this.onPageDataProcessed(pageData, isInitialData); + if (refetchTimeseries) { + const refetchCommand = Object.assign(new EntityDataCmd(), this.dataCommand, {query: null}); + this.subscriber.subscriptionCommands = [refetchCommand]; + this.subscriber.update(); + } + } + + private isTimeseriesRefetchRequired(isInitialData: boolean, pageData: PageData): boolean { + if (isInitialData || this.reconnectPageExpected || !this.entityDataSubscriptionOptions.isPaginatedDataSubscription) { + return false; + } + const hasTimeseries = this.entityDataSubscriptionOptions.type === widgetType.timeseries && this.tsFields.length > 0 || + this.aggTsValues.length > 0 && !this.isFloatingTimewindow || + this.history && this.aggTsComparisonValues.length > 0; + return hasTimeseries && entityPageDataChanged(this.pageData, pageData) && + pageData.data.every(entityData => isEmpty(entityData.timeseries) && isEmpty(entityData.aggLatest)); + } + + private markPendingHistory(refetchTimeseries: boolean) { + this.pendingTsHistory.clear(); + this.pendingAggTsHistory.clear(); + if (refetchTimeseries) { + for (let dataIndex = 0; dataIndex < this.pageData.data.length; dataIndex++) { + if (this.dataAggregators?.[dataIndex]) { + this.pendingTsHistory.add(dataIndex); + } + if (this.tsLatestDataAggregators?.[dataIndex]) { + this.pendingAggTsHistory.add(dataIndex); + } + } + } + } + + private isRefetchedTsHistory(entityData: EntityData, isUpdate: boolean): boolean { + return isUpdate && isDefinedAndNotNull(entityData.latest) && !isEmpty(entityData.timeseries); + } + + private restorePreviousData(prevPageData: PageData, + prevDatasourceData: {[index: number]: {[key: string]: DataSetHolder}}) { + const prevDataIndexByEntityId = new Map(); + prevPageData.data.forEach((entityData, index) => prevDataIndexByEntityId.set(entityData.entityId.id, index)); + this.pageData.data.forEach((entityData, dataIndex) => { + const prevEntityData = prevDatasourceData[prevDataIndexByEntityId.get(entityData.entityId.id)]; + if (!prevEntityData) { + return; + } + if (!this.data[dataIndex]) { + this.data[dataIndex] = []; + } + for (const key of Object.keys(this.dataKeys)) { + const dataKey = this.dataKeys[key]; + const keyEntries: Array<[string, SubscriptionDataKey]> = Array.isArray(dataKey) ? + dataKey.map((subscriptionDataKey, index) => [`${key}_${index}`, subscriptionDataKey]) : [[key, dataKey]]; + for (const [datasourceKey, subscriptionDataKey] of keyEntries) { + const prevHolder = prevEntityData[datasourceKey]; + if (prevHolder?.data?.length && !this.data[dataIndex][subscriptionDataKey.index]?.data?.length) { + this.data[dataIndex][subscriptionDataKey.index] = prevHolder; + } + } + } + }); + } + + private updateRealtimeCommand(targetCommand: EntityDataCmd, resetAggregators: boolean) { + if (this.history || !(this.entityDataSubscriptionOptions.type === widgetType.timeseries && this.tsFields.length || + this.aggTsValues.length > 0 && !this.isFloatingTimewindow)) { + return; + } + const newSubsTw = this.listener.updateRealtimeSubscription(); + this.subsTw = newSubsTw; + if (this.entityDataSubscriptionOptions.type === widgetType.timeseries && this.tsFields.length) { + targetCommand.tsCmd.startTs = this.subsTw.startTs; + targetCommand.tsCmd.timeWindow = this.subsTw.aggregation.timeWindow; + if (typeof this.subsTw.aggregation.interval === 'number') { + targetCommand.tsCmd.interval = this.subsTw.aggregation.interval; + targetCommand.tsCmd.intervalType = IntervalType.MILLISECONDS; + } else { + targetCommand.tsCmd.intervalType = this.subsTw.aggregation.interval; + } + targetCommand.tsCmd.timeZoneId = this.subsTw.timezone; + targetCommand.tsCmd.limit = this.subsTw.aggregation.limit; + targetCommand.tsCmd.agg = this.subsTw.aggregation.type; + targetCommand.tsCmd.fetchLatestPreviousPoint = this.subsTw.aggregation.stateData; + if (resetAggregators) { + this.dataAggregators.forEach((dataAggregator) => { + dataAggregator.reset(newSubsTw); + }); + } + } + if (this.aggTsValues.length > 0 && !this.isFloatingTimewindow) { + targetCommand.aggTsCmd.startTs = this.subsTw.startTs; + targetCommand.aggTsCmd.timeWindow = this.subsTw.aggregation.timeWindow; + if (resetAggregators) { + this.tsLatestDataAggregators.forEach((dataAggregator) => { + dataAggregator.reset(newSubsTw); + }); + } + } + } + + private onPageDataProcessed(pageData: PageData, isInitialData: boolean) { if (!this.dataResolved) { this.dataResolved = true; this.entityDataResolveSubject.next( @@ -981,6 +1072,7 @@ export class EntityDataSubscription { private processEntityData(entityData: EntityData, dataIndex: number, isUpdate: boolean, dataUpdatedCb: DataUpdatedCb) { + const aggTsHistoryPending = this.pendingAggTsHistory.has(dataIndex); if (this.entityDataSubscriptionOptions.type === widgetType.latest || this.entityDataSubscriptionOptions.type === widgetType.timeseries) { if (entityData.aggLatest) { @@ -998,6 +1090,7 @@ export class EntityDataSubscription { } } if (Object.keys(aggData).length > 0 && this.tsLatestDataAggregators && this.tsLatestDataAggregators[dataIndex]) { + this.pendingAggTsHistory.delete(dataIndex); const dataAggregator = this.tsLatestDataAggregators[dataIndex]; let prevDataCb: onAggregatedData; if (!isUpdate) { @@ -1028,7 +1121,8 @@ export class EntityDataSubscription { this.entityDataSubscriptionOptions.type === widgetType.timeseries, dataUpdatedCb); } const aggTsKeys = this.aggTsValues.filter(key => keys.includes(key.key)); - if (!this.history && aggTsKeys.length && this.tsLatestDataAggregators && this.tsLatestDataAggregators[dataIndex]) { + if (!this.history && aggTsKeys.length && this.tsLatestDataAggregators && this.tsLatestDataAggregators[dataIndex] && + !aggTsHistoryPending) { const dataAggregator = this.tsLatestDataAggregators[dataIndex]; const indexedData: IndexedSubscriptionData = []; for (const aggKey of aggTsKeys) { @@ -1046,22 +1140,25 @@ export class EntityDataSubscription { if (this.entityDataSubscriptionOptions.type === widgetType.timeseries && entityData.timeseries) { const subscriptionData = this.toSubscriptionData(entityData.timeseries, true); if (this.dataAggregators && this.dataAggregators[dataIndex]) { - const dataAggregator = this.dataAggregators[dataIndex]; - const keyNames = Object.keys(subscriptionData); - const dataKeys = this.timeseriesDataKeysByKeyNames(keyNames); - const indexedData: IndexedSubscriptionData = []; - for (const dataKey of dataKeys) { - indexedData[dataKey.index] = subscriptionData[dataKey.name]; - } - let prevDataCb: onAggregatedData; - if (!isUpdate) { - prevDataCb = dataAggregator.updateOnDataCb((data, detectChanges) => { - this.onIndexedData(data, dataIndex, detectChanges, false, dataUpdatedCb); - }); - } - dataAggregator.onData(indexedData, false, this.history, true); - if (prevDataCb) { - dataAggregator.updateOnDataCb(prevDataCb); + if (!this.pendingTsHistory.has(dataIndex) || this.isRefetchedTsHistory(entityData, isUpdate)) { + this.pendingTsHistory.delete(dataIndex); + const dataAggregator = this.dataAggregators[dataIndex]; + const keyNames = Object.keys(subscriptionData); + const dataKeys = this.timeseriesDataKeysByKeyNames(keyNames); + const indexedData: IndexedSubscriptionData = []; + for (const dataKey of dataKeys) { + indexedData[dataKey.index] = subscriptionData[dataKey.name]; + } + let prevDataCb: onAggregatedData; + if (!isUpdate) { + prevDataCb = dataAggregator.updateOnDataCb((data, detectChanges) => { + this.onIndexedData(data, dataIndex, detectChanges, false, dataUpdatedCb); + }); + } + dataAggregator.onData(indexedData, false, this.history, true); + if (prevDataCb) { + dataAggregator.updateOnDataCb(prevDataCb); + } } } else if (!this.history && !isUpdate) { this.onData(subscriptionData, DataKeyType.timeseries, dataIndex, true, false, dataUpdatedCb);