From 712c061ebb8a78088cab7a24721b1b17fbaf5133 Mon Sep 17 00:00:00 2001 From: Ivana Date: Fri, 10 Jul 2020 13:22:24 +0200 Subject: [PATCH] WIP: Create v2 version --- .../elasticsearch/elastic_response.ts | 164 ++++++++---------- .../datasource/elasticsearch/metric_agg.ts | 13 +- .../datasource/elasticsearch/query_builder.ts | 18 +- 3 files changed, 79 insertions(+), 116 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/elastic_response.ts b/public/app/plugins/datasource/elasticsearch/elastic_response.ts index 3b556404eec..035902a3783 100644 --- a/public/app/plugins/datasource/elasticsearch/elastic_response.ts +++ b/public/app/plugins/datasource/elasticsearch/elastic_response.ts @@ -390,93 +390,22 @@ export class ElasticResponse { return result; } - getTimeSeries(isV2 = false) { - if (isV2) { - const dataFrame: DataFrame[] = []; - - for (let n = 0; n < this.response.responses.length; n++) { - const response = this.response.responses[n]; - if (response.error) { - throw this.getErrorFromElasticResponse(this.response, response.error); - } - - if (response.hits && response.hits.hits.length > 0) { - const { propNames, docs } = flattenHits(response.hits.hits); - if (docs.length > 0) { - const series = createEmptyDataFrame(propNames, this.targets[0].timeField); - - // Add a row for each document - for (const doc of docs) { - series.add(doc); - } - - dataFrame.push(series); - } - } - - if (response.aggregations) { - const aggregations = response.aggregations; - const target = this.targets[n]; - const tmpSeriesList: any[] = []; - const table = new TableModel(); - - this.processBuckets(aggregations, target, tmpSeriesList, table, {}, 0); - this.trimDatapoints(tmpSeriesList, target); - this.nameSeries(tmpSeriesList, target); - - if (table.rows.length > 0) { - dataFrame.push(toDataFrame(table)); - } - - for (let y = 0; y < tmpSeriesList.length; y++) { - let series = toDataFrame(tmpSeriesList[y]); - - // When log results, show aggregations only in graph. Log fields are then going to be shown in table. - - dataFrame.push(series); - } - } - } - - return { data: dataFrame }; - } else { - const seriesList = []; - - for (let i = 0; i < this.response.responses.length; i++) { - const response = this.response.responses[i]; - if (response.error) { - throw this.getErrorFromElasticResponse(this.response, response.error); - } - - if (response.hits && response.hits.hits.length > 0) { - this.processHits(response.hits, seriesList); - } - - if (response.aggregations) { - const aggregations = response.aggregations; - const target = this.targets[i]; - const tmpSeriesList: any[] = []; - const table = new TableModel(); - - this.processBuckets(aggregations, target, tmpSeriesList, table, {}, 0); - this.trimDatapoints(tmpSeriesList, target); - this.nameSeries(tmpSeriesList, target); - - for (let y = 0; y < tmpSeriesList.length; y++) { - seriesList.push(tmpSeriesList[y]); - } - - if (table.rows.length > 0) { - seriesList.push(table); - } - } - } - - return { data: seriesList }; + getTimeSeries(processToDataFrames = false): DataQueryResponse { + if (processToDataFrames) { + return this.processResponseToDataFrames(false); } + return this.processResponseToSeries(); } getLogs(logMessageField?: string, logLevelField?: string): DataQueryResponse { + return this.processResponseToDataFrames(true, logMessageField, logLevelField); + } + + processResponseToDataFrames( + isLogsRequest: boolean, + logMessageField?: string, + logLevelField?: string + ): DataQueryResponse { const dataFrame: DataFrame[] = []; for (let n = 0; n < this.response.responses.length; n++) { @@ -485,17 +414,24 @@ export class ElasticResponse { throw this.getErrorFromElasticResponse(this.response, response.error); } - const { propNames, docs } = flattenHits(response.hits.hits); - if (docs.length > 0) { - let series = createEmptyDataFrame(propNames, this.targets[0].timeField, logMessageField, logLevelField); + if (response.hits && response.hits.hits.length > 0) { + const { propNames, docs } = flattenHits(response.hits.hits); + if (docs.length > 0) { + const series = createEmptyDataFrame(propNames, this.targets[0].timeField, logMessageField, logLevelField); - // Add a row for each document - for (const doc of docs) { - series.add(doc); + // Add a row for each document + for (const doc of docs) { + if (logLevelField) { + // Remap level field based on the datasource config. This field is then used in explore to figure out the + // log level. We may rewrite some actual data in the level field if they are different. + doc['level'] = doc[logLevelField]; + } + + series.add(doc); + } + + dataFrame.push(series); } - - series = addPreferredVisualisationType(series, 'logs'); - dataFrame.push(series); } if (response.aggregations) { @@ -508,11 +444,17 @@ export class ElasticResponse { this.trimDatapoints(tmpSeriesList, target); this.nameSeries(tmpSeriesList, target); + if (table.rows.length > 0) { + dataFrame.push(toDataFrame(table)); + } + for (let y = 0; y < tmpSeriesList.length; y++) { let series = toDataFrame(tmpSeriesList[y]); // When log results, show aggregations only in graph. Log fields are then going to be shown in table. - series = addPreferredVisualisationType(series, 'graph'); + if (isLogsRequest) { + series = addPreferredVisualisationType(series, 'graph'); + } dataFrame.push(series); } @@ -521,6 +463,42 @@ export class ElasticResponse { return { data: dataFrame }; } + + processResponseToSeries = () => { + const seriesList = []; + + for (let i = 0; i < this.response.responses.length; i++) { + const response = this.response.responses[i]; + if (response.error) { + throw this.getErrorFromElasticResponse(this.response, response.error); + } + + if (response.hits && response.hits.hits.length > 0) { + this.processHits(response.hits, seriesList); + } + + if (response.aggregations) { + const aggregations = response.aggregations; + const target = this.targets[i]; + const tmpSeriesList: any[] = []; + const table = new TableModel(); + + this.processBuckets(aggregations, target, tmpSeriesList, table, {}, 0); + this.trimDatapoints(tmpSeriesList, target); + this.nameSeries(tmpSeriesList, target); + + for (let y = 0; y < tmpSeriesList.length; y++) { + seriesList.push(tmpSeriesList[y]); + } + + if (table.rows.length > 0) { + seriesList.push(table); + } + } + } + + return { data: seriesList }; + }; } type Doc = { diff --git a/public/app/plugins/datasource/elasticsearch/metric_agg.ts b/public/app/plugins/datasource/elasticsearch/metric_agg.ts index f90ae256894..869547feb69 100644 --- a/public/app/plugins/datasource/elasticsearch/metric_agg.ts +++ b/public/app/plugins/datasource/elasticsearch/metric_agg.ts @@ -101,14 +101,7 @@ export class ElasticMetricAggCtrl { $scope.updateMovingAvgModelSettings(); break; } - case 'raw_document': { - $scope.agg.settings.size = $scope.agg.settings.size || 500; - $scope.settingsLinkText = 'Size: ' + $scope.agg.settings.size; - $scope.target.metrics.splice(0, $scope.target.metrics.length, $scope.agg); - - $scope.target.bucketAggs = []; - break; - } + case 'raw_document': case 'raw_document_v2': { $scope.agg.settings.size = $scope.agg.settings.size || 500; $scope.settingsLinkText = 'Size: ' + $scope.agg.settings.size; @@ -173,8 +166,8 @@ export class ElasticMetricAggCtrl { // reset back to metric/group by query if ( - ($scope.target.bucketAggs.length === 0 && $scope.agg.type !== 'raw_document') || - $scope.agg.type !== 'raw_document_v2' + $scope.target.bucketAggs.length === 0 && + ($scope.agg.type !== 'raw_document' || $scope.agg.type !== 'raw_document_v2') ) { $scope.target.bucketAggs = [queryDef.defaultBucketAgg()]; } diff --git a/public/app/plugins/datasource/elasticsearch/query_builder.ts b/public/app/plugins/datasource/elasticsearch/query_builder.ts index bb7644b0095..1af06169b82 100644 --- a/public/app/plugins/datasource/elasticsearch/query_builder.ts +++ b/public/app/plugins/datasource/elasticsearch/query_builder.ts @@ -212,25 +212,17 @@ export class ElasticQueryBuilder { // If target doesn't have bucketAggs and type is not raw_document, it is invalid query. if (target.bucketAggs.length === 0) { metric = target.metrics[0]; - //tu treba pridat - // if (!metric || metric.type !== 'raw_document') { - // throw { message: 'Invalid query' }; - // } + + if (!metric || !(metric.type === 'raw_document' || metric.type === 'raw_document_v2')) { + throw { message: 'Invalid query' }; + } } /* Handle document query: * Check if metric type is raw_document. If metric doesn't have size (or size is 0), update size to 500. * Otherwise it will not be a valid query and error will be thrown. */ - if (target.metrics?.[0]?.type === 'raw_document') { - metric = target.metrics[0]; - const size = (metric.settings && metric.settings.size !== 0 && metric.settings.size) || 500; - return this.documentQuery(query, size); - } - - /* Handle document query v2: - */ - if (target.metrics?.[0]?.type === 'raw_document_v2') { + if (target.metrics?.[0]?.type === 'raw_document' || target.metrics?.[0]?.type === 'raw_document_v2') { metric = target.metrics[0]; const size = (metric.settings && metric.settings.size !== 0 && metric.settings.size) || 500; return this.documentQuery(query, size);