浏览代码

elasticsearch: improve validation and error message when adding/testing elasticsearch ds, #7836

Torkel Ödegaard 8 年之前
父节点
当前提交
3b235612fd

+ 15 - 5
public/app/plugins/datasource/elasticsearch/datasource.js

@@ -168,12 +168,22 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticRes
     };
 
     this.testDatasource = function() {
-      timeSrv.setTime({ from: 'now-1m', to: 'now' });
-      return this._get('/_stats').then(function() {
-        return { status: "success", message: "Data source is working", title: "Success" };
-      }, function(err) {
+      timeSrv.setTime({ from: 'now-1m', to: 'now' }, true);
+      // validate that the index exist and has date field
+      return this.getFields({type: 'date'}).then(function(dateFields) {
+        var timeField = _.find(dateFields, {text: this.timeField});
+        if (!timeField) {
+          return { status: "error", message: "No date field named " + this.timeField + ' found', title: "Error" };
+        }
+        return { status: "success", message: "Index OK. Time field name OK.", title: "Success" };
+      }.bind(this), function(err) {
+        console.log(err);
         if (err.data && err.data.error) {
-          return { status: "error", message: angular.toJson(err.data.error), title: "Error" };
+          var message = angular.toJson(err.data.error);
+          if (err.data.error.reason) {
+            message = err.data.error.reason;
+          }
+          return { status: "error", message: message, title: "Error" };
         } else {
           return { status: "error", message: err.status, title: "Error" };
         }

+ 3 - 1
public/app/plugins/datasource/elasticsearch/query_builder.js

@@ -280,10 +280,12 @@ function (queryDef) {
         }
       });
     }
+
     var size = 500;
-    if(queryDef.size){
+    if (queryDef.size) {
       size = queryDef.size;
     }
+
     query.aggs =  {
       "1": {
         "terms": {