grafana/public/app/plugins/datasource/elasticsearch/query_builder.ts

518 lines
15 KiB
TypeScript
Raw Normal View History

import { InternalTimeZones } from '@grafana/data';
import {
Filters,
Histogram,
DateHistogram,
Terms,
} from './components/QueryEditor/BucketAggregationsEditor/aggregations';
import {
isMetricAggregationWithField,
isMetricAggregationWithSettings,
isMovingAverageWithModelSettings,
isPipelineAggregation,
isPipelineAggregationWithMultipleBucketPaths,
MetricAggregation,
MetricAggregationWithInlineScript,
} from './components/QueryEditor/MetricAggregationsEditor/aggregations';
import { defaultBucketAgg, defaultMetricAgg, findMetricById, highlightTags } from './query_def';
import { ElasticsearchQuery, TermsQuery } from './types';
import { convertOrderByToMetricId, getScriptValue } from './utils';
export class ElasticQueryBuilder {
timeField: string;
constructor(options: { timeField: string }) {
this.timeField = options.timeField;
}
getRangeFilter() {
const filter: any = {};
filter[this.timeField] = {
2017-12-20 05:33:33 -06:00
gte: '$timeFrom',
lte: '$timeTo',
format: 'epoch_millis',
};
return filter;
}
buildTermsAgg(aggDef: Terms, queryNode: { terms?: any; aggs?: any }, target: ElasticsearchQuery) {
queryNode.terms = { field: aggDef.field };
2015-09-06 07:45:12 -05:00
if (!aggDef.settings) {
return queryNode;
}
// TODO: This default should be somewhere else together with the one used in the UI
const size = aggDef.settings?.size ? parseInt(aggDef.settings.size, 10) : 500;
queryNode.terms.size = size === 0 ? 500 : size;
if (aggDef.settings.orderBy !== void 0) {
2015-09-06 07:45:12 -05:00
queryNode.terms.order = {};
if (aggDef.settings.orderBy === '_term') {
queryNode.terms.order['_key'] = aggDef.settings.order;
} else {
queryNode.terms.order[aggDef.settings.orderBy] = aggDef.settings.order;
}
2015-09-06 07:45:12 -05:00
// if metric ref, look it up and add it to this agg level
const metricId = convertOrderByToMetricId(aggDef.settings.orderBy);
if (metricId) {
for (let metric of target.metrics || []) {
if (metric.id === metricId) {
if (metric.type === 'count') {
queryNode.terms.order = { _count: aggDef.settings.order };
} else if (isMetricAggregationWithField(metric)) {
queryNode.aggs = {};
queryNode.aggs[metric.id] = {
[metric.type]: { field: metric.field },
};
}
2015-09-06 07:45:12 -05:00
break;
}
}
}
}
if (aggDef.settings.min_doc_count !== void 0) {
queryNode.terms.min_doc_count = parseInt(aggDef.settings.min_doc_count, 10);
if (isNaN(queryNode.terms.min_doc_count)) {
queryNode.terms.min_doc_count = aggDef.settings.min_doc_count;
}
}
if (aggDef.settings.missing) {
queryNode.terms.missing = aggDef.settings.missing;
}
2015-09-06 07:45:12 -05:00
return queryNode;
}
2015-09-06 07:45:12 -05:00
getDateHistogramAgg(aggDef: DateHistogram) {
const esAgg: any = {};
const settings = aggDef.settings || {};
esAgg.field = aggDef.field || this.timeField;
esAgg.min_doc_count = settings.min_doc_count || 0;
2017-12-20 05:33:33 -06:00
esAgg.extended_bounds = { min: '$timeFrom', max: '$timeTo' };
esAgg.format = 'epoch_millis';
if (settings.timeZone && settings.timeZone !== InternalTimeZones.utc) {
esAgg.time_zone = settings.timeZone;
}
if (settings.offset !== '') {
esAgg.offset = settings.offset;
}
const interval = settings.interval === 'auto' ? '${__interval_ms}ms' : settings.interval;
esAgg.fixed_interval = interval;
return esAgg;
}
getHistogramAgg(aggDef: Histogram) {
const esAgg: any = {};
const settings = aggDef.settings || {};
esAgg.interval = settings.interval;
esAgg.field = aggDef.field;
esAgg.min_doc_count = settings.min_doc_count || 0;
return esAgg;
}
getFiltersAgg(aggDef: Filters) {
const filterObj: Record<string, { query_string: { query: string; analyze_wildcard: boolean } }> = {};
for (let { query, label } of aggDef.settings?.filters || []) {
filterObj[label || query] = {
query_string: {
query: query,
2017-12-20 05:33:33 -06:00
analyze_wildcard: true,
},
};
}
return filterObj;
}
documentQuery(query: any, size: number) {
query.size = size;
query.sort = [
{
[this.timeField]: { order: 'desc', unmapped_type: 'boolean' },
},
{
_doc: { order: 'desc' },
},
];
2016-11-08 15:18:59 -06:00
query.script_fields = {};
return query;
}
addAdhocFilters(query: any, adhocFilters: any) {
if (!adhocFilters) {
return;
}
let i, filter, condition: any, queryCondition: any;
for (i = 0; i < adhocFilters.length; i++) {
filter = adhocFilters[i];
condition = {};
condition[filter.key] = filter.value;
queryCondition = {};
queryCondition[filter.key] = { query: filter.value };
switch (filter.operator) {
2017-12-20 05:33:33 -06:00
case '=':
if (!query.query.bool.must) {
query.query.bool.must = [];
}
query.query.bool.must.push({ match_phrase: queryCondition });
break;
2017-12-20 05:33:33 -06:00
case '!=':
if (!query.query.bool.must_not) {
query.query.bool.must_not = [];
}
query.query.bool.must_not.push({ match_phrase: queryCondition });
break;
2017-12-20 05:33:33 -06:00
case '<':
condition[filter.key] = { lt: filter.value };
query.query.bool.filter.push({ range: condition });
break;
2017-12-20 05:33:33 -06:00
case '>':
condition[filter.key] = { gt: filter.value };
query.query.bool.filter.push({ range: condition });
break;
2017-12-20 05:33:33 -06:00
case '=~':
query.query.bool.filter.push({ regexp: condition });
break;
2017-12-20 05:33:33 -06:00
case '!~':
query.query.bool.filter.push({
2017-12-20 05:33:33 -06:00
bool: { must_not: { regexp: condition } },
});
break;
}
}
Create annotations (#8197) * annotations: add 25px space for events section * annotations: restored create annotation action * annotations: able to use fa icons as event markers * annotations: initial emoji support from twemoji lib * annotations: adjust fa icon position * annotations: initial emoji picker * annotation: include user info into annotation requests * annotation: add icon info * annotation: display user info in tooltip * annotation: fix region saving * annotation: initial region markers * annotation: fix region clearing (add flot-temp-elem class) * annotation: adjust styles a bit * annotations: minor fixes * annoations: removed userId look in loop, need a sql join or a user cache for this * annotation: fix invisible events * lib: changed twitter emoij lib to be npm dependency * annotation: add icon picker to Add Annotation dialog * annotation: save icon to annotation table * annotation: able to set custom icon for annotation added by user * annotations: fix emoji after library upgrade (switch to 72px) * emoji: temporary remove bad code points * annotations: improve icon picker * annotations: icon show icon picker at the top * annotations: use svg for emoji * annotations: fix region drawing when add annotation editor opened * annotations: use flot lib for drawing region fill * annotations: move regions building into event_manager * annotations: don't draw additional space if no events are got * annotations: deduplicate events * annotations: properly render cut regions * annotations: fix cut region building * annotations: refactor * annotations: adjust event section size * add-annotations: fix undefined default icon * create-annotations: edit event (frontend part) * fixed bug causes error when hover event marker * create-annotations: update event (backend) * ignore grafana-server debug binary in git (created VS Code) * create-annotations: use PUT request for updating annotation. * create-annotations: fixed time format when editing existing event * create-annotations: support for region update * create-annotations: fix bug with limit and event type * create-annotations: delete annotation * create-annotations: show only selected icon in edit mode * create-annotations: show event editor only for users with at least Editor role * create-annotations: handle double-sized emoji codepoints * create-annotations: refactor use CP_SEPARATOR from emojiDef * create-annotations: update emoji list, add categories. * create-annotations: copy SVG emoji into public/vendor/npm and use it as a base path * create-annotations: initial tabs for emoji picker * emoji-picker: adjust styles * emoji-picker: minor refactor * emoji-picker: refactor - rename and move into one directory * emoji-picker: build emoji elements on app load, not on picker open * emoji-picker: fix emoji searching * emoji-picker: refactor * emoji-picker: capitalize category name * emoji-picker: refactor move buildEmojiElem() into emoji_converter.ts for future reuse. * jquery.flot.events: refactor use buildEmojiElem() for making emojis, remove unused code for font awesome based icons. * emoji_converter: handle converting error * tech: updated * merged with master * shore: clean up some stuff * annotation: wip tags * annotation: filtering by tags * tags: parse out spaces etc. from a tags string * annotations: use tagsinput component for tag filtering * annotation: wip work on how we query alert & panel annotations * annotations: support for updating tags in an annotation * linting * annotations: work on unifying how alert history annotations and manual panel annotations are created * tslint: fixes * tags: create tag on blur as well Currently, the tags directive only creates the tag when the user presses enter. This change means the tag is created on blur as well (when the user clicks outside the input field). * annotations: fix update after refactoring * annotations: progress on how alert annotations are fetched * annotations: minor progress * annotations: progress * annotation: minor progress * annotations: move tag parsing from tooltip to ds Instead of parsing a tag string into an array in the annotation_tooltip class, this moves the parsing to the datasources. InfluxDB ds already does that parsing. Graphite now has it. * annotations: more work on querying * annotations: change from tags as string to array when saving in the db and in the api. * annotations: delete tag link if removed on edit * annotation: more work on depricating annotation title * annotations: delete tag links on delete * annotations: fix for find * annotation: added user to annotation tooltip and added alertName to annoation dto * annotations: use id from route instead from cmd for updating * annotations: http api docs * create annotation: last edits * annotations: minor fix for querying annotations before dashboard saved * annotations: fix for popover placement when legend is on the side (and doubel render pass is causing original marker to be removed) * annotations: changing how the built in query gets added * annotation: added time to header in edit mode * tests: fixed jshint built issue
2017-10-07 03:31:39 -05:00
}
build(target: ElasticsearchQuery, adhocFilters?: any) {
// make sure query has defaults;
target.metrics = target.metrics || [defaultMetricAgg()];
target.bucketAggs = target.bucketAggs || [defaultBucketAgg()];
target.timeField = this.timeField;
let metric: MetricAggregation;
let i, j, pv, nestedAggs;
const query: any = {
size: 0,
query: {
bool: {
filter: [{ range: this.getRangeFilter() }],
2017-12-20 05:33:33 -06:00
},
},
};
if (target.query && target.query !== '') {
query.query.bool.filter = [
...query.query.bool.filter,
{
query_string: {
analyze_wildcard: true,
query: target.query,
},
},
];
}
this.addAdhocFilters(query, adhocFilters);
// 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];
if (!metric || !(metric.type === 'raw_document' || metric.type === 'raw_data')) {
2017-12-20 05:33:33 -06:00
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' || target.metrics?.[0]?.type === 'raw_data') {
metric = target.metrics[0];
// TODO: This default should be somewhere else together with the one used in the UI
const size = metric.settings?.size ? parseInt(metric.settings.size, 10) : 500;
return this.documentQuery(query, size || 500);
}
nestedAggs = query;
for (i = 0; i < target.bucketAggs.length; i++) {
const aggDef = target.bucketAggs[i];
const esAgg: any = {};
switch (aggDef.type) {
2017-12-20 05:33:33 -06:00
case 'date_histogram': {
esAgg['date_histogram'] = this.getDateHistogramAgg(aggDef);
break;
}
2017-12-20 05:33:33 -06:00
case 'histogram': {
esAgg['histogram'] = this.getHistogramAgg(aggDef);
break;
}
2017-12-20 05:33:33 -06:00
case 'filters': {
esAgg['filters'] = { filters: this.getFiltersAgg(aggDef) };
break;
}
2017-12-20 05:33:33 -06:00
case 'terms': {
2015-09-06 07:45:12 -05:00
this.buildTermsAgg(aggDef, esAgg, target);
break;
}
2017-12-20 05:33:33 -06:00
case 'geohash_grid': {
esAgg['geohash_grid'] = {
field: aggDef.field,
precision: aggDef.settings?.precision,
};
break;
}
}
nestedAggs.aggs = nestedAggs.aggs || {};
nestedAggs.aggs[aggDef.id] = esAgg;
nestedAggs = esAgg;
}
nestedAggs.aggs = {};
for (i = 0; i < target.metrics.length; i++) {
metric = target.metrics[i];
2017-12-20 05:33:33 -06:00
if (metric.type === 'count') {
continue;
}
const aggField: any = {};
Elasticsearch: Add Top Metrics Aggregation and X-Pack support (#33041) * Elasticsearch: Add Top Metrics Aggregation * Adding support for non-timeseries visualizations * removing console.logs * restoring loadOptions type * Honor xpack setting * Adding test for elastic_response * adding test for query builder * Adding support of alerting * Fixing separator spelling * Fixing linting issues * attempting to reduce cyclomatic complexity * Adding elastic77 Docker block * Update public/app/plugins/datasource/elasticsearch/components/QueryEditor/MetricAggregationsEditor/MetricEditor.test.tsx Co-authored-by: Giordano Ricci <grdnricci@gmail.com> * refactoring MetricsEditor tests * Fixing typo * Change getFields type & move TopMetrics to a separate component * Fix SegmentAsync styles in TopMetrics Settings * Fix field types for TopMetrics * WIP * Refactoring client side to support multiple top metrics * Adding tests and finishing go implimentation * removing fmt lib from debugging * fixing tests * reducing the cyclomatic complexity * Update public/app/plugins/datasource/elasticsearch/elastic_response.ts Co-authored-by: Giordano Ricci <grdnricci@gmail.com> * Update public/app/plugins/datasource/elasticsearch/hooks/useFields.ts Co-authored-by: Giordano Ricci <grdnricci@gmail.com> * Checking for possible nil value * Fixing types * fix fake-data-gen param * fix useFields hook * Removing aggregateBy and size * Fixing go tests * Fixing TS tests * fixing tests * Fixes * Remove date from top_metrics fields * Restore previous formatting * Update pkg/tsdb/elasticsearch/client/models.go Co-authored-by: Dimitris Sotirakis <dimitrios.sotirakis@grafana.com> * Update pkg/tsdb/elasticsearch/client/models.go Co-authored-by: Dimitris Sotirakis <dimitrios.sotirakis@grafana.com> * Fix code review comments on processTopMetricValue * Remove underscore from variable names * Remove intermediate array definition * Refactor test to use testify Co-authored-by: Giordano Ricci <grdnricci@gmail.com> Co-authored-by: Elfo404 <me@giordanoricci.com> Co-authored-by: Dimitris Sotirakis <dimitrios.sotirakis@grafana.com>
2021-06-04 05:07:59 -05:00
let metricAgg: any = {};
2015-12-08 05:07:56 -06:00
if (isPipelineAggregation(metric)) {
if (isPipelineAggregationWithMultipleBucketPaths(metric)) {
if (metric.pipelineVariables) {
metricAgg = {
buckets_path: {},
};
for (j = 0; j < metric.pipelineVariables.length; j++) {
pv = metric.pipelineVariables[j];
if (pv.name && pv.pipelineAgg && /^\d*$/.test(pv.pipelineAgg)) {
const appliedAgg = findMetricById(target.metrics, pv.pipelineAgg);
if (appliedAgg) {
if (appliedAgg.type === 'count') {
metricAgg.buckets_path[pv.name] = '_count';
} else {
metricAgg.buckets_path[pv.name] = pv.pipelineAgg;
}
}
}
2018-11-15 12:06:47 -06:00
}
} else {
continue;
2018-11-15 12:06:47 -06:00
}
2015-12-09 07:21:48 -06:00
} else {
if (metric.field && /^\d*$/.test(metric.field)) {
const appliedAgg = findMetricById(target.metrics, metric.field);
if (appliedAgg) {
if (appliedAgg.type === 'count') {
metricAgg = { buckets_path: '_count' };
} else {
metricAgg = { buckets_path: metric.field };
}
}
} else {
continue;
}
2015-12-09 07:21:48 -06:00
}
} else if (isMetricAggregationWithField(metric)) {
metricAgg = { field: metric.field };
}
2015-12-08 05:07:56 -06:00
if (isMetricAggregationWithSettings(metric)) {
Object.entries(metric.settings || {})
.filter(([_, v]) => v !== null)
.forEach(([k, v]) => {
metricAgg[k] =
k === 'script' ? this.buildScript(getScriptValue(metric as MetricAggregationWithInlineScript)) : v;
});
// Elasticsearch isn't generally too picky about the data types in the request body,
// however some fields are required to be numeric.
// Users might have already created some of those with before, where the values were numbers.
Elasticsearch: Add Top Metrics Aggregation and X-Pack support (#33041) * Elasticsearch: Add Top Metrics Aggregation * Adding support for non-timeseries visualizations * removing console.logs * restoring loadOptions type * Honor xpack setting * Adding test for elastic_response * adding test for query builder * Adding support of alerting * Fixing separator spelling * Fixing linting issues * attempting to reduce cyclomatic complexity * Adding elastic77 Docker block * Update public/app/plugins/datasource/elasticsearch/components/QueryEditor/MetricAggregationsEditor/MetricEditor.test.tsx Co-authored-by: Giordano Ricci <grdnricci@gmail.com> * refactoring MetricsEditor tests * Fixing typo * Change getFields type & move TopMetrics to a separate component * Fix SegmentAsync styles in TopMetrics Settings * Fix field types for TopMetrics * WIP * Refactoring client side to support multiple top metrics * Adding tests and finishing go implimentation * removing fmt lib from debugging * fixing tests * reducing the cyclomatic complexity * Update public/app/plugins/datasource/elasticsearch/elastic_response.ts Co-authored-by: Giordano Ricci <grdnricci@gmail.com> * Update public/app/plugins/datasource/elasticsearch/hooks/useFields.ts Co-authored-by: Giordano Ricci <grdnricci@gmail.com> * Checking for possible nil value * Fixing types * fix fake-data-gen param * fix useFields hook * Removing aggregateBy and size * Fixing go tests * Fixing TS tests * fixing tests * Fixes * Remove date from top_metrics fields * Restore previous formatting * Update pkg/tsdb/elasticsearch/client/models.go Co-authored-by: Dimitris Sotirakis <dimitrios.sotirakis@grafana.com> * Update pkg/tsdb/elasticsearch/client/models.go Co-authored-by: Dimitris Sotirakis <dimitrios.sotirakis@grafana.com> * Fix code review comments on processTopMetricValue * Remove underscore from variable names * Remove intermediate array definition * Refactor test to use testify Co-authored-by: Giordano Ricci <grdnricci@gmail.com> Co-authored-by: Elfo404 <me@giordanoricci.com> Co-authored-by: Dimitris Sotirakis <dimitrios.sotirakis@grafana.com>
2021-06-04 05:07:59 -05:00
switch (metric.type) {
case 'moving_avg':
metricAgg = {
...metricAgg,
...(metricAgg?.window !== undefined && { window: this.toNumber(metricAgg.window) }),
...(metricAgg?.predict !== undefined && { predict: this.toNumber(metricAgg.predict) }),
...(isMovingAverageWithModelSettings(metric) && {
settings: {
...metricAgg.settings,
...Object.fromEntries(
Object.entries(metricAgg.settings || {})
// Only format properties that are required to be numbers
.filter(([settingName]) => ['alpha', 'beta', 'gamma', 'period'].includes(settingName))
// omitting undefined
.filter(([_, stringValue]) => stringValue !== undefined)
.map(([_, stringValue]) => [_, this.toNumber(stringValue)])
),
},
}),
};
break;
case 'serial_diff':
metricAgg = {
...metricAgg,
...(metricAgg.lag !== undefined && {
lag: this.toNumber(metricAgg.lag),
}),
};
break;
case 'top_metrics':
metricAgg = {
metrics: metric.settings?.metrics?.map((field) => ({ field })),
size: 1,
};
if (metric.settings?.orderBy) {
metricAgg.sort = [{ [metric.settings?.orderBy]: metric.settings?.order }];
}
break;
}
}
aggField[metric.type] = metricAgg;
nestedAggs.aggs[metric.id] = aggField;
}
return query;
}
private buildScript(script: string) {
return script;
}
private toNumber(stringValue: unknown): unknown | number {
const parsedValue = parseFloat(`${stringValue}`);
if (isNaN(parsedValue)) {
return stringValue;
}
return parsedValue;
}
getTermsQuery(queryDef: TermsQuery) {
const query: any = {
size: 0,
query: {
bool: {
2017-12-20 05:33:33 -06:00
filter: [{ range: this.getRangeFilter() }],
},
},
};
if (queryDef.query) {
query.query.bool.filter.push({
query_string: {
analyze_wildcard: true,
2017-12-20 05:33:33 -06:00
query: queryDef.query,
},
});
2016-05-10 12:38:22 -05:00
}
let size = 500;
if (queryDef.size) {
size = queryDef.size;
}
query.aggs = {
2017-12-20 05:33:33 -06:00
'1': {
terms: {
field: queryDef.field,
size: size,
order: {},
2017-12-20 05:33:33 -06:00
},
},
};
// Default behaviour is to order results by { _key: asc }
// queryDef.order allows selection of asc/desc
// queryDef.orderBy allows selection of doc_count ordering (defaults desc)
const { orderBy = 'key', order = orderBy === 'doc_count' ? 'desc' : 'asc' } = queryDef;
if (['asc', 'desc'].indexOf(order) < 0) {
throw { message: `Invalid query sort order ${order}` };
}
switch (orderBy) {
case 'key':
case 'term':
const keyname = '_key';
query.aggs['1'].terms.order[keyname] = order;
break;
case 'doc_count':
query.aggs['1'].terms.order['_count'] = order;
break;
default:
throw { message: `Invalid query sort type ${orderBy}` };
}
return query;
}
getLogsQuery(target: ElasticsearchQuery, limit: number, adhocFilters?: any) {
let query: any = {
size: 0,
query: {
bool: {
filter: [{ range: this.getRangeFilter() }],
},
},
};
this.addAdhocFilters(query, adhocFilters);
if (target.query) {
query.query.bool.filter.push({
query_string: {
analyze_wildcard: true,
query: target.query,
},
});
}
query = this.documentQuery(query, limit);
return {
...query,
aggs: this.build(target, null).aggs,
highlight: {
fields: {
'*': {},
},
pre_tags: [highlightTags.pre],
post_tags: [highlightTags.post],
fragment_size: 2147483647,
},
};
}
}