Skip to content

Commit

Permalink
Prepare for Grafana 11 (#67)
Browse files Browse the repository at this point in the history
* ft: removed MutableDataFrame, ArrayVector, toArray

* updated test cases for convertToDataFrame
  • Loading branch information
vitPinchuk committed Apr 22, 2024
1 parent 6c0df95 commit 5c20a60
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 21 deletions.
115 changes: 114 additions & 1 deletion src/utils/frame.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FieldType } from '@grafana/data';

import { prepareModel } from './frame';
import { convertToDataFrame, prepareModel } from './frame';

jest.mock('uuid', () => ({
v4: jest.fn(() => '123456'),
Expand Down Expand Up @@ -45,4 +45,117 @@ describe('Frame Utils', () => {
expect(prepareModel(dataFrame).rows).toEqual([]);
});
});

/**
* convertToDataFrame
*/
describe('convertToDataFrame', () => {
it('should map rows and verify field values', () => {
/**
* Model
*/
const model = {
meta: {},
fields: [
{
name: 'Field',
type: 'string',
id: '053c4d18-9954-4fea-b8ef-83eb2da95147',
},
{
name: 'value',
type: 'number',
id: '5a544f7c-27ff-4042-bcb7-2873e1eef6c3',
},
],
rows: [
{
value: ['Test 1', 10],
id: '12380cd9-9ddf-4a26-bdae-87bfcaf590e4',
},
{
value: ['Test 2', 151],
id: 'df1f7a2b-2fc4-40a4-a88b-d661079131bb',
},
],
};

const frame = convertToDataFrame(model as any);

/**
* Should return correct values
*/
expect(frame.fields[0].values).toEqual(['Test 1', 'Test 2']);
expect(frame.fields[1].values).toEqual([10, 151]);
});

it('should handle fields without values', () => {
/**
* Model
*/
const model = {
meta: {},
fields: [
{
name: 'Field',
type: 'string',
id: '053c4d18-9954-4fea-b8ef-83eb2da95147',
},
{
name: 'value',
type: 'number',
id: '5a544f7c-27ff-4042-bcb7-2873e1eef6c3',
},
],
rows: [],
};

const frame = convertToDataFrame(model as any);

/**
* Should return correct values
*/
expect(frame.fields[0].values).toEqual([]);
expect(frame.fields[1].values).toEqual([]);
});

it('should handle fields if types different', () => {
/**
* Model
*/
const model = {
meta: {},
fields: [
{
name: 'Field',
type: 'string',
id: '053c4d18-9954-4fea-b8ef-83eb2da95147',
},
{
name: 'value',
type: 'boolean',
id: '5a544f7c-27ff-4042-bcb7-2873e1eef6c3',
},
],
rows: [
{
value: ['Test 1', 10],
id: '12380cd9-9ddf-4a26-bdae-87bfcaf590e4',
},
{
value: ['Test 2', 151],
id: 'df1f7a2b-2fc4-40a4-a88b-d661079131bb',
},
],
};

const frame = convertToDataFrame(model as any);

/**
* Should return correct values
*/
expect(frame.fields[0].values).toEqual(['Test 1', 'Test 2']);
expect(frame.fields[1].values).toEqual([null, null]);
});
});
});
30 changes: 14 additions & 16 deletions src/utils/frame.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataFrameDTO, FieldType, MutableDataFrame, toDataFrameDTO } from '@grafana/data';
import { createDataFrame, DataFrameDTO, FieldType, toDataFrameDTO } from '@grafana/data';
import { v4 as uuidv4 } from 'uuid';

import { DataFrameModel, ModelField, ModelRows, NullableString } from '../types';
Expand All @@ -11,26 +11,24 @@ export const convertToDataFrame = (model: DataFrameModel): DataFrameDTO => {
/**
* Create Frame
*/
const frame = new MutableDataFrame({
name: model.name,
meta: {
preferredVisualisationType: model.meta?.preferredVisualisationType,
custom: model.meta?.custom,
},
fields: model.fields.map((field) => ({ name: field.name, type: field.type })),
});
let frame = createDataFrame(model);

/**
* Verify fields
*/
model.rows.forEach((row) =>
frame.appendRow(
row.value.map((field, i) => {
return verifyFieldValue(field, frame.fields[i].type).value;
})
)

const fields = frame.fields.map((field, fieldIndex) =>
model.rows.map((row) => verifyFieldValue(row.value[fieldIndex], field.type).value)
);

frame = {
...frame,
fields: frame.fields.map((field, fieldIndex) => ({
...field,
values: fields[fieldIndex],
})),
};

return toDataFrameDTO(frame);
};

Expand All @@ -46,7 +44,7 @@ export const prepareModel = (frame: DataFrameDTO): DataFrameModel => {
*/
if (frame.fields.length !== 0) {
fields = frame.fields.map(
(field) => ({ name: field.name, type: field.type ?? FieldType.string, id: uuidv4() } as ModelField)
(field) => ({ name: field.name, type: field.type ?? FieldType.string, id: uuidv4() }) as ModelField
);

rows = Array.from({ length: frame.fields[0].values?.length ?? 0 }).map((row, i) => ({
Expand Down
6 changes: 2 additions & 4 deletions src/utils/variable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArrayVector, DataFrame, FieldType, ScopedVars } from '@grafana/data';
import { DataFrame, FieldType, ScopedVars } from '@grafana/data';
import { getTemplateSrv } from '@grafana/runtime';

/**
Expand All @@ -12,9 +12,7 @@ export const interpolateVariables = (frame: DataFrame, scopedVars: ScopedVars) =
* Update String fields
*/
if (field.type === FieldType.string) {
field.values = new ArrayVector(
field.values.toArray().map((value) => getTemplateSrv().replace(value, scopedVars, 'csv'))
);
field.values = field.values.map((value) => getTemplateSrv().replace(value, scopedVars, 'csv'));
}

frame.fields[i] = field;
Expand Down

0 comments on commit 5c20a60

Please sign in to comment.