DataFrame: Fixes to dealing with empty results (#19119)

* DataFrame: Fixes to dealing with empty results

* review feedback fixes
This commit is contained in:
Torkel Ödegaard
2019-09-16 13:02:26 +02:00
committed by GitHub
parent 9d0a076eb1
commit e968a2aa86
4 changed files with 33 additions and 8 deletions

View File

@@ -106,7 +106,7 @@ describe('toDataFrame', () => {
});
describe('SerisData backwards compatibility', () => {
it('converts TimeSeries to series and back again', () => {
it('can convert TimeSeries to series and back again', () => {
const timeseries = {
target: 'Field Name',
datapoints: [[100, 1], [200, 2]],
@@ -120,6 +120,17 @@ describe('SerisData backwards compatibility', () => {
expect(roundtrip.target).toBe(timeseries.target);
});
it('can convert empty table to DataFrame then back to legacy', () => {
const table = {
columns: [],
rows: [],
};
const series = toDataFrame(table);
const roundtrip = toLegacyResponseData(series) as TableData;
expect(roundtrip.columns.length).toBe(0);
});
it('converts TableData to series and back again', () => {
const table = {
columns: [{ text: 'a', unit: 'ms' }, { text: 'b', unit: 'zz' }, { text: 'c', unit: 'yy' }],
@@ -135,7 +146,17 @@ describe('SerisData backwards compatibility', () => {
expect(roundtrip).toMatchObject(table);
});
it('converts DataFrame to TableData to series and back again', () => {
it('can convert empty TableData to DataFrame', () => {
const table = {
columns: [],
rows: [],
};
const series = toDataFrame(table);
expect(series.fields.length).toBe(0);
});
it('can convert DataFrame to TableData to series and back again', () => {
const json: DataFrameDTO = {
refId: 'Z',
meta: {

View File

@@ -32,12 +32,13 @@ function convertTableToDataFrame(table: TableData): DataFrame {
type: FieldType.other,
};
});
// Fill in the field values
for (const row of table.rows) {
for (let i = 0; i < fields.length; i++) {
fields[i].values.buffer.push(row[i]);
}
}
for (const f of fields) {
const t = guessFieldTypeForField(f);
if (t) {
@@ -50,7 +51,7 @@ function convertTableToDataFrame(table: TableData): DataFrame {
refId: table.refId,
meta: table.meta,
name: table.name,
length: fields[0].values.length,
length: table.rows.length,
};
}
@@ -258,9 +259,10 @@ export const toDataFrame = (data: any): DataFrame => {
export const toLegacyResponseData = (frame: DataFrame): TimeSeries | TableData => {
const { fields } = frame;
const length = fields[0].values.length;
const rowCount = frame.length;
const rows: any[][] = [];
for (let i = 0; i < length; i++) {
for (let i = 0; i < rowCount; i++) {
const row: any[] = [];
for (let j = 0; j < fields.length; j++) {
row.push(fields[j].values.get(i));