Browse Source

HUE-7062 [search] Add backend logic for formula and remove old ops

Romain Rigaux 8 years ago
parent
commit
4a8ca7e

+ 3 - 3
desktop/libs/dashboard/src/dashboard/api.py

@@ -369,7 +369,7 @@ def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_t
     'limit': 10,
     'mincount': 0,
     'isDate': False,
-    'aggregate': {'function': 'unique', 'ops': [], 'percentiles': [{'value': 50}]}
+    'aggregate': {'function': 'unique', 'formula': '', 'generated_formula': '', 'percentiles': [{'value': 50}]}
   }
 
   if widget_type in ('tree-widget', 'heatmap-widget', 'map-widget'):
@@ -378,7 +378,7 @@ def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_t
     facet_type = 'nested'
     properties['facets'] = []
     properties['domain'] = {'blockParent': [], 'blockChildren': []}
-    properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 10, 'aggregate': {'function': 'unique', 'ops': [], 'percentiles': [{'value': 50}]}}
+    properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 10, 'aggregate': {'function': 'unique', 'formula': '', 'generated_formula': '', 'percentiles': [{'value': 50}]}}
     properties['scope'] = 'world'
     properties['limit'] = 100
   else:
@@ -403,7 +403,7 @@ def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_t
       else:
         facet_type = 'nested'
 
-      properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 10, 'aggregate': {'function': 'unique', 'ops': [], 'percentiles': [{'value': 50}]}}
+      properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 10, 'aggregate': {'function': 'unique', 'formula': '', 'generated_formula': '', 'percentiles': [{'value': 50}]}}
       properties['facets'] = []
       properties['domain'] = {'blockParent': [], 'blockChildren': []}
 

+ 5 - 1
desktop/libs/dashboard/src/dashboard/static/dashboard/js/search.ko.js

@@ -956,7 +956,7 @@ var Collection = function (vm, collection) {
     facet.properties.facets_form.mincount(1);
 
     facet.properties.facets_form.aggregate.function('count');
-    facet.properties.facets_form.aggregate.ops.removeAll();
+    facet.properties.facets_form.aggregate.formula('');
     facet.properties.facets_form.aggregate.percentiles.removeAll();
     facet.properties.facets_form.aggregate.percentiles.push({'value': 50});
 
@@ -1064,6 +1064,10 @@ var Collection = function (vm, collection) {
     });
   });
 
+  self.template.facetFieldsNames = ko.computed(function () {
+    return ['formula'].concat(self.template.fieldsNames());
+  });
+
   self.template.sortedGeogFieldsNames = ko.computed(function () {
     return $.map(
       $.grep(self.availableFacetFields(), function(field) {

+ 20 - 35
desktop/libs/dashboard/src/dashboard/templates/common_search.mako

@@ -717,7 +717,7 @@ ${ dashboard.layout_skeleton(suffix='search') }
       <a data-bind="visible: ko.toJSON(properties.facets_form.field), click: $root.collection.addPivotFacetValue2" class="pull-right" href="javascript:void(0)">
         <i class="fa fa-fw fa-plus"></i> ${ _('Add') }
       </a>
-      <select data-bind="options: $root.collection.template.fieldsNames, value: properties.facets_form.field, optionsCaption: '${ _ko('Field...') }', selectize: $root.collection.template.fieldsNames" class="hit-options" style="margin-bottom: 0"></select>
+      <select data-bind="options: $root.collection.template.facetFieldsNames, value: properties.facets_form.field, optionsCaption: '${ _ko('Field...') }', selectize: $root.collection.template.facetFieldsNames" class="hit-options" style="margin-bottom: 0"></select>
       <div class="clearfix"></div>
     </div>
     <div class="content" style="border: 1px dashed #d8d8d8; border-top: none">
@@ -2111,44 +2111,34 @@ ${ dashboard.layout_skeleton(suffix='search') }
 <script type="text/html" id="metric-form">
   <div data-bind="visible: $root.isEditing" style="margin-bottom: 20px">
     <!-- ko if: $data.function() != 'field' && $parent.properties -->
-      <select data-bind="options: $parent.properties.facets_form.metrics, optionsText: 'label', optionsValue: 'value', value: $data.function" class="input-medium"></select>
+      <select data-bind="options: $parent.properties.facets_form.metrics, optionsText: 'label', optionsValue: 'value', value: $data.function" class="input-small"></select>
     <!-- /ko -->
 
     <!-- ko if: $data.function() != 'field' && $data.metrics -->
-      <select data-bind="options: $data.metrics, optionsText: 'label', optionsValue: 'value', value: $data.function" class="input-medium"></select>
-    <!-- /ko -->
-
-    <!-- ko if: $data.function() == 'field' -->
-      <select data-bind="options: $root.collection.template.fieldsNames, value: value, selectizeOptions: {create: true}, optionsCaption: '${ _ko('Field...') }', selectize: $root.collection.template.fieldsNames" class="hit-options" style="margin-bottom: 0"></select>
+      <select data-bind="options: $data.metrics, optionsText: 'label', optionsValue: 'value', value: $data.function" class="input-small"></select>
     <!-- /ko -->
 
     <!-- ko if: $data.function() == 'percentile' -->
       <!-- ko foreach: percentiles() -->
         <input type="number" class="input-mini" data-bind="value: value"/>
-        <a href="javascript: void(0)" data-bind="click: function() { $parent.percentiles.push(ko.mapping.fromJS({'value': 50})); }">
-          <i class="fa fa-plus" title="${ _('Add') }"></i>
-        </a>
+        <a href="javascript: void(0)" data-bind="click: function() { $parent.percentiles.remove($data); }">
         <i class="fa fa-minus" title="${ _('Delete') }"></i>
+        </a>
       <!-- /ko -->
+      <a href="javascript: void(0)" data-bind="click: function() { percentiles.push(ko.mapping.fromJS({'value': 50})); }">
+        <i class="fa fa-plus" title="${ _('Add') }"></i>
+      </a>
     <!-- /ko -->
 
-    <!-- ko if: ['mul', 'add', 'sub'].indexOf($data.function()) != -1 -->
-      <i class="fa fa-plus" title="${ _('Add') }"></i>
-    <!-- /ko -->
-
-    <a href="javascript: void(0)" data-bind="visible: typeof $parent.ops != 'undefined', click: function() { $parent.ops.pop($data); }">
-      <i class="fa fa-minus" title="${ _('Delete') }"></i>
-    </a>
-
     <br/>
-    <a href="javascript: void(0)" data-bind="click: function() {
-        ops.push(ko.mapping.fromJS({'function': 'mul', 'ops': [{'function': 'field', 'value': '', 'ops': []}, {'function': 'field', 'value': '1', 'ops': []}]})); }
-      ">
-      <i class="fa fa-plus" title="${ _('Add formula operation') }"></i>
-    </a>
-    <!-- ko foreach: ops() -->
-      <span data-bind="template: { name: 'metric-form' }"></span>
+
+    <!-- ko if: typeof $parent.properties != "undefined" -->
+      <input data-bind="value: formula, visible: $parent.properties.facets_form.field() == 'formula'"></input>
+    <!-- /ko -->
+    <!-- ko if: typeof $parent.properties == "undefined" -->
+      <input data-bind="value: formula, visible: $parent.field() == 'formula'"></input>
     <!-- /ko -->
+    <input data-bind="value: generated_formula" type="hidden"></input>
   </div>
 
   <div data-bind="visible: ! $root.isEditing(), text: getHitOption($data.function)" class="muted"></div>
@@ -2835,7 +2825,7 @@ var searchViewModel;
 moment.suppressDeprecationWarnings = true;
 
 var NUMERIC_HIT_OPTIONS = [
-    // No count
+    { value: "unique", label: "Count" },
     { value: "unique", label: "${ _('Unique Count') }" },
     { value: "avg", label: "${ _('Average') }" },
     { value: "sum", label: "${ _('Sum') }" },
@@ -2843,31 +2833,26 @@ var NUMERIC_HIT_OPTIONS = [
     { value: "max", label: "${ _('Max') }" },
     { value: "median", label: "${ _('Median') }" },
     { value: "percentile", label: "${ _('Percentiles') }" },
-    { value: "mul", label: "${ _('Multiply') }" },
-    { value: "add", label: "${ _('Add') }" },
-    { value: "sub", label: "${ _('Substract') }" },
     { value: "stddev", label: "${ _('Stddev') }" },
     { value: "variance", label: "${ _('Variance') }" }
 ];
 var DATETIME_HIT_OPTIONS = [
+    { value: "unique", label: "Count" },
     { value: "unique", label: "${ _('Unique Count') }" },
-    { value: "ms", label: "${ _('Substract dates') }" }
 ];
 var ALPHA_HIT_COUNTER_OPTIONS = [
+    { value: "unique", label: "Count" },
     { value: "unique", label: "${ _('Unique Count') }" },
     { value: "min", label: "${ _('Min') }" },
     { value: "max", label: "${ _('Max') }" }
 ];
 var ALPHA_HIT_OPTIONS = [
-    { value: "count", label: "Count" },
+    { value: "unique", label: "Count" },
     { value: "unique", label: "${ _('Unique Count') }" },
     { value: "min", label: "${ _('Min') }" },
     { value: "max", label: "${ _('Max') }" }
 ];
-var HIT_OPTIONS = [
-    { value: "count", label: "${ _('Count') }" }
-  ].concat(NUMERIC_HIT_OPTIONS)\
-  .concat([{ value: "ms", label: "${ _('Substract dates') }" }])
+var HIT_OPTIONS = NUMERIC_HIT_OPTIONS
 ;
 
 

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

@@ -799,8 +799,8 @@ class SolrApi(object):
     else:
       f = facet['aggregate']
 
-    if not f['ops']:
-      f['ops'] = [{'function': 'field', 'value': facet['field'], 'ops': []}]
+    if not f['formula']:
+      f['formula'] = facet['field']
 
     return cls.__get_aggregate_function(f)
 
@@ -810,8 +810,8 @@ class SolrApi(object):
       return f['value']
     else:
       fields = []
-      for _f in f['ops']:
-        fields.append(cls.__get_aggregate_function(_f))
+      if f['formula']:
+        fields.append(f['formula'])
       if f['function'] == 'median':
         f['function'] = 'percentile'
         fields.append('50')