2019-10-31 04:48:05 -05:00
|
|
|
import { colors } from '@grafana/ui';
|
2019-09-03 10:24:22 -05:00
|
|
|
import {
|
2019-10-31 04:48:05 -05:00
|
|
|
getFlotPairs,
|
|
|
|
getColorFromHexRgbOrName,
|
|
|
|
getDisplayProcessor,
|
2019-09-03 10:24:22 -05:00
|
|
|
NullValueMode,
|
|
|
|
reduceField,
|
|
|
|
FieldType,
|
|
|
|
DisplayValue,
|
|
|
|
GraphSeriesXY,
|
|
|
|
getTimeField,
|
|
|
|
DataFrame,
|
2019-11-07 05:37:46 -06:00
|
|
|
FieldDisplayOptions,
|
|
|
|
getSeriesTimeStep,
|
|
|
|
TimeZone,
|
|
|
|
hasMsResolution,
|
|
|
|
MS_DATE_TIME_FORMAT,
|
|
|
|
DEFAULT_DATE_TIME_FORMAT,
|
2019-09-03 10:24:22 -05:00
|
|
|
} from '@grafana/data';
|
2019-08-06 03:49:54 -05:00
|
|
|
|
2019-04-24 03:14:18 -05:00
|
|
|
import { SeriesOptions, GraphOptions } from './types';
|
|
|
|
import { GraphLegendEditorLegendOptions } from './GraphLegendEditor';
|
|
|
|
|
|
|
|
export const getGraphSeriesModel = (
|
2019-09-03 10:24:22 -05:00
|
|
|
dataFrames: DataFrame[],
|
2019-11-07 05:37:46 -06:00
|
|
|
timeZone: TimeZone,
|
2019-04-24 03:14:18 -05:00
|
|
|
seriesOptions: SeriesOptions,
|
|
|
|
graphOptions: GraphOptions,
|
2019-11-07 05:37:46 -06:00
|
|
|
legendOptions: GraphLegendEditorLegendOptions,
|
|
|
|
fieldOptions?: FieldDisplayOptions
|
2019-04-24 03:14:18 -05:00
|
|
|
) => {
|
|
|
|
const graphs: GraphSeriesXY[] = [];
|
|
|
|
|
|
|
|
const displayProcessor = getDisplayProcessor({
|
2019-09-13 09:38:21 -05:00
|
|
|
config: {
|
2019-12-04 02:08:07 -06:00
|
|
|
unit: fieldOptions?.defaults?.unit,
|
2019-05-04 03:08:48 -05:00
|
|
|
decimals: legendOptions.decimals,
|
|
|
|
},
|
2019-04-24 03:14:18 -05:00
|
|
|
});
|
|
|
|
|
2019-11-07 05:37:46 -06:00
|
|
|
let fieldColumnIndex = -1;
|
2019-09-03 10:24:22 -05:00
|
|
|
for (const series of dataFrames) {
|
2019-09-01 07:44:22 -05:00
|
|
|
const { timeField } = getTimeField(series);
|
|
|
|
if (!timeField) {
|
2019-04-24 03:14:18 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-09-01 07:44:22 -05:00
|
|
|
for (const field of series.fields) {
|
|
|
|
if (field.type !== FieldType.number) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-07 05:37:46 -06:00
|
|
|
// Storing index of series field for future inspection
|
|
|
|
fieldColumnIndex++;
|
2019-09-01 07:44:22 -05:00
|
|
|
|
2019-04-30 11:21:22 -05:00
|
|
|
// Use external calculator just to make sure it works :)
|
|
|
|
const points = getFlotPairs({
|
2019-09-01 07:44:22 -05:00
|
|
|
xField: timeField,
|
2019-08-15 11:18:51 -05:00
|
|
|
yField: field,
|
2019-04-30 11:21:22 -05:00
|
|
|
nullValueMode: NullValueMode.Null,
|
|
|
|
});
|
2019-04-24 03:14:18 -05:00
|
|
|
|
2019-04-30 11:21:22 -05:00
|
|
|
if (points.length > 0) {
|
2019-08-15 11:18:51 -05:00
|
|
|
const seriesStats = reduceField({ field, reducers: legendOptions.stats });
|
2019-05-03 15:16:33 -05:00
|
|
|
let statsDisplayValues: DisplayValue[];
|
2019-04-24 03:14:18 -05:00
|
|
|
|
2019-04-30 11:21:22 -05:00
|
|
|
if (legendOptions.stats) {
|
|
|
|
statsDisplayValues = legendOptions.stats.map<DisplayValue>(stat => {
|
|
|
|
const statDisplayValue = displayProcessor(seriesStats[stat]);
|
2019-04-24 03:14:18 -05:00
|
|
|
|
2019-04-30 11:21:22 -05:00
|
|
|
return {
|
|
|
|
...statDisplayValue,
|
|
|
|
title: stat,
|
|
|
|
};
|
2019-04-24 03:14:18 -05:00
|
|
|
});
|
|
|
|
}
|
2019-04-30 11:21:22 -05:00
|
|
|
|
2019-11-07 07:58:50 -06:00
|
|
|
let seriesColor;
|
|
|
|
if (seriesOptions[field.name] && seriesOptions[field.name].color) {
|
|
|
|
// Case when panel has settings provided via SeriesOptions, i.e. graph panel
|
|
|
|
seriesColor = getColorFromHexRgbOrName(seriesOptions[field.name].color);
|
|
|
|
} else if (field.config && field.config.color) {
|
|
|
|
// Case when color settings are set on field, i.e. Explore logs histogram (see makeSeriesForLogs)
|
|
|
|
seriesColor = field.config.color;
|
|
|
|
} else {
|
|
|
|
seriesColor = colors[graphs.length % colors.length];
|
|
|
|
}
|
2019-04-30 11:21:22 -05:00
|
|
|
|
2019-11-07 05:37:46 -06:00
|
|
|
field.config = fieldOptions
|
|
|
|
? {
|
|
|
|
...field.config,
|
|
|
|
unit: fieldOptions.defaults.unit,
|
|
|
|
decimals: fieldOptions.defaults.decimals,
|
|
|
|
color: seriesColor,
|
|
|
|
}
|
2019-11-07 07:58:50 -06:00
|
|
|
: { ...field.config, color: seriesColor };
|
2019-11-07 05:37:46 -06:00
|
|
|
|
|
|
|
field.display = getDisplayProcessor({ config: { ...field.config }, type: field.type });
|
|
|
|
|
|
|
|
// Time step is used to determine bars width when graph is rendered as bar chart
|
|
|
|
const timeStep = getSeriesTimeStep(timeField);
|
|
|
|
const useMsDateFormat = hasMsResolution(timeField);
|
|
|
|
|
|
|
|
timeField.display = getDisplayProcessor({
|
|
|
|
type: timeField.type,
|
|
|
|
isUtc: timeZone === 'utc',
|
|
|
|
config: {
|
2019-12-04 02:08:07 -06:00
|
|
|
unit: `time:${useMsDateFormat ? MS_DATE_TIME_FORMAT : DEFAULT_DATE_TIME_FORMAT}`,
|
2019-11-07 05:37:46 -06:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-04-30 11:21:22 -05:00
|
|
|
graphs.push({
|
|
|
|
label: field.name,
|
|
|
|
data: points,
|
|
|
|
color: seriesColor,
|
|
|
|
info: statsDisplayValues,
|
|
|
|
isVisible: true,
|
2019-08-13 00:32:43 -05:00
|
|
|
yAxis: {
|
|
|
|
index: (seriesOptions[field.name] && seriesOptions[field.name].yAxis) || 1,
|
|
|
|
},
|
2019-11-07 05:37:46 -06:00
|
|
|
// This index is used later on to retrieve appropriate series/time for X and Y axes
|
|
|
|
seriesIndex: fieldColumnIndex,
|
|
|
|
timeField: { ...timeField },
|
|
|
|
valueField: { ...field },
|
|
|
|
timeStep,
|
2019-04-30 11:21:22 -05:00
|
|
|
});
|
2019-04-24 03:14:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return graphs;
|
|
|
|
};
|