mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Azure Monitor: Clean up fields when editing Metrics (#41762)
This commit is contained in:
parent
daa3cb831d
commit
f8192bf428
@ -109,46 +109,8 @@ describe('Azure Monitor QueryEditor', () => {
|
|||||||
azureMonitor: {
|
azureMonitor: {
|
||||||
...mockQuery.azureMonitor,
|
...mockQuery.azureMonitor,
|
||||||
metricName: 'metric-b',
|
metricName: 'metric-b',
|
||||||
},
|
aggregation: undefined,
|
||||||
});
|
timeGrain: '',
|
||||||
});
|
|
||||||
|
|
||||||
it('should auto select a default aggregation if none exists once a metric is selected', async () => {
|
|
||||||
const mockDatasource = createMockDatasource();
|
|
||||||
const onChange = jest.fn();
|
|
||||||
const mockQuery = createMockQuery();
|
|
||||||
(mockQuery.azureMonitor ?? {}).aggregation = undefined;
|
|
||||||
mockDatasource.getMetricNames = jest.fn().mockResolvedValue([
|
|
||||||
{
|
|
||||||
value: 'metric-a',
|
|
||||||
text: 'Metric A',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
value: 'metric-b',
|
|
||||||
text: 'Metric B',
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
render(
|
|
||||||
<MetricsQueryEditor
|
|
||||||
subscriptionId="123"
|
|
||||||
query={createMockQuery()}
|
|
||||||
datasource={mockDatasource}
|
|
||||||
variableOptionGroup={variableOptionGroup}
|
|
||||||
onChange={onChange}
|
|
||||||
setError={() => {}}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
await waitFor(() => expect(screen.getByTestId('azure-monitor-metrics-query-editor')).toBeInTheDocument());
|
|
||||||
|
|
||||||
const metrics = await screen.findByLabelText('Metric');
|
|
||||||
await selectOptionInTest(metrics, 'Metric B');
|
|
||||||
|
|
||||||
expect(onChange).toHaveBeenLastCalledWith({
|
|
||||||
...mockQuery,
|
|
||||||
azureMonitor: {
|
|
||||||
...mockQuery.azureMonitor,
|
|
||||||
metricName: 'metric-b',
|
|
||||||
aggregation: 'Average',
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -2,6 +2,7 @@ import { renderHook } from '@testing-library/react-hooks';
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
DataHook,
|
DataHook,
|
||||||
|
updateSubscriptions,
|
||||||
useAsyncState,
|
useAsyncState,
|
||||||
useMetricNames,
|
useMetricNames,
|
||||||
useResourceGroups,
|
useResourceGroups,
|
||||||
@ -340,8 +341,82 @@ describe('AzureMonitor: metrics dataHooks', () => {
|
|||||||
|
|
||||||
expect(onChange).toHaveBeenCalledWith({
|
expect(onChange).toHaveBeenCalledWith({
|
||||||
...query,
|
...query,
|
||||||
azureMonitor: scenario.expectedClearedQueryPartial,
|
azureMonitor: {
|
||||||
|
...scenario.expectedClearedQueryPartial,
|
||||||
|
dimensionFilters: [],
|
||||||
|
timeGrain: '',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('AzureMonitor: updateSubscriptions', () => {
|
||||||
|
const bareQuery = {
|
||||||
|
refId: 'A',
|
||||||
|
queryType: AzureQueryType.AzureMonitor,
|
||||||
|
};
|
||||||
|
|
||||||
|
[
|
||||||
|
{
|
||||||
|
description: 'should not update with no subscriptions',
|
||||||
|
query: bareQuery,
|
||||||
|
subscriptionOptions: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: 'should not update with the subscription as an option',
|
||||||
|
query: { ...bareQuery, subscription: 'foo' },
|
||||||
|
subscriptionOptions: [{ label: 'foo', value: 'foo' }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: 'should update with the first subscription',
|
||||||
|
query: { ...bareQuery },
|
||||||
|
subscriptionOptions: [{ label: 'foo', value: 'foo' }],
|
||||||
|
onChangeArgs: {
|
||||||
|
...bareQuery,
|
||||||
|
subscription: 'foo',
|
||||||
|
azureMonitor: {
|
||||||
|
dimensionFilters: [],
|
||||||
|
timeGrain: '',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: 'should update with the default subscription if the current subsription does not exists',
|
||||||
|
query: { ...bareQuery, subscription: 'bar' },
|
||||||
|
subscriptionOptions: [{ label: 'foo', value: 'foo' }],
|
||||||
|
onChangeArgs: {
|
||||||
|
...bareQuery,
|
||||||
|
subscription: 'foo',
|
||||||
|
azureMonitor: {
|
||||||
|
dimensionFilters: [],
|
||||||
|
timeGrain: '',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: 'should clean up if neither the default sub nor the current sub exists',
|
||||||
|
query: { ...bareQuery, subscription: 'bar' },
|
||||||
|
subscriptionOptions: [{ label: 'foo', value: 'foo' }],
|
||||||
|
defaultSubscription: 'foobar',
|
||||||
|
onChangeArgs: {
|
||||||
|
...bareQuery,
|
||||||
|
subscription: '',
|
||||||
|
azureMonitor: {
|
||||||
|
dimensionFilters: [],
|
||||||
|
timeGrain: '',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
].forEach((test) => {
|
||||||
|
it(test.description, () => {
|
||||||
|
const onChange = jest.fn();
|
||||||
|
updateSubscriptions(test.query, test.subscriptionOptions, onChange, test.defaultSubscription);
|
||||||
|
if (test.onChangeArgs) {
|
||||||
|
expect(onChange).toHaveBeenCalledWith(test.onChangeArgs);
|
||||||
|
} else {
|
||||||
|
expect(onChange).not.toHaveBeenCalled();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
@ -55,8 +55,38 @@ export function useAsyncState<T>(asyncFn: () => Promise<T>, setError: Function,
|
|||||||
return finalValue;
|
return finalValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useSubscriptions: DataHook = (query, datasource, onChange, setError) => {
|
export const updateSubscriptions = (
|
||||||
|
query: AzureMonitorQuery,
|
||||||
|
subscriptionOptions: AzureMonitorOption[],
|
||||||
|
onChange: OnChangeFn,
|
||||||
|
defaultSubscription?: string
|
||||||
|
) => {
|
||||||
const { subscription } = query;
|
const { subscription } = query;
|
||||||
|
|
||||||
|
// Return early if subscriptions havent loaded, or if the query already has a subscription
|
||||||
|
if (!subscriptionOptions.length || (subscription && hasOption(subscriptionOptions, subscription))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultSub = defaultSubscription || subscriptionOptions[0].value;
|
||||||
|
|
||||||
|
if (!subscription && defaultSub && hasOption(subscriptionOptions, defaultSub)) {
|
||||||
|
onChange(setSubscriptionID(query, defaultSub));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the current subscription is in the list of subscriptions
|
||||||
|
if (subscription && !hasOption(subscriptionOptions, subscription)) {
|
||||||
|
if (hasOption(subscriptionOptions, defaultSub)) {
|
||||||
|
// Use the default sub if is on theh list
|
||||||
|
onChange(setSubscriptionID(query, defaultSub));
|
||||||
|
} else {
|
||||||
|
// Neither the current subscription nor the defaultSub is on the list, remove it
|
||||||
|
onChange(setSubscriptionID(query, ''));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useSubscriptions: DataHook = (query, datasource, onChange, setError) => {
|
||||||
const defaultSubscription = datasource.azureMonitorDatasource.defaultSubscriptionId;
|
const defaultSubscription = datasource.azureMonitorDatasource.defaultSubscriptionId;
|
||||||
|
|
||||||
const subscriptionOptions = useAsyncState(
|
const subscriptionOptions = useAsyncState(
|
||||||
@ -69,17 +99,8 @@ export const useSubscriptions: DataHook = (query, datasource, onChange, setError
|
|||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Return early if subscriptions havent loaded, or if the query already has a subscription
|
updateSubscriptions(query, subscriptionOptions, onChange, defaultSubscription);
|
||||||
if (!subscriptionOptions.length || (subscription && hasOption(subscriptionOptions, subscription))) {
|
}, [subscriptionOptions, query, defaultSubscription, onChange]);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const defaultSub = defaultSubscription || subscriptionOptions[0].value;
|
|
||||||
|
|
||||||
if (!subscription && defaultSub && hasOption(subscriptionOptions, defaultSub)) {
|
|
||||||
onChange(setSubscriptionID(query, defaultSub));
|
|
||||||
}
|
|
||||||
}, [subscriptionOptions, query, subscription, defaultSubscription, onChange]);
|
|
||||||
|
|
||||||
return subscriptionOptions;
|
return subscriptionOptions;
|
||||||
};
|
};
|
||||||
|
@ -11,6 +11,13 @@ export function setSubscriptionID(query: AzureMonitorQuery, subscriptionID: stri
|
|||||||
azureMonitor: {
|
azureMonitor: {
|
||||||
...query.azureMonitor,
|
...query.azureMonitor,
|
||||||
resourceGroup: undefined,
|
resourceGroup: undefined,
|
||||||
|
metricDefinition: undefined,
|
||||||
|
metricNamespace: undefined,
|
||||||
|
resourceName: undefined,
|
||||||
|
metricName: undefined,
|
||||||
|
aggregation: undefined,
|
||||||
|
timeGrain: '',
|
||||||
|
dimensionFilters: [],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -25,7 +32,13 @@ export function setResourceGroup(query: AzureMonitorQuery, resourceGroup: string
|
|||||||
azureMonitor: {
|
azureMonitor: {
|
||||||
...query.azureMonitor,
|
...query.azureMonitor,
|
||||||
resourceGroup: resourceGroup,
|
resourceGroup: resourceGroup,
|
||||||
|
metricDefinition: undefined,
|
||||||
|
metricNamespace: undefined,
|
||||||
resourceName: undefined,
|
resourceName: undefined,
|
||||||
|
metricName: undefined,
|
||||||
|
aggregation: undefined,
|
||||||
|
timeGrain: '',
|
||||||
|
dimensionFilters: [],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -44,6 +57,9 @@ export function setResourceType(query: AzureMonitorQuery, resourceType: string |
|
|||||||
resourceName: undefined,
|
resourceName: undefined,
|
||||||
metricNamespace: undefined,
|
metricNamespace: undefined,
|
||||||
metricName: undefined,
|
metricName: undefined,
|
||||||
|
aggregation: undefined,
|
||||||
|
timeGrain: '',
|
||||||
|
dimensionFilters: [],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -60,6 +76,11 @@ export function setResourceName(query: AzureMonitorQuery, resourceName: string |
|
|||||||
azureMonitor: {
|
azureMonitor: {
|
||||||
...query.azureMonitor,
|
...query.azureMonitor,
|
||||||
resourceName: resourceName,
|
resourceName: resourceName,
|
||||||
|
metricNamespace: undefined,
|
||||||
|
metricName: undefined,
|
||||||
|
aggregation: undefined,
|
||||||
|
timeGrain: '',
|
||||||
|
dimensionFilters: [],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -75,6 +96,9 @@ export function setMetricNamespace(query: AzureMonitorQuery, metricNamespace: st
|
|||||||
...query.azureMonitor,
|
...query.azureMonitor,
|
||||||
metricNamespace: metricNamespace,
|
metricNamespace: metricNamespace,
|
||||||
metricName: undefined,
|
metricName: undefined,
|
||||||
|
aggregation: undefined,
|
||||||
|
timeGrain: '',
|
||||||
|
dimensionFilters: [],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -89,6 +113,9 @@ export function setMetricName(query: AzureMonitorQuery, metricName: string | und
|
|||||||
azureMonitor: {
|
azureMonitor: {
|
||||||
...query.azureMonitor,
|
...query.azureMonitor,
|
||||||
metricName: metricName,
|
metricName: metricName,
|
||||||
|
aggregation: undefined,
|
||||||
|
timeGrain: '',
|
||||||
|
dimensionFilters: [],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user