mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Transformations: support a synchronous transformation pattern (#37780)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { toDataFrame, ArrayVector, DataFrame, FieldType, toDataFrameDTO, DataFrameDTO } from '@grafana/data';
|
||||
import { prepareTimeSeries, PrepareTimeSeriesOptions, timeSeriesFormat } from './prepareTimeSeries';
|
||||
import { prepareTimeSeriesTransformer, PrepareTimeSeriesOptions, timeSeriesFormat } from './prepareTimeSeries';
|
||||
|
||||
describe('Prepair time series transformer', () => {
|
||||
it('should transform wide to many', () => {
|
||||
@@ -19,7 +19,7 @@ describe('Prepair time series transformer', () => {
|
||||
format: timeSeriesFormat.TimeSeriesMany,
|
||||
};
|
||||
|
||||
expect(prepareTimeSeries(source, config)).toEqual([
|
||||
expect(prepareTimeSeriesTransformer.transformer(config)(source)).toEqual([
|
||||
toEquableDataFrame({
|
||||
name: 'wide',
|
||||
refId: 'A',
|
||||
@@ -59,7 +59,7 @@ describe('Prepair time series transformer', () => {
|
||||
format: timeSeriesFormat.TimeSeriesMany,
|
||||
};
|
||||
|
||||
expect(prepareTimeSeries(source, config)).toEqual([
|
||||
expect(prepareTimeSeriesTransformer.transformer(config)(source)).toEqual([
|
||||
toEquableDataFrame({
|
||||
name: 'wide',
|
||||
refId: 'A',
|
||||
@@ -107,7 +107,7 @@ describe('Prepair time series transformer', () => {
|
||||
format: timeSeriesFormat.TimeSeriesMany,
|
||||
};
|
||||
|
||||
expect(prepareTimeSeries(source, config)).toEqual([
|
||||
expect(prepareTimeSeriesTransformer.transformer(config)(source)).toEqual([
|
||||
toEquableDataFrame({
|
||||
name: 'wide',
|
||||
refId: 'A',
|
||||
@@ -162,7 +162,9 @@ describe('Prepair time series transformer', () => {
|
||||
format: timeSeriesFormat.TimeSeriesMany,
|
||||
};
|
||||
|
||||
expect(toEquableDataFrames(prepareTimeSeries(source, config))).toEqual(toEquableDataFrames(source));
|
||||
expect(toEquableDataFrames(prepareTimeSeriesTransformer.transformer(config)(source))).toEqual(
|
||||
toEquableDataFrames(source)
|
||||
);
|
||||
});
|
||||
|
||||
it('should return empty array when no timeseries exist', () => {
|
||||
@@ -191,7 +193,7 @@ describe('Prepair time series transformer', () => {
|
||||
format: timeSeriesFormat.TimeSeriesMany,
|
||||
};
|
||||
|
||||
expect(prepareTimeSeries(source, config)).toEqual([]);
|
||||
expect(prepareTimeSeriesTransformer.transformer(config)(source)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {
|
||||
DataTransformerInfo,
|
||||
SynchronousDataTransformerInfo,
|
||||
DataFrame,
|
||||
FieldType,
|
||||
DataTransformerID,
|
||||
@@ -63,31 +63,30 @@ export function toTimeSeriesMany(data: DataFrame[]): DataFrame[] {
|
||||
return result;
|
||||
}
|
||||
|
||||
export function prepareTimeSeries(data: DataFrame[], options: PrepareTimeSeriesOptions): DataFrame[] {
|
||||
const format = options?.format ?? timeSeriesFormat.TimeSeriesWide;
|
||||
if (format === timeSeriesFormat.TimeSeriesMany) {
|
||||
return toTimeSeriesMany(data);
|
||||
}
|
||||
|
||||
// Join by the first frame
|
||||
const frame = outerJoinDataFrames({
|
||||
frames: data,
|
||||
joinBy: fieldMatchers.get(FieldMatcherID.firstTimeField).get({}),
|
||||
enforceSort: true,
|
||||
keepOriginIndices: true,
|
||||
});
|
||||
return frame ? [frame] : [];
|
||||
}
|
||||
|
||||
export const prepareTimeSeriesTransformer: DataTransformerInfo<PrepareTimeSeriesOptions> = {
|
||||
export const prepareTimeSeriesTransformer: SynchronousDataTransformerInfo<PrepareTimeSeriesOptions> = {
|
||||
id: DataTransformerID.prepareTimeSeries,
|
||||
name: 'Prepare time series',
|
||||
description: `Will stretch data frames from the wide format into the long format. This is really helpful to be able to keep backwards compatability for panels not supporting the new wide format.`,
|
||||
defaultOptions: {},
|
||||
|
||||
/**
|
||||
* Return a modified copy of the series. If the transform is not or should not
|
||||
* be applied, just return the input series
|
||||
*/
|
||||
operator: (options) => (source) => source.pipe(map((data) => prepareTimeSeries(data, options))),
|
||||
operator: (options) => (source) =>
|
||||
source.pipe(map((data) => prepareTimeSeriesTransformer.transformer(options)(data))),
|
||||
|
||||
transformer: (options: PrepareTimeSeriesOptions) => {
|
||||
const format = options?.format ?? timeSeriesFormat.TimeSeriesWide;
|
||||
if (format === timeSeriesFormat.TimeSeriesMany) {
|
||||
return toTimeSeriesMany;
|
||||
}
|
||||
|
||||
return (data: DataFrame[]) => {
|
||||
// Join by the first frame
|
||||
const frame = outerJoinDataFrames({
|
||||
frames: data,
|
||||
joinBy: fieldMatchers.get(FieldMatcherID.firstTimeField).get({}),
|
||||
enforceSort: true,
|
||||
keepOriginIndices: true,
|
||||
});
|
||||
return frame ? [frame] : [];
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user