query_ctrl.ts 2.6 KB

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