Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: migrate asset id on the frontend in the query editor #350

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};
Loading