diff --git a/packages/grafana-prometheus/src/configuration/PromSettings.tsx b/packages/grafana-prometheus/src/configuration/PromSettings.tsx index 7d82845a4f3..32b54510116 100644 --- a/packages/grafana-prometheus/src/configuration/PromSettings.tsx +++ b/packages/grafana-prometheus/src/configuration/PromSettings.tsx @@ -3,6 +3,7 @@ import { SyntheticEvent, useState } from 'react'; import { DataSourcePluginOptionsEditorProps, + DataSourceSettings, onUpdateDatasourceJsonDataOptionChecked, SelectableValue, updateDatasourcePluginJsonDataOption, @@ -60,14 +61,19 @@ export const NON_NEGATIVE_INTEGER_REGEX = /^(0|[1-9]\d*)(\.\d+)?(e\+?\d+)?$/; // const durationError = 'Value is not valid, you can use number with time unit specifier: y, M, w, d, h, m, s'; export const countError = 'Value is not valid, you can use non-negative integers, including scientific notation'; -export const PromSettings = (props: Props) => { - const { options, onOptionsChange } = props; +const getOptionsWithDefaults = (options: DataSourceSettings) => { + if (options.jsonData.httpMethod) { + return options; + } // We are explicitly adding httpMethod so, it is correctly displayed in dropdown. // This way, it is more predictable for users. - if (!options.jsonData.httpMethod) { - options.jsonData.httpMethod = 'POST'; - } + return { ...options, jsonData: { ...options.jsonData, httpMethod: 'POST' } }; +}; + +export const PromSettings = (props: Props) => { + const { onOptionsChange } = props; + const optionsWithDefaults = getOptionsWithDefaults(props.options); const theme = useTheme2(); const styles = overhaulStyles(theme); @@ -111,15 +117,15 @@ export const PromSettings = (props: Props) => { } interactive={true} - disabled={options.readOnly} + disabled={optionsWithDefaults.readOnly} > <> updateValidDuration({ ...validDuration, @@ -141,13 +147,13 @@ export const PromSettings = (props: Props) => { labelWidth={PROM_CONFIG_LABEL_WIDTH} tooltip={<>Set the Prometheus query timeout. {docsTip()}} interactive={true} - disabled={options.readOnly} + disabled={optionsWithDefaults.readOnly} > <> @@ -174,16 +180,16 @@ export const PromSettings = (props: Props) => { labelWidth={PROM_CONFIG_LABEL_WIDTH} tooltip={<>Set default editor option for all users of this data source. {docsTip()}} interactive={true} - disabled={options.readOnly} + disabled={optionsWithDefaults.readOnly} > o.value === options.jsonData.prometheusType)} - onChange={onChangeHandler('prometheusType', options, onOptionsChange)} + value={prometheusFlavorSelectItems.find( + (o) => o.value === optionsWithDefaults.jsonData.prometheusType + )} + onChange={onChangeHandler('prometheusType', optionsWithDefaults, onOptionsChange)} width={40} data-testid={selectors.components.DataSource.Prometheus.configPage.prometheusType} /> @@ -259,27 +269,27 @@ export const PromSettings = (props: Props) => {
- {options.jsonData.prometheusType && ( + {optionsWithDefaults.jsonData.prometheusType && (
- Use this to set the version of your {options.jsonData.prometheusType} instance if it is not - automatically configured. {docsTip()} + Use this to set the version of your {optionsWithDefaults.jsonData.prometheusType} instance if it + is not automatically configured. {docsTip()} } interactive={true} - disabled={options.readOnly} + disabled={optionsWithDefaults.readOnly} > o.value === options.jsonData.cacheLevel) ?? PrometheusCacheLevel.Low + cacheValueOptions.find((o) => o.value === optionsWithDefaults.jsonData.cacheLevel) ?? + PrometheusCacheLevel.Low } data-testid={selectors.components.DataSource.Prometheus.configPage.cacheLevel} /> @@ -328,13 +339,17 @@ export const PromSettings = (props: Props) => { } interactive={true} - disabled={options.readOnly} + disabled={optionsWithDefaults.readOnly} > <> @@ -372,10 +387,10 @@ export const PromSettings = (props: Props) => { } interactive={true} className={styles.switchField} - disabled={options.readOnly} + disabled={optionsWithDefaults.readOnly} > @@ -384,7 +399,7 @@ export const PromSettings = (props: Props) => {
- {options.jsonData.incrementalQuerying && ( + {optionsWithDefaults.jsonData.incrementalQuerying && ( { } interactive={true} - disabled={options.readOnly} + disabled={optionsWithDefaults.readOnly} > <> { }) } className="width-20" - value={options.jsonData.incrementalQueryOverlapWindow ?? defaultPrometheusQueryOverlapWindow} - onChange={onChangeHandler('incrementalQueryOverlapWindow', options, onOptionsChange)} + value={ + optionsWithDefaults.jsonData.incrementalQueryOverlapWindow ?? defaultPrometheusQueryOverlapWindow + } + onChange={onChangeHandler('incrementalQueryOverlapWindow', optionsWithDefaults, onOptionsChange)} spellCheck={false} data-testid={selectors.components.DataSource.Prometheus.configPage.queryOverlapWindow} /> @@ -425,10 +442,10 @@ export const PromSettings = (props: Props) => { tooltip={<>This feature will disable recording rules Turn this on to improve dashboard performance} interactive={true} className={styles.switchField} - disabled={options.readOnly} + disabled={optionsWithDefaults.readOnly} > @@ -452,12 +469,12 @@ export const PromSettings = (props: Props) => { } interactive={true} - disabled={options.readOnly} + disabled={optionsWithDefaults.readOnly} > { } interactive={true} label="HTTP method" - disabled={options.readOnly} + disabled={optionsWithDefaults.readOnly} >