queryCtrl.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. define([
  2. 'angular',
  3. 'lodash'
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.controllers');
  8. var seriesList = null;
  9. module.controller('InfluxQueryCtrl_08', function($scope, $timeout) {
  10. $scope.init = function() {
  11. var target = $scope.target;
  12. target.function = target.function || 'mean';
  13. target.column = target.column || 'value';
  14. // backward compatible correction of schema
  15. if (target.condition_value) {
  16. target.condition = target.condition_key + ' ' + target.condition_op + ' ' + target.condition_value;
  17. delete target.condition_key;
  18. delete target.condition_op;
  19. delete target.condition_value;
  20. }
  21. if (target.groupby_field_add === false) {
  22. target.groupby_field = '';
  23. delete target.groupby_field_add;
  24. }
  25. $scope.rawQuery = false;
  26. $scope.functions = [
  27. 'count', 'mean', 'sum', 'min',
  28. 'max', 'mode', 'distinct', 'median',
  29. 'derivative', 'stddev', 'first', 'last',
  30. 'difference'
  31. ];
  32. $scope.operators = ['=', '=~', '>', '<', '!~', '<>'];
  33. $scope.oldSeries = target.series;
  34. $scope.$on('typeahead-updated', function() {
  35. $timeout($scope.get_data);
  36. });
  37. };
  38. $scope.showQuery = function () {
  39. $scope.target.rawQuery = true;
  40. };
  41. $scope.hideQuery = function () {
  42. $scope.target.rawQuery = false;
  43. };
  44. // Cannot use typeahead and ng-change on blur at the same time
  45. $scope.seriesBlur = function() {
  46. if ($scope.oldSeries !== $scope.target.series) {
  47. $scope.oldSeries = $scope.target.series;
  48. $scope.columnList = null;
  49. $scope.get_data();
  50. }
  51. };
  52. $scope.changeFunction = function(func) {
  53. $scope.target.function = func;
  54. $scope.get_data();
  55. };
  56. // called outside of digest
  57. $scope.listColumns = function(query, callback) {
  58. if (!$scope.columnList) {
  59. $scope.$apply(function() {
  60. $scope.datasource.listColumns($scope.target.series).then(function(columns) {
  61. $scope.columnList = columns;
  62. callback(columns);
  63. });
  64. });
  65. }
  66. else {
  67. return $scope.columnList;
  68. }
  69. };
  70. $scope.listSeries = function(query, callback) {
  71. if (query !== '') {
  72. seriesList = [];
  73. $scope.datasource.listSeries(query).then(function(series) {
  74. seriesList = series;
  75. callback(seriesList);
  76. });
  77. }
  78. else {
  79. return seriesList;
  80. }
  81. };
  82. $scope.moveMetricQuery = function(fromIndex, toIndex) {
  83. _.move($scope.panel.targets, fromIndex, toIndex);
  84. };
  85. $scope.duplicate = function() {
  86. var clone = angular.copy($scope.target);
  87. $scope.panel.targets.push(clone);
  88. };
  89. });
  90. });