mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
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:
parent
f93a9c794d
commit
20591cc42b
@ -27,10 +27,13 @@ const defaultProps = {
|
|||||||
describe('QueryEditor', () => {
|
describe('QueryEditor', () => {
|
||||||
it('should migrate the given query', async () => {
|
it('should migrate the given query', async () => {
|
||||||
const datasource = createMockDatasource();
|
const datasource = createMockDatasource();
|
||||||
|
const onChange = jest.fn();
|
||||||
datasource.migrateQuery = jest.fn().mockReturnValue(defaultProps.query);
|
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(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 () => {
|
it('should set a known query type', async () => {
|
||||||
|
@ -20,10 +20,13 @@ export const QueryEditor = (props: Props) => {
|
|||||||
const query = useMemo(() => {
|
const query = useMemo(() => {
|
||||||
if (!migrated) {
|
if (!migrated) {
|
||||||
setMigrated(true);
|
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;
|
return oldQ;
|
||||||
}, [oldQ, datasource, migrated]);
|
}, [oldQ, datasource, onChange, migrated]);
|
||||||
|
|
||||||
const sloQuery = { ...defaultSLOQuery(datasource), ...query.sloQuery };
|
const sloQuery = { ...defaultSLOQuery(datasource), ...query.sloQuery };
|
||||||
const onSLOQueryChange = (q: SLOQuery) => {
|
const onSLOQueryChange = (q: SLOQuery) => {
|
||||||
|
@ -223,14 +223,16 @@ export default class CloudMonitoringDatasource extends DataSourceWithBackend<
|
|||||||
// This is a manual port of the migration code in cloudmonitoring.go
|
// This is a manual port of the migration code in cloudmonitoring.go
|
||||||
// DO NOT UPDATE THIS CODE WITHOUT UPDATING THE BACKEND CODE
|
// DO NOT UPDATE THIS CODE WITHOUT UPDATING THE BACKEND CODE
|
||||||
migrateQuery(query: CloudMonitoringQuery): CloudMonitoringQuery {
|
migrateQuery(query: CloudMonitoringQuery): CloudMonitoringQuery {
|
||||||
|
const { hide, refId, datasource, key, queryType, maxLines, metric, intervalMs, type, ...rest } = query as any;
|
||||||
if (
|
if (
|
||||||
!query.hasOwnProperty('metricQuery') &&
|
!query.hasOwnProperty('metricQuery') &&
|
||||||
!query.hasOwnProperty('sloQuery') &&
|
!query.hasOwnProperty('sloQuery') &&
|
||||||
!query.hasOwnProperty('timeSeriesQuery') &&
|
!query.hasOwnProperty('timeSeriesQuery') &&
|
||||||
!query.hasOwnProperty('timeSeriesList')
|
!query.hasOwnProperty('timeSeriesList')
|
||||||
) {
|
) {
|
||||||
const { hide, refId, datasource, key, queryType, maxLines, metric, intervalMs, type, ...rest } = query as any;
|
|
||||||
return {
|
return {
|
||||||
|
datasource,
|
||||||
|
key,
|
||||||
refId,
|
refId,
|
||||||
intervalMs,
|
intervalMs,
|
||||||
hide,
|
hide,
|
||||||
|
Loading…
Reference in New Issue
Block a user