query_ctrl.js 2.4 KB

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