mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Transformers: Support adding the row index using calculate field transformer (#65148)
This commit is contained in:
@@ -222,6 +222,22 @@ describe('calculateField transformer w/ timeseries', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('can add index field', async () => {
|
||||
const cfg = {
|
||||
id: DataTransformerID.calculateField,
|
||||
options: {
|
||||
mode: CalculateFieldMode.Index,
|
||||
replaceFields: true,
|
||||
},
|
||||
};
|
||||
|
||||
await expect(transformDataFrame([cfg], [seriesBC])).toEmitValuesWith((received) => {
|
||||
const data = received[0][0];
|
||||
expect(data.fields.length).toEqual(1);
|
||||
expect(data.fields[0].values.toArray()).toEqual([0, 1]);
|
||||
});
|
||||
});
|
||||
|
||||
it('uses template variable substituion', async () => {
|
||||
const cfg = {
|
||||
id: DataTransformerID.calculateField,
|
||||
|
||||
@@ -5,7 +5,7 @@ import { getTimeField } from '../../dataframe/processDataFrame';
|
||||
import { getFieldDisplayName } from '../../field';
|
||||
import { DataFrame, DataTransformerInfo, Field, FieldType, NullValueMode, Vector } from '../../types';
|
||||
import { BinaryOperationID, binaryOperators } from '../../utils/binaryOperators';
|
||||
import { ArrayVector, BinaryOperationVector, ConstantVector } from '../../vector';
|
||||
import { ArrayVector, BinaryOperationVector, ConstantVector, IndexVector } from '../../vector';
|
||||
import { AsNumberVector } from '../../vector/AsNumberVector';
|
||||
import { RowVector } from '../../vector/RowVector';
|
||||
import { doStandardCalcs, fieldReducers, ReducerID } from '../fieldReducer';
|
||||
@@ -19,6 +19,7 @@ import { noopTransformer } from './noop';
|
||||
export enum CalculateFieldMode {
|
||||
ReduceRow = 'reduceRow',
|
||||
BinaryOperation = 'binary',
|
||||
Index = 'index',
|
||||
}
|
||||
|
||||
export interface ReduceOptions {
|
||||
@@ -98,6 +99,22 @@ export const calculateFieldTransformer: DataTransformerInfo<CalculateFieldTransf
|
||||
};
|
||||
|
||||
creator = getBinaryCreator(defaults(binaryOptions, defaultBinaryOptions), data);
|
||||
} else if (mode === CalculateFieldMode.Index) {
|
||||
return data.map((frame) => {
|
||||
const f = {
|
||||
name: options.alias ?? 'Row',
|
||||
type: FieldType.number,
|
||||
values: new IndexVector(frame.length),
|
||||
config: {
|
||||
min: 0,
|
||||
max: frame.length - 1,
|
||||
},
|
||||
};
|
||||
return {
|
||||
...frame,
|
||||
fields: options.replaceFields ? [f] : [...frame.fields, f],
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// Nothing configured
|
||||
@@ -235,16 +252,21 @@ export function getNameFromOptions(options: CalculateFieldTransformerOptions) {
|
||||
return options.alias;
|
||||
}
|
||||
|
||||
if (options.mode === CalculateFieldMode.BinaryOperation) {
|
||||
const { binary } = options;
|
||||
return `${binary?.left ?? ''} ${binary?.operator ?? ''} ${binary?.right ?? ''}`;
|
||||
}
|
||||
|
||||
if (options.mode === CalculateFieldMode.ReduceRow) {
|
||||
const r = fieldReducers.getIfExists(options.reduce?.reducer);
|
||||
if (r) {
|
||||
return r.name;
|
||||
switch (options.mode) {
|
||||
case CalculateFieldMode.BinaryOperation: {
|
||||
const { binary } = options;
|
||||
return `${binary?.left ?? ''} ${binary?.operator ?? ''} ${binary?.right ?? ''}`;
|
||||
}
|
||||
case CalculateFieldMode.ReduceRow:
|
||||
{
|
||||
const r = fieldReducers.getIfExists(options.reduce?.reducer);
|
||||
if (r) {
|
||||
return r.name;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CalculateFieldMode.Index:
|
||||
return 'Row';
|
||||
}
|
||||
|
||||
return 'math';
|
||||
|
||||
Reference in New Issue
Block a user