From d17bfa1789c7f0cb571bef423cda81bf54cac1d7 Mon Sep 17 00:00:00 2001 From: saradei-ni Date: Thu, 17 Oct 2024 09:23:30 +0300 Subject: [PATCH] feat(asset): load dependencies only once, change ttlcache to map (#81) Co-authored-by: Robert Aradei --- src/core/query-builder.utils.test.ts | 7 ++-- src/core/query-builder.utils.ts | 5 ++- .../AssetCalibrationDataSource.ts | 32 +++++++++++-------- .../AssetCalibrationDataSource.test.ts.snap | 2 +- .../AssetCalibrationQueryEditor.tsx | 12 +++---- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/core/query-builder.utils.test.ts b/src/core/query-builder.utils.test.ts index bd2ffe6e..bcbc9fe1 100644 --- a/src/core/query-builder.utils.test.ts +++ b/src/core/query-builder.utils.test.ts @@ -1,5 +1,4 @@ import { expressionBuilderCallback, expressionReaderCallback, ExpressionTransformFunction, transformComputedFieldsQuery } from "./query-builder.utils" -import TTLCache from "@isaacs/ttlcache"; describe('QueryBuilderUtils', () => { describe('transformComputedFieldsQuery', () => { @@ -50,9 +49,9 @@ describe('QueryBuilderUtils', () => { }); it('should handle options correctly', () => { - const options = new Map>(); - const cache = new TTLCache(); - cache.set('key', 'value', { ttl: 1 }); + const options = new Map>(); + const cache = new Map(); + cache.set('key', 'value'); options.set('Object1', cache); const query = 'Object1 = "value1"'; diff --git a/src/core/query-builder.utils.ts b/src/core/query-builder.utils.ts index 65e45b90..2a9d6173 100644 --- a/src/core/query-builder.utils.ts +++ b/src/core/query-builder.utils.ts @@ -1,6 +1,5 @@ import { QueryBuilderCustomOperation } from "smart-webcomponents-react"; import { QueryBuilderOption } from "./types"; -import TTLCache from "@isaacs/ttlcache"; /** * Should be used when looking to build a custom expression for a field @@ -9,7 +8,7 @@ import TTLCache from "@isaacs/ttlcache"; * @param options The options to be used in the transformation * @returns The transformed value */ -export type ExpressionTransformFunction = (value: string, operation: string, options?: TTLCache) => string; +export type ExpressionTransformFunction = (value: string, operation: string, options?: Map) => string; /** * Supported operations for computed fields @@ -27,7 +26,7 @@ export const computedFieldsupportedOperations = ['=', '!=']; export function transformComputedFieldsQuery( query: string, computedDataFields: Map, - options?: Map> + options?: Map> ) { for (const [field, transformation] of computedDataFields.entries()) { const regex = new RegExp(`\\b${field}\\s*(${computedFieldsupportedOperations.join('|')})\\s*"([^"]*)"`, 'g'); diff --git a/src/datasources/asset-calibration/AssetCalibrationDataSource.ts b/src/datasources/asset-calibration/AssetCalibrationDataSource.ts index 3c513a09..74f35e61 100644 --- a/src/datasources/asset-calibration/AssetCalibrationDataSource.ts +++ b/src/datasources/asset-calibration/AssetCalibrationDataSource.ts @@ -23,8 +23,6 @@ import { import { ExpressionTransformFunction, transformComputedFieldsQuery } from 'core/query-builder.utils'; import { AssetCalibrationFieldNames } from './constants'; import { AssetModel, AssetsResponse } from 'datasources/asset-common/types'; -import TTLCache from '@isaacs/ttlcache'; -import { metadataCacheTTL } from 'datasources/data-frame/constants'; import { SystemMetadata } from 'datasources/system/types'; import { defaultOrderBy, defaultProjection } from 'datasources/system/constants'; import { QueryBuilderOperations } from 'core/query-builder.constants'; @@ -37,13 +35,16 @@ export class AssetCalibrationDataSource extends DataSourceBase void; + private workspacesLeaded!: () => void; + + public areSystemsLoaded$ = new Promise(resolve => this.systemsLoaded = resolve); + public areWorkspacesLoaded$ = new Promise(resolve => this.workspacesLeaded = resolve); public error = ''; - public readonly systemAliasCache: TTLCache = new TTLCache({ ttl: metadataCacheTTL }); - public readonly workspacesCache: TTLCache = new TTLCache({ ttl: metadataCacheTTL }); + public readonly systemAliasCache = new Map([]); + public readonly workspacesCache = new Map([]); public readonly busTypeCache = new Map([ ...BusTypeOptions.map(busType => [busType.value, busType.label]) as Array<[BusType, string]> ]); @@ -52,6 +53,7 @@ export class AssetCalibrationDataSource extends DataSourceBase; constructor( readonly instanceSettings: DataSourceInstanceSettings, @@ -59,10 +61,14 @@ export class AssetCalibrationDataSource extends DataSourceBase { - await this.loadDependencies(); + await this.dependenciesLoadedPromise; + + this.error = ''; + this.validateQueryRange(query, options); if (query.filter) { @@ -80,7 +86,7 @@ export class AssetCalibrationDataSource extends DataSourceBase [field, this.multipleValuesQuery(field)] as [AssetCalibrationFieldNames, ExpressionTransformFunction]), [ AssetCalibrationFieldNames.LOCATION, - (value: string, operation: string, options?: TTLCache) => { + (value: string, operation: string, options?: Map) => { let values = [value]; if (this.isMultiSelectValue(value)) { @@ -126,7 +132,7 @@ export class AssetCalibrationDataSource extends DataSourceBase>([ + private readonly queryTransformationOptions = new Map>([ [AssetCalibrationFieldNames.LOCATION, this.systemAliasCache] ]); @@ -289,8 +295,9 @@ export class AssetCalibrationDataSource extends DataSourceBase this.systemAliasCache.set(system.id, system)); + + this.systemsLoaded(); } private async loadWorkspaces(): Promise { @@ -303,13 +310,12 @@ export class AssetCalibrationDataSource extends DataSourceBase this.workspacesCache.set(workspace.id, workspace)); + + this.workspacesLeaded(); } async loadDependencies(): Promise { - this.error = ''; - await this.loadSystems(); await this.loadWorkspaces(); } diff --git a/src/datasources/asset-calibration/__snapshots__/AssetCalibrationDataSource.test.ts.snap b/src/datasources/asset-calibration/__snapshots__/AssetCalibrationDataSource.test.ts.snap index 3d9e333f..79d09809 100644 --- a/src/datasources/asset-calibration/__snapshots__/AssetCalibrationDataSource.test.ts.snap +++ b/src/datasources/asset-calibration/__snapshots__/AssetCalibrationDataSource.test.ts.snap @@ -195,7 +195,7 @@ exports[`queries calibration forecast with minion ID location groupBy 1`] = ` { "name": "Group", "values": [ - "Minion1-alias", + "Minion1", "Minion2", "Minion3", ], diff --git a/src/datasources/asset-calibration/components/AssetCalibrationQueryEditor.tsx b/src/datasources/asset-calibration/components/AssetCalibrationQueryEditor.tsx index 049c4ae8..e9a028f5 100644 --- a/src/datasources/asset-calibration/components/AssetCalibrationQueryEditor.tsx +++ b/src/datasources/asset-calibration/components/AssetCalibrationQueryEditor.tsx @@ -20,16 +20,12 @@ export const AssetCalibrationQueryEditor = ({ query, onChange, onRunQuery, datas const [areDependenciesLoaded, setAreDependenciesLoaded] = useState(false); useEffect(() => { - if (datasource.areWorkspacesLoaded) { + Promise.all([datasource.areSystemsLoaded$, datasource.areWorkspacesLoaded$]).then(() => { setWorkspaces(Array.from(datasource.workspacesCache.values())); - } - - if (datasource.areSystemsLoaded) { setSystems(Array.from(datasource.systemAliasCache.values())); - } - - setAreDependenciesLoaded(datasource.areSystemsLoaded && datasource.areWorkspacesLoaded); - }, [datasource, datasource.areSystemsLoaded, datasource.areWorkspacesLoaded]); + setAreDependenciesLoaded(true); + }); + }, [datasource]); const handleQueryChange = (value: AssetCalibrationQuery, runQuery: boolean): void => { onChange(value);