mirror of
https://github.com/grafana/grafana.git
synced 2026-08-11 05:34:53 -05:00
Making the multi-query table transform the default table transform
This commit is contained in:
@@ -98,13 +98,15 @@ describe('when transforming time series table.', () => {
|
||||
|
||||
describe('table data sets', () => {
|
||||
describe('Table', () => {
|
||||
const transform = 'table';
|
||||
var panel = {
|
||||
transform: 'table',
|
||||
transform,
|
||||
};
|
||||
var time = new Date().getTime();
|
||||
var rawData = [
|
||||
|
||||
var nonTableData = [
|
||||
{
|
||||
type: 'table',
|
||||
type: 'foo',
|
||||
columns: [
|
||||
{ text: 'Time' },
|
||||
{ text: 'Label Key 1' },
|
||||
@@ -116,42 +118,6 @@ describe('when transforming time series table.', () => {
|
||||
}
|
||||
];
|
||||
|
||||
describe('getColumns', function() {
|
||||
it('should return data columns', function() {
|
||||
var columns = transformers['table'].getColumns(rawData);
|
||||
expect(columns[0].text).toBe('Time');
|
||||
expect(columns[1].text).toBe('Label Key 1');
|
||||
expect(columns[2].text).toBe('Value');
|
||||
});
|
||||
});
|
||||
|
||||
describe('transform', function() {
|
||||
beforeEach(() => {
|
||||
table = transformDataToTable(rawData, panel);
|
||||
});
|
||||
|
||||
it ('should return 3 columns', () => {
|
||||
expect(table.columns.length).toBe(3);
|
||||
expect(table.columns[0].text).toBe('Time');
|
||||
expect(table.columns[1].text).toBe('Label Key 1');
|
||||
expect(table.columns[2].text).toBe('Value');
|
||||
});
|
||||
|
||||
it ('should return 1 row', () => {
|
||||
expect(table.rows.length).toBe(1);
|
||||
expect(table.rows[0][0]).toBe(time);
|
||||
expect(table.rows[0][1]).toBe('Label Value 1');
|
||||
expect(table.rows[0][2]).toBe(42);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Multi-Query Table', () => {
|
||||
const transform = 'multiquery_table';
|
||||
var panel = {
|
||||
transform,
|
||||
};
|
||||
var time = new Date().getTime();
|
||||
var singleQueryData = [
|
||||
{
|
||||
type: 'table',
|
||||
@@ -223,7 +189,7 @@ describe('when transforming time series table.', () => {
|
||||
var columns = transformers[transform].getColumns(singleQueryData);
|
||||
expect(columns[0].text).toBe('Time');
|
||||
expect(columns[1].text).toBe('Label Key 1');
|
||||
expect(columns[2].text).toBe('Value A');
|
||||
expect(columns[2].text).toBe('Value #A');
|
||||
});
|
||||
|
||||
it('should return the union of data columns given a multiple queries', function() {
|
||||
@@ -231,8 +197,8 @@ describe('when transforming time series table.', () => {
|
||||
expect(columns[0].text).toBe('Time');
|
||||
expect(columns[1].text).toBe('Label Key 1');
|
||||
expect(columns[2].text).toBe('Label Key 2');
|
||||
expect(columns[3].text).toBe('Value A');
|
||||
expect(columns[4].text).toBe('Value B');
|
||||
expect(columns[3].text).toBe('Value #A');
|
||||
expect(columns[4].text).toBe('Value #B');
|
||||
});
|
||||
|
||||
it('should return the union of data columns given a multiple queries with different labels', function() {
|
||||
@@ -240,18 +206,22 @@ describe('when transforming time series table.', () => {
|
||||
expect(columns[0].text).toBe('Time');
|
||||
expect(columns[1].text).toBe('Label Key 1');
|
||||
expect(columns[2].text).toBe('Label Key 2');
|
||||
expect(columns[3].text).toBe('Value A');
|
||||
expect(columns[4].text).toBe('Value B');
|
||||
expect(columns[3].text).toBe('Value #A');
|
||||
expect(columns[4].text).toBe('Value #B');
|
||||
});
|
||||
});
|
||||
|
||||
describe('transform', function() {
|
||||
it ('should throw an error with non-table data', () => {
|
||||
expect(() => transformDataToTable(nonTableData, panel)).toThrow();
|
||||
});
|
||||
|
||||
it ('should return 3 columns for single queries', () => {
|
||||
table = transformDataToTable(singleQueryData, panel);
|
||||
expect(table.columns.length).toBe(3);
|
||||
expect(table.columns[0].text).toBe('Time');
|
||||
expect(table.columns[1].text).toBe('Label Key 1');
|
||||
expect(table.columns[2].text).toBe('Value A');
|
||||
expect(table.columns[2].text).toBe('Value #A');
|
||||
});
|
||||
|
||||
it ('should return the union of columns for multiple queries', () => {
|
||||
@@ -260,8 +230,8 @@ describe('when transforming time series table.', () => {
|
||||
expect(table.columns[0].text).toBe('Time');
|
||||
expect(table.columns[1].text).toBe('Label Key 1');
|
||||
expect(table.columns[2].text).toBe('Label Key 2');
|
||||
expect(table.columns[3].text).toBe('Value A');
|
||||
expect(table.columns[4].text).toBe('Value B');
|
||||
expect(table.columns[3].text).toBe('Value #A');
|
||||
expect(table.columns[4].text).toBe('Value #B');
|
||||
});
|
||||
|
||||
it ('should return 1 row for a single query', () => {
|
||||
|
||||
@@ -135,24 +135,7 @@ transformers['table'] = {
|
||||
if (!data || data.length === 0) {
|
||||
return [];
|
||||
}
|
||||
return data[0].columns;
|
||||
},
|
||||
transform: function(data, panel, model) {
|
||||
if (!data || data.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (data[0].type !== 'table') {
|
||||
throw {message: 'Query result is not in table format, try using another transform.'};
|
||||
}
|
||||
model.columns = data[0].columns;
|
||||
model.rows = data[0].rows;
|
||||
}
|
||||
};
|
||||
|
||||
transformers['multiquery_table'] = {
|
||||
description: 'Multi-Query Table',
|
||||
getColumns: function(data) {
|
||||
// Track column indexes: name -> index
|
||||
const columnNames = {};
|
||||
|
||||
@@ -173,7 +156,7 @@ transformers['multiquery_table'] = {
|
||||
// Append one value column per data set
|
||||
data.forEach((_, i) => {
|
||||
// Value (A), Value (B),...
|
||||
const text = `Value ${String.fromCharCode(65 + i)}`;
|
||||
const text = `Value #${String.fromCharCode(65 + i)}`;
|
||||
columnNames[text] = columns.length;
|
||||
columns.push({ text });
|
||||
});
|
||||
@@ -185,8 +168,9 @@ transformers['multiquery_table'] = {
|
||||
return;
|
||||
}
|
||||
|
||||
if (data[0].type !== 'table') {
|
||||
throw {message: 'Query result is not in table format, try using another transform.'};
|
||||
const noTableIndex = _.findIndex(data, d => d.type !== 'table');
|
||||
if (noTableIndex > -1) {
|
||||
throw {message: `Result of query #${String.fromCharCode(65 + noTableIndex)} is not in table format, try using another transform.`};
|
||||
}
|
||||
|
||||
// Track column indexes: name -> index
|
||||
@@ -213,8 +197,8 @@ transformers['multiquery_table'] = {
|
||||
|
||||
// Append one value column per data set
|
||||
data.forEach((_, i) => {
|
||||
// Value (A), Value (B),...
|
||||
const text = `Value ${String.fromCharCode(65 + i)}`;
|
||||
// Value #A, Value #B,...
|
||||
const text = `Value #${String.fromCharCode(65 + i)}`;
|
||||
columnNames[text] = columns.length;
|
||||
columns.push({ text });
|
||||
columnIndexes[i].push(columnNames[text]);
|
||||
|
||||
Reference in New Issue
Block a user