metricKeys.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_metrics_index + '/';
  11. var httpOptions = {};
  12. if (config.elasticsearchBasicAuth) {
  13. httpOptions.withCredentials = true;
  14. httpOptions.headers = {
  15. "Authorization": "Basic " + config.elasticsearchBasicAuth
  16. };
  17. }
  18. $scope.init = function () {
  19. $scope.metricPath = "prod.apps.api.boobarella.*";
  20. $scope.metricCounter = 0;
  21. };
  22. $scope.createIndex = function () {
  23. $scope.errorText = null;
  24. $scope.infoText = null;
  25. deleteIndex()
  26. .then(createIndex)
  27. .then(function () {
  28. $scope.infoText = "Index created!";
  29. })
  30. .then(null, function (err) {
  31. $scope.errorText = angular.toJson(err);
  32. });
  33. };
  34. $scope.loadMetricsFromPath = function() {
  35. $scope.errorText = null;
  36. $scope.infoText = null;
  37. $scope.metricCounter = 0;
  38. return loadMetricsRecursive($scope.metricPath)
  39. .then(function() {
  40. $scope.infoText = "Indexing completed!";
  41. }, function(err) {
  42. $scope.errorText = "Error: " + err;
  43. });
  44. };
  45. $scope.loadAll = function() {
  46. $scope.infoText = "Fetching all metrics from graphite...";
  47. return $http.get(config.graphiteUrl + "/metrics/index.json")
  48. .then(saveMetricsArray)
  49. .then(function () {
  50. $scope.infoText = "Indexing complete!";
  51. })
  52. .then(null, function(err) {
  53. $scope.errorText = err;
  54. });
  55. };
  56. function saveMetricsArray(data, currentIndex)
  57. {
  58. if (!data && !data.data && data.data.length === 0) {
  59. return $q.reject('No metrics from graphite');
  60. }
  61. if (data.data.length === currentIndex) {
  62. return $q.when('done');
  63. }
  64. currentIndex = currentIndex || 0;
  65. return saveMetricKey(data.data[currentIndex])
  66. .then(function() {
  67. return saveMetricsArray(data, currentIndex + 1);
  68. });
  69. }
  70. function deleteIndex()
  71. {
  72. var deferred = $q.defer();
  73. $http.delete(elasticSearchUrlForMetricIndex, httpOptions)
  74. .success(function() {
  75. deferred.resolve('ok');
  76. })
  77. .error(function(data, status) {
  78. if (status === 404) {
  79. deferred.resolve('ok');
  80. }
  81. else {
  82. deferred.reject('elastic search returned unexpected error');
  83. }
  84. });
  85. return deferred.promise;
  86. }
  87. function createIndex()
  88. {
  89. return $http.put(elasticSearchUrlForMetricIndex, {
  90. settings: {
  91. analysis: {
  92. analyzer: {
  93. metric_path_ngram : { tokenizer : "my_ngram_tokenizer" }
  94. },
  95. tokenizer: {
  96. my_ngram_tokenizer : {
  97. type : "nGram",
  98. min_gram : "3",
  99. max_gram : "8",
  100. token_chars: [ "letter", "digit", "punctuation", "symbol"]
  101. }
  102. }
  103. }
  104. },
  105. mappings: {
  106. metricKey: {
  107. properties: {
  108. metricPath: {
  109. type: "multi_field",
  110. fields: {
  111. "metricPath": { type: "string", index: "analyzed", index_analyzer: "standard" },
  112. "metricPath_ng": { type: "string", index: "analyzed", index_analyzer: "metric_path_ngram" }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }, httpOptions);
  119. }
  120. function receiveMetric(result) {
  121. var data = result.data;
  122. if (!data || data.length === 0) {
  123. console.log('no data');
  124. return;
  125. }
  126. var funcs = _.map(data, function(metric) {
  127. if (metric.expandable) {
  128. return loadMetricsRecursive(metric.id + ".*");
  129. }
  130. if (metric.leaf) {
  131. return saveMetricKey(metric.id);
  132. }
  133. });
  134. return $q.all(funcs);
  135. }
  136. function saveMetricKey(metricId) {
  137. // Create request with id as title. Rethink this.
  138. var request = $scope.ejs.Document(config.grafana_metrics_index, 'metricKey', metricId).source({
  139. metricPath: metricId
  140. });
  141. return request.doIndex(
  142. function() {
  143. $scope.infoText = "Indexing " + metricId;
  144. $scope.metricCounter = $scope.metricCounter + 1;
  145. },
  146. function() {
  147. $scope.errorText = "failed to save metric " + metricId;
  148. }
  149. );
  150. }
  151. function loadMetricsRecursive(metricPath)
  152. {
  153. return $http.get(config.graphiteUrl + '/metrics/find/?query=' + metricPath).then(receiveMetric);
  154. }
  155. });
  156. });