queryCtrl.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.functions = [
  26. 'count', 'mean', 'sum', 'min',
  27. 'max', 'mode', 'distinct', 'median',
  28. 'derivative', 'stddev', 'first', 'last',
  29. 'difference'
  30. ];
  31. $scope.operators = ['=', '=~', '>', '<', '!~', '<>'];
  32. $scope.oldSeries = target.series;
  33. $scope.$on('typeahead-updated', function() {
  34. $timeout($scope.get_data);
  35. });
  36. };
  37. $scope.toggleQueryMode = function () {
  38. $scope.target.rawQuery = !$scope.target.rawQuery;
  39. };
  40. // Cannot use typeahead and ng-change on blur at the same time
  41. $scope.seriesBlur = function() {
  42. if ($scope.oldSeries !== $scope.target.series) {
  43. $scope.oldSeries = $scope.target.series;
  44. $scope.columnList = null;
  45. $scope.get_data();
  46. }
  47. };
  48. $scope.changeFunction = function(func) {
  49. $scope.target.function = func;
  50. $scope.get_data();
  51. };
  52. // called outside of digest
  53. $scope.listColumns = function(query, callback) {
  54. if (!$scope.columnList) {
  55. $scope.$apply(function() {
  56. $scope.datasource.listColumns($scope.target.series).then(function(columns) {
  57. $scope.columnList = columns;
  58. callback(columns);
  59. });
  60. });
  61. }
  62. else {
  63. return $scope.columnList;
  64. }
  65. };
  66. $scope.listSeries = function(query, callback) {
  67. if (query !== '') {
  68. seriesList = [];
  69. $scope.datasource.listSeries(query).then(function(series) {
  70. seriesList = series;
  71. callback(seriesList);
  72. });
  73. }
  74. else {
  75. return seriesList;
  76. }
  77. };
  78. $scope.moveMetricQuery = function(fromIndex, toIndex) {
  79. _.move($scope.panel.targets, fromIndex, toIndex);
  80. };
  81. $scope.duplicate = function() {
  82. var clone = angular.copy($scope.target);
  83. $scope.panel.targets.push(clone);
  84. };
  85. });
  86. });