query_ctrl.ts 3.0 KB

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