query_ctrl.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.queryUpdated();
  16. }
  17. getFields(type) {
  18. var jsonStr = angular.toJson({ find: 'fields', type: type });
  19. return this.datasource
  20. .metricFindQuery(jsonStr)
  21. .then(this.uiSegmentSrv.transformToSegments(false))
  22. .catch(this.handleQueryError.bind(this));
  23. }
  24. queryUpdated() {
  25. var newJson = angular.toJson(this.datasource.queryBuilder.build(this.target), true);
  26. if (this.rawQueryOld && newJson !== this.rawQueryOld) {
  27. this.refresh();
  28. }
  29. this.rawQueryOld = newJson;
  30. this.$rootScope.appEvent('elastic-query-updated');
  31. }
  32. getCollapsedText() {
  33. var metricAggs = this.target.metrics;
  34. var bucketAggs = this.target.bucketAggs;
  35. var metricAggTypes = queryDef.getMetricAggTypes(this.esVersion);
  36. var bucketAggTypes = queryDef.bucketAggTypes;
  37. var text = '';
  38. if (this.target.query) {
  39. text += 'Query: ' + this.target.query + ', ';
  40. }
  41. text += 'Metrics: ';
  42. _.each(metricAggs, (metric, index) => {
  43. var aggDef = _.find(metricAggTypes, { value: metric.type });
  44. text += aggDef.text + '(';
  45. if (aggDef.requiresField) {
  46. text += metric.field;
  47. }
  48. text += '), ';
  49. });
  50. _.each(bucketAggs, (bucketAgg, index) => {
  51. if (index === 0) {
  52. text += ' Group by: ';
  53. }
  54. var aggDef = _.find(bucketAggTypes, { value: bucketAgg.type });
  55. text += aggDef.text + '(';
  56. if (aggDef.requiresField) {
  57. text += bucketAgg.field;
  58. }
  59. text += '), ';
  60. });
  61. if (this.target.alias) {
  62. text += 'Alias: ' + this.target.alias;
  63. }
  64. return text;
  65. }
  66. handleQueryError(err) {
  67. this.error = err.message || 'Failed to issue metric query';
  68. return [];
  69. }
  70. }