grafana/src/app/controllers/dashLoader.js

175 lines
5.5 KiB
JavaScript
Raw Normal View History

2013-09-13 15:52:13 -05:00
define([
'angular',
'underscore',
'moment'
2013-09-13 15:52:13 -05:00
],
function (angular, _, moment) {
2013-09-13 15:52:13 -05:00
'use strict';
var module = angular.module('kibana.controllers');
module.controller('dashLoader', function($scope, $rootScope, $http, alertSrv, $location, playlistSrv, elastic) {
2013-09-13 15:52:13 -05:00
$scope.init = function() {
$scope.gist_pattern = /(^\d{5,}$)|(^[a-z0-9]{10,}$)|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
$scope.gist = $scope.gist || {};
$scope.elasticsearch = $scope.elasticsearch || {};
$rootScope.$on('save-dashboard', function() {
$scope.elasticsearch_save('dashboard', false);
});
2013-12-29 06:17:04 -06:00
$rootScope.$on('zoom-out', function() {
$scope.zoom(2);
});
2013-09-13 15:52:13 -05:00
};
$scope.exitFullscreen = function() {
$rootScope.$emit('panel-fullscreen-exit');
};
2013-09-13 15:52:13 -05:00
$scope.showDropdown = function(type) {
2014-06-07 14:00:05 -05:00
if(_.isUndefined($scope.dashboard)) {
return true;
}
2014-06-07 14:00:05 -05:00
var _l = $scope.dashboard.loader;
2013-09-13 15:52:13 -05:00
if(type === 'load') {
return (_l.load_elasticsearch || _l.load_gist || _l.load_local);
}
if(type === 'save') {
return (_l.save_elasticsearch || _l.save_gist || _l.save_local || _l.save_default);
2013-09-13 15:52:13 -05:00
}
if(type === 'share') {
return (_l.save_temp);
}
return false;
};
$scope.set_default = function() {
2014-06-07 14:00:05 -05:00
if($scope.dashboard.set_default($location.path())) {
alertSrv.set('Home Set','This page has been set as your default dashboard','success',5000);
2013-09-13 15:52:13 -05:00
} else {
alertSrv.set('Incompatible Browser','Sorry, your browser is too old for this feature','error',5000);
}
};
$scope.purge_default = function() {
2014-06-07 14:00:05 -05:00
if($scope.dashboard.purge_default()) {
alertSrv.set('Local Default Clear','Your default dashboard has been reset to the default',
'success',5000);
2013-09-13 15:52:13 -05:00
} else {
alertSrv.set('Incompatible Browser','Sorry, your browser is too old for this feature','error',5000);
}
};
$scope.elasticsearch_save = function(type, ttl) {
elastic.saveDashboard($scope.dashboard, $scope.dashboard.title)
.then(function(result) {
alertSrv.set('Dashboard Saved', 'Dashboard has been saved to Elasticsearch as "' + result.title + '"','success', 5000);
2014-02-01 03:51:35 -06:00
if(type === 'temp') {
$scope.share = $scope.dashboard.share_link($scope.dashboard.title, 'temp', result.title);
}
else {
$location.path(result.url);
}
2014-02-01 03:51:35 -06:00
2014-06-07 14:00:05 -05:00
$rootScope.$emit('dashboard-saved', $scope.dashboard);
}, function(err) {
alertSrv.set('Save failed', err, 'error',5000);
});
2013-09-13 15:52:13 -05:00
};
$scope.elasticsearch_delete = function(id) {
if (!confirm('Are you sure you want to delete dashboard?')) {
return;
}
2014-06-07 14:00:05 -05:00
$scope.dashboard.elasticsearch_delete(id).then(
2013-09-13 15:52:13 -05:00
function(result) {
if(!_.isUndefined(result)) {
if(result.found) {
alertSrv.set('Dashboard Deleted',id+' has been deleted','success',5000);
// Find the deleted dashboard in the cached list and remove it
var toDelete = _.where($scope.elasticsearch.dashboards,{_id:id})[0];
$scope.elasticsearch.dashboards = _.without($scope.elasticsearch.dashboards,toDelete);
} else {
alertSrv.set('Dashboard Not Found','Could not find '+id+' in Elasticsearch','warning',5000);
}
} else {
alertSrv.set('Dashboard Not Deleted','An error occurred deleting the dashboard','error',5000);
}
}
);
};
$scope.save_gist = function() {
2014-06-07 14:00:05 -05:00
$scope.dashboard.save_gist($scope.gist.title).then(function(link) {
2014-06-06 23:38:33 -05:00
if (!_.isUndefined(link)) {
2013-09-13 15:52:13 -05:00
$scope.gist.last = link;
alertSrv.set('Gist saved','You will be able to access your exported dashboard file at '+
2014-06-06 23:38:33 -05:00
'<a href="'+link+'">'+link+'</a> in a moment','success');
2013-09-13 15:52:13 -05:00
} else {
alertSrv.set('Save failed','Gist could not be saved','error',5000);
}
});
};
$scope.gist_dblist = function(id) {
2014-06-07 14:00:05 -05:00
$scope.dashboard.gist_list(id).then(function(files) {
2014-06-06 23:38:33 -05:00
if (files && files.length > 0) {
2013-09-13 15:52:13 -05:00
$scope.gist.files = files;
} else {
alertSrv.set('Gist Failed','Could not retrieve dashboard list from gist','error',5000);
}
});
};
2013-12-29 06:17:04 -06:00
// function $scope.zoom
// factor :: Zoom factor, so 0.5 = cuts timespan in half, 2 doubles timespan
$scope.zoom = function(factor) {
var _range = $scope.filter.timeRange();
2013-12-29 06:17:04 -06:00
var _timespan = (_range.to.valueOf() - _range.from.valueOf());
var _center = _range.to.valueOf() - _timespan/2;
var _to = (_center + (_timespan*factor)/2);
var _from = (_center - (_timespan*factor)/2);
// If we're not already looking into the future, don't.
if(_to > Date.now() && _range.to < Date.now()) {
var _offset = _to - Date.now();
_from = _from - _offset;
_to = Date.now();
}
$scope.filter.setTime({
2013-12-29 06:17:04 -06:00
from:moment.utc(_from).toDate(),
to:moment.utc(_to).toDate(),
});
};
$scope.openSaveDropdown = function() {
$scope.isFavorite = playlistSrv.isCurrentFavorite($scope.dashboard);
};
$scope.markAsFavorite = function() {
playlistSrv.markAsFavorite($scope.dashboard);
$scope.isFavorite = true;
};
$scope.removeAsFavorite = function() {
2014-06-07 14:00:05 -05:00
playlistSrv.removeAsFavorite($scope.dashboard);
$scope.isFavorite = false;
};
$scope.stopPlaylist = function() {
playlistSrv.stop(1);
};
2013-09-13 15:52:13 -05:00
});
});