2019-12-12 14:55:30 -08:00
|
|
|
import set from 'lodash/set';
|
2019-12-23 06:22:54 +01:00
|
|
|
import {
|
|
|
|
|
GrafanaTheme,
|
|
|
|
|
DynamicConfigValue,
|
|
|
|
|
FieldConfig,
|
|
|
|
|
InterpolateFunction,
|
|
|
|
|
DataFrame,
|
|
|
|
|
Field,
|
|
|
|
|
FieldType,
|
|
|
|
|
FieldConfigSource,
|
2019-12-28 17:32:58 -08:00
|
|
|
ThresholdsMode,
|
|
|
|
|
FieldColorMode,
|
|
|
|
|
ColorScheme,
|
2019-12-23 06:22:54 +01:00
|
|
|
} from '../types';
|
2019-12-12 14:55:30 -08:00
|
|
|
import { fieldMatchers, ReducerID, reduceField } from '../transformations';
|
|
|
|
|
import { FieldMatcher } from '../types/transformations';
|
|
|
|
|
import isNumber from 'lodash/isNumber';
|
|
|
|
|
import toNumber from 'lodash/toNumber';
|
|
|
|
|
import { getDisplayProcessor } from './displayProcessor';
|
2019-12-28 17:32:58 -08:00
|
|
|
import { guessFieldTypeForField } from '../dataframe';
|
2019-12-12 14:55:30 -08:00
|
|
|
|
|
|
|
|
interface OverrideProps {
|
|
|
|
|
match: FieldMatcher;
|
|
|
|
|
properties: DynamicConfigValue[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface GlobalMinMax {
|
|
|
|
|
min: number;
|
|
|
|
|
max: number;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-23 06:22:54 +01:00
|
|
|
export interface ApplyFieldOverrideOptions {
|
|
|
|
|
data?: DataFrame[];
|
|
|
|
|
fieldOptions: FieldConfigSource;
|
|
|
|
|
replaceVariables: InterpolateFunction;
|
|
|
|
|
theme: GrafanaTheme;
|
|
|
|
|
autoMinMax?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 14:55:30 -08:00
|
|
|
export function findNumericFieldMinMax(data: DataFrame[]): GlobalMinMax {
|
|
|
|
|
let min = Number.MAX_VALUE;
|
|
|
|
|
let max = Number.MIN_VALUE;
|
|
|
|
|
|
|
|
|
|
const reducers = [ReducerID.min, ReducerID.max];
|
|
|
|
|
for (const frame of data) {
|
|
|
|
|
for (const field of frame.fields) {
|
|
|
|
|
if (field.type === FieldType.number) {
|
|
|
|
|
const stats = reduceField({ field, reducers });
|
|
|
|
|
if (stats[ReducerID.min] < min) {
|
|
|
|
|
min = stats[ReducerID.min];
|
|
|
|
|
}
|
|
|
|
|
if (stats[ReducerID.max] > max) {
|
|
|
|
|
max = stats[ReducerID.max];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { min, max };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a copy of the DataFrame with all rules applied
|
|
|
|
|
*/
|
2019-12-23 06:22:54 +01:00
|
|
|
export function applyFieldOverrides(options: ApplyFieldOverrideOptions): DataFrame[] {
|
2019-12-13 08:36:49 -08:00
|
|
|
if (!options.data) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2019-12-23 06:22:54 +01:00
|
|
|
|
2019-12-13 08:36:49 -08:00
|
|
|
const source = options.fieldOptions;
|
2019-12-12 14:55:30 -08:00
|
|
|
if (!source) {
|
2019-12-13 08:36:49 -08:00
|
|
|
return options.data;
|
2019-12-12 14:55:30 -08:00
|
|
|
}
|
2019-12-23 06:22:54 +01:00
|
|
|
|
2019-12-12 14:55:30 -08:00
|
|
|
let range: GlobalMinMax | undefined = undefined;
|
|
|
|
|
|
|
|
|
|
// Prepare the Matchers
|
|
|
|
|
const override: OverrideProps[] = [];
|
|
|
|
|
if (source.overrides) {
|
|
|
|
|
for (const rule of source.overrides) {
|
|
|
|
|
const info = fieldMatchers.get(rule.matcher.id);
|
|
|
|
|
if (info) {
|
|
|
|
|
override.push({
|
2019-12-23 06:22:54 +01:00
|
|
|
match: info.get(rule.matcher.options),
|
2019-12-12 14:55:30 -08:00
|
|
|
properties: rule.properties,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-13 08:36:49 -08:00
|
|
|
return options.data.map((frame, index) => {
|
2019-12-12 14:55:30 -08:00
|
|
|
let name = frame.name;
|
|
|
|
|
if (!name) {
|
|
|
|
|
name = `Series[${index}]`;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-23 06:22:54 +01:00
|
|
|
const fields: Field[] = frame.fields.map(field => {
|
2019-12-12 14:55:30 -08:00
|
|
|
// Config is mutable within this scope
|
|
|
|
|
const config: FieldConfig = { ...field.config } || {};
|
|
|
|
|
if (field.type === FieldType.number) {
|
|
|
|
|
setFieldConfigDefaults(config, source.defaults);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find any matching rules and then override
|
|
|
|
|
for (const rule of override) {
|
|
|
|
|
if (rule.match(field)) {
|
|
|
|
|
for (const prop of rule.properties) {
|
|
|
|
|
setDynamicConfigValue(config, {
|
|
|
|
|
value: prop,
|
|
|
|
|
config,
|
|
|
|
|
field,
|
|
|
|
|
data: frame,
|
2019-12-13 08:36:49 -08:00
|
|
|
replaceVariables: options.replaceVariables,
|
2019-12-12 14:55:30 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-28 17:32:58 -08:00
|
|
|
// Try harder to set a real value that is not 'other'
|
|
|
|
|
let type = field.type;
|
|
|
|
|
if (!type || type === FieldType.other) {
|
|
|
|
|
const t = guessFieldTypeForField(field);
|
|
|
|
|
if (t) {
|
|
|
|
|
type = t;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Some units have an implied range
|
|
|
|
|
if (config.unit === 'percent') {
|
|
|
|
|
if (!isNumber(config.min)) {
|
|
|
|
|
config.min = 0;
|
|
|
|
|
}
|
|
|
|
|
if (!isNumber(config.max)) {
|
|
|
|
|
config.max = 100;
|
|
|
|
|
}
|
|
|
|
|
} else if (config.unit === 'percentunit') {
|
|
|
|
|
if (!isNumber(config.min)) {
|
|
|
|
|
config.min = 0;
|
|
|
|
|
}
|
|
|
|
|
if (!isNumber(config.max)) {
|
|
|
|
|
config.max = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 14:55:30 -08:00
|
|
|
// Set the Min/Max value automatically
|
2019-12-13 08:36:49 -08:00
|
|
|
if (options.autoMinMax && field.type === FieldType.number) {
|
2019-12-12 14:55:30 -08:00
|
|
|
if (!isNumber(config.min) || !isNumber(config.max)) {
|
|
|
|
|
if (!range) {
|
2019-12-13 08:36:49 -08:00
|
|
|
range = findNumericFieldMinMax(options.data!); // Global value
|
2019-12-12 14:55:30 -08:00
|
|
|
}
|
|
|
|
|
if (!isNumber(config.min)) {
|
|
|
|
|
config.min = range.min;
|
|
|
|
|
}
|
|
|
|
|
if (!isNumber(config.max)) {
|
|
|
|
|
config.max = range.max;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-28 17:32:58 -08:00
|
|
|
// Overwrite the configs
|
|
|
|
|
const f: Field = {
|
2019-12-12 14:55:30 -08:00
|
|
|
...field,
|
|
|
|
|
config,
|
2019-12-28 17:32:58 -08:00
|
|
|
type,
|
2019-12-12 14:55:30 -08:00
|
|
|
};
|
2019-12-28 17:32:58 -08:00
|
|
|
// and set the display processor using it
|
|
|
|
|
f.display = getDisplayProcessor({ field: f, theme: options.theme });
|
|
|
|
|
return f;
|
2019-12-12 14:55:30 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...frame,
|
|
|
|
|
fields,
|
|
|
|
|
name,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface DynamicConfigValueOptions {
|
|
|
|
|
value: DynamicConfigValue;
|
|
|
|
|
config: FieldConfig;
|
|
|
|
|
field: Field;
|
|
|
|
|
data: DataFrame;
|
|
|
|
|
replaceVariables: InterpolateFunction;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const numericFieldProps: any = {
|
|
|
|
|
decimals: true,
|
|
|
|
|
min: true,
|
|
|
|
|
max: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function prepareConfigValue(key: string, input: any, options?: DynamicConfigValueOptions): any {
|
|
|
|
|
if (options) {
|
|
|
|
|
// TODO template variables etc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (numericFieldProps[key]) {
|
|
|
|
|
const num = toNumber(input);
|
|
|
|
|
if (isNaN(num)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return num;
|
|
|
|
|
} else if (input) {
|
|
|
|
|
// skips empty string
|
|
|
|
|
if (key === 'unit' && input === 'none') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function setDynamicConfigValue(config: FieldConfig, options: DynamicConfigValueOptions) {
|
|
|
|
|
const { value } = options;
|
|
|
|
|
const v = prepareConfigValue(value.path, value.value, options);
|
|
|
|
|
set(config, value.path, v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* For numeric values, only valid numbers will be applied
|
|
|
|
|
* for units, 'none' will be skipped
|
|
|
|
|
*/
|
|
|
|
|
export function setFieldConfigDefaults(config: FieldConfig, props?: FieldConfig) {
|
|
|
|
|
if (props) {
|
|
|
|
|
const keys = Object.keys(props);
|
|
|
|
|
for (const key of keys) {
|
|
|
|
|
const val = prepareConfigValue(key, (props as any)[key]);
|
|
|
|
|
if (val === null || val === undefined) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
set(config, key, val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-28 17:32:58 -08:00
|
|
|
validateFieldConfig(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This checks that all options on FieldConfig make sense. It mutates any value that needs
|
|
|
|
|
* fixed. In particular this makes sure that the first threshold value is -Infinity (not valid in JSON)
|
|
|
|
|
*/
|
|
|
|
|
export function validateFieldConfig(config: FieldConfig) {
|
|
|
|
|
const { thresholds } = config;
|
|
|
|
|
if (thresholds) {
|
|
|
|
|
if (!thresholds.mode) {
|
|
|
|
|
thresholds.mode = ThresholdsMode.Absolute;
|
|
|
|
|
}
|
|
|
|
|
if (!thresholds.steps) {
|
|
|
|
|
thresholds.steps = [];
|
|
|
|
|
} else if (thresholds.steps.length) {
|
|
|
|
|
// First value is always -Infinity
|
|
|
|
|
// JSON saves it as null
|
|
|
|
|
thresholds.steps[0].value = -Infinity;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!config.color) {
|
|
|
|
|
if (thresholds) {
|
|
|
|
|
config.color = {
|
|
|
|
|
mode: FieldColorMode.Thresholds,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
// No Color settings
|
|
|
|
|
} else if (!config.color.mode) {
|
|
|
|
|
// Without a mode, skip color altogether
|
|
|
|
|
delete config.color;
|
|
|
|
|
} else {
|
|
|
|
|
const { color } = config;
|
|
|
|
|
if (color.mode === FieldColorMode.Scheme) {
|
|
|
|
|
if (!color.schemeName) {
|
|
|
|
|
color.schemeName = ColorScheme.BrBG;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
delete color.schemeName;
|
|
|
|
|
}
|
2019-12-12 14:55:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify that max > min (swap if necessary)
|
|
|
|
|
if (config.hasOwnProperty('min') && config.hasOwnProperty('max') && config.min! > config.max!) {
|
|
|
|
|
const tmp = config.max;
|
|
|
|
|
config.max = config.min;
|
|
|
|
|
config.min = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|