importCtrl.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. define([
  2. 'angular',
  3. 'lodash',
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.controllers');
  8. module.controller('ImportCtrl', function($scope, $http, backendSrv, datasourceSrv) {
  9. $scope.init = function() {
  10. $scope.datasources = [];
  11. $scope.sourceName = 'grafana';
  12. $scope.destName = 'grafana';
  13. $scope.imported = [];
  14. $scope.dashboards = [];
  15. $scope.infoText = '';
  16. $scope.importing = false;
  17. _.each(datasourceSrv.getAll(), function(ds) {
  18. if (ds.type === 'influxdb' || ds.type === 'elasticsearch') {
  19. $scope.sourceName = ds.name;
  20. $scope.datasources.push(ds.name);
  21. } else if (ds.type === 'grafana') {
  22. $scope.datasources.push(ds.name);
  23. }
  24. });
  25. };
  26. $scope.startImport = function() {
  27. $scope.sourceDs = datasourceSrv.get($scope.sourceName);
  28. $scope.destDs = datasourceSrv.get($scope.destName);
  29. $scope.sourceDs.searchDashboards('title:').then(function(results) {
  30. $scope.dashboards = results.dashboards;
  31. if ($scope.dashboards.length === 0) {
  32. $scope.infoText = 'No dashboards found';
  33. return;
  34. }
  35. $scope.importing = true;
  36. $scope.imported = [];
  37. $scope.next();
  38. });
  39. };
  40. $scope.next = function() {
  41. if ($scope.dashboards.length === 0) {
  42. $scope.infoText = "Done! Imported " + $scope.imported.length + " dashboards";
  43. }
  44. var dash = $scope.dashboards.shift();
  45. if (!dash.title) {
  46. console.log(dash);
  47. return;
  48. }
  49. var infoObj = {name: dash.title, info: 'Importing...'};
  50. $scope.imported.push(infoObj);
  51. $scope.infoText = "Importing " + $scope.imported.length + '/' + ($scope.imported.length + $scope.dashboards.length);
  52. $scope.sourceDs.getDashboard(dash.id).then(function(loadedDash) {
  53. $scope.destDs.saveDashboard(loadedDash).then(function() {
  54. infoObj.info = "Done!";
  55. $scope.next();
  56. }, function(err) {
  57. infoObj.info = "Error: " + err;
  58. $scope.next();
  59. });
  60. });
  61. };
  62. $scope.init();
  63. });
  64. });