Skip to content

Commit

Permalink
Use buildEsQuery for table, series and annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
markov00 committed Nov 21, 2018
1 parent c8c0dfd commit 1d136a0
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 203 deletions.
2 changes: 1 addition & 1 deletion src/core_plugins/metrics/public/components/_error.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
display: flex;
align-items: center;
justify-content: center;

flex-direction: column;
// Calculate colors similar to EuiCallout
$tempBackgroundColor: tintOrShade($euiColorDanger, 90%, 70%);
background-color: $tempBackgroundColor;
Expand Down
10 changes: 3 additions & 7 deletions src/core_plugins/metrics/public/kbn_vis_types/request_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,25 @@
import { validateInterval } from '../lib/validate_interval';
import { timezoneProvider } from 'ui/vis/lib/timezone';
import { timefilter } from 'ui/timefilter';
import { buildEsQuery } from '@kbn/es-query';

const MetricsRequestHandlerProvider = function (Private, Notifier, config, $http) {
const notify = new Notifier({ location: 'Metrics' });

return {
name: 'metrics',
handler: function ({ aggs, uiState, timeRange, filters, query, visParams }) {
handler: function ({ uiState, timeRange, filters, query, visParams }) {
const timezone = Private(timezoneProvider)();
return new Promise((resolve) => {
const panel = visParams;
const uiStateObj = uiState.get(panel.type, {});
const parsedTimeRange = timefilter.calculateBounds(timeRange);
const scaledDataFormat = config.get('dateFormat:scaled');
const dateFormat = config.get('dateFormat');
const esQueryConfigs = {
allowLeadingWildcards: config.get('query:allowLeadingWildcards'),
queryStringOptions: config.get('query:queryString:options'),
};
if (panel && panel.id) {
const params = {
timerange: { timezone, ...parsedTimeRange },
filters: [buildEsQuery(aggs.indexPattern, [query], filters, esQueryConfigs)],
query,
filters,
panels: [panel],
state: uiStateObj
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import buildProcessorFunction from './build_processor_function';
import processors from './request_processors/annotations';

export default function buildAnnotationRequest(req, panel, annotation) {
const processor = buildProcessorFunction(processors, req, panel, annotation);
export default function buildAnnotationRequest(req, panel, annotation, esQueryConfig, indexPattern) {
const processor = buildProcessorFunction(processors, req, panel, annotation, esQueryConfig, indexPattern);
const doc = processor({});
return doc;
}
44 changes: 25 additions & 19 deletions src/core_plugins/metrics/server/lib/vis_data/get_annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import buildAnnotationRequest from './build_annotation_request';
import handleAnnotationResponse from './handle_annotation_response';
import { getIndexPatternObject } from './helpers/get_index_pattern';

function validAnnotation(annotation) {
return annotation.index_pattern &&
Expand All @@ -28,28 +29,19 @@ function validAnnotation(annotation) {
annotation.template;
}

export default async (req, panel) => {
export default async (req, panel, esQueryConfig) => {
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('data');
const bodies = panel.annotations
const bodiesPromises = panel.annotations
.filter(validAnnotation)
.map(annotation => {

const indexPattern = annotation.index_pattern;
const bodies = [];

bodies.push({
index: indexPattern,
ignore: [404],
timeout: '90s',
requestTimeout: 90000,
ignoreUnavailable: true,
});

bodies.push(buildAnnotationRequest(req, panel, annotation));
return bodies;
return getAnnotationBody(req, panel, annotation, esQueryConfig);
});

if (!bodies.length) return { responses: [] };
const bodies = await Promise.all(bodiesPromises);
if (!bodies.length) {
return {
responses: [],
};
}
try {
const resp = await callWithRequest(req, 'msearch', {
body: bodies.reduce((acc, item) => acc.concat(item), [])
Expand All @@ -66,6 +58,20 @@ export default async (req, panel) => {
if (error.message === 'missing-indices') return { responses: [] };
throw error;
}

};

async function getAnnotationBody(req, panel, annotation, esQueryConfig) {
const indexPatternString = annotation.index_pattern;
const indexPatternObject = await getIndexPatternObject(req, indexPatternString);
const request = buildAnnotationRequest(req, panel, annotation, esQueryConfig, indexPatternObject);
return [
{
index: indexPatternString,
ignore: [404],
timeout: '90s',
requestTimeout: 90000,
ignoreUnavailable: true,
},
request,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import { getTableData } from './get_table_data';
import { getSeriesData } from './get_series_data';
export default function getPanelData(req) {
return panel => {
if (panel.type === 'table') return getTableData(req, panel);
if (panel.type === 'table') {
return getTableData(req, panel);
}
return getSeriesData(req, panel);
};
}
63 changes: 37 additions & 26 deletions src/core_plugins/metrics/server/lib/vis_data/get_series_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,44 @@ import getRequestParams from './series/get_request_params';
import handleResponseBody from './series/handle_response_body';
import handleErrorResponse from './handle_error_response';
import getAnnotations from './get_annotations';
export function getSeriesData(req, panel) {
import { getEsQueryConfig } from './helpers/get_es_query_uisettings';

export async function getSeriesData(req, panel) {
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('data');
const bodies = panel.series.map(series => getRequestParams(req, panel, series));
const params = {
body: bodies.reduce((acc, items) => acc.concat(items), [])
};
return callWithRequest(req, 'msearch', params)
.then(resp => {
const series = resp.responses.map(handleResponseBody(panel));
return {
[panel.id]: {
id: panel.id,
series: series.reduce((acc, series) => acc.concat(series), [])
}
};
})
.then(resp => {
if (!panel.annotations || panel.annotations.length === 0) return resp;
return getAnnotations(req, panel).then(annotations => {
resp[panel.id].annotations = annotations;
const esQueryConfig = await getEsQueryConfig(req);

try {
const bodiesPromises = panel.series.map(series => getRequestParams(req, panel, series, esQueryConfig));
const bodies = await Promise.all(bodiesPromises);
const params = {
body: bodies.reduce((acc, items) => acc.concat(items), [])
};
return callWithRequest(req, 'msearch', params)
.then(resp => {
const series = resp.responses.map(handleResponseBody(panel));
return {
[panel.id]: {
id: panel.id,
series: series.reduce((acc, series) => acc.concat(series), [])
}
};
})
.then(resp => {
if (!panel.annotations || panel.annotations.length === 0) return resp;
return getAnnotations(req, panel, esQueryConfig).then(annotations => {
resp[panel.id].annotations = annotations;
return resp;
});
})
.then(resp => {
resp.type = panel.type;
return resp;
});
})
.then(resp => {
resp.type = panel.type;
return resp;
})
.catch(handleErrorResponse(panel));
})
.catch(handleErrorResponse(panel));
} catch(e) {
console.error(e);
// TODO handle kql query errors
return handleErrorResponse(e);
}
}

15 changes: 11 additions & 4 deletions src/core_plugins/metrics/server/lib/vis_data/get_table_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@
* specific language governing permissions and limitations
* under the License.
*/

import { get } from 'lodash';
import buildRequestBody from './table/build_request_body';
import handleErrorResponse from './handle_error_response';
import { get } from 'lodash';
import processBucket from './table/process_bucket';
import { getIndexPatternObject } from './helpers/get_index_pattern';
import { getEsQueryConfig } from './helpers/get_es_query_uisettings';


export async function getTableData(req, panel) {
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('data');
const indexPatternString = panel.index_pattern;

const esQueryConfig = await getEsQueryConfig(req);
const indexPatternObject = await getIndexPatternObject(req, indexPatternString);
const params = {
index: panel.index_pattern,
body: buildRequestBody(req, panel)
index: indexPatternString,
body: buildRequestBody(req, panel, esQueryConfig, indexPatternObject)
};
try {
const resp = await callWithRequest(req, 'search', params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,12 @@
* under the License.
*/

import buildRequestBody from './build_request_body';
export default (req, panel, entities) => {
const bodies = [];
entities.forEach(entity => {
bodies.push({
index: panel.index_pattern,
ignore: [404],
timeout: '90s',
requestTimeout: 90000,
ignoreUnavailable: true,
});
bodies.push(buildRequestBody(req, panel, entity));
});
return bodies;
};
export async function getEsQueryConfig(req) {
const uiSettings = req.getUiSettingsService();
const allowLeadingWildcards = await uiSettings.get('query:allowLeadingWildcards');
const queryStringOptions = await uiSettings.get('query:queryString:options');
return {
allowLeadingWildcards,
queryStringOptions: JSON.parse(queryStringOptions),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@
* under the License.
*/

import buildProcessorFunction from '../build_processor_function';
import _ from 'lodash';
import processors from '../response_processors/table';
export async function getIndexPatternObject(req, indexPatternString) {
// getting the matching index pattern
const savedObjectClient = req.getSavedObjectsClient();
const indexPatternObjects = await savedObjectClient.find({
type: 'index-pattern',
fields: ['title', 'fields'],
search: `"${indexPatternString}"`,
search_fields: ['title'],
});

export default function handleResponseBody(panel) {
return resp => {
if (resp.error) {
const err = new Error(resp.error.type);
err.response = JSON.stringify(resp);
throw err;
}
return panel.columns.map(column => {
const processor = buildProcessorFunction(processors, resp, panel, column);
return _.first(processor([]));
// getting the index pattern fields
const indexPatterns = indexPatternObjects.saved_objects
.filter(obj => obj.attributes.title === indexPatternString)
.map(indexPattern => {
const { title, fields } = indexPattern.attributes;
return {
title,
fields: JSON.parse(fields),
};
});
};
return indexPatterns.length === 1 ? indexPatterns[0] : null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@

import getBucketSize from '../../helpers/get_bucket_size';
import getTimerange from '../../helpers/get_timerange';
export default function query(req, panel, annotation) {
import { buildEsQuery } from '@kbn/es-query';

export default function query(req, panel, annotation, esQueryConfig, indexPattern) {
return next => doc => {
const timeField = annotation.time_field;
const {
bucketSize
} = getBucketSize(req, 'auto');
const { bucketSize } = getBucketSize(req, 'auto');
const { from, to } = getTimerange(req);

doc.size = 0;
doc.query = {
bool: {
must: []
}
};

const queries = req.payload.query ? [req.payload.query] : [];
console.log(JSON.stringify(annotation, null, 2));
const filters = !annotation.ignore_global_filters ? req.payload.filters : [];
doc.query = buildEsQuery(indexPattern, queries, filters, esQueryConfig);
const timerange = {
range: {
[timeField]: {
Expand All @@ -54,11 +52,6 @@ export default function query(req, panel, annotation) {
});
}

const globalFilters = req.payload.filters;
if (!annotation.ignore_global_filters) {
doc.query.bool.must = doc.query.bool.must.concat(globalFilters);
}

if (!annotation.ignore_panel_filters && panel.filter) {
doc.query.bool.must.push({
query_string: {
Expand All @@ -76,7 +69,5 @@ export default function query(req, panel, annotation) {
}

return next(doc);

};
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@

import offsetTime from '../../offset_time';
import getIntervalAndTimefield from '../../get_interval_and_timefield';
export default function query(req, panel, series) {
import { buildEsQuery } from '@kbn/es-query';

export default function query(req, panel, series, esQueryConfig, indexPattern) {
return next => doc => {
const { timeField } = getIntervalAndTimefield(panel, series);
const { from, to } = offsetTime(req, series.offset_time);

doc.size = 0;
doc.query = {
bool: {
must: []
}
};
const queries = req.payload.query ? [req.payload.query] : [];
const filters = !panel.ignore_global_filter ? req.payload.filters : [];
doc.query = buildEsQuery(indexPattern, queries, filters, esQueryConfig);

const timerange = {
range: {
Expand All @@ -42,11 +42,6 @@ export default function query(req, panel, series) {
};
doc.query.bool.must.push(timerange);

const globalFilters = req.payload.filters;
if (globalFilters && !panel.ignore_global_filter) {
doc.query.bool.must = doc.query.bool.must.concat(globalFilters);
}

if (panel.filter) {
doc.query.bool.must.push({
query_string: {
Expand Down
Loading

0 comments on commit 1d136a0

Please sign in to comment.