diff --git a/src/utils/frame.test.ts b/src/utils/frame.test.ts index 32b6282..d5c6264 100644 --- a/src/utils/frame.test.ts +++ b/src/utils/frame.test.ts @@ -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'), @@ -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]); + }); + }); }); diff --git a/src/utils/frame.ts b/src/utils/frame.ts index 9d9b722..71c793a 100644 --- a/src/utils/frame.ts +++ b/src/utils/frame.ts @@ -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'; @@ -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); }; @@ -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) => ({ diff --git a/src/utils/variable.ts b/src/utils/variable.ts index 7d9b7b5..b084abc 100644 --- a/src/utils/variable.ts +++ b/src/utils/variable.ts @@ -1,4 +1,4 @@ -import { ArrayVector, DataFrame, FieldType, ScopedVars } from '@grafana/data'; +import { DataFrame, FieldType, ScopedVars } from '@grafana/data'; import { getTemplateSrv } from '@grafana/runtime'; /** @@ -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;