graphiteImport.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. define([
  2. 'angular',
  3. 'app',
  4. 'underscore'
  5. ],
  6. function (angular, app, _) {
  7. 'use strict';
  8. var module = angular.module('kibana.controllers');
  9. module.controller('GraphiteImportCtrl', function($scope, $rootScope, $timeout, graphiteSrv, dashboard) {
  10. $scope.init = function() {
  11. console.log('hej!');
  12. };
  13. $scope.listAll = function(query) {
  14. delete $scope.error;
  15. graphiteSrv.listDashboards(query)
  16. .then(function(results) {
  17. $scope.dashboards = results;
  18. })
  19. .then(null, function(err) {
  20. $scope.error = err.message || 'Error while fetching list of dashboards';
  21. });
  22. };
  23. $scope.import = function(dashName) {
  24. delete $scope.error;
  25. graphiteSrv.loadDashboard(dashName)
  26. .then(function(results) {
  27. if (!results.data || !results.data.state) {
  28. throw { message: 'no dashboard state received from graphite' };
  29. }
  30. graphiteToGrafanaTranslator(results.data.state);
  31. })
  32. .then(null, function(err) {
  33. $scope.error = err.message || 'Failed to import dashboard';
  34. });
  35. };
  36. function graphiteToGrafanaTranslator(state) {
  37. var graphsPerRow = 2;
  38. var rowHeight = 300;
  39. var rowTemplate;
  40. var currentRow;
  41. var panel;
  42. rowTemplate = {
  43. title: '',
  44. panels: [],
  45. height: rowHeight
  46. };
  47. currentRow = angular.copy(rowTemplate);
  48. var newDashboard = angular.copy(dashboard.current);
  49. newDashboard.rows = [];
  50. newDashboard.title = state.name;
  51. newDashboard.rows.push(currentRow);
  52. _.each(state.graphs, function(graph) {
  53. if (currentRow.panels.length === graphsPerRow) {
  54. currentRow = angular.copy(rowTemplate);
  55. newDashboard.rows.push(currentRow);
  56. }
  57. panel = {
  58. type: 'graphite',
  59. span: 12 / graphsPerRow,
  60. title: graph[1].title,
  61. targets: []
  62. };
  63. _.each(graph[1].target, function(target) {
  64. panel.targets.push({
  65. target: target
  66. });
  67. });
  68. currentRow.panels.push(panel);
  69. });
  70. dashboard.dash_load(newDashboard);
  71. }
  72. });
  73. });