Skip to content

Commit

Permalink
feat(asset): load dependencies only once, change ttlcache to map (#81)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Aradei <[email protected]>
  • Loading branch information
saradei-ni and Robert Aradei authored Oct 17, 2024
1 parent 28f5128 commit d17bfa1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
7 changes: 3 additions & 4 deletions src/core/query-builder.utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { expressionBuilderCallback, expressionReaderCallback, ExpressionTransformFunction, transformComputedFieldsQuery } from "./query-builder.utils"
import TTLCache from "@isaacs/ttlcache";

describe('QueryBuilderUtils', () => {
describe('transformComputedFieldsQuery', () => {
Expand Down Expand Up @@ -50,9 +49,9 @@ describe('QueryBuilderUtils', () => {
});

it('should handle options correctly', () => {
const options = new Map<string, TTLCache<string, unknown>>();
const cache = new TTLCache<string, unknown>();
cache.set('key', 'value', { ttl: 1 });
const options = new Map<string, Map<string, unknown>>();
const cache = new Map<string, unknown>();
cache.set('key', 'value');
options.set('Object1', cache);

const query = 'Object1 = "value1"';
Expand Down
5 changes: 2 additions & 3 deletions src/core/query-builder.utils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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, unknown>) => string;
export type ExpressionTransformFunction = (value: string, operation: string, options?: Map<string, unknown>) => string;

/**
* Supported operations for computed fields
Expand All @@ -27,7 +26,7 @@ export const computedFieldsupportedOperations = ['=', '!='];
export function transformComputedFieldsQuery(
query: string,
computedDataFields: Map<string, ExpressionTransformFunction>,
options?: Map<string, TTLCache<string, unknown>>
options?: Map<string, Map<string, unknown>>
) {
for (const [field, transformation] of computedDataFields.entries()) {
const regex = new RegExp(`\\b${field}\\s*(${computedFieldsupportedOperations.join('|')})\\s*"([^"]*)"`, 'g');
Expand Down
32 changes: 19 additions & 13 deletions src/datasources/asset-calibration/AssetCalibrationDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -37,13 +35,16 @@ export class AssetCalibrationDataSource extends DataSourceBase<AssetCalibrationQ
filter: ''
};

public areSystemsLoaded = false;
public areWorkspacesLoaded = false;
private systemsLoaded!: () => void;
private workspacesLeaded!: () => void;

public areSystemsLoaded$ = new Promise<void>(resolve => this.systemsLoaded = resolve);
public areWorkspacesLoaded$ = new Promise<void>(resolve => this.workspacesLeaded = resolve);

public error = '';

public readonly systemAliasCache: TTLCache<string, SystemMetadata> = new TTLCache<string, SystemMetadata>({ ttl: metadataCacheTTL });
public readonly workspacesCache: TTLCache<string, Workspace> = new TTLCache<string, Workspace>({ ttl: metadataCacheTTL });
public readonly systemAliasCache = new Map<string, SystemMetadata>([]);
public readonly workspacesCache = new Map<string, Workspace>([]);
public readonly busTypeCache = new Map<BusType, string>([
...BusTypeOptions.map(busType => [busType.value, busType.label]) as Array<[BusType, string]>
]);
Expand All @@ -52,17 +53,22 @@ export class AssetCalibrationDataSource extends DataSourceBase<AssetCalibrationQ
]);

private readonly baseUrl = this.instanceSettings.url + '/niapm/v1';
private dependenciesLoadedPromise: Promise<void>;

constructor(
readonly instanceSettings: DataSourceInstanceSettings,
readonly backendSrv: BackendSrv = getBackendSrv(),
readonly templateSrv: TemplateSrv = getTemplateSrv(),
) {
super(instanceSettings, backendSrv, templateSrv);
this.dependenciesLoadedPromise = this.loadDependencies();
}

async runQuery(query: AssetCalibrationQuery, options: DataQueryRequest): Promise<DataFrameDTO> {
await this.loadDependencies();
await this.dependenciesLoadedPromise;

this.error = '';

this.validateQueryRange(query, options);

if (query.filter) {
Expand All @@ -80,7 +86,7 @@ export class AssetCalibrationDataSource extends DataSourceBase<AssetCalibrationQ
...Object.values(AssetCalibrationFieldNames).map(field => [field, this.multipleValuesQuery(field)] as [AssetCalibrationFieldNames, ExpressionTransformFunction]),
[
AssetCalibrationFieldNames.LOCATION,
(value: string, operation: string, options?: TTLCache<string, unknown>) => {
(value: string, operation: string, options?: Map<string, unknown>) => {
let values = [value];

if (this.isMultiSelectValue(value)) {
Expand Down Expand Up @@ -126,7 +132,7 @@ export class AssetCalibrationDataSource extends DataSourceBase<AssetCalibrationQ
return operation === QueryBuilderOperations.EQUALS.name ? '||' : '&&';
}

private readonly queryTransformationOptions = new Map<AssetCalibrationFieldNames, TTLCache<string, unknown>>([
private readonly queryTransformationOptions = new Map<AssetCalibrationFieldNames, Map<string, unknown>>([
[AssetCalibrationFieldNames.LOCATION, this.systemAliasCache]
]);

Expand Down Expand Up @@ -289,8 +295,9 @@ export class AssetCalibrationDataSource extends DataSourceBase<AssetCalibrationQ
this.error = parseErrorMessage(error)!;
});

this.areSystemsLoaded = true;
systems?.forEach(system => this.systemAliasCache.set(system.id, system));

this.systemsLoaded();
}

private async loadWorkspaces(): Promise<void> {
Expand All @@ -303,13 +310,12 @@ export class AssetCalibrationDataSource extends DataSourceBase<AssetCalibrationQ
this.error = parseErrorMessage(error)!;
});

this.areWorkspacesLoaded = true;
workspaces?.forEach(workspace => this.workspacesCache.set(workspace.id, workspace));

this.workspacesLeaded();
}

async loadDependencies(): Promise<void> {
this.error = '';

await this.loadSystems();
await this.loadWorkspaces();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ exports[`queries calibration forecast with minion ID location groupBy 1`] = `
{
"name": "Group",
"values": [
"Minion1-alias",
"Minion1",
"Minion2",
"Minion3",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@ export const AssetCalibrationQueryEditor = ({ query, onChange, onRunQuery, datas
const [areDependenciesLoaded, setAreDependenciesLoaded] = useState<boolean>(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);
Expand Down

0 comments on commit d17bfa1

Please sign in to comment.