query_ctrl.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import './bucket_agg';
  2. import './metric_agg';
  3. import './pipeline_variables';
  4. import angular from 'angular';
  5. import _ from 'lodash';
  6. import * as queryDef from './query_def';
  7. import { QueryCtrl } from 'app/plugins/sdk';
  8. export class ElasticQueryCtrl extends QueryCtrl {
  9. static templateUrl = 'partials/query.editor.html';
  10. esVersion: any;
  11. rawQueryOld: string;
  12. /** @ngInject */
  13. constructor($scope, $injector, private $rootScope, private uiSegmentSrv) {
  14. super($scope, $injector);
  15. this.esVersion = this.datasource.esVersion;
  16. this.target = this.target || {};
  17. this.target.metrics = this.target.metrics || [queryDef.defaultMetricAgg()];
  18. this.target.bucketAggs = this.target.bucketAggs || [queryDef.defaultBucketAgg()];
  19. if (this.target.bucketAggs.length === 0) {
  20. const metric = this.target.metrics[0];
  21. if (!metric || metric.type !== 'raw_document') {
  22. this.target.bucketAggs = [queryDef.defaultBucketAgg()];
  23. }
  24. this.refresh();
  25. }
  26. this.queryUpdated();
  27. }
  28. getFields(type) {
  29. const jsonStr = angular.toJson({ find: 'fields', type: type });
  30. return this.datasource
  31. .metricFindQuery(jsonStr)
  32. .then(this.uiSegmentSrv.transformToSegments(false))
  33. .catch(this.handleQueryError.bind(this));
  34. }
  35. queryUpdated() {
  36. const newJson = angular.toJson(this.datasource.queryBuilder.build(this.target), true);
  37. if (this.rawQueryOld && newJson !== this.rawQueryOld) {
  38. this.refresh();
  39. }
  40. this.rawQueryOld = newJson;
  41. this.$rootScope.appEvent('elastic-query-updated');
  42. }
  43. getCollapsedText() {
  44. const metricAggs = this.target.metrics;
  45. const bucketAggs = this.target.bucketAggs;
  46. const metricAggTypes = queryDef.getMetricAggTypes(this.esVersion);
  47. const bucketAggTypes = queryDef.bucketAggTypes;
  48. let text = '';
  49. if (this.target.query) {
  50. text += 'Query: ' + this.target.query + ', ';
  51. }
  52. text += 'Metrics: ';
  53. _.each(metricAggs, (metric, index) => {
  54. const aggDef: any = _.find(metricAggTypes, { value: metric.type });
  55. text += aggDef.text + '(';
  56. if (aggDef.requiresField) {
  57. text += metric.field;
  58. }
  59. if (aggDef.supportsMultipleBucketPaths) {
  60. text += metric.settings.script.replace(new RegExp('params.', 'g'), '');
  61. }
  62. text += '), ';
  63. });
  64. _.each(bucketAggs, (bucketAgg: any, index: number) => {
  65. if (index === 0) {
  66. text += ' Group by: ';
  67. }
  68. const aggDef: any = _.find(bucketAggTypes, { value: bucketAgg.type });
  69. text += aggDef.text + '(';
  70. if (aggDef.requiresField) {
  71. text += bucketAgg.field;
  72. }
  73. text += '), ';
  74. });
  75. if (this.target.alias) {
  76. text += 'Alias: ' + this.target.alias;
  77. }
  78. return text;
  79. }
  80. handleQueryError(err) {
  81. this.error = err.message || 'Failed to issue metric query';
  82. return [];
  83. }
  84. }