CloudMonitor: Fix query migration (#63491)

* Update query and set required fields for explore queries

* Update test case

- Validate onChange is called with the new query
This commit is contained in:
Andreas Christou 2023-02-21 16:21:20 +00:00 committed by GitHub
parent f93a9c794d
commit 20591cc42b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 4 deletions

View File

@ -27,10 +27,13 @@ const defaultProps = {
describe('QueryEditor', () => {
it('should migrate the given query', async () => {
const datasource = createMockDatasource();
const onChange = jest.fn();
datasource.migrateQuery = jest.fn().mockReturnValue(defaultProps.query);
render(<QueryEditor {...defaultProps} datasource={datasource} />);
render(<QueryEditor {...defaultProps} datasource={datasource} onChange={onChange} />);
await waitFor(() => expect(datasource.migrateQuery).toHaveBeenCalledTimes(1));
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
await waitFor(() => expect(onChange).toHaveBeenCalledWith(defaultProps.query));
});
it('should set a known query type', async () => {

View File

@ -20,10 +20,13 @@ export const QueryEditor = (props: Props) => {
const query = useMemo(() => {
if (!migrated) {
setMigrated(true);
return datasource.migrateQuery(oldQ);
const migratedQuery = datasource.migrateQuery(oldQ);
// Update the query once the migrations have been completed.
onChange({ ...migratedQuery });
return migratedQuery;
}
return oldQ;
}, [oldQ, datasource, migrated]);
}, [oldQ, datasource, onChange, migrated]);
const sloQuery = { ...defaultSLOQuery(datasource), ...query.sloQuery };
const onSLOQueryChange = (q: SLOQuery) => {

View File

@ -223,14 +223,16 @@ export default class CloudMonitoringDatasource extends DataSourceWithBackend<
// This is a manual port of the migration code in cloudmonitoring.go
// DO NOT UPDATE THIS CODE WITHOUT UPDATING THE BACKEND CODE
migrateQuery(query: CloudMonitoringQuery): CloudMonitoringQuery {
const { hide, refId, datasource, key, queryType, maxLines, metric, intervalMs, type, ...rest } = query as any;
if (
!query.hasOwnProperty('metricQuery') &&
!query.hasOwnProperty('sloQuery') &&
!query.hasOwnProperty('timeSeriesQuery') &&
!query.hasOwnProperty('timeSeriesList')
) {
const { hide, refId, datasource, key, queryType, maxLines, metric, intervalMs, type, ...rest } = query as any;
return {
datasource,
key,
refId,
intervalMs,
hide,