123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- import { findIndex, isObject, map } from 'lodash';
- import { Column, TableData } from '@grafana/data';
- import TableModel, { mergeTablesIntoModel } from 'app/core/table_model';
- import TimeSeries from 'app/core/time_series2';
- import flatten from 'app/core/utils/flatten';
- import { TableTransform } from './types';
- const transformers: { [key: string]: TableTransform } = {};
- export const timeSeriesFormatFilterer = (data: any): any[] => {
- if (!Array.isArray(data)) {
- return data.datapoints ? [data] : [];
- }
- return data.reduce((acc, series) => {
- if (!series.datapoints) {
- return acc;
- }
- return acc.concat(series);
- }, []);
- };
- export const tableDataFormatFilterer = (data: any): any[] => {
- if (!Array.isArray(data)) {
- return data.columns ? [data] : [];
- }
- return data.reduce((acc, series) => {
- if (!series.columns) {
- return acc;
- }
- return acc.concat(series);
- }, []);
- };
- transformers['timeseries_to_rows'] = {
- description: 'Time series to rows',
- getColumns: () => {
- return [];
- },
- transform: (data, panel, model) => {
- model.columns = [{ text: 'Time', type: 'date' }, { text: 'Metric' }, { text: 'Value' }];
- const filteredData = timeSeriesFormatFilterer(data);
- for (let i = 0; i < filteredData.length; i++) {
- const series = filteredData[i];
- for (let y = 0; y < series.datapoints.length; y++) {
- const dp = series.datapoints[y];
- model.rows.push([dp[1], series.target, dp[0]]);
- }
- }
- },
- };
- transformers['timeseries_to_columns'] = {
- description: 'Time series to columns',
- getColumns: () => {
- return [];
- },
- transform: (data, panel, model) => {
- model.columns.push({ text: 'Time', type: 'date' });
- // group by time
- const points: any = {};
- const filteredData = timeSeriesFormatFilterer(data);
- for (let i = 0; i < filteredData.length; i++) {
- const series = filteredData[i];
- model.columns.push({ text: series.target });
- for (let y = 0; y < series.datapoints.length; y++) {
- const dp = series.datapoints[y];
- const timeKey = dp[1].toString();
- if (!points[timeKey]) {
- points[timeKey] = { time: dp[1] };
- points[timeKey][i] = dp[0];
- } else {
- points[timeKey][i] = dp[0];
- }
- }
- }
- for (const time in points) {
- const point = points[time];
- const values = [point.time];
- for (let i = 0; i < filteredData.length; i++) {
- const value = point[i];
- values.push(value);
- }
- model.rows.push(values);
- }
- },
- };
- transformers['timeseries_aggregations'] = {
- description: 'Time series aggregations',
- getColumns: () => {
- return [
- { text: 'Avg', value: 'avg' },
- { text: 'Min', value: 'min' },
- { text: 'Max', value: 'max' },
- { text: 'Total', value: 'total' },
- { text: 'Current', value: 'current' },
- { text: 'Count', value: 'count' },
- ];
- },
- transform: (data, panel, model) => {
- let i, y;
- model.columns.push({ text: 'Metric' });
- for (i = 0; i < panel.columns.length; i++) {
- model.columns.push({ text: panel.columns[i].text });
- }
- const filteredData = timeSeriesFormatFilterer(data);
- for (i = 0; i < filteredData.length; i++) {
- const series = new TimeSeries({
- datapoints: filteredData[i].datapoints,
- alias: filteredData[i].target,
- });
- series.getFlotPairs('connected');
- const cells = [series.alias];
- for (y = 0; y < panel.columns.length; y++) {
- cells.push(series.stats[panel.columns[y].value]);
- }
- model.rows.push(cells);
- }
- },
- };
- transformers['annotations'] = {
- description: 'Annotations',
- getColumns: () => {
- return [];
- },
- transform: (data, panel, model) => {
- model.columns.push({ text: 'Time', type: 'date' });
- model.columns.push({ text: 'Title' });
- model.columns.push({ text: 'Text' });
- model.columns.push({ text: 'Tags' });
- if (!data || !data.annotations || data.annotations.length === 0) {
- return;
- }
- for (let i = 0; i < data.annotations.length; i++) {
- const evt = data.annotations[i];
- model.rows.push([evt.time, evt.title, evt.text, evt.tags]);
- }
- },
- };
- transformers['table'] = {
- description: 'Table',
- getColumns: (data) => {
- if (!data || data.length === 0) {
- return [];
- }
- // Single query returns data columns as is
- if (data.length === 1) {
- return [...data[0].columns];
- }
- const filteredData = tableDataFormatFilterer(data);
- // Track column indexes: name -> index
- const columnNames: any = {};
- // Union of all columns
- const columns = filteredData.reduce((acc: Column[], series: TableData) => {
- series.columns.forEach((col) => {
- const { text } = col;
- if (columnNames[text] === undefined) {
- columnNames[text] = acc.length;
- acc.push(col);
- }
- });
- return acc;
- }, []);
- return columns;
- },
- transform: (data: any[], panel, model) => {
- if (!data || data.length === 0) {
- return;
- }
- const filteredData = tableDataFormatFilterer(data);
- const noTableIndex = findIndex(filteredData, (d) => 'columns' in d && 'rows' in d);
- if (noTableIndex < 0) {
- throw {
- message: `Result of query #${String.fromCharCode(
- 65 + noTableIndex
- )} is not in table format, try using another transform.`,
- };
- }
- mergeTablesIntoModel(model, ...filteredData);
- },
- };
- transformers['json'] = {
- description: 'JSON Data',
- getColumns: (data) => {
- if (!data || data.length === 0) {
- return [];
- }
- const names: any = {};
- for (let i = 0; i < data.length; i++) {
- const series = data[i];
- if (series.type !== 'docs') {
- continue;
- }
- // only look at 100 docs
- const maxDocs = Math.min(series.datapoints.length, 100);
- for (let y = 0; y < maxDocs; y++) {
- const doc = series.datapoints[y];
- const flattened = flatten(doc, {});
- for (const propName in flattened) {
- names[propName] = true;
- }
- }
- }
- return map(names, (value, key) => {
- return { text: key, value: key };
- });
- },
- transform: (data, panel, model) => {
- let i, y, z;
- for (const column of panel.columns) {
- const tableCol: any = { text: column.text };
- // if filterable data then set columns to filterable
- if (data.length > 0 && data[0].filterable) {
- tableCol.filterable = true;
- }
- model.columns.push(tableCol);
- }
- if (model.columns.length === 0) {
- model.columns.push({ text: 'JSON' });
- }
- for (i = 0; i < data.length; i++) {
- const series = data[i];
- for (y = 0; y < series.datapoints.length; y++) {
- const dp = series.datapoints[y];
- const values = [];
- if (isObject(dp) && panel.columns.length > 0) {
- const flattened = flatten(dp);
- for (z = 0; z < panel.columns.length; z++) {
- values.push(flattened[panel.columns[z].value]);
- }
- } else {
- values.push(JSON.stringify(dp));
- }
- model.rows.push(values);
- }
- }
- },
- };
- function transformDataToTable(data: any, panel: any) {
- const model = new TableModel();
- if (!data || data.length === 0) {
- return model;
- }
- const transformer = transformers[panel.transform];
- if (!transformer) {
- throw { message: 'Transformer ' + panel.transform + ' not found' };
- }
- transformer.transform(data, panel, model);
- return model;
- }
- export { transformers, transformDataToTable };
|