dashboard-from-es.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'config',
  5. 'underscore'
  6. ],
  7. function (angular, $, config, _) {
  8. "use strict";
  9. var module = angular.module('kibana.routes');
  10. module.config(function($routeProvider) {
  11. $routeProvider
  12. .when('/dashboard/elasticsearch/:id', {
  13. templateUrl: 'app/partials/dashboard.html',
  14. controller : 'DashFromElasticProvider',
  15. });
  16. });
  17. module.controller('DashFromElasticProvider', function($scope, $rootScope, $http, $routeParams, alertSrv) {
  18. var elasticsearch_load = function(id) {
  19. var url = config.elasticsearch + "/" + config.grafana_index + "/dashboard/" + id;
  20. var options = {
  21. url: url +'?' + new Date().getTime(),
  22. method: "GET",
  23. transformResponse: function(response) {
  24. var esResponse = angular.fromJson(response);
  25. if (esResponse._source && esResponse._source.dashboard) {
  26. return angular.fromJson(esResponse._source.dashboard);
  27. } else {
  28. return false;
  29. }
  30. }
  31. };
  32. if (config.elasticsearchBasicAuth) {
  33. options.withCredentials = true;
  34. options.headers = {
  35. "Authorization": "Basic " + config.elasticsearchBasicAuth
  36. };
  37. }
  38. return $http(options)
  39. .error(function(data, status) {
  40. if(status === 0) {
  41. alertSrv.set('Error',"Could not contact Elasticsearch at " +
  42. config.elasticsearch + ". Please ensure that Elasticsearch is reachable from your browser.",'error');
  43. } else {
  44. alertSrv.set('Error',"Could not find dashboard " + id, 'error');
  45. }
  46. return false;
  47. });
  48. };
  49. elasticsearch_load($routeParams.id).then(function(result) {
  50. $scope.emitAppEvent('setup-dashboard', result.data);
  51. });
  52. });
  53. });