mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Azure Monitor: Blank result formats are not overwritten (#93493)
* if result format is null from previous query, it will be null and not correct to a different format * update to not have empty value in result format field * fix tests * add check to see if options are in list * reformat stuff so logic lives on the right layer * linterrrrrrrrrrrrrrrrrrRR * Apply suggestions from code review * if result format is null from previous query, it will be null and not correct to a different format * update to not have empty value in result format field * fix tests * add check to see if options are in list * reformat stuff so logic lives on the right layer * linterrrrrrrrrrrrrrrrrrRR * frontend linter * linter * feedback :)
This commit is contained in:
parent
f1ba7deff5
commit
8b6cec2356
@ -64,22 +64,20 @@ func meetsBasicLogsCriteria(resources []string, fromAlert bool, basicLogsEnabled
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// This function should be part of migration function
|
||||
func ParseResultFormat(queryResultFormat *dataquery.ResultFormat, queryType dataquery.AzureQueryType) dataquery.ResultFormat {
|
||||
var resultFormat dataquery.ResultFormat
|
||||
if queryResultFormat != nil {
|
||||
resultFormat = *queryResultFormat
|
||||
if queryResultFormat != nil && *queryResultFormat != "" {
|
||||
return *queryResultFormat
|
||||
}
|
||||
if resultFormat == "" {
|
||||
if queryType == dataquery.AzureQueryTypeAzureLogAnalytics {
|
||||
// Default to logs format for logs queries
|
||||
resultFormat = dataquery.ResultFormatLogs
|
||||
}
|
||||
if queryType == dataquery.AzureQueryTypeAzureTraces {
|
||||
// Default to table format for traces queries as many traces may be returned
|
||||
resultFormat = dataquery.ResultFormatTable
|
||||
}
|
||||
if queryType == dataquery.AzureQueryTypeAzureLogAnalytics {
|
||||
// Default to time series format for logs queries. It was time series before this change
|
||||
return dataquery.ResultFormatTimeSeries
|
||||
}
|
||||
return resultFormat
|
||||
if queryType == dataquery.AzureQueryTypeAzureTraces {
|
||||
// Default to table format for traces queries as many traces may be returned
|
||||
return dataquery.ResultFormatTable
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func getApiURL(resourceOrWorkspace string, isAppInsightsQuery bool, basicLogsQuery bool) string {
|
||||
|
@ -17,10 +17,10 @@ func TestParseResultFormat(t *testing.T) {
|
||||
expectedResultFormat dataquery.ResultFormat
|
||||
}{
|
||||
{
|
||||
name: "returns the logs format as default for logs queries if input format is nil",
|
||||
name: "returns the time series format as default for logs queries if input format is nil",
|
||||
queryResultFormat: nil,
|
||||
queryType: dataquery.AzureQueryTypeAzureLogAnalytics,
|
||||
expectedResultFormat: dataquery.ResultFormatLogs,
|
||||
expectedResultFormat: dataquery.ResultFormatTimeSeries,
|
||||
},
|
||||
{
|
||||
name: "returns the table format as default for traces queries if input format is nil",
|
||||
@ -32,7 +32,7 @@ func TestParseResultFormat(t *testing.T) {
|
||||
name: "returns the logs format as default for logs queries if input format is empty",
|
||||
queryResultFormat: &emptyResultFormat,
|
||||
queryType: dataquery.AzureQueryTypeAzureLogAnalytics,
|
||||
expectedResultFormat: dataquery.ResultFormatLogs,
|
||||
expectedResultFormat: dataquery.ResultFormatTimeSeries,
|
||||
},
|
||||
{
|
||||
name: "returns the table format as default for traces queries if input format is empty",
|
||||
|
@ -5,6 +5,7 @@ import { dateTime, LoadingState } from '@grafana/data';
|
||||
|
||||
import createMockDatasource from '../../__mocks__/datasource';
|
||||
import createMockQuery from '../../__mocks__/query';
|
||||
import { ResultFormat } from '../../dataquery.gen';
|
||||
import { createMockResourcePickerData } from '../MetricsQueryEditor/MetricsQueryEditor.test';
|
||||
|
||||
import LogsQueryEditor from './LogsQueryEditor';
|
||||
@ -498,4 +499,57 @@ describe('LogsQueryEditor', () => {
|
||||
expect(await screen.queryByLabelText(/This query is processing 0.50 GiB when run./)).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('format as options', () => {
|
||||
it('sets to time series if there is a query with empty result format', async () => {
|
||||
const mockDatasource = createMockDatasource({ resourcePickerData: createMockResourcePickerData() });
|
||||
const query = createMockQuery({
|
||||
azureLogAnalytics: {
|
||||
resultFormat: undefined,
|
||||
},
|
||||
});
|
||||
const onChange = jest.fn();
|
||||
|
||||
await act(async () => {
|
||||
render(
|
||||
<LogsQueryEditor
|
||||
query={query}
|
||||
datasource={mockDatasource}
|
||||
variableOptionGroup={variableOptionGroup}
|
||||
onChange={onChange}
|
||||
setError={() => {}}
|
||||
basicLogsEnabled={false}
|
||||
/>
|
||||
);
|
||||
});
|
||||
const newQuery = {
|
||||
...query,
|
||||
azureLogAnalytics: { ...query.azureLogAnalytics, resultFormat: ResultFormat.TimeSeries },
|
||||
};
|
||||
expect(onChange).toHaveBeenCalledWith(newQuery);
|
||||
});
|
||||
it('sets to logs if the query is new', async () => {
|
||||
const mockDatasource = createMockDatasource({ resourcePickerData: createMockResourcePickerData() });
|
||||
const query = { ...createMockQuery(), azureLogAnalytics: undefined };
|
||||
const onChange = jest.fn();
|
||||
|
||||
await act(async () => {
|
||||
render(
|
||||
<LogsQueryEditor
|
||||
query={query}
|
||||
datasource={mockDatasource}
|
||||
variableOptionGroup={variableOptionGroup}
|
||||
onChange={onChange}
|
||||
setError={() => {}}
|
||||
basicLogsEnabled={false}
|
||||
/>
|
||||
);
|
||||
});
|
||||
const newQuery = {
|
||||
...query,
|
||||
azureLogAnalytics: { resultFormat: ResultFormat.Logs },
|
||||
};
|
||||
expect(onChange).toHaveBeenCalledWith(newQuery);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -17,7 +17,7 @@ import AdvancedResourcePicker from './AdvancedResourcePicker';
|
||||
import { LogsManagement } from './LogsManagement';
|
||||
import QueryField from './QueryField';
|
||||
import { TimeManagement } from './TimeManagement';
|
||||
import { setBasicLogsQuery, setFormatAs, setKustoQuery } from './setQueryValue';
|
||||
import { onLoad, setBasicLogsQuery, setFormatAs, setKustoQuery } from './setQueryValue';
|
||||
import useMigrations from './useMigrations';
|
||||
import { shouldShowBasicLogsToggle } from './utils';
|
||||
|
||||
@ -226,6 +226,7 @@ const LogsQueryEditor = ({
|
||||
defaultValue={ResultFormat.Logs}
|
||||
setFormatAs={setFormatAs}
|
||||
resultFormat={query.azureLogAnalytics?.resultFormat}
|
||||
onLoad={onLoad}
|
||||
/>
|
||||
)}
|
||||
{portalLinkButton}
|
||||
|
@ -1,3 +1,5 @@
|
||||
import { SelectableValue } from '@grafana/data';
|
||||
|
||||
import { AzureMonitorQuery, ResultFormat } from '../../types';
|
||||
|
||||
export function setKustoQuery(query: AzureMonitorQuery, kustoQuery: string): AzureMonitorQuery {
|
||||
@ -49,3 +51,16 @@ export function setBasicLogsQuery(query: AzureMonitorQuery, basicLogsQuery: bool
|
||||
},
|
||||
};
|
||||
}
|
||||
export function onLoad(
|
||||
query: AzureMonitorQuery,
|
||||
defaultValue: ResultFormat,
|
||||
handleChange: (change: SelectableValue<ResultFormat>) => void
|
||||
) {
|
||||
if (!query.azureLogAnalytics) {
|
||||
handleChange({ value: defaultValue });
|
||||
return;
|
||||
}
|
||||
if (!query.azureLogAnalytics.resultFormat) {
|
||||
handleChange({ value: ResultFormat.TimeSeries });
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ import FormatAsField from '../shared/FormatAsField';
|
||||
|
||||
import Filters from './Filters';
|
||||
import TraceTypeField from './TraceTypeField';
|
||||
import { setDefaultTracesQuery, setFormatAs, setQueryOperationId } from './setQueryValue';
|
||||
import { onLoad, setDefaultTracesQuery, setFormatAs, setQueryOperationId } from './setQueryValue';
|
||||
|
||||
interface TracesQueryEditorProps {
|
||||
query: AzureMonitorQuery;
|
||||
@ -169,6 +169,7 @@ const TracesQueryEditor = ({
|
||||
setFormatAs={setFormatAs}
|
||||
resultFormat={query.azureTraces?.resultFormat}
|
||||
range={range}
|
||||
onLoad={onLoad}
|
||||
/>
|
||||
</EditorFieldGroup>
|
||||
</EditorRow>
|
||||
|
@ -1,3 +1,5 @@
|
||||
import { SelectableValue } from '@grafana/data';
|
||||
|
||||
import { AzureMonitorQuery, AzureQueryType, AzureTracesFilter, ResultFormat } from '../../types';
|
||||
|
||||
// Used when switching from a traces exemplar query to a standard Azure Traces query
|
||||
@ -49,3 +51,13 @@ export function setFilters(query: AzureMonitorQuery, filters: AzureTracesFilter[
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function onLoad(
|
||||
query: AzureMonitorQuery,
|
||||
defaultValue: ResultFormat,
|
||||
handleChange: (change: SelectableValue<ResultFormat>) => void
|
||||
) {
|
||||
if (!query.azureTraces?.resultFormat) {
|
||||
handleChange({ value: defaultValue });
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
|
||||
import { SelectableValue } from '@grafana/data';
|
||||
|
||||
import createMockDatasource from '../../__mocks__/datasource';
|
||||
import createMockQuery from '../../__mocks__/query';
|
||||
import { ResultFormat } from '../../types';
|
||||
import { setFormatAs } from '../TracesQueryEditor/setQueryValue';
|
||||
import { AzureMonitorQuery, ResultFormat } from '../../types';
|
||||
|
||||
import FormatAsField from './FormatAsField';
|
||||
|
||||
@ -12,6 +13,21 @@ const options = [
|
||||
{ label: 'Trace', value: ResultFormat.Trace },
|
||||
{ label: 'Time Series', value: ResultFormat.TimeSeries },
|
||||
];
|
||||
const setFormatAs = (query: AzureMonitorQuery, formatAs: ResultFormat): AzureMonitorQuery => {
|
||||
return {
|
||||
...query,
|
||||
azureTraces: {
|
||||
...query.azureTraces,
|
||||
resultFormat: formatAs,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const onLoad = (
|
||||
_query: AzureMonitorQuery,
|
||||
_defaultValue: ResultFormat,
|
||||
_handleChange: (change: SelectableValue<ResultFormat>) => void
|
||||
) => {};
|
||||
|
||||
const props = {
|
||||
query: createMockQuery(),
|
||||
@ -25,6 +41,7 @@ const props = {
|
||||
defaultValue: ResultFormat.Table,
|
||||
setFormatAs,
|
||||
resultFormat: undefined,
|
||||
onLoad: onLoad,
|
||||
};
|
||||
|
||||
describe('FormatAsField', () => {
|
||||
|
@ -16,6 +16,7 @@ const FormatAsField = ({
|
||||
inputId,
|
||||
options: formatOptions,
|
||||
defaultValue,
|
||||
onLoad,
|
||||
setFormatAs,
|
||||
resultFormat,
|
||||
}: FormatAsFieldProps) => {
|
||||
@ -35,20 +36,18 @@ const FormatAsField = ({
|
||||
);
|
||||
|
||||
useEffectOnce(() => {
|
||||
if (!resultFormat) {
|
||||
//sets to default if the value is not found in the list
|
||||
if (!formatOptions.find((item) => item.value === resultFormat)) {
|
||||
handleChange({ value: defaultValue });
|
||||
} else {
|
||||
if (!formatOptions.find((item) => item.value === resultFormat)) {
|
||||
handleChange({ value: defaultValue });
|
||||
}
|
||||
}
|
||||
onLoad(query, defaultValue, handleChange);
|
||||
});
|
||||
|
||||
return (
|
||||
<Field label="Format as" data-testid={selectors.components.queryEditor.logsQueryEditor.formatSelection.input}>
|
||||
<Select
|
||||
inputId={`${inputId}-format-as-field`}
|
||||
value={resultFormat ?? defaultValue}
|
||||
value={resultFormat}
|
||||
onChange={handleChange}
|
||||
options={options}
|
||||
width={38}
|
||||
|
@ -188,6 +188,11 @@ export interface FormatAsFieldProps extends AzureQueryEditorFieldProps {
|
||||
defaultValue: ResultFormat;
|
||||
setFormatAs: (query: AzureMonitorQuery, formatAs: ResultFormat) => AzureMonitorQuery;
|
||||
resultFormat?: ResultFormat;
|
||||
onLoad: (
|
||||
query: AzureMonitorQuery,
|
||||
defaultValue: ResultFormat,
|
||||
handleChange: (change: SelectableValue<ResultFormat>) => void
|
||||
) => void;
|
||||
}
|
||||
|
||||
export interface AzureResourceSummaryItem {
|
||||
|
Loading…
Reference in New Issue
Block a user