diff --git a/src/app/controllers/search.js b/src/app/controllers/search.js index 9f4d7721225..70b082ccb3a 100644 --- a/src/app/controllers/search.js +++ b/src/app/controllers/search.js @@ -9,10 +9,9 @@ function (angular, _, config, $) { var module = angular.module('kibana.controllers'); - module.controller('SearchCtrl', function($scope, $rootScope, $element, $location, ejsResource, elasticClient) { + module.controller('SearchCtrl', function($scope, $rootScope, $element, $location, elastic) { $scope.init = function() { - $scope.ejs = ejsResource(config.elasticsearch, config.elasticsearchBasicAuth); $scope.giveSearchFocus = 0; $scope.selectedIndex = -1; $scope.results = {dashboards: [], tags: [], metrics: []}; @@ -72,7 +71,7 @@ function (angular, _, config, $) { sort: ["_uid"] }; - return elasticClient.post('dashboard/_search', query) + return elastic.post('dashboard/_search', query) .then(function(results) { if(_.isUndefined(results.hits)) { $scope.results.dashboards = []; diff --git a/src/app/services/elasticsearch/es-client.js b/src/app/services/elasticsearch/es-client.js index 2fb8bcb3156..6b13345e44a 100644 --- a/src/app/services/elasticsearch/es-client.js +++ b/src/app/services/elasticsearch/es-client.js @@ -7,13 +7,12 @@ function(angular, config) { var module = angular.module('kibana.services'); - module.service('elasticClient', function($http) { - - this.post = function(url, data) { + module.service('elastic', function($http) { + this._request = function(method, url, data) { var options = { url: config.elasticsearch + "/" + config.grafana_index + "/" + url, - method: 'POST', + method: method, data: data }; @@ -23,11 +22,35 @@ function(angular, config) { }; } - return $http(options) + return $http(options); + }; + + this.post = function(url, data) { + return this._request('POST', url, data) .then(function(results) { return results.data; - }, function(results) { - return results.data; + }, function(err) { + return err.data; + }); + }; + + 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._request('PUT', '/dashboard/' + encodeURIComponent(title), data) + .then(function() { + return { title: title, url: '/dashboard/elasticsearch/' + title }; + }, function(err) { + throw 'Failed to save to elasticsearch ' + err.data; }); };