mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Azure Monitor: Fix aggregation reset bug (#33287)
Fix aggregation reset bug
This commit is contained in:
parent
4643bfa539
commit
ed2388bfea
@ -25,8 +25,8 @@ export default function createMockDatasource() {
|
||||
getMetricNamespaces: jest.fn().mockResolvedValueOnce([]),
|
||||
getMetricNames: jest.fn().mockResolvedValueOnce([]),
|
||||
getMetricMetadata: jest.fn().mockResolvedValueOnce({
|
||||
primaryAggType: 'average',
|
||||
supportedAggTypes: [],
|
||||
primaryAggType: 'Average',
|
||||
supportedAggTypes: ['Average', 'Maximum', 'Minimum'],
|
||||
supportedTimeGrains: [],
|
||||
dimensions: [],
|
||||
}),
|
||||
|
@ -75,7 +75,9 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureM
|
||||
item.azureMonitor.metricDefinition &&
|
||||
item.azureMonitor.metricDefinition !== defaultDropdownValue &&
|
||||
item.azureMonitor.metricName &&
|
||||
item.azureMonitor.metricName !== defaultDropdownValue
|
||||
item.azureMonitor.metricName !== defaultDropdownValue &&
|
||||
item.azureMonitor.aggregation &&
|
||||
item.azureMonitor.aggregation !== defaultDropdownValue
|
||||
);
|
||||
}
|
||||
|
||||
@ -123,7 +125,8 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureM
|
||||
}
|
||||
|
||||
buildAzurePortalUrl(metricQuery: AzureMetricQuery, subscriptionId: string, timeRange: TimeRange) {
|
||||
const aggregationType = aggregationTypeMap[metricQuery.aggregation] ?? aggregationTypeMap.Average;
|
||||
const aggregationType =
|
||||
(metricQuery.aggregation && aggregationTypeMap[metricQuery.aggregation]) ?? aggregationTypeMap.Average;
|
||||
|
||||
const chartDef = this.stringifyAzurePortalUrlParam({
|
||||
v2charts: [
|
||||
|
@ -68,7 +68,7 @@ describe('Azure Monitor QueryEditor', () => {
|
||||
metricNamespace: undefined,
|
||||
resourceName: undefined,
|
||||
metricName: undefined,
|
||||
aggregation: 'None',
|
||||
aggregation: undefined,
|
||||
timeGrain: '',
|
||||
dimensionFilters: [],
|
||||
},
|
||||
@ -104,7 +104,7 @@ describe('Azure Monitor QueryEditor', () => {
|
||||
const metrics = await screen.findByLabelText('Metric');
|
||||
await selectEvent.select(metrics, 'Metric B');
|
||||
|
||||
expect(onChange).toHaveBeenCalledWith({
|
||||
expect(onChange).toHaveBeenLastCalledWith({
|
||||
...mockQuery,
|
||||
azureMonitor: {
|
||||
...mockQuery.azureMonitor,
|
||||
@ -112,4 +112,72 @@ describe('Azure Monitor QueryEditor', () => {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
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 selectEvent.select(metrics, 'Metric B');
|
||||
|
||||
expect(onChange).toHaveBeenLastCalledWith({
|
||||
...mockQuery,
|
||||
azureMonitor: {
|
||||
...mockQuery.azureMonitor,
|
||||
metricName: 'metric-b',
|
||||
aggregation: 'Average',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should change the aggregation type when selected', async () => {
|
||||
const mockDatasource = createMockDatasource();
|
||||
const onChange = jest.fn();
|
||||
const mockQuery = createMockQuery();
|
||||
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 aggregation = await screen.findByLabelText('Aggregation');
|
||||
await selectEvent.select(aggregation, 'Maximum');
|
||||
|
||||
expect(onChange).toHaveBeenLastCalledWith({
|
||||
...mockQuery,
|
||||
azureMonitor: {
|
||||
...mockQuery.azureMonitor,
|
||||
aggregation: 'Maximum',
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -45,7 +45,7 @@ const NamespaceField: React.FC<AzureQueryEditorFieldProps> = ({
|
||||
resourceName: undefined,
|
||||
metricNamespace: undefined,
|
||||
metricName: undefined,
|
||||
aggregation: 'None',
|
||||
aggregation: undefined,
|
||||
timeGrain: '',
|
||||
dimensionFilters: [],
|
||||
},
|
||||
|
@ -47,7 +47,7 @@ const ResourceGroupsField: React.FC<AzureQueryEditorFieldProps> = ({
|
||||
resourceName: undefined,
|
||||
metricNamespace: undefined,
|
||||
metricName: undefined,
|
||||
aggregation: 'None',
|
||||
aggregation: undefined,
|
||||
timeGrain: '',
|
||||
dimensionFilters: [],
|
||||
},
|
||||
|
@ -45,7 +45,7 @@ const ResourceNameField: React.FC<AzureQueryEditorFieldProps> = ({
|
||||
|
||||
metricNamespace: undefined,
|
||||
metricName: undefined,
|
||||
aggregation: 'None',
|
||||
aggregation: undefined,
|
||||
timeGrain: '',
|
||||
dimensionFilters: [],
|
||||
},
|
||||
|
@ -84,7 +84,7 @@ const SubscriptionField: React.FC<SubscriptionFieldProps> = ({
|
||||
metricNamespace: undefined,
|
||||
resourceName: undefined,
|
||||
metricName: undefined,
|
||||
aggregation: 'None',
|
||||
aggregation: undefined,
|
||||
timeGrain: '',
|
||||
dimensionFilters: [],
|
||||
};
|
||||
|
@ -50,8 +50,11 @@ export function useMetricsMetadata(
|
||||
...query,
|
||||
azureMonitor: {
|
||||
...query.azureMonitor,
|
||||
aggregation: metadata.primaryAggType,
|
||||
timeGrain: 'auto',
|
||||
aggregation:
|
||||
query.azureMonitor.aggregation && metadata.supportedAggTypes.includes(query.azureMonitor.aggregation)
|
||||
? query.azureMonitor.aggregation
|
||||
: metadata.primaryAggType,
|
||||
timeGrain: query.azureMonitor.timeGrain || 'auto',
|
||||
allowedTimeGrainsMs: convertTimeGrainsToMs(metadata.supportedTimeGrains),
|
||||
},
|
||||
});
|
||||
|
@ -63,7 +63,7 @@ export interface AzureMetricQuery {
|
||||
timeGrainUnit?: string;
|
||||
timeGrain: string;
|
||||
allowedTimeGrainsMs: number[];
|
||||
aggregation: string;
|
||||
aggregation: string | undefined;
|
||||
dimensionFilters: AzureMetricDimension[];
|
||||
alias: string;
|
||||
top: string;
|
||||
|
Loading…
Reference in New Issue
Block a user