queryCtrl.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. define([
  2. 'angular',
  3. 'lodash'
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.controllers');
  8. module.controller('InfluxQueryCtrl', function($scope, $timeout, $sce, templateSrv, $q) {
  9. $scope.init = function() {
  10. $scope.segments = $scope.target.segments || [];
  11. $scope.functionsSelect = [
  12. 'count', 'mean', 'sum', 'min',
  13. 'max', 'mode', 'distinct', 'median',
  14. 'derivative', 'stddev', 'first', 'last',
  15. 'difference'
  16. ];
  17. checkOtherSegments(0);
  18. };
  19. $scope.toggleQueryMode = function () {
  20. $scope.target.rawQuery = !$scope.target.rawQuery;
  21. };
  22. $scope.moveMetricQuery = function(fromIndex, toIndex) {
  23. _.move($scope.panel.targets, fromIndex, toIndex);
  24. };
  25. $scope.duplicate = function() {
  26. var clone = angular.copy($scope.target);
  27. $scope.panel.targets.push(clone);
  28. };
  29. $scope.getAltSegments = function (index) {
  30. $scope.altSegments = [];
  31. var measurement = $scope.segments[0].value;
  32. var queryType, query;
  33. if (index === 0) {
  34. queryType = 'MEASUREMENTS';
  35. query = 'SHOW MEASUREMENTS';
  36. } else if (index % 2 === 1) {
  37. queryType = 'TAG_KEYS';
  38. query = 'SHOW TAG KEYS FROM "' + measurement + '"';
  39. } else {
  40. queryType = 'TAG_VALUES';
  41. query = 'SHOW TAG VALUES FROM "' + measurement + '" WITH KEY = ' + $scope.segments[$scope.segments.length - 2].value;
  42. }
  43. console.log('getAltSegments: query' , query);
  44. return $scope.datasource.metricFindQuery(query, queryType).then(function(results) {
  45. console.log('get alt segments: response', results);
  46. $scope.altSegments = _.map(results, function(segment) {
  47. return new MetricSegment({ value: segment.text, expandable: segment.expandable });
  48. });
  49. _.each(templateSrv.variables, function(variable) {
  50. $scope.altSegments.unshift(new MetricSegment({
  51. type: 'template',
  52. value: '$' + variable.name,
  53. expandable: true,
  54. }));
  55. });
  56. }, function(err) {
  57. $scope.parserError = err.message || 'Failed to issue metric query';
  58. });
  59. };
  60. $scope.segmentValueChanged = function (segment, segmentIndex) {
  61. delete $scope.parserError;
  62. if (segment.expandable) {
  63. return checkOtherSegments(segmentIndex + 1).then(function () {
  64. setSegmentFocus(segmentIndex + 1);
  65. $scope.targetChanged();
  66. });
  67. }
  68. else {
  69. $scope.segments = $scope.segments.splice(0, segmentIndex + 1);
  70. }
  71. setSegmentFocus(segmentIndex + 1);
  72. $scope.targetChanged();
  73. };
  74. $scope.targetChanged = function() {
  75. if ($scope.parserError) {
  76. return;
  77. }
  78. $scope.target.measurement = '';
  79. $scope.target.tags = {};
  80. $scope.target.measurement = $scope.segments[0].value;
  81. for (var i = 1; i+1 < $scope.segments.length; i += 2) {
  82. var key = $scope.segments[i].value;
  83. $scope.target.tags[key] = $scope.segments[i+1].value;
  84. }
  85. $scope.$parent.get_data();
  86. };
  87. function checkOtherSegments(fromIndex) {
  88. if (fromIndex === 0) {
  89. $scope.segments.push(MetricSegment.newSelectMetric());
  90. return;
  91. }
  92. if ($scope.segments.length === 0) {
  93. throw('should always have a scope segment?');
  94. }
  95. if (_.last($scope.segments).fake) {
  96. return $q.when([]);
  97. } else if ($scope.segments.length % 2 === 1) {
  98. $scope.segments.push(MetricSegment.newSelectTag());
  99. return $q.when([]);
  100. } else {
  101. $scope.segments.push(MetricSegment.newSelectTagValue());
  102. return $q.when([]);
  103. }
  104. }
  105. function setSegmentFocus(segmentIndex) {
  106. _.each($scope.segments, function(segment, index) {
  107. segment.focus = segmentIndex === index;
  108. });
  109. }
  110. function MetricSegment(options) {
  111. if (options === '*' || options.value === '*') {
  112. this.value = '*';
  113. this.html = $sce.trustAsHtml('<i class="fa fa-asterisk"><i>');
  114. this.expandable = true;
  115. return;
  116. }
  117. if (_.isString(options)) {
  118. this.value = options;
  119. this.html = $sce.trustAsHtml(this.value);
  120. return;
  121. }
  122. this.fake = options.fake;
  123. this.value = options.value;
  124. this.type = options.type;
  125. this.expandable = options.expandable;
  126. this.html = $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
  127. }
  128. MetricSegment.newSelectMetric = function() {
  129. return new MetricSegment({value: 'select metric', fake: true});
  130. };
  131. MetricSegment.newSelectTag = function() {
  132. return new MetricSegment({value: 'select tag', fake: true});
  133. };
  134. MetricSegment.newSelectTagValue = function() {
  135. return new MetricSegment({value: 'select tag value', fake: true});
  136. };
  137. });
  138. });