2017-12-21 01:39:31 -06:00
|
|
|
import _ from 'lodash';
|
2018-10-19 08:29:58 -05:00
|
|
|
import flatten from 'app/core/utils/flatten';
|
|
|
|
import TimeSeries from 'app/core/time_series2';
|
|
|
|
import TableModel, { mergeTablesIntoModel } from 'app/core/table_model';
|
2015-11-04 02:41:03 -06:00
|
|
|
|
2018-08-29 07:27:29 -05:00
|
|
|
const transformers = {};
|
2015-11-04 02:41:03 -06:00
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
transformers['timeseries_to_rows'] = {
|
|
|
|
description: 'Time series to rows',
|
2018-09-04 07:27:03 -05:00
|
|
|
getColumns: () => {
|
2015-11-13 10:36:11 -06:00
|
|
|
return [];
|
|
|
|
},
|
2018-09-04 07:27:03 -05:00
|
|
|
transform: (data, panel, model) => {
|
2017-12-21 01:39:31 -06:00
|
|
|
model.columns = [{ text: 'Time', type: 'date' }, { text: 'Metric' }, { text: 'Value' }];
|
2015-11-04 02:41:03 -06:00
|
|
|
|
2018-08-30 01:58:43 -05:00
|
|
|
for (let i = 0; i < data.length; i++) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const series = data[i];
|
2018-08-30 01:58:43 -05:00
|
|
|
for (let y = 0; y < series.datapoints.length; y++) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const dp = series.datapoints[y];
|
2015-11-05 06:13:13 -06:00
|
|
|
model.rows.push([dp[1], series.target, dp[0]]);
|
2015-11-04 02:41:03 -06:00
|
|
|
}
|
|
|
|
}
|
2017-12-21 01:39:31 -06:00
|
|
|
},
|
2015-11-04 02:41:03 -06:00
|
|
|
};
|
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
transformers['timeseries_to_columns'] = {
|
|
|
|
description: 'Time series to columns',
|
2018-09-04 07:27:03 -05:00
|
|
|
getColumns: () => {
|
2015-11-13 10:36:11 -06:00
|
|
|
return [];
|
|
|
|
},
|
2018-09-04 07:27:03 -05:00
|
|
|
transform: (data, panel, model) => {
|
2017-12-21 01:39:31 -06:00
|
|
|
model.columns.push({ text: 'Time', type: 'date' });
|
2015-11-04 02:41:03 -06:00
|
|
|
|
|
|
|
// group by time
|
2018-08-29 07:27:29 -05:00
|
|
|
const points = {};
|
2015-11-04 02:41:03 -06:00
|
|
|
|
2017-04-20 04:16:37 -05:00
|
|
|
for (let i = 0; i < data.length; i++) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const series = data[i];
|
2017-12-19 09:06:54 -06:00
|
|
|
model.columns.push({ text: series.target });
|
2015-11-04 02:41:03 -06:00
|
|
|
|
2018-08-30 01:58:43 -05:00
|
|
|
for (let y = 0; y < series.datapoints.length; y++) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const dp = series.datapoints[y];
|
|
|
|
const timeKey = dp[1].toString();
|
2015-11-04 02:41:03 -06:00
|
|
|
|
|
|
|
if (!points[timeKey]) {
|
2017-12-19 09:06:54 -06:00
|
|
|
points[timeKey] = { time: dp[1] };
|
2015-11-04 02:41:03 -06:00
|
|
|
points[timeKey][i] = dp[0];
|
2016-01-13 14:22:39 -06:00
|
|
|
} else {
|
2015-11-04 02:41:03 -06:00
|
|
|
points[timeKey][i] = dp[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 07:27:29 -05:00
|
|
|
for (const time in points) {
|
|
|
|
const point = points[time];
|
|
|
|
const values = [point.time];
|
2015-11-04 02:41:03 -06:00
|
|
|
|
2017-04-20 04:16:37 -05:00
|
|
|
for (let i = 0; i < data.length; i++) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const value = point[i];
|
2015-11-04 15:44:08 -06:00
|
|
|
values.push(value);
|
2015-11-04 02:41:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
model.rows.push(values);
|
|
|
|
}
|
2017-12-21 01:39:31 -06:00
|
|
|
},
|
2015-11-04 02:41:03 -06:00
|
|
|
};
|
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
transformers['timeseries_aggregations'] = {
|
|
|
|
description: 'Time series aggregations',
|
2018-09-04 07:27:03 -05:00
|
|
|
getColumns: () => {
|
2015-11-13 10:36:11 -06:00
|
|
|
return [
|
2017-12-21 01:39:31 -06:00
|
|
|
{ text: 'Avg', value: 'avg' },
|
|
|
|
{ text: 'Min', value: 'min' },
|
|
|
|
{ text: 'Max', value: 'max' },
|
|
|
|
{ text: 'Total', value: 'total' },
|
|
|
|
{ text: 'Current', value: 'current' },
|
|
|
|
{ text: 'Count', value: 'count' },
|
2015-11-13 10:36:11 -06:00
|
|
|
];
|
|
|
|
},
|
2018-09-04 07:27:03 -05:00
|
|
|
transform: (data, panel, model) => {
|
2018-08-30 01:58:43 -05:00
|
|
|
let i, y;
|
2017-12-21 01:39:31 -06:00
|
|
|
model.columns.push({ text: 'Metric' });
|
2015-11-13 10:36:11 -06:00
|
|
|
|
|
|
|
for (i = 0; i < panel.columns.length; i++) {
|
2017-12-19 09:06:54 -06:00
|
|
|
model.columns.push({ text: panel.columns[i].text });
|
2015-11-13 10:36:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < data.length; i++) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const series = new TimeSeries({
|
2015-11-13 10:36:11 -06:00
|
|
|
datapoints: data[i].datapoints,
|
2017-12-21 01:39:31 -06:00
|
|
|
alias: data[i].target,
|
2015-11-13 10:36:11 -06:00
|
|
|
});
|
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
series.getFlotPairs('connected');
|
2018-08-29 07:27:29 -05:00
|
|
|
const cells = [series.alias];
|
2015-11-13 10:36:11 -06:00
|
|
|
|
|
|
|
for (y = 0; y < panel.columns.length; y++) {
|
|
|
|
cells.push(series.stats[panel.columns[y].value]);
|
|
|
|
}
|
|
|
|
|
|
|
|
model.rows.push(cells);
|
|
|
|
}
|
2017-12-21 01:39:31 -06:00
|
|
|
},
|
2015-11-13 10:36:11 -06:00
|
|
|
};
|
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
transformers['annotations'] = {
|
|
|
|
description: 'Annotations',
|
2018-09-04 07:27:03 -05:00
|
|
|
getColumns: () => {
|
2015-11-13 10:36:11 -06:00
|
|
|
return [];
|
|
|
|
},
|
2018-09-04 07:27:03 -05:00
|
|
|
transform: (data, panel, model) => {
|
2017-12-21 01:39:31 -06:00
|
|
|
model.columns.push({ text: 'Time', type: 'date' });
|
|
|
|
model.columns.push({ text: 'Title' });
|
|
|
|
model.columns.push({ text: 'Text' });
|
|
|
|
model.columns.push({ text: 'Tags' });
|
2015-11-10 09:15:23 -06:00
|
|
|
|
2017-01-17 05:17:18 -06:00
|
|
|
if (!data || !data.annotations || data.annotations.length === 0) {
|
2015-11-10 09:15:23 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-30 01:58:43 -05:00
|
|
|
for (let i = 0; i < data.annotations.length; i++) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const evt = data.annotations[i];
|
2017-11-13 08:18:39 -06:00
|
|
|
model.rows.push([evt.time, evt.title, evt.text, evt.tags]);
|
2015-11-10 09:15:23 -06:00
|
|
|
}
|
2017-12-21 01:39:31 -06:00
|
|
|
},
|
2015-11-04 10:23:16 -06:00
|
|
|
};
|
2015-11-04 02:41:03 -06:00
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
transformers['table'] = {
|
|
|
|
description: 'Table',
|
2018-09-04 07:27:03 -05:00
|
|
|
getColumns: data => {
|
2015-12-03 08:09:39 -06:00
|
|
|
if (!data || data.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-12-05 04:25:10 -06:00
|
|
|
// Single query returns data columns as is
|
|
|
|
if (data.length === 1) {
|
|
|
|
return [...data[0].columns];
|
|
|
|
}
|
|
|
|
|
2017-12-01 07:27:22 -06:00
|
|
|
// Track column indexes: name -> index
|
|
|
|
const columnNames = {};
|
|
|
|
|
2017-12-11 05:42:53 -06:00
|
|
|
// Union of all columns
|
2017-12-12 06:18:06 -06:00
|
|
|
const columns = data.reduce((acc, series) => {
|
|
|
|
series.columns.forEach(col => {
|
2017-12-01 07:27:22 -06:00
|
|
|
const { text } = col;
|
2017-12-11 05:42:53 -06:00
|
|
|
if (columnNames[text] === undefined) {
|
|
|
|
columnNames[text] = acc.length;
|
|
|
|
acc.push(col);
|
2017-12-01 07:27:22 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return columns;
|
|
|
|
},
|
2018-09-04 07:27:03 -05:00
|
|
|
transform: (data, panel, model) => {
|
2017-12-01 07:27:22 -06:00
|
|
|
if (!data || data.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
const noTableIndex = _.findIndex(data, d => d.type !== 'table');
|
2017-12-04 10:55:00 -06:00
|
|
|
if (noTableIndex > -1) {
|
2017-12-19 09:06:54 -06:00
|
|
|
throw {
|
|
|
|
message: `Result of query #${String.fromCharCode(
|
|
|
|
65 + noTableIndex
|
2017-12-21 01:39:31 -06:00
|
|
|
)} is not in table format, try using another transform.`,
|
2017-12-19 09:06:54 -06:00
|
|
|
};
|
2017-12-01 07:27:22 -06:00
|
|
|
}
|
|
|
|
|
2018-10-19 08:29:58 -05:00
|
|
|
mergeTablesIntoModel(model, ...data);
|
2017-12-21 01:39:31 -06:00
|
|
|
},
|
2017-12-01 07:27:22 -06:00
|
|
|
};
|
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
transformers['json'] = {
|
|
|
|
description: 'JSON Data',
|
2018-09-04 07:27:03 -05:00
|
|
|
getColumns: data => {
|
2015-11-13 10:36:11 -06:00
|
|
|
if (!data || data.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-08-29 07:27:29 -05:00
|
|
|
const names: any = {};
|
2018-08-30 01:58:43 -05:00
|
|
|
for (let i = 0; i < data.length; i++) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const series = data[i];
|
2017-12-21 01:39:31 -06:00
|
|
|
if (series.type !== 'docs') {
|
2015-11-13 10:36:11 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-11-23 09:10:32 -06:00
|
|
|
// only look at 100 docs
|
2018-08-29 07:27:29 -05:00
|
|
|
const maxDocs = Math.min(series.datapoints.length, 100);
|
2018-08-30 01:58:43 -05:00
|
|
|
for (let y = 0; y < maxDocs; y++) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const doc = series.datapoints[y];
|
|
|
|
const flattened = flatten(doc, null);
|
|
|
|
for (const propName in flattened) {
|
2015-11-13 10:36:11 -06:00
|
|
|
names[propName] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 07:27:03 -05:00
|
|
|
return _.map(names, (value, key) => {
|
2017-12-19 09:06:54 -06:00
|
|
|
return { text: key, value: key };
|
2015-11-13 10:36:11 -06:00
|
|
|
});
|
|
|
|
},
|
2018-09-04 07:27:03 -05:00
|
|
|
transform: (data, panel, model) => {
|
2018-08-30 01:58:43 -05:00
|
|
|
let i, y, z;
|
2017-08-02 09:15:22 -05:00
|
|
|
|
2018-08-26 10:14:40 -05:00
|
|
|
for (const column of panel.columns) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const tableCol: any = { text: column.text };
|
2017-08-02 09:15:22 -05:00
|
|
|
|
|
|
|
// if filterable data then set columns to filterable
|
|
|
|
if (data.length > 0 && data[0].filterable) {
|
|
|
|
tableCol.filterable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
model.columns.push(tableCol);
|
2015-11-05 06:13:13 -06:00
|
|
|
}
|
2015-11-04 02:41:03 -06:00
|
|
|
|
2015-11-05 06:13:13 -06:00
|
|
|
if (model.columns.length === 0) {
|
2017-12-21 01:39:31 -06:00
|
|
|
model.columns.push({ text: 'JSON' });
|
2015-11-05 06:13:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < data.length; i++) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const series = data[i];
|
2015-11-04 15:44:08 -06:00
|
|
|
|
2015-11-05 06:13:13 -06:00
|
|
|
for (y = 0; y < series.datapoints.length; y++) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const dp = series.datapoints[y];
|
|
|
|
const values = [];
|
2015-11-05 06:13:13 -06:00
|
|
|
|
2015-11-23 09:10:32 -06:00
|
|
|
if (_.isObject(dp) && panel.columns.length > 0) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const flattened = flatten(dp, null);
|
2015-11-23 09:10:32 -06:00
|
|
|
for (z = 0; z < panel.columns.length; z++) {
|
|
|
|
values.push(flattened[panel.columns[z].value]);
|
|
|
|
}
|
|
|
|
} else {
|
2015-11-05 08:55:42 -06:00
|
|
|
values.push(JSON.stringify(dp));
|
2015-11-05 06:13:13 -06:00
|
|
|
}
|
2015-11-23 09:10:32 -06:00
|
|
|
|
2015-11-05 06:13:13 -06:00
|
|
|
model.rows.push(values);
|
2015-11-05 02:56:19 -06:00
|
|
|
}
|
|
|
|
}
|
2017-12-21 01:39:31 -06:00
|
|
|
},
|
2015-11-05 02:56:19 -06:00
|
|
|
};
|
2015-11-04 15:44:08 -06:00
|
|
|
|
2015-12-03 08:09:39 -06:00
|
|
|
function transformDataToTable(data, panel) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const model = new TableModel();
|
2015-12-03 08:09:39 -06:00
|
|
|
|
|
|
|
if (!data || data.length === 0) {
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
2018-08-29 07:27:29 -05:00
|
|
|
const transformer = transformers[panel.transform];
|
2015-12-03 08:09:39 -06:00
|
|
|
if (!transformer) {
|
2017-12-21 01:39:31 -06:00
|
|
|
throw { message: 'Transformer ' + panel.transform + ' not found' };
|
2015-12-03 08:09:39 -06:00
|
|
|
}
|
|
|
|
|
2017-05-09 05:07:06 -05:00
|
|
|
transformer.transform(data, panel, model);
|
2015-12-03 08:09:39 -06:00
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
export { transformers, transformDataToTable };
|