query_ctrl.ts 2.2 KB

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