query_ctrl.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. define([
  2. 'angular',
  3. ],
  4. function (angular) {
  5. 'use strict';
  6. var module = angular.module('grafana.controllers');
  7. module.controller('ElasticQueryCtrl', function($scope, $timeout, uiSegmentSrv) {
  8. $scope.esVersion = $scope.datasource.esVersion;
  9. $scope.init = function() {
  10. var target = $scope.target;
  11. if (!target) { return; }
  12. $scope.queryUpdated();
  13. };
  14. $scope.getFields = function(type) {
  15. var jsonStr = angular.toJson({find: 'fields', type: type});
  16. return $scope.datasource.metricFindQuery(jsonStr)
  17. .then(uiSegmentSrv.transformToSegments(false))
  18. .then(null, $scope.handleQueryError);
  19. };
  20. $scope.queryUpdated = function() {
  21. var newJson = angular.toJson($scope.datasource.queryBuilder.build($scope.target), true);
  22. if (newJson !== $scope.oldQueryRaw) {
  23. $scope.rawQueryOld = newJson;
  24. $scope.get_data();
  25. }
  26. $scope.appEvent('elastic-query-updated');
  27. };
  28. $scope.handleQueryError = function(err) {
  29. $scope.parserError = err.message || 'Failed to issue metric query';
  30. return [];
  31. };
  32. $scope.init();
  33. });
  34. });