diff --git a/src/components/query/QueryEditor.tsx b/src/components/query/QueryEditor.tsx index d7b6d77c..6bcf5e4f 100644 --- a/src/components/query/QueryEditor.tsx +++ b/src/components/query/QueryEditor.tsx @@ -1,5 +1,5 @@ import defaults from 'lodash/defaults'; -import React from 'react'; +import React, { useEffect } from 'react'; import { QueryEditorProps, SelectableValue } from '@grafana/data'; import { DataSource } from 'SitewiseDataSource'; import { SitewiseQuery, SitewiseOptions, QueryType, ListAssetsQuery, ListTimeSeriesQuery } from 'types'; @@ -8,6 +8,7 @@ import { QueryTypeInfo, siteWiseQueryTypes, changeQueryType } from 'queryInfo'; import { standardRegionOptions } from 'regions'; import { ListAssetsQueryEditor } from './ListAssetsQueryEditor'; import { PropertyQueryEditor } from './PropertyQueryEditor'; +import { migrateQuery } from '../../migrations/migrateQuery'; import { EditorField, EditorFieldGroup, EditorRow, EditorRows } from '@grafana/experimental'; import { QueryEditorHeader } from '@grafana/aws-sdk'; import { ClientCacheRow } from './ClientCacheRow'; @@ -27,6 +28,14 @@ export function QueryEditor(props: Props) { const { datasource } = props; const query = defaults(props.query, queryDefaults); + useEffect(() => { + const migratedQuery = migrateQuery(query); + + if (query !== migratedQuery) { + props.onChange(migratedQuery); + } + }, [query.assetId]); + const defaultRegion: SelectableValue = { label: `Default`, description: datasource.options?.defaultRegion, diff --git a/src/migrations/migrateQuery.test.ts b/src/migrations/migrateQuery.test.ts new file mode 100644 index 00000000..fc2dd3aa --- /dev/null +++ b/src/migrations/migrateQuery.test.ts @@ -0,0 +1,17 @@ +import { QueryType, SitewiseQuery } from '../types'; +import { migrateQuery } from './migrateQuery'; + +describe('migrateQuery()', () => { + it('should return the same query by reference equality if there are no migrations', () => { + const query: SitewiseQuery = { refId: 'a', queryType: QueryType.PropertyAggregate }; + const migratedQuery = migrateQuery(query); + expect(query).toBe(migratedQuery); + }); + + it('should migrate assetId to assetIds if assetIds does not exist', () => { + const query: SitewiseQuery = { refId: 'a', queryType: QueryType.PropertyAggregate, assetId: 'asset-id' }; + const migratedQuery = migrateQuery(query); + expect(query).not.toBe(migratedQuery); + expect(migratedQuery.assetIds).toEqual(expect.arrayContaining(['asset-id'])); + }); +}); diff --git a/src/migrations/migrateQuery.ts b/src/migrations/migrateQuery.ts new file mode 100644 index 00000000..9c310cfd --- /dev/null +++ b/src/migrations/migrateQuery.ts @@ -0,0 +1,15 @@ +import { SitewiseQuery } from '../types'; + +const migrateAssetId = (query: SitewiseQuery): SitewiseQuery => { + if (query.assetId && !query.assetIds) { + return { ...query, assetIds: [query.assetId] }; + } + + return query; +}; + +export const migrateQuery = (query: SitewiseQuery): SitewiseQuery => { + let migratedQuery = migrateAssetId(query); + + return migratedQuery; +};