query_builder.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. import { gte, lt } from 'semver';
  2. import { InternalTimeZones } from '@grafana/data';
  3. import {
  4. Filters,
  5. Histogram,
  6. DateHistogram,
  7. Terms,
  8. } from './components/QueryEditor/BucketAggregationsEditor/aggregations';
  9. import {
  10. isMetricAggregationWithField,
  11. isMetricAggregationWithSettings,
  12. isMovingAverageWithModelSettings,
  13. isPipelineAggregation,
  14. isPipelineAggregationWithMultipleBucketPaths,
  15. MetricAggregation,
  16. MetricAggregationWithInlineScript,
  17. } from './components/QueryEditor/MetricAggregationsEditor/aggregations';
  18. import { defaultBucketAgg, defaultMetricAgg, findMetricById, highlightTags } from './query_def';
  19. import { ElasticsearchQuery, TermsQuery } from './types';
  20. import { convertOrderByToMetricId, getScriptValue } from './utils';
  21. export class ElasticQueryBuilder {
  22. timeField: string;
  23. esVersion: string;
  24. constructor(options: { timeField: string; esVersion: string }) {
  25. this.timeField = options.timeField;
  26. this.esVersion = options.esVersion;
  27. }
  28. getRangeFilter() {
  29. const filter: any = {};
  30. filter[this.timeField] = {
  31. gte: '$timeFrom',
  32. lte: '$timeTo',
  33. format: 'epoch_millis',
  34. };
  35. return filter;
  36. }
  37. buildTermsAgg(aggDef: Terms, queryNode: { terms?: any; aggs?: any }, target: ElasticsearchQuery) {
  38. queryNode.terms = { field: aggDef.field };
  39. if (!aggDef.settings) {
  40. return queryNode;
  41. }
  42. // TODO: This default should be somewhere else together with the one used in the UI
  43. const size = aggDef.settings?.size ? parseInt(aggDef.settings.size, 10) : 500;
  44. queryNode.terms.size = size === 0 ? 500 : size;
  45. if (aggDef.settings.orderBy !== void 0) {
  46. queryNode.terms.order = {};
  47. if (aggDef.settings.orderBy === '_term' && gte(this.esVersion, '6.0.0')) {
  48. queryNode.terms.order['_key'] = aggDef.settings.order;
  49. } else {
  50. queryNode.terms.order[aggDef.settings.orderBy] = aggDef.settings.order;
  51. }
  52. // if metric ref, look it up and add it to this agg level
  53. const metricId = convertOrderByToMetricId(aggDef.settings.orderBy);
  54. if (metricId) {
  55. for (let metric of target.metrics || []) {
  56. if (metric.id === metricId) {
  57. if (metric.type === 'count') {
  58. queryNode.terms.order = { _count: aggDef.settings.order };
  59. } else if (isMetricAggregationWithField(metric)) {
  60. queryNode.aggs = {};
  61. queryNode.aggs[metric.id] = {
  62. [metric.type]: { field: metric.field },
  63. };
  64. }
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. if (aggDef.settings.min_doc_count !== void 0) {
  71. queryNode.terms.min_doc_count = parseInt(aggDef.settings.min_doc_count, 10);
  72. if (isNaN(queryNode.terms.min_doc_count)) {
  73. queryNode.terms.min_doc_count = aggDef.settings.min_doc_count;
  74. }
  75. }
  76. if (aggDef.settings.missing) {
  77. queryNode.terms.missing = aggDef.settings.missing;
  78. }
  79. return queryNode;
  80. }
  81. getDateHistogramAgg(aggDef: DateHistogram) {
  82. const esAgg: any = {};
  83. const settings = aggDef.settings || {};
  84. esAgg.field = aggDef.field || this.timeField;
  85. esAgg.min_doc_count = settings.min_doc_count || 0;
  86. esAgg.extended_bounds = { min: '$timeFrom', max: '$timeTo' };
  87. esAgg.format = 'epoch_millis';
  88. if (settings.timeZone && settings.timeZone !== InternalTimeZones.utc) {
  89. esAgg.time_zone = settings.timeZone;
  90. }
  91. if (settings.offset !== '') {
  92. esAgg.offset = settings.offset;
  93. }
  94. const interval = settings.interval === 'auto' ? '$__interval' : settings.interval;
  95. if (gte(this.esVersion, '8.0.0')) {
  96. // The deprecation was actually introduced in 7.0.0, we might want to use that instead of the removal date,
  97. // but it woudl be a breaking change on our side.
  98. esAgg.fixed_interval = interval;
  99. } else {
  100. esAgg.interval = interval;
  101. }
  102. return esAgg;
  103. }
  104. getHistogramAgg(aggDef: Histogram) {
  105. const esAgg: any = {};
  106. const settings = aggDef.settings || {};
  107. esAgg.interval = settings.interval;
  108. esAgg.field = aggDef.field;
  109. esAgg.min_doc_count = settings.min_doc_count || 0;
  110. return esAgg;
  111. }
  112. getFiltersAgg(aggDef: Filters) {
  113. const filterObj: Record<string, { query_string: { query: string; analyze_wildcard: boolean } }> = {};
  114. for (let { query, label } of aggDef.settings?.filters || []) {
  115. filterObj[label || query] = {
  116. query_string: {
  117. query: query,
  118. analyze_wildcard: true,
  119. },
  120. };
  121. }
  122. return filterObj;
  123. }
  124. documentQuery(query: any, size: number) {
  125. query.size = size;
  126. query.sort = [
  127. {
  128. [this.timeField]: { order: 'desc', unmapped_type: 'boolean' },
  129. },
  130. {
  131. _doc: { order: 'desc' },
  132. },
  133. ];
  134. // fields field not supported on ES 5.x
  135. if (lt(this.esVersion, '5.0.0')) {
  136. query.fields = ['*', '_source'];
  137. }
  138. query.script_fields = {};
  139. return query;
  140. }
  141. addAdhocFilters(query: any, adhocFilters: any) {
  142. if (!adhocFilters) {
  143. return;
  144. }
  145. let i, filter, condition: any, queryCondition: any;
  146. for (i = 0; i < adhocFilters.length; i++) {
  147. filter = adhocFilters[i];
  148. condition = {};
  149. condition[filter.key] = filter.value;
  150. queryCondition = {};
  151. queryCondition[filter.key] = { query: filter.value };
  152. switch (filter.operator) {
  153. case '=':
  154. if (!query.query.bool.must) {
  155. query.query.bool.must = [];
  156. }
  157. query.query.bool.must.push({ match_phrase: queryCondition });
  158. break;
  159. case '!=':
  160. if (!query.query.bool.must_not) {
  161. query.query.bool.must_not = [];
  162. }
  163. query.query.bool.must_not.push({ match_phrase: queryCondition });
  164. break;
  165. case '<':
  166. condition[filter.key] = { lt: filter.value };
  167. query.query.bool.filter.push({ range: condition });
  168. break;
  169. case '>':
  170. condition[filter.key] = { gt: filter.value };
  171. query.query.bool.filter.push({ range: condition });
  172. break;
  173. case '=~':
  174. query.query.bool.filter.push({ regexp: condition });
  175. break;
  176. case '!~':
  177. query.query.bool.filter.push({
  178. bool: { must_not: { regexp: condition } },
  179. });
  180. break;
  181. }
  182. }
  183. }
  184. build(target: ElasticsearchQuery, adhocFilters?: any) {
  185. // make sure query has defaults;
  186. target.metrics = target.metrics || [defaultMetricAgg()];
  187. target.bucketAggs = target.bucketAggs || [defaultBucketAgg()];
  188. target.timeField = this.timeField;
  189. let metric: MetricAggregation;
  190. let i, j, pv, nestedAggs;
  191. const query: any = {
  192. size: 0,
  193. query: {
  194. bool: {
  195. filter: [{ range: this.getRangeFilter() }],
  196. },
  197. },
  198. };
  199. if (target.query && target.query !== '') {
  200. query.query.bool.filter = [
  201. ...query.query.bool.filter,
  202. {
  203. query_string: {
  204. analyze_wildcard: true,
  205. query: target.query,
  206. },
  207. },
  208. ];
  209. }
  210. this.addAdhocFilters(query, adhocFilters);
  211. // If target doesn't have bucketAggs and type is not raw_document, it is invalid query.
  212. if (target.bucketAggs.length === 0) {
  213. metric = target.metrics[0];
  214. if (!metric || !(metric.type === 'raw_document' || metric.type === 'raw_data')) {
  215. throw { message: 'Invalid query' };
  216. }
  217. }
  218. /* Handle document query:
  219. * Check if metric type is raw_document. If metric doesn't have size (or size is 0), update size to 500.
  220. * Otherwise it will not be a valid query and error will be thrown.
  221. */
  222. if (target.metrics?.[0]?.type === 'raw_document' || target.metrics?.[0]?.type === 'raw_data') {
  223. metric = target.metrics[0];
  224. // TODO: This default should be somewhere else together with the one used in the UI
  225. const size = metric.settings?.size ? parseInt(metric.settings.size, 10) : 500;
  226. return this.documentQuery(query, size || 500);
  227. }
  228. nestedAggs = query;
  229. for (i = 0; i < target.bucketAggs.length; i++) {
  230. const aggDef = target.bucketAggs[i];
  231. const esAgg: any = {};
  232. switch (aggDef.type) {
  233. case 'date_histogram': {
  234. esAgg['date_histogram'] = this.getDateHistogramAgg(aggDef);
  235. break;
  236. }
  237. case 'histogram': {
  238. esAgg['histogram'] = this.getHistogramAgg(aggDef);
  239. break;
  240. }
  241. case 'filters': {
  242. esAgg['filters'] = { filters: this.getFiltersAgg(aggDef) };
  243. break;
  244. }
  245. case 'terms': {
  246. this.buildTermsAgg(aggDef, esAgg, target);
  247. break;
  248. }
  249. case 'geohash_grid': {
  250. esAgg['geohash_grid'] = {
  251. field: aggDef.field,
  252. precision: aggDef.settings?.precision,
  253. };
  254. break;
  255. }
  256. }
  257. nestedAggs.aggs = nestedAggs.aggs || {};
  258. nestedAggs.aggs[aggDef.id] = esAgg;
  259. nestedAggs = esAgg;
  260. }
  261. nestedAggs.aggs = {};
  262. for (i = 0; i < target.metrics.length; i++) {
  263. metric = target.metrics[i];
  264. if (metric.type === 'count') {
  265. continue;
  266. }
  267. const aggField: any = {};
  268. let metricAgg: any = {};
  269. if (isPipelineAggregation(metric)) {
  270. if (isPipelineAggregationWithMultipleBucketPaths(metric)) {
  271. if (metric.pipelineVariables) {
  272. metricAgg = {
  273. buckets_path: {},
  274. };
  275. for (j = 0; j < metric.pipelineVariables.length; j++) {
  276. pv = metric.pipelineVariables[j];
  277. if (pv.name && pv.pipelineAgg && /^\d*$/.test(pv.pipelineAgg)) {
  278. const appliedAgg = findMetricById(target.metrics, pv.pipelineAgg);
  279. if (appliedAgg) {
  280. if (appliedAgg.type === 'count') {
  281. metricAgg.buckets_path[pv.name] = '_count';
  282. } else {
  283. metricAgg.buckets_path[pv.name] = pv.pipelineAgg;
  284. }
  285. }
  286. }
  287. }
  288. } else {
  289. continue;
  290. }
  291. } else {
  292. if (metric.field && /^\d*$/.test(metric.field)) {
  293. const appliedAgg = findMetricById(target.metrics, metric.field);
  294. if (appliedAgg) {
  295. if (appliedAgg.type === 'count') {
  296. metricAgg = { buckets_path: '_count' };
  297. } else {
  298. metricAgg = { buckets_path: metric.field };
  299. }
  300. }
  301. } else {
  302. continue;
  303. }
  304. }
  305. } else if (isMetricAggregationWithField(metric)) {
  306. metricAgg = { field: metric.field };
  307. }
  308. if (isMetricAggregationWithSettings(metric)) {
  309. Object.entries(metric.settings || {})
  310. .filter(([_, v]) => v !== null)
  311. .forEach(([k, v]) => {
  312. metricAgg[k] =
  313. k === 'script' ? this.buildScript(getScriptValue(metric as MetricAggregationWithInlineScript)) : v;
  314. });
  315. // Elasticsearch isn't generally too picky about the data types in the request body,
  316. // however some fields are required to be numeric.
  317. // Users might have already created some of those with before, where the values were numbers.
  318. switch (metric.type) {
  319. case 'moving_avg':
  320. metricAgg = {
  321. ...metricAgg,
  322. ...(metricAgg?.window !== undefined && { window: this.toNumber(metricAgg.window) }),
  323. ...(metricAgg?.predict !== undefined && { predict: this.toNumber(metricAgg.predict) }),
  324. ...(isMovingAverageWithModelSettings(metric) && {
  325. settings: {
  326. ...metricAgg.settings,
  327. ...Object.fromEntries(
  328. Object.entries(metricAgg.settings || {})
  329. // Only format properties that are required to be numbers
  330. .filter(([settingName]) => ['alpha', 'beta', 'gamma', 'period'].includes(settingName))
  331. // omitting undefined
  332. .filter(([_, stringValue]) => stringValue !== undefined)
  333. .map(([_, stringValue]) => [_, this.toNumber(stringValue)])
  334. ),
  335. },
  336. }),
  337. };
  338. break;
  339. case 'serial_diff':
  340. metricAgg = {
  341. ...metricAgg,
  342. ...(metricAgg.lag !== undefined && {
  343. lag: this.toNumber(metricAgg.lag),
  344. }),
  345. };
  346. break;
  347. case 'top_metrics':
  348. metricAgg = {
  349. metrics: metric.settings?.metrics?.map((field) => ({ field })),
  350. size: 1,
  351. };
  352. if (metric.settings?.orderBy) {
  353. metricAgg.sort = [{ [metric.settings?.orderBy]: metric.settings?.order }];
  354. }
  355. break;
  356. }
  357. }
  358. aggField[metric.type] = metricAgg;
  359. nestedAggs.aggs[metric.id] = aggField;
  360. }
  361. return query;
  362. }
  363. private buildScript(script: string) {
  364. if (gte(this.esVersion, '5.6.0')) {
  365. return script;
  366. }
  367. return {
  368. inline: script,
  369. };
  370. }
  371. private toNumber(stringValue: unknown): unknown | number {
  372. const parsedValue = parseFloat(`${stringValue}`);
  373. if (isNaN(parsedValue)) {
  374. return stringValue;
  375. }
  376. return parsedValue;
  377. }
  378. getTermsQuery(queryDef: TermsQuery) {
  379. const query: any = {
  380. size: 0,
  381. query: {
  382. bool: {
  383. filter: [{ range: this.getRangeFilter() }],
  384. },
  385. },
  386. };
  387. if (queryDef.query) {
  388. query.query.bool.filter.push({
  389. query_string: {
  390. analyze_wildcard: true,
  391. query: queryDef.query,
  392. },
  393. });
  394. }
  395. let size = 500;
  396. if (queryDef.size) {
  397. size = queryDef.size;
  398. }
  399. query.aggs = {
  400. '1': {
  401. terms: {
  402. field: queryDef.field,
  403. size: size,
  404. order: {},
  405. },
  406. },
  407. };
  408. // Default behaviour is to order results by { _key: asc }
  409. // queryDef.order allows selection of asc/desc
  410. // queryDef.orderBy allows selection of doc_count ordering (defaults desc)
  411. const { orderBy = 'key', order = orderBy === 'doc_count' ? 'desc' : 'asc' } = queryDef;
  412. if (['asc', 'desc'].indexOf(order) < 0) {
  413. throw { message: `Invalid query sort order ${order}` };
  414. }
  415. switch (orderBy) {
  416. case 'key':
  417. case 'term':
  418. const keyname = gte(this.esVersion, '6.0.0') ? '_key' : '_term';
  419. query.aggs['1'].terms.order[keyname] = order;
  420. break;
  421. case 'doc_count':
  422. query.aggs['1'].terms.order['_count'] = order;
  423. break;
  424. default:
  425. throw { message: `Invalid query sort type ${orderBy}` };
  426. }
  427. return query;
  428. }
  429. getLogsQuery(target: ElasticsearchQuery, limit: number, adhocFilters?: any) {
  430. let query: any = {
  431. size: 0,
  432. query: {
  433. bool: {
  434. filter: [{ range: this.getRangeFilter() }],
  435. },
  436. },
  437. };
  438. this.addAdhocFilters(query, adhocFilters);
  439. if (target.query) {
  440. query.query.bool.filter.push({
  441. query_string: {
  442. analyze_wildcard: true,
  443. query: target.query,
  444. },
  445. });
  446. }
  447. query = this.documentQuery(query, limit);
  448. return {
  449. ...query,
  450. aggs: this.build(target, null).aggs,
  451. highlight: {
  452. fields: {
  453. '*': {},
  454. },
  455. pre_tags: [highlightTags.pre],
  456. post_tags: [highlightTags.post],
  457. fragment_size: 2147483647,
  458. },
  459. };
  460. }
  461. }