Files
grafana/public/app/controllers/metricKeys.js

187 lines
5.0 KiB
JavaScript
Raw Normal View History

2013-12-12 22:30:20 +01:00
define([
'angular',
2014-08-07 14:35:19 +02:00
'lodash',
2013-12-12 22:30:20 +01:00
'config'
],
function (angular, _, config) {
'use strict';
2014-07-28 18:11:52 +02:00
var module = angular.module('grafana.controllers');
2013-12-12 22:30:20 +01:00
2013-12-13 14:30:33 +01:00
module.controller('MetricKeysCtrl', function($scope, $http, $q) {
2014-01-19 16:13:28 +01:00
var elasticSearchUrlForMetricIndex = config.elasticsearch + '/' + config.grafana_metrics_index + '/';
var httpOptions = {};
if (config.elasticsearchBasicAuth) {
httpOptions.withCredentials = true;
httpOptions.headers = {
"Authorization": "Basic " + config.elasticsearchBasicAuth
};
}
2013-12-12 22:30:20 +01:00
$scope.init = function () {
$scope.metricPath = "prod.apps.api.boobarella.*";
2013-12-13 14:30:33 +01:00
$scope.metricCounter = 0;
2013-12-12 22:30:20 +01:00
};
$scope.createIndex = function () {
$scope.errorText = null;
$scope.infoText = null;
deleteIndex()
.then(createIndex)
.then(function () {
$scope.infoText = "Index created!";
})
.then(null, function (err) {
$scope.errorText = angular.toJson(err);
});
};
$scope.loadMetricsFromPath = function() {
$scope.errorText = null;
$scope.infoText = null;
$scope.metricCounter = 0;
return loadMetricsRecursive($scope.metricPath)
.then(function() {
$scope.infoText = "Indexing completed!";
}, function(err) {
$scope.errorText = "Error: " + err;
});
};
$scope.loadAll = function() {
2013-12-16 22:12:11 +01:00
$scope.infoText = "Fetching all metrics from graphite...";
getFromEachGraphite('/metrics/index.json', saveMetricsArray)
2014-06-07 06:38:33 +02:00
.then(function() {
2013-12-16 22:12:11 +01:00
$scope.infoText = "Indexing complete!";
}).then(null, function(err) {
2013-12-16 22:12:11 +01:00
$scope.errorText = err;
});
};
function getFromEachGraphite(request, data_callback, error_callback) {
2014-06-07 06:38:33 +02:00
return $q.all(_.map(config.datasources, function(datasource) {
if (datasource.type = 'graphite') {
return $http.get(datasource.url + request)
.then(data_callback, error_callback);
}
2014-06-07 06:38:33 +02:00
}));
}
function saveMetricsArray(data, currentIndex) {
if (!data && !data.data && data.data.length === 0) {
2013-12-16 22:12:11 +01:00
return $q.reject('No metrics from graphite');
}
if (data.data.length === currentIndex) {
return $q.when('done');
}
currentIndex = currentIndex || 0;
return saveMetricKey(data.data[currentIndex])
.then(function() {
return saveMetricsArray(data, currentIndex + 1);
});
}
2013-12-13 14:30:33 +01:00
function deleteIndex()
{
var deferred = $q.defer();
$http.delete(elasticSearchUrlForMetricIndex, httpOptions)
2013-12-13 14:30:33 +01:00
.success(function() {
deferred.resolve('ok');
})
.error(function(data, status) {
if (status === 404) {
deferred.resolve('ok');
}
else {
deferred.reject('elastic search returned unexpected error');
}
});
return deferred.promise;
2013-12-12 22:30:20 +01:00
}
2013-12-13 14:30:33 +01:00
function createIndex()
{
return $http.put(elasticSearchUrlForMetricIndex, {
settings: {
analysis: {
analyzer: {
metric_path_ngram : { tokenizer : "my_ngram_tokenizer" }
},
tokenizer: {
my_ngram_tokenizer : {
type : "nGram",
min_gram : "3",
max_gram : "8",
2014-06-07 06:38:33 +02:00
token_chars: ["letter", "digit", "punctuation", "symbol"]
2013-12-13 14:30:33 +01:00
}
}
}
2013-12-12 22:30:20 +01:00
},
2013-12-13 14:30:33 +01:00
mappings: {
metricKey: {
properties: {
metricPath: {
type: "multi_field",
fields: {
"metricPath": { type: "string", index: "analyzed", index_analyzer: "standard" },
"metricPath_ng": { type: "string", index: "analyzed", index_analyzer: "metric_path_ngram" }
}
}
}
}
2013-12-12 22:30:20 +01:00
}
}, httpOptions);
2013-12-12 22:30:20 +01:00
}
2013-12-13 14:30:33 +01:00
function receiveMetric(result) {
var data = result.data;
if (!data || data.length === 0) {
2013-12-12 22:30:20 +01:00
console.log('no data');
return;
}
2013-12-13 14:30:33 +01:00
var funcs = _.map(data, function(metric) {
2013-12-12 22:30:20 +01:00
if (metric.expandable) {
2013-12-13 14:30:33 +01:00
return loadMetricsRecursive(metric.id + ".*");
2013-12-12 22:30:20 +01:00
}
if (metric.leaf) {
2013-12-13 14:30:33 +01:00
return saveMetricKey(metric.id);
2013-12-12 22:30:20 +01:00
}
});
2013-12-13 14:30:33 +01:00
return $q.all(funcs);
2013-12-12 22:30:20 +01:00
}
function saveMetricKey(metricId) {
// Create request with id as title. Rethink this.
2014-01-19 16:13:28 +01:00
var request = $scope.ejs.Document(config.grafana_metrics_index, 'metricKey', metricId).source({
2013-12-12 22:30:20 +01:00
metricPath: metricId
});
2013-12-13 14:30:33 +01:00
return request.doIndex(
function() {
2013-12-13 14:30:33 +01:00
$scope.infoText = "Indexing " + metricId;
$scope.metricCounter = $scope.metricCounter + 1;
2013-12-12 22:30:20 +01:00
},
function() {
2013-12-13 14:30:33 +01:00
$scope.errorText = "failed to save metric " + metricId;
2013-12-12 22:30:20 +01:00
}
);
}
2013-12-13 14:30:33 +01:00
function loadMetricsRecursive(metricPath)
2013-12-12 22:30:20 +01:00
{
2014-06-07 06:38:33 +02:00
return getFromEachGraphite('/metrics/find/?query=' + metricPath, receiveMetric);
2013-12-12 22:30:20 +01:00
}
});
});