2019-12-22 23:22:54 -06:00
|
|
|
import {
|
|
|
|
LogsModel,
|
|
|
|
GraphSeriesXY,
|
|
|
|
DataFrame,
|
|
|
|
FieldType,
|
|
|
|
TimeZone,
|
|
|
|
toDataFrame,
|
|
|
|
getDisplayProcessor,
|
2020-02-06 06:34:52 -06:00
|
|
|
ExploreMode,
|
2019-12-22 23:22:54 -06:00
|
|
|
} from '@grafana/data';
|
2020-02-06 06:34:52 -06:00
|
|
|
import { ExploreItemState } from 'app/types/explore';
|
2019-06-03 07:54:32 -05:00
|
|
|
import TableModel, { mergeTablesIntoModel } from 'app/core/table_model';
|
2019-08-26 01:11:07 -05:00
|
|
|
import { sortLogsResult, refreshIntervalToSortOrder } from 'app/core/utils/explore';
|
2019-07-01 14:00:29 -05:00
|
|
|
import { dataFrameToLogsModel } from 'app/core/logs_model';
|
2019-08-13 00:32:43 -05:00
|
|
|
import { getGraphSeriesModel } from 'app/plugins/panel/graph2/getGraphSeriesModel';
|
2019-12-22 23:22:54 -06:00
|
|
|
import { config } from 'app/core/config';
|
2019-06-03 07:54:32 -05:00
|
|
|
|
|
|
|
export class ResultProcessor {
|
2019-11-07 05:37:46 -06:00
|
|
|
constructor(
|
|
|
|
private state: ExploreItemState,
|
|
|
|
private dataFrames: DataFrame[],
|
|
|
|
private intervalMs: number,
|
|
|
|
private timeZone: TimeZone
|
|
|
|
) {}
|
2019-06-03 07:54:32 -05:00
|
|
|
|
2019-11-26 03:01:32 -06:00
|
|
|
getGraphResult(): GraphSeriesXY[] | null {
|
2019-06-03 07:54:32 -05:00
|
|
|
if (this.state.mode !== ExploreMode.Metrics) {
|
2019-09-12 10:28:46 -05:00
|
|
|
return null;
|
2019-06-03 07:54:32 -05:00
|
|
|
}
|
|
|
|
|
2019-09-03 08:57:00 -05:00
|
|
|
const onlyTimeSeries = this.dataFrames.filter(isTimeSeries);
|
2019-06-03 07:54:32 -05:00
|
|
|
|
2019-09-12 10:28:46 -05:00
|
|
|
if (onlyTimeSeries.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-09-03 05:09:52 -05:00
|
|
|
return getGraphSeriesModel(
|
|
|
|
onlyTimeSeries,
|
2019-11-07 05:37:46 -06:00
|
|
|
this.timeZone,
|
2019-09-03 05:09:52 -05:00
|
|
|
{},
|
|
|
|
{ showBars: false, showLines: true, showPoints: false },
|
|
|
|
{ asTable: false, isVisible: true, placement: 'under' }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-18 01:38:50 -06:00
|
|
|
getTableResult(): DataFrame | null {
|
2019-06-03 07:54:32 -05:00
|
|
|
if (this.state.mode !== ExploreMode.Metrics) {
|
2019-09-12 10:28:46 -05:00
|
|
|
return null;
|
2019-06-03 07:54:32 -05:00
|
|
|
}
|
|
|
|
|
2019-09-03 05:36:21 -05:00
|
|
|
// For now ignore time series
|
|
|
|
// We can change this later, just need to figure out how to
|
|
|
|
// Ignore time series only for prometheus
|
2019-09-03 08:57:00 -05:00
|
|
|
const onlyTables = this.dataFrames.filter(frame => !isTimeSeries(frame));
|
2019-09-03 05:36:21 -05:00
|
|
|
|
2019-09-12 10:28:46 -05:00
|
|
|
if (onlyTables.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-09-03 05:36:21 -05:00
|
|
|
const tables = onlyTables.map(frame => {
|
|
|
|
const { fields } = frame;
|
|
|
|
const fieldCount = fields.length;
|
2019-09-16 06:02:26 -05:00
|
|
|
const rowCount = frame.length;
|
2019-09-03 05:36:21 -05:00
|
|
|
|
|
|
|
const columns = fields.map(field => ({
|
|
|
|
text: field.name,
|
|
|
|
type: field.type,
|
|
|
|
filterable: field.config.filterable,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const rows: any[][] = [];
|
|
|
|
for (let i = 0; i < rowCount; i++) {
|
|
|
|
const row: any[] = [];
|
|
|
|
for (let j = 0; j < fieldCount; j++) {
|
|
|
|
row.push(frame.fields[j].values.get(i));
|
|
|
|
}
|
|
|
|
rows.push(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new TableModel({
|
|
|
|
columns,
|
|
|
|
rows,
|
|
|
|
meta: frame.meta,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-18 01:38:50 -06:00
|
|
|
const mergedTable = mergeTablesIntoModel(new TableModel(), ...tables);
|
2019-12-22 23:22:54 -06:00
|
|
|
const data = toDataFrame(mergedTable);
|
|
|
|
|
|
|
|
// set display processor
|
|
|
|
for (const field of data.fields) {
|
|
|
|
field.display = getDisplayProcessor({
|
2019-12-28 19:32:58 -06:00
|
|
|
field,
|
2019-12-22 23:22:54 -06:00
|
|
|
theme: config.theme,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2019-09-03 05:09:52 -05:00
|
|
|
}
|
2019-06-03 07:54:32 -05:00
|
|
|
|
2019-11-26 03:01:32 -06:00
|
|
|
getLogsResult(): LogsModel | null {
|
2019-06-03 07:54:32 -05:00
|
|
|
if (this.state.mode !== ExploreMode.Logs) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-09-03 05:09:52 -05:00
|
|
|
|
2019-11-07 05:37:46 -06:00
|
|
|
const newResults = dataFrameToLogsModel(this.dataFrames, this.intervalMs, this.timeZone);
|
2019-08-26 01:11:07 -05:00
|
|
|
const sortOrder = refreshIntervalToSortOrder(this.state.refreshInterval);
|
|
|
|
const sortedNewResults = sortLogsResult(newResults, sortOrder);
|
2019-09-05 07:04:01 -05:00
|
|
|
const rows = sortedNewResults.rows;
|
|
|
|
const series = sortedNewResults.series;
|
2019-07-10 07:57:23 -05:00
|
|
|
return { ...sortedNewResults, rows, series };
|
2019-09-03 05:09:52 -05:00
|
|
|
}
|
2019-06-03 07:54:32 -05:00
|
|
|
}
|
2019-09-03 08:57:00 -05:00
|
|
|
|
|
|
|
export function isTimeSeries(frame: DataFrame): boolean {
|
|
|
|
if (frame.fields.length === 2) {
|
|
|
|
if (frame.fields[1].type === FieldType.time) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|