metricAgg.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'jquery',
  5. ],
  6. function (angular, _, $) {
  7. 'use strict';
  8. var module = angular.module('grafana.directives');
  9. module.controller('ElasticMetricAggCtrl', function($scope, uiSegmentSrv, $q) {
  10. var metricAggs = $scope.target.metrics;
  11. $scope.metricAggTypes = [
  12. {text: "Count", value: 'count' },
  13. {text: "Average of", value: 'avg' },
  14. {text: "Sum of", value: 'sum' },
  15. {text: "Max of", value: 'max' },
  16. {text: "Min of", value: 'min' },
  17. {text: "Standard Deviations", value: 'std_dev' },
  18. ];
  19. $scope.init = function() {
  20. $scope.agg = metricAggs[$scope.index];
  21. if (!$scope.agg.field) {
  22. $scope.agg.field = 'select field';
  23. }
  24. }
  25. $scope.$watchCollection("target.metrics", function() {
  26. $scope.isFirst = $scope.index === 0;
  27. $scope.isLast = $scope.index === metricAggs.length - 1;
  28. $scope.isSingle = metricAggs.length === 1;
  29. });
  30. $scope.toggleOptions = function() {
  31. $scope.showOptions = !$scope.showOptions;
  32. }
  33. $scope.addMetricAgg = function() {
  34. var addIndex = metricAggs.length;
  35. var id = _.reduce($scope.target.bucketAggs.concat($scope.target.metrics), function(max, val) {
  36. return parseInt(val.id) > max ? parseInt(val.id) : max;
  37. }, 0);
  38. metricAggs.splice(addIndex, 0, {type: "count", field: "select field", id: (id+1).toString()});
  39. };
  40. $scope.removeMetricAgg = function() {
  41. metricAggs.splice($scope.index, 1);
  42. $scope.onChange();
  43. };
  44. $scope.init();
  45. });
  46. module.directive('elasticMetricAgg', function() {
  47. return {
  48. templateUrl: 'app/plugins/datasource/elasticsearch/partials/metricAgg.html',
  49. controller: 'ElasticMetricAggCtrl',
  50. restrict: 'E',
  51. scope: {
  52. target: "=",
  53. index: "=",
  54. onChange: "&",
  55. getFields: "&",
  56. },
  57. link: function postLink($scope, elem) {
  58. }
  59. };
  60. });
  61. });