Skip to content

Commit

Permalink
fix: migrate asset id on the frontend in the query editor (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinwcyu authored Sep 3, 2024
1 parent 4fe6184 commit 9d7aa13
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/components/query/QueryEditor.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand All @@ -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<string> = {
label: `Default`,
description: datasource.options?.defaultRegion,
Expand Down
17 changes: 17 additions & 0 deletions src/migrations/migrateQuery.test.ts
Original file line number Diff line number Diff line change
@@ -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']));
});
});
15 changes: 15 additions & 0 deletions src/migrations/migrateQuery.ts
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 9d7aa13

Please sign in to comment.