metricKeys.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. define([
  2. 'angular',
  3. 'underscore',
  4. 'config'
  5. ],
  6. function (angular, _, config) {
  7. 'use strict';
  8. var module = angular.module('kibana.controllers');
  9. module.controller('MetricKeysCtrl', function($scope, $http, $q) {
  10. var elasticSearchUrlForMetricIndex = config.elasticsearch + '/' + config.grafana_index + '/';
  11. $scope.init = function () {
  12. $scope.metricPath = "prod.apps.api.boobarella.*";
  13. $scope.metricCounter = 0;
  14. };
  15. function deleteIndex()
  16. {
  17. var deferred = $q.defer();
  18. $http.delete(elasticSearchUrlForMetricIndex)
  19. .success(function() {
  20. deferred.resolve('ok');
  21. })
  22. .error(function(data, status) {
  23. if (status === 404) {
  24. deferred.resolve('ok');
  25. }
  26. else {
  27. deferred.reject('elastic search returned unexpected error');
  28. }
  29. });
  30. return deferred.promise;
  31. }
  32. function createIndex()
  33. {
  34. return $http.put(elasticSearchUrlForMetricIndex, {
  35. settings: {
  36. analysis: {
  37. analyzer: {
  38. metric_path_ngram : { tokenizer : "my_ngram_tokenizer" }
  39. },
  40. tokenizer: {
  41. my_ngram_tokenizer : {
  42. type : "nGram",
  43. min_gram : "3",
  44. max_gram : "8",
  45. token_chars: [ "letter", "digit", "punctuation", "symbol"]
  46. }
  47. }
  48. }
  49. },
  50. mappings: {
  51. metricKey: {
  52. properties: {
  53. metricPath: {
  54. /* type: "string",
  55. index: "analyzed",
  56. index_analyzer: "metric_path_ngram"
  57. */
  58. type: "multi_field",
  59. fields: {
  60. "metricPath": { type: "string", index: "analyzed", index_analyzer: "standard" },
  61. "metricPath_ng": { type: "string", index: "analyzed", index_analyzer: "metric_path_ngram" }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. });
  68. }
  69. $scope.createIndex = function () {
  70. $scope.errorText = null;
  71. $scope.infoText = null;
  72. deleteIndex()
  73. .then(createIndex)
  74. .then(function () {
  75. $scope.infoText = "Index created!";
  76. })
  77. .then(null, function (err) {
  78. $scope.errorText = angular.toJson(err);
  79. });
  80. };
  81. $scope.loadMetricsFromPath = function () {
  82. $scope.errorText = null;
  83. $scope.infoText = null;
  84. $scope.metricCounter = 0;
  85. return loadMetricsRecursive($scope.metricPath)
  86. .then(function() {
  87. $scope.infoText = "Indexing completed!";
  88. }, function(err) {
  89. $scope.errorText = "Error: " + err;
  90. });
  91. };
  92. function receiveMetric(result) {
  93. var data = result.data;
  94. if (!data || data.length === 0) {
  95. console.log('no data');
  96. return;
  97. }
  98. var funcs = _.map(data, function(metric) {
  99. if (metric.expandable) {
  100. return loadMetricsRecursive(metric.id + ".*");
  101. }
  102. if (metric.leaf) {
  103. return saveMetricKey(metric.id);
  104. }
  105. });
  106. return $q.all(funcs);
  107. }
  108. function saveMetricKey(metricId) {
  109. // Create request with id as title. Rethink this.
  110. var request = $scope.ejs.Document(config.grafana_index, 'metricKey', metricId).source({
  111. metricPath: metricId
  112. });
  113. return request.doIndex(
  114. function() {
  115. $scope.infoText = "Indexing " + metricId;
  116. $scope.metricCounter = $scope.metricCounter + 1;
  117. },
  118. function() {
  119. $scope.errorText = "failed to save metric " + metricId;
  120. }
  121. );
  122. }
  123. function loadMetricsRecursive(metricPath)
  124. {
  125. return $http.get(config.graphiteUrl + '/metrics/find/?query=' + metricPath).then(receiveMetric);
  126. }
  127. });
  128. });