From 15c52cacfb86e34988906b164d509f207f8a76a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 23 Feb 2015 10:50:50 +0100 Subject: [PATCH] Import views are now consolidated into one view, and found through one navigation (dashboard search -> Import, Closes #1511 --- src/app/controllers/all.js | 1 - src/app/controllers/graphiteImport.js | 108 ------------------ src/app/features/dashboard/all.js | 1 + .../features/dashboard/graphiteImportCtrl.js | 94 +++++++++++++++ .../dashboard/partials/graphiteImport.html | 34 ++++++ .../features/dashboard/partials/import.html | 7 +- src/app/partials/dasheditor.html | 8 +- src/app/partials/import.html | 35 ------ 8 files changed, 136 insertions(+), 152 deletions(-) delete mode 100644 src/app/controllers/graphiteImport.js create mode 100644 src/app/features/dashboard/graphiteImportCtrl.js create mode 100644 src/app/features/dashboard/partials/graphiteImport.html delete mode 100644 src/app/partials/import.html diff --git a/src/app/controllers/all.js b/src/app/controllers/all.js index bf5c8164397..f735963e886 100644 --- a/src/app/controllers/all.js +++ b/src/app/controllers/all.js @@ -3,7 +3,6 @@ define([ './pulldown', './search', './metricKeys', - './graphiteImport', './inspectCtrl', './jsonEditorCtrl', './loginCtrl', diff --git a/src/app/controllers/graphiteImport.js b/src/app/controllers/graphiteImport.js deleted file mode 100644 index 091f4b8fe5f..00000000000 --- a/src/app/controllers/graphiteImport.js +++ /dev/null @@ -1,108 +0,0 @@ -define([ - 'angular', - 'app', - 'lodash', - 'kbn' -], -function (angular, app, _, kbn) { - 'use strict'; - - var module = angular.module('grafana.controllers'); - - module.controller('GraphiteImportCtrl', function($scope, $rootScope, $timeout, datasourceSrv, $location) { - - $scope.init = function() { - $scope.datasources = datasourceSrv.getMetricSources(); - $scope.setDatasource(null); - }; - - $scope.setDatasource = function(datasource) { - $scope.datasource = datasourceSrv.get(datasource); - - if (!$scope.datasource) { - $scope.error = "Cannot find datasource " + datasource; - return; - } - }; - - $scope.listAll = function(query) { - delete $scope.error; - - $scope.datasource.listDashboards(query) - .then(function(results) { - $scope.dashboards = results; - }) - .then(null, function(err) { - $scope.error = err.message || 'Error while fetching list of dashboards'; - }); - }; - - $scope.import = function(dashName) { - delete $scope.error; - - $scope.datasource.loadDashboard(dashName) - .then(function(results) { - if (!results.data || !results.data.state) { - throw { message: 'no dashboard state received from graphite' }; - } - - graphiteToGrafanaTranslator(results.data.state, $scope.datasource.name); - }) - .then(null, function(err) { - $scope.error = err.message || 'Failed to import dashboard'; - }); - }; - - function graphiteToGrafanaTranslator(state, datasource) { - var graphsPerRow = 2; - var rowHeight = 300; - var rowTemplate; - var currentRow; - var panel; - - rowTemplate = { - title: '', - panels: [], - height: rowHeight - }; - - currentRow = angular.copy(rowTemplate); - - var newDashboard = angular.copy($scope.dashboard); - newDashboard.rows = []; - newDashboard.title = state.name; - newDashboard.rows.push(currentRow); - - _.each(state.graphs, function(graph, index) { - if (currentRow.panels.length === graphsPerRow) { - currentRow = angular.copy(rowTemplate); - newDashboard.rows.push(currentRow); - } - - panel = { - type: 'graph', - span: 12 / graphsPerRow, - title: graph[1].title, - targets: [], - datasource: datasource, - id: index + 1 - }; - - _.each(graph[1].target, function(target) { - panel.targets.push({ - target: target - }); - }); - - currentRow.panels.push(panel); - }); - - window.grafanaImportDashboard = newDashboard; - $location.path('/dashboard/import/' + kbn.slugifyForUrl(newDashboard.title)); - - $scope.dismiss(); - } - - }); - -}); diff --git a/src/app/features/dashboard/all.js b/src/app/features/dashboard/all.js index cadab505e5b..0bd631447a7 100644 --- a/src/app/features/dashboard/all.js +++ b/src/app/features/dashboard/all.js @@ -13,5 +13,6 @@ define([ './timeSrv', './unsavedChangesSrv', './directives/dashSearchView', + './graphiteImportCtrl', './importCtrl', ], function () {}); diff --git a/src/app/features/dashboard/graphiteImportCtrl.js b/src/app/features/dashboard/graphiteImportCtrl.js new file mode 100644 index 00000000000..5a9205bd817 --- /dev/null +++ b/src/app/features/dashboard/graphiteImportCtrl.js @@ -0,0 +1,94 @@ +define([ + 'angular', + 'app', + 'lodash', + 'kbn' +], +function (angular, app, _, kbn) { + 'use strict'; + + var module = angular.module('grafana.controllers'); + + module.controller('GraphiteImportCtrl', function($scope, datasourceSrv, dashboardSrv, $location) { + + $scope.init = function() { + $scope.datasources = []; + _.each(datasourceSrv.getAll(), function(ds) { + if (ds.type === 'graphite') { + $scope.sourceName = ds.name; + $scope.datasources.push(ds.name); + } + }); + }; + + $scope.listAll = function() { + $scope.datasource = datasourceSrv.get($scope.sourceName); + + $scope.datasource.listDashboards('').then(function(results) { + $scope.dashboards = results; + }, function(err) { + var message = err.message || err.statusText || 'Error'; + $scope.appEvent('alert-error', ['Failed to load dashboard list from graphite', message]); + }); + }; + + $scope.import = function(dashName) { + $scope.datasource.loadDashboard(dashName).then(function(results) { + if (!results.data || !results.data.state) { + throw { message: 'no dashboard state received from graphite' }; + } + + graphiteToGrafanaTranslator(results.data.state, $scope.datasource.name); + }, function(err) { + var message = err.message || err.statusText || 'Error'; + $scope.appEvent('alert-error', ['Failed to load dashboard from graphite', message]); + }); + }; + + function graphiteToGrafanaTranslator(state, datasource) { + var graphsPerRow = 2; + var rowHeight = 300; + var rowTemplate; + var currentRow; + var panel; + + rowTemplate = { + title: '', + panels: [], + height: rowHeight + }; + + currentRow = angular.copy(rowTemplate); + + var newDashboard = dashboardSrv.create({}); + newDashboard.rows = []; + newDashboard.title = state.name; + newDashboard.rows.push(currentRow); + + _.each(state.graphs, function(graph, index) { + if (currentRow.panels.length === graphsPerRow) { + currentRow = angular.copy(rowTemplate); + newDashboard.rows.push(currentRow); + } + + panel = { + type: 'graph', + span: 12 / graphsPerRow, + title: graph[1].title, + targets: [], + datasource: datasource, + id: index + 1 + }; + + _.each(graph[1].target, function(target) { + panel.targets.push({ target: target }); + }); + + currentRow.panels.push(panel); + }); + + window.grafanaImportDashboard = newDashboard; + $location.path('/dashboard/import/' + kbn.slugifyForUrl(newDashboard.title)); + } + }); +}); diff --git a/src/app/features/dashboard/partials/graphiteImport.html b/src/app/features/dashboard/partials/graphiteImport.html new file mode 100644 index 00000000000..0082fa92047 --- /dev/null +++ b/src/app/features/dashboard/partials/graphiteImport.html @@ -0,0 +1,34 @@ +
+ +
+

Load dashboard from Graphite-Web

+ +
+
    +
  • + Data source +
  • +
  • + +
  • +
  • + +
  • +
    +
+
+ + + + + + +
{{dash.name}} +
+ +
+
diff --git a/src/app/features/dashboard/partials/import.html b/src/app/features/dashboard/partials/import.html index 965331bdd15..af02069657a 100644 --- a/src/app/features/dashboard/partials/import.html +++ b/src/app/features/dashboard/partials/import.html @@ -7,7 +7,7 @@

- Import dashboard + Import file Load dashboard JSON layout from file

@@ -23,7 +23,7 @@

Migrate dashboards

-
+
  • Dashboard source @@ -62,6 +62,9 @@
+ +
+
diff --git a/src/app/partials/dasheditor.html b/src/app/partials/dasheditor.html index 843f73b24db..2dc2b18e523 100644 --- a/src/app/partials/dasheditor.html +++ b/src/app/partials/dasheditor.html @@ -5,7 +5,7 @@
-
+
@@ -70,11 +70,7 @@
-
- -
- -
+
diff --git a/src/app/partials/import.html b/src/app/partials/import.html deleted file mode 100644 index e89aff41342..00000000000 --- a/src/app/partials/import.html +++ /dev/null @@ -1,35 +0,0 @@ -
-
Import dashboards from graphite web
- -
-
-
- - - -
- -
-
- -
- - - - - -
{{dash.name}} - - import - -
-
- -
- {{error}} -
-