Commit 99d2f537 by Torkel Ödegaard

sharing temp dashboard is now working again

parent 6c32365e
...@@ -45,7 +45,6 @@ function (angular, $, config, _) { ...@@ -45,7 +45,6 @@ function (angular, $, config, _) {
$scope.setupDashboard = function(event, dashboardData) { $scope.setupDashboard = function(event, dashboardData) {
timer.cancel_all(); timer.cancel_all();
alertSrv.clearAll();
$rootScope.fullscreen = false; $rootScope.fullscreen = false;
......
...@@ -64,17 +64,23 @@ function (angular, _, moment) { ...@@ -64,17 +64,23 @@ function (angular, _, moment) {
} }
}; };
$scope.elasticsearch_save = function(type, ttl) { $scope.saveForSharing = function() {
elastic.saveForSharing($scope.dashboard)
.then(function(result) {
$scope.share = { url: result.url, title: result.title };
}, function(err) {
alertSrv.set('Save for sharing failed', err, 'error',5000);
});
};
$scope.saveDashboard = function() {
elastic.saveDashboard($scope.dashboard, $scope.dashboard.title) elastic.saveDashboard($scope.dashboard, $scope.dashboard.title)
.then(function(result) { .then(function(result) {
alertSrv.set('Dashboard Saved', 'Dashboard has been saved to Elasticsearch as "' + result.title + '"','success', 5000); alertSrv.set('Dashboard Saved', 'Dashboard has been saved to Elasticsearch as "' + result.title + '"','success', 5000);
if(type === 'temp') { $location.path(result.url);
$scope.share = $scope.dashboard.share_link($scope.dashboard.title, 'temp', result.title);
}
else {
$location.path(result.url);
}
$rootScope.$emit('dashboard-saved', $scope.dashboard); $rootScope.$emit('dashboard-saved', $scope.dashboard);
......
...@@ -48,6 +48,15 @@ function (angular, _, config, $) { ...@@ -48,6 +48,15 @@ function (angular, _, config, $) {
} }
}; };
$scope.shareDashboard = function(title, id) {
var baseUrl = window.location.href.replace(window.location.hash,'');
$scope.share = {
title: title,
url: baseUrl + '#dashboard/elasticsearch/' + encodeURIComponent(id)
};
};
$scope.searchDasboards = function(queryString) { $scope.searchDasboards = function(queryString) {
var tagsOnly = queryString.indexOf('tags!:') === 0; var tagsOnly = queryString.indexOf('tags!:') === 0;
if (tagsOnly) { if (tagsOnly) {
...@@ -71,7 +80,7 @@ function (angular, _, config, $) { ...@@ -71,7 +80,7 @@ function (angular, _, config, $) {
sort: ["_uid"] sort: ["_uid"]
}; };
return elastic.post('dashboard/_search', query) return elastic.post('/dashboard/_search', query)
.then(function(results) { .then(function(results) {
if(_.isUndefined(results.hits)) { if(_.isUndefined(results.hits)) {
$scope.results.dashboards = []; $scope.results.dashboards = [];
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
<li ng-show="dashboard.loader.save_elasticsearch"> <li ng-show="dashboard.loader.save_elasticsearch">
<form class="input-prepend nomargin save-dashboard-dropdown-save-form"> <form class="input-prepend nomargin save-dashboard-dropdown-save-form">
<input class='input-medium' ng-model="dashboard.title" type="text" ng-model="elasticsearch.title"/> <input class='input-medium' ng-model="dashboard.title" type="text" ng-model="elasticsearch.title"/>
<button class="btn" ng-click="elasticsearch_save('dashboard')"><i class="icon-save"></i></button> <button class="btn" ng-click="saveDashboard()"><i class="icon-save"></i></button>
</form> </form>
</li> </li>
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
<li ng-show="dashboard.loader.save_local"> <li ng-show="dashboard.loader.save_local">
<a class="link" ng-click="dashboard.to_file()">Export dashboard</a> <a class="link" ng-click="dashboard.to_file()">Export dashboard</a>
</li> </li>
<li ng-show="showDropdown('share')"><a bs-tooltip="'Share'" data-placement="bottom" ng-click="elasticsearch_save('temp',dashboard.loader.save_temp_ttl)" config-modal="app/partials/dashLoaderShare.html">Share temp copy</i></a></li> <li ng-show="showDropdown('share')"><a bs-tooltip="'Share'" data-placement="bottom" ng-click="saveForSharing()" config-modal="app/partials/dashLoaderShare.html">Share temp copy</i></a></li>
<li ng-show="dashboard.loader.save_gist" style="margin:10px"> <li ng-show="dashboard.loader.save_gist" style="margin:10px">
<h6>Gist</h6> <h6>Gist</h6>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
</div> </div>
<div class="modal-body"> <div class="modal-body">
<label>Share this dashboard with this URL</label> <label>Share this dashboard with this URL</label>
<input ng-model='share.link' type="text" style="width:90%" onclick="this.select()" onfocus="this.select()" ng-change="share = dashboard.share_link(share.title,share.type,share.id)"> <input ng-model='share.url' type="text" style="width:90%" onclick="this.select()" onfocus="this.select()">
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-success" ng-click="dismiss();$broadcast('render')">Close</button> <button type="button" class="btn btn-success" ng-click="dismiss();$broadcast('render')">Close</button>
......
...@@ -86,7 +86,7 @@ ...@@ -86,7 +86,7 @@
{{tag}} {{tag}}
</a> </a>
</td> </td>
<td><a><i class="icon-share" ng-click="share = dashboard.share_link(row._id,'elasticsearch',row._id)" config-modal="app/partials/dashLoaderShare.html"></i></a></td> <td><a><i class="icon-share" ng-click="shareDashboard(row._id, row._id)" config-modal="app/partials/dashLoaderShare.html"></i></a></td>
</tr> </tr>
</table> </table>
</div> </div>
......
...@@ -13,36 +13,31 @@ function (angular, $, config) { ...@@ -13,36 +13,31 @@ function (angular, $, config) {
.when('/dashboard/elasticsearch/:id', { .when('/dashboard/elasticsearch/:id', {
templateUrl: 'app/partials/dashboard.html', templateUrl: 'app/partials/dashboard.html',
controller : 'DashFromElasticProvider', controller : 'DashFromElasticProvider',
})
.when('/dashboard/temp/:id', {
templateUrl: 'app/partials/dashboard.html',
controller : 'DashFromElasticProvider',
}); });
}); });
module.controller('DashFromElasticProvider', function($scope, $rootScope, $http, $routeParams, alertSrv) { module.controller('DashFromElasticProvider', function($scope, $rootScope, elastic, $routeParams, alertSrv) {
var elasticsearch_load = function(id) { var elasticsearch_load = function(id) {
var url = config.elasticsearch + "/" + config.grafana_index + "/dashboard/" + id; var url = '/dashboard/' + id;
var options = { // hack to check if it is a temp dashboard
url: url +'?' + new Date().getTime(), if (window.location.href.indexOf('dashboard/temp') > 0) {
method: "GET", url = '/temp/' + id;
transformResponse: function(response) { }
var esResponse = angular.fromJson(response);
if (esResponse._source && esResponse._source.dashboard) { return elastic.get(url)
return angular.fromJson(esResponse._source.dashboard); .then(function(result) {
if (result._source && result._source.dashboard) {
return angular.fromJson(result._source.dashboard);
} else { } else {
return false; return false;
} }
} }, function(data, status) {
};
if (config.elasticsearchBasicAuth) {
options.withCredentials = true;
options.headers = {
"Authorization": "Basic " + config.elasticsearchBasicAuth
};
}
return $http(options)
.error(function(data, status) {
if(status === 0) { if(status === 0) {
alertSrv.set('Error',"Could not contact Elasticsearch at " + alertSrv.set('Error',"Could not contact Elasticsearch at " +
config.elasticsearch + ". Please ensure that Elasticsearch is reachable from your browser.",'error'); config.elasticsearch + ". Please ensure that Elasticsearch is reachable from your browser.",'error');
...@@ -53,8 +48,8 @@ function (angular, $, config) { ...@@ -53,8 +48,8 @@ function (angular, $, config) {
}); });
}; };
elasticsearch_load($routeParams.id).then(function(result) { elasticsearch_load($routeParams.id).then(function(dashboard) {
$scope.emitAppEvent('setup-dashboard', result.data); $scope.emitAppEvent('setup-dashboard', dashboard);
}); });
}); });
......
...@@ -11,7 +11,7 @@ function(angular, config) { ...@@ -11,7 +11,7 @@ function(angular, config) {
this._request = function(method, url, data) { this._request = function(method, url, data) {
var options = { var options = {
url: config.elasticsearch + "/" + config.grafana_index + "/" + url, url: config.elasticsearch + "/" + config.grafana_index + url,
method: method, method: method,
data: data data: data
}; };
...@@ -25,16 +25,45 @@ function(angular, config) { ...@@ -25,16 +25,45 @@ function(angular, config) {
return $http(options); return $http(options);
}; };
this.get = function(url) {
return this._request('GET', url)
.then(function(results) {
return results.data;
});
};
this.post = function(url, data) { this.post = function(url, data) {
return this._request('POST', url, data) return this._request('POST', url, data)
.then(function(results) { .then(function(results) {
return results.data; return results.data;
});
};
this.saveForSharing = function(dashboard) {
var data = {
user: 'guest',
group: 'guest',
title: dashboard.title,
tags: dashboard.tags,
dashboard: angular.toJson(dashboard)
};
var ttl = dashboard.loader.save_temp_ttl;
return this._request('POST', '/temp/?ttl=' + ttl, data)
.then(function(result) {
var baseUrl = window.location.href.replace(window.location.hash,'');
var url = baseUrl + "#dashboard/temp/" + result.data._id;
return { title: dashboard.title, url: url };
}, function(err) { }, function(err) {
return err.data; throw "Failed to save to temp dashboard to elasticsearch " + err.data;
}); });
}; };
this.saveDashboard = function(dashboard, title, ttl) { this.saveDashboard = function(dashboard, title) {
var dashboardClone = angular.copy(dashboard); var dashboardClone = angular.copy(dashboard);
title = dashboardClone.title = title ? title : dashboard.title; title = dashboardClone.title = title ? title : dashboard.title;
......
define([
'angular',
'config'
],
function(angular, config) {
"use strict";
var module = angular.module('kibana.services');
module.service('elastic', function($http) {
this.put = function(url, data) {
url = config.elasticsearch + '/' + config.grafana_index + url;
var options = {
url: url,
method: 'PUT',
data: data
};
return $http(options);
};
this.saveDashboard = function(dashboard, title, ttl) {
var dashboardClone = angular.copy(dashboard);
title = dashboardClone.title = title ? title : dashboard.title;
var data = {
user: 'guest',
group: 'guest',
title: title,
tags: dashboardClone.tags,
dashboard: angular.toJson(dashboardClone)
};
return this.put('/dashboard/' + encodeURIComponent(title), data)
.then(function() {
return { title: title, url: '/dashboard/elasticsearch/' + title };
}, function(err) {
throw 'Failed to save to elasticsearch ' + err.data;
});
};
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment