mirror of
https://github.com/grafana/grafana.git
synced 2024-11-30 12:44:10 -06:00
Transforms: Keep refId for labelsTopFields (#61916)
This commit is contained in:
parent
18465bc04f
commit
a3d93febd7
@ -1,3 +1,5 @@
|
||||
import { Subscription } from 'rxjs';
|
||||
|
||||
import { toDataFrame, toDataFrameDTO } from '../../dataframe';
|
||||
import { DataFrame, DataTransformerConfig, FieldDTO, FieldType } from '../../types';
|
||||
import { mockTransformationsRegistry } from '../../utils/tests/mockTransformationsRegistry';
|
||||
@ -6,11 +8,73 @@ import { transformDataFrame } from '../transformDataFrame';
|
||||
import { DataTransformerID } from './ids';
|
||||
import { LabelsToFieldsMode, LabelsToFieldsOptions, labelsToFieldsTransformer } from './labelsToFields';
|
||||
|
||||
function labelsToFieldTransform(source: DataFrame[]): Promise<DataFrame[]> {
|
||||
const cfg: DataTransformerConfig<LabelsToFieldsOptions> = {
|
||||
id: DataTransformerID.labelsToFields,
|
||||
options: {
|
||||
mode: LabelsToFieldsMode.Rows,
|
||||
},
|
||||
};
|
||||
|
||||
const observable = transformDataFrame([cfg], source);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const subscription = new Subscription();
|
||||
|
||||
subscription.add(
|
||||
observable.subscribe({
|
||||
next: (value) => {
|
||||
subscription.unsubscribe();
|
||||
resolve(JSON.parse(JSON.stringify(value)));
|
||||
},
|
||||
error: (err) => {
|
||||
subscription.unsubscribe();
|
||||
reject(err);
|
||||
},
|
||||
})
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
describe('Labels as Columns', () => {
|
||||
beforeAll(() => {
|
||||
mockTransformationsRegistry([labelsToFieldsTransformer]);
|
||||
});
|
||||
|
||||
it('transform keep the refId of dataFrames', async () => {
|
||||
const input = [
|
||||
toDataFrame({
|
||||
refId: 'the-ref-id-A',
|
||||
fields: [
|
||||
{ name: 'time', type: FieldType.time, values: [1000, 2000] },
|
||||
{ name: 'Value', type: FieldType.number, values: [1, 2], labels: { labelA: 'valueA', labelB: 'valueB' } },
|
||||
],
|
||||
}),
|
||||
toDataFrame({
|
||||
refId: 'the-ref-id-B',
|
||||
fields: [
|
||||
{ name: 'time', type: FieldType.time, values: [1000, 2000] },
|
||||
{ name: 'Value', type: FieldType.number, values: [1, 2], labels: { labelA: 'valueA', labelB: 'valueB' } },
|
||||
],
|
||||
}),
|
||||
];
|
||||
|
||||
const output = await labelsToFieldTransform(input);
|
||||
|
||||
const expectedOutput = [
|
||||
{
|
||||
refId: 'the-ref-id-A',
|
||||
},
|
||||
{
|
||||
refId: 'the-ref-id-B',
|
||||
},
|
||||
];
|
||||
|
||||
for (let i = 0; i < output.length; i++) {
|
||||
expect(output[i]).toMatchObject(expectedOutput[i]);
|
||||
}
|
||||
});
|
||||
|
||||
it('data frame with two labels', async () => {
|
||||
const cfg: DataTransformerConfig<LabelsToFieldsOptions> = {
|
||||
id: DataTransformerID.labelsToFields,
|
||||
|
@ -90,6 +90,7 @@ export const labelsToFieldsTransformer: SynchronousDataTransformerInfo<LabelsToF
|
||||
}
|
||||
|
||||
result.push({
|
||||
...frame,
|
||||
fields: newFields,
|
||||
length: frame.length,
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user