query_ctrl.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import './bucket_agg';
  3. import './metric_agg';
  4. import angular from 'angular';
  5. import _ from 'lodash';
  6. import 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 $timeout, private uiSegmentSrv) {
  14. super($scope, $injector);
  15. this.esVersion = this.datasource.esVersion;
  16. this.queryUpdated();
  17. }
  18. getFields(type) {
  19. var jsonStr = angular.toJson({find: 'fields', type: type});
  20. return this.datasource.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 (newJson !== this.rawQueryOld) {
  27. this.rawQueryOld = newJson;
  28. this.refresh();
  29. }
  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. }