2019-12-22 23:22:54 -06:00
|
|
|
import {
|
|
|
|
LogsModel,
|
|
|
|
GraphSeriesXY,
|
|
|
|
DataFrame,
|
|
|
|
FieldType,
|
|
|
|
TimeZone,
|
|
|
|
getDisplayProcessor,
|
2020-02-06 06:34:52 -06:00
|
|
|
ExploreMode,
|
2020-05-11 15:47:15 -05:00
|
|
|
PreferredVisualisationType,
|
2020-06-22 08:30:16 -05:00
|
|
|
standardTransformers,
|
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-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
|
|
|
}
|
|
|
|
|
2020-04-25 15:48:20 -05:00
|
|
|
const onlyTimeSeries = this.dataFrames.filter(frame => isTimeSeries(frame, this.state.datasourceInstance?.meta.id));
|
2020-05-11 15:47:15 -05:00
|
|
|
const timeSeriesToShowInGraph = onlyTimeSeries.filter(frame => shouldShowInVisualisationType(frame, 'graph'));
|
2019-06-03 07:54:32 -05:00
|
|
|
|
2020-05-11 15:47:15 -05:00
|
|
|
if (timeSeriesToShowInGraph.length === 0) {
|
2019-09-12 10:28:46 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-09-03 05:09:52 -05:00
|
|
|
return getGraphSeriesModel(
|
2020-05-11 15:47:15 -05:00
|
|
|
timeSeriesToShowInGraph,
|
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
|
|
|
}
|
|
|
|
|
2020-05-27 09:45:53 -05:00
|
|
|
const onlyTables = this.dataFrames
|
|
|
|
.filter((frame: DataFrame) => shouldShowInVisualisationType(frame, 'table'))
|
|
|
|
.sort((frameA: DataFrame, frameB: DataFrame) => {
|
|
|
|
const frameARefId = frameA.refId!;
|
|
|
|
const frameBRefId = frameB.refId!;
|
|
|
|
|
|
|
|
if (frameARefId > frameBRefId) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (frameARefId < frameBRefId) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
2019-09-03 05:36:21 -05:00
|
|
|
|
2019-09-12 10:28:46 -05:00
|
|
|
if (onlyTables.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-29 16:20:57 -05:00
|
|
|
const hasOnlyTimeseries = onlyTables.every(df => isTimeSeries(df));
|
|
|
|
|
|
|
|
// If we have only timeseries we do join on default time column which makes more sense. If we are showing
|
|
|
|
// non timeseries or some mix of data we are not trying to join on anything and just try to merge them in
|
|
|
|
// single table, which may not make sense in most cases, but it's up to the user to query something sensible.
|
|
|
|
const transformer = hasOnlyTimeseries
|
|
|
|
? standardTransformers.seriesToColumnsTransformer.transformer({})
|
|
|
|
: standardTransformers.mergeTransformer.transformer({});
|
|
|
|
|
|
|
|
const data = transformer(onlyTables)[0];
|
2019-12-22 23:22:54 -06:00
|
|
|
|
|
|
|
// 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,
|
2020-04-27 08:28:06 -05:00
|
|
|
timeZone: this.timeZone,
|
2019-12-22 23:22:54 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-05-29 08:39:13 -05:00
|
|
|
const newResults = dataFrameToLogsModel(this.dataFrames, this.intervalMs, this.timeZone, this.state.absoluteRange);
|
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
|
|
|
|
2020-04-30 05:41:03 -05:00
|
|
|
function isTimeSeries(frame: DataFrame, datasource?: string): boolean {
|
2020-04-25 15:48:20 -05:00
|
|
|
// TEMP: Temporary hack. Remove when logs/metrics unification is done
|
|
|
|
if (datasource && datasource === 'cloudwatch') {
|
|
|
|
return isTimeSeriesCloudWatch(frame);
|
|
|
|
}
|
|
|
|
|
2019-09-03 08:57:00 -05:00
|
|
|
if (frame.fields.length === 2) {
|
2020-04-03 13:11:28 -05:00
|
|
|
if (frame.fields[0].type === FieldType.time) {
|
2019-09-03 08:57:00 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2020-05-11 15:47:15 -05:00
|
|
|
function shouldShowInVisualisationType(frame: DataFrame, visualisation: PreferredVisualisationType) {
|
|
|
|
if (frame.meta?.preferredVisualisationType && frame.meta?.preferredVisualisationType !== visualisation) {
|
2020-04-30 05:41:03 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-25 15:48:20 -05:00
|
|
|
// TEMP: Temporary hack. Remove when logs/metrics unification is done
|
2020-04-30 05:41:03 -05:00
|
|
|
function isTimeSeriesCloudWatch(frame: DataFrame): boolean {
|
2020-04-25 15:48:20 -05:00
|
|
|
return (
|
|
|
|
frame.fields.some(field => field.type === FieldType.time) &&
|
|
|
|
frame.fields.some(field => field.type === FieldType.number)
|
|
|
|
);
|
|
|
|
}
|