Transformations: Preserve base threshold when using Config from query results (#96781)

* Merge in default threshold base if exists

* Fix tests to not expect default threshold

* Add config from query example to gdev

* Add a test around the merging of thresholds

* Revert "Add config from query example to gdev"

This reverts commit a239224ebb.

* update gdev

* Add note about keeping the base threshold

* Update public/app/features/transformers/docs/content.ts

Co-authored-by: Isabel Matwawana <76437239+imatwawana@users.noreply.github.com>

* Merge in generated code

---------

Co-authored-by: Leon Sorokin <leeoniya@gmail.com>
Co-authored-by: Isabel Matwawana <76437239+imatwawana@users.noreply.github.com>
This commit is contained in:
Kristina 2024-11-21 12:46:29 -06:00 committed by GitHub
parent e9fae5bd7f
commit f9eb013334
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 869 additions and 552 deletions

File diff suppressed because it is too large Load Diff

View File

@ -286,6 +286,8 @@ In the field mapping specify:
Grafana builds value mappings from your query result and applies them to the real data query results. You should see values being mapped and colored according to the config query results.
> **Note:** When you use this transformation for thresholds, the visualization continues to use the panel's base threshold.
### Convert field type
Use this transformation to modify the field type of a specified field.

View File

@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`setFieldConfigDefaults applies the base threshold when one does not exist in the config 1`] = `
{
"mode": "absolute",
"steps": [
{
"color": "red",
"value": -Infinity,
},
{
"color": "green",
"value": 50,
},
],
}
`;

View File

@ -730,6 +730,33 @@ describe('setFieldConfigDefaults', () => {
}
`);
});
it('applies the base threshold when one does not exist in the config', () => {
const defaultConfig: FieldConfig = {
thresholds: {
mode: ThresholdsMode.Absolute,
steps: [{ value: -Infinity, color: 'red' }],
},
};
const config: FieldConfig = {
thresholds: {
mode: ThresholdsMode.Absolute,
steps: [{ value: 50, color: 'green' }],
},
};
const context: FieldOverrideEnv = {
data: [],
field: { type: FieldType.number } as Field,
dataFrameIndex: 0,
fieldConfigRegistry: customFieldRegistry,
};
setFieldConfigDefaults(config, defaultConfig, context);
expect(config.thresholds).toMatchSnapshot();
});
});
describe('setDynamicConfigValue', () => {

View File

@ -2,7 +2,7 @@ import { isNumber, set, unset, get, cloneDeep } from 'lodash';
import { useMemo, useRef } from 'react';
import usePrevious from 'react-use/lib/usePrevious';
import { VariableFormatID } from '@grafana/schema';
import { ThresholdsMode, VariableFormatID } from '@grafana/schema';
import { compareArrayValues, compareDataFrameStructures } from '../dataframe/frameComparisons';
import { guessFieldTypeForField } from '../dataframe/processDataFrame';
@ -347,6 +347,18 @@ export function setFieldConfigDefaults(config: FieldConfig, defaults: FieldConfi
// Combine the data source links and the panel default config links
config.links = [...config.links, ...defaults.links];
}
// if we have a base threshold set by default but not on the config, we need to merge it in
const defaultBaseStep =
defaults?.thresholds?.mode === ThresholdsMode.Absolute &&
defaults.thresholds?.steps.find((step) => step.value === -Infinity);
if (
config.thresholds?.mode === ThresholdsMode.Absolute &&
!config.thresholds.steps.some((step) => step.value === -Infinity) &&
defaultBaseStep
) {
config.thresholds.steps = [defaultBaseStep, ...config.thresholds.steps];
}
for (const fieldConfigProperty of context.fieldConfigRegistry.list()) {
if (fieldConfigProperty.isCustom && !config.custom) {
config.custom = {};

View File

@ -93,7 +93,7 @@ describe('config from data', () => {
const results = extractConfigFromQuery(options, [config, seriesA]);
expect(results.length).toBe(1);
const thresholdConfig = results[0].fields[1].config.thresholds?.steps[1];
const thresholdConfig = results[0].fields[1].config.thresholds?.steps[0];
expect(thresholdConfig).toBeDefined();
expect(thresholdConfig?.color).toBe('orange');
expect(thresholdConfig?.value).toBe(50);

View File

@ -179,6 +179,9 @@ In the field mapping specify:
| Color | Value mappings / Color | All values |
Grafana builds value mappings from your query result and applies them to the real data query results. You should see values being mapped and colored according to the config query results.
> **Note:** When you use this transformation for thresholds, the visualization continues to use the panel's base threshold.
`;
},
},
@ -1251,7 +1254,6 @@ Select this option to transform the time series data frame from the long format
| 2023-01-01 00:00:00 | 10 | 20 |
| 2023-01-01 01:00:00 | 15 | 25 |
> **Note:** This transformation is available in Grafana 7.5.10+ and Grafana 8.0.6+.
`;
},
links: [
@ -1453,7 +1455,6 @@ Here is the result after applying the Series to rows transformation.
This transformation facilitates the consolidation of results from multiple time series queries, providing a streamlined and unified dataset for efficient analysis and visualization in a tabular format.
> **Note:** This transformation is available in Grafana 7.1+.
`;
},
},

View File

@ -169,7 +169,7 @@ export const configMapHandlers: FieldToConfigMapHandler[] = [
if (!config.thresholds) {
config.thresholds = {
mode: ThresholdsMode.Absolute,
steps: [{ value: -Infinity, color: 'green' }],
steps: [],
};
}

View File

@ -111,7 +111,7 @@ describe('Rows to fields', () => {
});
const result = rowsToFields({}, input);
expect(result.fields[0].config.thresholds?.steps[1].value).toBe(30);
expect(result.fields[0].config.thresholds?.steps[0].value).toBe(30);
});
it('Will extract other string fields to labels', () => {