query_ctrl.ts 2.2 KB

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