mirror of https://github.com/grafana/grafana.git
193 lines
6.3 KiB
JavaScript
193 lines
6.3 KiB
JavaScript
/*
|
|
|
|
## Dashcontrol
|
|
|
|
Dash control allows for saving, loading and sharing of dashboards. Do not
|
|
disable the dashcontrol module as a special instance of it allows for loading
|
|
the default dashboard from dashboards/default
|
|
|
|
### Parameters
|
|
* save
|
|
** gist :: Allow saving to gist. Requires registering an oauth domain with Github
|
|
** elasticsearch :: Allow saving to a special Kibana index within Elasticsearch
|
|
** local :: Allow saving to local file
|
|
* load
|
|
** gist :: Allow loading from gists
|
|
** elasticsearch :: Allow searching and loading of elasticsearch saved dashboards
|
|
** local :: Allow loading of dashboards from Elasticsearch
|
|
* hide_control :: Upon save, hide this panel
|
|
* elasticsearch_size :: show this many dashboards under the ES section in the load drop down
|
|
* temp :: Allow saving of temp dashboards
|
|
* temp_ttl :: How long should temp dashboards persist
|
|
|
|
### Group Events
|
|
#### Sends
|
|
* dashboard :: An object containing an entire dashboard to be loaded
|
|
|
|
*/
|
|
|
|
angular.module('kibana.dashcontrol', [])
|
|
.controller('dashcontrol', function($scope, $routeParams, $http, timer, dashboard) {
|
|
|
|
$scope.panel = $scope.panel || {};
|
|
// Set and populate defaults
|
|
var _d = {
|
|
status : "Stable",
|
|
group : "default",
|
|
save : {
|
|
gist: false,
|
|
elasticsearch: true,
|
|
local: true,
|
|
'default': true
|
|
},
|
|
load : {
|
|
gist: true,
|
|
elasticsearch: true,
|
|
local: true
|
|
},
|
|
hide_control: false,
|
|
elasticsearch_size: 20,
|
|
temp: true,
|
|
temp_ttl: '30d'
|
|
}
|
|
_.defaults($scope.panel,_d);
|
|
|
|
// A hash of defaults for the dashboard object
|
|
var _dash = {
|
|
title: "",
|
|
editable: true,
|
|
rows: [],
|
|
services: {}
|
|
}
|
|
|
|
$scope.init = function() {
|
|
$scope.gist_pattern = /(^\d{5,}$)|(^[a-z0-9]{10,}$)|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
|
|
$scope.gist = {};
|
|
$scope.elasticsearch = {};
|
|
}
|
|
|
|
$scope.set_default = function() {
|
|
if(dashboard.set_default()) {
|
|
$scope.alert('Local Default Set',dashboard.current.title+' has been set as your local default','success',5000)
|
|
} else {
|
|
$scope.alert('Incompatible Browser','Sorry, your browser is too old for this feature','error',5000)
|
|
}
|
|
}
|
|
|
|
$scope.purge_default = function() {
|
|
if(dashboard.purge_default()) {
|
|
$scope.alert('Local Default Clear','Your local default dashboard has been cleared','success',5000)
|
|
} else {
|
|
$scope.alert('Incompatible Browser','Sorry, your browser is too old for this feature','error',5000)
|
|
}
|
|
}
|
|
|
|
$scope.elasticsearch_save = function(type,ttl) {
|
|
dashboard.elasticsearch_save(type,($scope.elasticsearch.title || dashboard.current.title),ttl).then(
|
|
function(result) {
|
|
if(!_.isUndefined(result._id)) {
|
|
$scope.alert('Dashboard Saved','This dashboard has been saved to Elasticsearch as "' +
|
|
result._id + '"','success',5000)
|
|
if(type === 'temp') {
|
|
$scope.share = dashboard.share_link(dashboard.current.title,'temp',result._id)
|
|
}
|
|
} else {
|
|
$scope.alert('Save failed','Dashboard could not be saved to Elasticsearch','error',5000)
|
|
}
|
|
})
|
|
}
|
|
|
|
$scope.elasticsearch_delete = function(id) {
|
|
dashboard.elasticsearch_delete(id).then(
|
|
function(result) {
|
|
if(!_.isUndefined(result)) {
|
|
if(result.found) {
|
|
$scope.alert('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 {
|
|
$scope.alert('Dashboard Not Found','Could not find '+id+' in Elasticsearch','warning',5000)
|
|
}
|
|
} else {
|
|
$scope.alert('Dashboard Not Deleted','An error occurred deleting the dashboard',error,5000)
|
|
}
|
|
}
|
|
)
|
|
}
|
|
|
|
$scope.elasticsearch_dblist = function(query) {
|
|
dashboard.elasticsearch_list(query,$scope.panel.elasticsearch_size).then(
|
|
function(result) {
|
|
if(!_.isUndefined(result.hits)) {
|
|
$scope.panel.error = false;
|
|
$scope.hits = result.hits.total;
|
|
$scope.elasticsearch.dashboards = result.hits.hits
|
|
}
|
|
})
|
|
}
|
|
|
|
$scope.save_gist = function() {
|
|
dashboard.save_gist($scope.gist.title).then(
|
|
function(link) {
|
|
if(!_.isUndefined(link)) {
|
|
$scope.gist.last = link;
|
|
$scope.alert('Gist saved','You will be able to access your exported dashboard file at <a href="'+link+'">'+link+'</a> in a moment','success');
|
|
} else {
|
|
$scope.alert('Save failed','Gist could not be saved','error',5000)
|
|
}
|
|
})
|
|
}
|
|
|
|
$scope.gist_dblist = function(id) {
|
|
dashboard.gist_list(id).then(
|
|
function(files) {
|
|
if(files && files.length > 0) {
|
|
$scope.gist.files = files;
|
|
} else {
|
|
$scope.alert('Gist Failed','Could not retrieve dashboard list from gist','error',5000)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
.directive('dashUpload', function(timer, dashboard){
|
|
return {
|
|
restrict: 'A',
|
|
link: function(scope, elem, attrs) {
|
|
function file_selected(evt) {
|
|
var files = evt.target.files; // FileList object
|
|
|
|
// files is a FileList of File objects. List some properties.
|
|
var output = [];
|
|
for (var i = 0, f; f = files[i]; i++) {
|
|
var reader = new FileReader();
|
|
reader.onload = (function(theFile) {
|
|
return function(e) {
|
|
dashboard.dash_load(JSON.parse(e.target.result))
|
|
scope.$apply();
|
|
};
|
|
})(f);
|
|
reader.readAsText(f);
|
|
}
|
|
}
|
|
|
|
// Check for the various File API support.
|
|
if (window.File && window.FileReader && window.FileList && window.Blob) {
|
|
// Something
|
|
document.getElementById('dashupload').addEventListener('change', file_selected, false);
|
|
} else {
|
|
alert('Sorry, the HTML5 File APIs are not fully supported in this browser.');
|
|
}
|
|
}
|
|
}
|
|
}).filter('gistid', function() {
|
|
var gist_pattern = /(\d{5,})|([a-z0-9]{10,})|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
|
|
return function(input, scope) {
|
|
//return input+"boners"
|
|
if(!(_.isUndefined(input))) {
|
|
var output = input.match(gist_pattern);
|
|
if(!_.isNull(output) && !_.isUndefined(output))
|
|
return output[0].replace(/.*\//, '');
|
|
}
|
|
}
|
|
});; |