mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
DataFrame: Fixes to dealing with empty results (#19119)
* DataFrame: Fixes to dealing with empty results * review feedback fixes
This commit is contained in:
@@ -106,7 +106,7 @@ describe('toDataFrame', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('SerisData backwards compatibility', () => {
|
describe('SerisData backwards compatibility', () => {
|
||||||
it('converts TimeSeries to series and back again', () => {
|
it('can convert TimeSeries to series and back again', () => {
|
||||||
const timeseries = {
|
const timeseries = {
|
||||||
target: 'Field Name',
|
target: 'Field Name',
|
||||||
datapoints: [[100, 1], [200, 2]],
|
datapoints: [[100, 1], [200, 2]],
|
||||||
@@ -120,6 +120,17 @@ describe('SerisData backwards compatibility', () => {
|
|||||||
expect(roundtrip.target).toBe(timeseries.target);
|
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', () => {
|
it('converts TableData to series and back again', () => {
|
||||||
const table = {
|
const table = {
|
||||||
columns: [{ text: 'a', unit: 'ms' }, { text: 'b', unit: 'zz' }, { text: 'c', unit: 'yy' }],
|
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);
|
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 = {
|
const json: DataFrameDTO = {
|
||||||
refId: 'Z',
|
refId: 'Z',
|
||||||
meta: {
|
meta: {
|
||||||
|
|||||||
@@ -32,12 +32,13 @@ function convertTableToDataFrame(table: TableData): DataFrame {
|
|||||||
type: FieldType.other,
|
type: FieldType.other,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
// Fill in the field values
|
|
||||||
for (const row of table.rows) {
|
for (const row of table.rows) {
|
||||||
for (let i = 0; i < fields.length; i++) {
|
for (let i = 0; i < fields.length; i++) {
|
||||||
fields[i].values.buffer.push(row[i]);
|
fields[i].values.buffer.push(row[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const f of fields) {
|
for (const f of fields) {
|
||||||
const t = guessFieldTypeForField(f);
|
const t = guessFieldTypeForField(f);
|
||||||
if (t) {
|
if (t) {
|
||||||
@@ -50,7 +51,7 @@ function convertTableToDataFrame(table: TableData): DataFrame {
|
|||||||
refId: table.refId,
|
refId: table.refId,
|
||||||
meta: table.meta,
|
meta: table.meta,
|
||||||
name: table.name,
|
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 => {
|
export const toLegacyResponseData = (frame: DataFrame): TimeSeries | TableData => {
|
||||||
const { fields } = frame;
|
const { fields } = frame;
|
||||||
|
|
||||||
const length = fields[0].values.length;
|
const rowCount = frame.length;
|
||||||
const rows: any[][] = [];
|
const rows: any[][] = [];
|
||||||
for (let i = 0; i < length; i++) {
|
|
||||||
|
for (let i = 0; i < rowCount; i++) {
|
||||||
const row: any[] = [];
|
const row: any[] = [];
|
||||||
for (let j = 0; j < fields.length; j++) {
|
for (let j = 0; j < fields.length; j++) {
|
||||||
row.push(fields[j].values.get(i));
|
row.push(fields[j].values.get(i));
|
||||||
|
|||||||
@@ -38,9 +38,11 @@ const testContext = (options: any = {}) => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const emptyTable = toDataFrame({ name: 'empty-table', refId: 'A', fields: [] });
|
||||||
|
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
mode: ExploreMode.Metrics,
|
mode: ExploreMode.Metrics,
|
||||||
dataFrames: [timeSeries, table],
|
dataFrames: [timeSeries, table, emptyTable],
|
||||||
graphResult: [] as TimeSeries[],
|
graphResult: [] as TimeSeries[],
|
||||||
tableResult: new TableModel(),
|
tableResult: new TableModel(),
|
||||||
logsResult: { hasUniqueLabels: false, rows: [] as LogRowModel[] },
|
logsResult: { hasUniqueLabels: false, rows: [] as LogRowModel[] },
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ export class ResultProcessor {
|
|||||||
const tables = onlyTables.map(frame => {
|
const tables = onlyTables.map(frame => {
|
||||||
const { fields } = frame;
|
const { fields } = frame;
|
||||||
const fieldCount = fields.length;
|
const fieldCount = fields.length;
|
||||||
const rowCount = fields[0].values.length;
|
const rowCount = frame.length;
|
||||||
|
|
||||||
const columns = fields.map(field => ({
|
const columns = fields.map(field => ({
|
||||||
text: field.name,
|
text: field.name,
|
||||||
|
|||||||
Reference in New Issue
Block a user