Просмотр исходного кода

more work on refactoring ES usage

Torkel Ödegaard 11 лет назад
Родитель
Сommit
6c32365e00
2 измененных файлов с 32 добавлено и 10 удалено
  1. 2 3
      src/app/controllers/search.js
  2. 30 7
      src/app/services/elasticsearch/es-client.js

+ 2 - 3
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 = [];

+ 30 - 7
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;
         });
     };