Bläddra i källkod

HUE-8138 [dashboard] Add metrics with compatible fields

Romain Rigaux 7 år sedan
förälder
incheckning
bbfe2e3b2c

+ 45 - 13
desktop/libs/dashboard/src/dashboard/static/dashboard/js/search.ko.js

@@ -457,7 +457,7 @@ var FieldAnalysis = function (vm, fieldName, fieldType) {
       }
     } else {
       if (self.terms.data().length == 0) {
-         self.getTerms();
+        self.getTerms();
       }
     }
   }
@@ -743,14 +743,22 @@ var Collection = function (vm, collection) {
     } else {
       return facet.widgetType() == 'hit-widget' ? ALPHA_HIT_COUNTER_OPTIONS : ALPHA_HIT_OPTIONS;
     }
-  }
+  };
 
   // Very top facet
   self._addObservablesToFacet = function(facet, vm) {
     if (facet.properties && facet.properties.facets_form && facet.properties.facets_form.aggregate) { // Only Solr 5+
-      facet.properties.facets_form.aggregate.metrics = ko.computed(function() {
-        var _field = self.getTemplateField(facet.properties.facets_form.field(), self.template.fieldsAttributes());
-        return self._get_field_operations(_field, facet);
+    	
+      // count not possible on hit-widget
+    	
+      facet.properties.facets_form.aggregate.metrics = HIT_OPTIONS;//ko.computed(function() {
+//        var _field = self.getTemplateField(facet.properties.facets_form.field(), self.template.fieldsAttributes());
+//        return self._get_field_operations(_field, facet);
+//      });
+
+      // Here we could weight the fields
+      facet.properties.facets_form.aggregate.facetFieldsNames = ko.computed(function() {
+        return self._getCompatibleMetricFields(facet.properties.facets_form);
       });
 
       facet.properties.facets_form.isEditing = ko.observable(true);
@@ -867,15 +875,34 @@ var Collection = function (vm, collection) {
         vm.search();
       });
 
-      nestedFacet.aggregate.metrics = ko.computed(function() {
-        var _field = self.getTemplateField(nestedFacet.field(), self.template.fieldsAttributes());
-        return self._get_field_operations(_field, facet);
+      nestedFacet.aggregate.metrics = HIT_OPTIONS;//ko.computed(function() {
+//        var _field = self.getTemplateField(nestedFacet.field(), self.template.fieldsAttributes());
+//        return self._get_field_operations(_field, facet);
+//      });
+      
+      nestedFacet.aggregate.facetFieldsNames = ko.computed(function() {
+        return self._getCompatibleMetricFields(nestedFacet);
       });
     }
 
     nestedFacet.isEditing = ko.observable(false);
   }
 
+  self._getCompatibleMetricFields = function(nestedFacet) {
+  	 if (['avg', 'sum', 'median', 'percentile', 'stddev', 'variance'].indexOf(nestedFacet.aggregate.function()) != -1) {
+   	   return $.grep(self.template.fieldsAttributes(), function(field) {
+   	     return isNumericColumn(field.type()) || isDateTimeColumn(field.type());
+   	   })
+   	   //function (field) {
+   	     //return field.name();
+   	   .sort(function (a, b) {
+   	     return a.name().toLowerCase().localeCompare(b.name().toLowerCase());
+   	   });
+   	 } else {
+  	   return self.template.facetFieldsNames();
+  	 }
+  };
+  
   self.facets = ko.mapping.fromJS(collection.facets);
 
   $.each(self.facets(), function (index, facet) {
@@ -1072,11 +1099,15 @@ var Collection = function (vm, collection) {
       'type': ko.mapping.toJS(facet.properties.facets_form.type),
       'isEditing': false
     });
-    pivot.aggregate.metrics = ko.computed(function() {
-      var _field = self.getTemplateField(pivot.field(), self.template.fieldsAttributes());
-      return self._get_field_operations(_field, facet);
-    });
+    pivot.aggregate.metrics = HIT_OPTIONS; //ko.computed(function() {
+//      var _field = self.getTemplateField(pivot.field(), self.template.fieldsAttributes());
+//      return self._get_field_operations(_field, facet);
+//    });
 
+    pivot.aggregate.facetFieldsNames = ko.computed(function() {
+      return self._getCompatibleMetricFields(pivot);
+    });
+    
     facet.properties.facets_form.field(null);
     facet.properties.facets_form.limit(5);
     facet.properties.facets_form.mincount(1);
@@ -1190,7 +1221,8 @@ var Collection = function (vm, collection) {
   });
 
   self.template.facetFieldsNames = ko.computed(function () {
-    return ['formula'].concat(self.template.fieldsNames());
+//    return self.template.fieldsNames();
+	  return self.template.fieldsAttributes();
   });
 
   self.template.sortedGeogFieldsNames = ko.computed(function () {

+ 14 - 8
desktop/libs/dashboard/src/dashboard/templates/common_search.mako

@@ -2171,12 +2171,14 @@ ${ dashboard.layout_skeleton(suffix='search') }
       <input type="number" class="input-mini" data-bind="value: percentile"/>
       <!-- /ko -->
 
-      <select data-bind="options: $root.collection.template.facetFieldsNames, value: $parent.field, optionsCaption: '${ _ko('Field...') }', selectize: $root.collection.template.facetFieldsNames" class="hit-options input-small" style="margin-bottom: 0"></select>
+      ##<select data-bind="options: facetFieldsNames, value: $parent.field, optionsCaption: '${ _ko('Field...') }', selectize: facetFieldsNames" class="hit-options input-small" style="margin-bottom: 0"></select>
+      <select data-bind="selectize: facetFieldsNames, value: $parent.field, optionsValue: 'name', optionsText: 'name', optionsCaption: '${ _ko('Field...') }'"></select>
+      ##<select data-bind="browserAwareSelectize: facetFieldsNames, value: $parent.field"></select>
 
       <div class="clearfix"></div>
       <br/>
 
-      <div data-bind="component: { name: 'hue-simple-ace-editor', params: { value: plain_formula, parsedValue: formula, autocomplete: { type: 'solrFormula', support: { fields: $root.collection.template.fieldsAttributes } }, singleLine: true, mode: $root.collection.engine() } }, visible: $parent.field() == 'formula'" class="margin-bottom-10"></div>
+      <div data-bind="component: { name: 'hue-simple-ace-editor', params: { value: plain_formula, parsedValue: formula, autocomplete: { type: 'solrFormula', support: { fields: $root.collection.template.fieldsAttributes } }, singleLine: true, mode: $root.collection.engine() } }, visible: $data.function() == 'formula'" class="margin-bottom-10"></div>
 
       <!-- ko if: $parents[1].widgetType() != 'hit-widget' -->
         <div class="facet-field-cnt">
@@ -2259,7 +2261,7 @@ ${ dashboard.layout_skeleton(suffix='search') }
         </a>
         <!-- /ko -->
         <!-- ko if: typeof $parents[0].isAdding !== 'undefined' && $parents[0].isAdding() -->
-        <a data-bind="visible: (typeof field !== 'undefined' && field() != 'formula') || aggregate.formula(), click: function() { $root.collection.addPivotFacetValue2($parents[0]) }" class="pull-right" href="javascript:void(0)">
+        <a data-bind="visible: aggregate.function() != 'formula' || aggregate.formula(), click: function() { $root.collection.addPivotFacetValue2($parents[0]) }" class="pull-right" href="javascript:void(0)">
           <i class="fa fa-plus"></i> ${ _('Add') }
         </a>
         <!-- /ko -->
@@ -3019,7 +3021,8 @@ var NUMERIC_HIT_OPTIONS = [
     { value: "median", label: "${ _('Median') }" },
     { value: "percentile", label: "${ _('Percentile') }" },
     { value: "stddev", label: "${ _('Stddev') }" },
-    { value: "variance", label: "${ _('Variance') }" }
+    { value: "variance", label: "${ _('Variance') }" },
+    { value: "formula", label: "${ _('Formula') }" },
 ];
 var DATETIME_HIT_OPTIONS = [
     { value: "count", label: "${ _('Group by') }" },
@@ -3028,21 +3031,24 @@ var DATETIME_HIT_OPTIONS = [
     { value: "min", label: "${ _('Min') }" },
     { value: "max", label: "${ _('Max') }" },
     { value: "median", label: "${ _('Median') }" },
-    { value: "percentile", label: "${ _('Percentile') }" }
+    { value: "percentile", label: "${ _('Percentile') }" },
+    { value: "formula", label: "${ _('Formula') }" },
 ];
 var ALPHA_HIT_COUNTER_OPTIONS = [
     ##{ value: "count", label: "${ _('Group by') }" },
     ##{ value: "counts", label: "Count" },
     { value: "unique", label: "${ _('Unique') }" },
     { value: "min", label: "${ _('Min') }" },
-    { value: "max", label: "${ _('Max') }" }
+    { value: "max", label: "${ _('Max') }" },
+    { value: "formula", label: "${ _('Formula') }" },
 ];
 var ALPHA_HIT_OPTIONS = [
     { value: "count", label: "${ _('Group by') }" },
     ## { value: "counts", label: "Count" },
     { value: "unique", label: "${ _('Unique') }" },
     { value: "min", label: "${ _('Min') }" },
-    { value: "max", label: "${ _('Max') }" }
+    { value: "max", label: "${ _('Max') }" },
+    { value: "formula", label: "${ _('Formula') }" },
 ];
 var HIT_OPTIONS = NUMERIC_HIT_OPTIONS
 ;
@@ -3063,7 +3069,7 @@ function getHitOption(value) {
 }
 
 function getPrettyMetric(facet) {
-  if (facet.field() == 'formula') {
+  if (facet.aggregate.function() == 'formula') {
     return facet.aggregate.plain_formula()
   } else if (facet.aggregate.function() == 'percentile') {
     return 'percentile(' + facet.aggregate.percentiles()[0]['value']() + ')';

+ 1 - 1
desktop/libs/libsolr/src/libsolr/api.py

@@ -895,7 +895,7 @@ class SolrApi(object):
   def _get_aggregate_function(cls, facet):
     f = facet['aggregate']
 
-    if f['formula']:
+    if f['function'] == 'formula':
       return f['formula']
     elif f['function'] == 'field':
       return f['value']