Browse Source

HUE-2173 [search] Adding function to bar charts

Romain Rigaux 10 years ago
parent
commit
b7f208b

+ 4 - 1
apps/search/src/search/models.py

@@ -736,7 +736,10 @@ def augment_solr_response(response, collection, query):
         buckets = []
         for bucket in response['facets'][name]['buckets']:
           buckets.append(bucket['val'])
-          buckets.append(bucket['count'])
+          if 'd2' in bucket:
+            buckets.append(bucket['d2'])
+          else:
+            buckets.append(bucket['count'])
 
         counts = pairwise2(facet['field'], selected_values.get(facet['id'], []), buckets)
         if collection_facet['properties']['sort'] == 'asc':

+ 6 - 2
apps/search/src/search/static/search/js/search.ko.js

@@ -543,21 +543,25 @@ var Collection = function (vm, collection) {
       pivot = ko.mapping.fromJS({
         'field': facet.properties.facets_form.field,
         'limit': facet.properties.facets_form.limit,
-        'mincount': facet.properties.facets_form.mincount
+        'mincount': facet.properties.facets_form.mincount,
+        'functionz': facet.properties.facets_form.function,
       });
       facet.properties.facets_form.field = null;
       facet.properties.facets_form.limit = 5;
       facet.properties.facets_form.mincount = 1;
+      facet.properties.facets_form.function = 'count';
     } else {
       if (typeof facet.properties.facets_form.field != 'undefined') {
         pivot = ko.mapping.fromJS({
           'field': facet.properties.facets_form.field(),
           'limit': facet.properties.facets_form.limit(),
-          'mincount': facet.properties.facets_form.mincount()
+          'mincount': facet.properties.facets_form.mincount(),
+          'functionz': facet.properties.facets_form.function()
         });
         facet.properties.facets_form.field(null);
         facet.properties.facets_form.limit(5);
         facet.properties.facets_form.mincount(1);
+        facet.properties.facets_form.function('count');
       }
     }
 

+ 54 - 15
apps/search/src/search/templates/search.mako

@@ -332,10 +332,12 @@ ${ dashboard.layout_skeleton() }
     <!-- /ko -->
     </div>
 
-    <!-- ko if: type() == 'pivot' -->
+    <!-- ko if: type() == 'pivot' || type() == 'terms' -->
       <div class="facet-field-tile" data-bind="visible: properties.scope() == 'tree' || properties.facets().length == 0">
         <div class="facet-field-cnt">
-          <span class="facet-field-label facet-field-label-fixed-width facet-field-label-fixed-width-double facet-field-label-title">${ _('Add a dimension') }</span>
+          <span class="facet-field-label facet-field-label-fixed-width facet-field-label-fixed-width-double facet-field-label-title">
+            ${ _('Add a dimension') }
+          </span>
         </div>
 
         <div class="facet-field-cnt">
@@ -347,6 +349,17 @@ ${ dashboard.layout_skeleton() }
           </span>
         </div>
 
+     <span class="facet-field-label">${ _('Metric') }</span>
+      <select data-bind="value: properties.facets_form.function">
+        <option value="unique" selected="selected" label="${ _('Unique Count') }">${ _('Unique Count') }</option>
+        <option value="avg" label="${ _('Average') }">${ _('Average') }</option>
+        <option value="sum" label="${ _('Sum') }">${ _('Sum') }</option>
+        <option value="min" label="${ _('Min') }">${ _('Min') }</option>
+        <option value="max" label="${ _('Max') }">${ _('Max') }</option>
+        <option value="sumsq" label="${ _('Sum of square') }">${ _('Sum of square') }</option>
+        <option value="median" label="${ _('Median') }">${ _('Median') }</option>
+      </select>
+
         <div class="facet-field-cnt">
           <span class="spinedit-cnt">
             <span class="facet-field-label facet-field-label-fixed-width">
@@ -807,18 +820,7 @@ ${ dashboard.layout_skeleton() }
       <a href="javascript:void(0)" data-bind="click: $root.collection.rangeZoomOut"><i class="fa fa-search-minus"></i> ${ _('reset') }</a>
       <span class="facet-field-label" data-bind="visible: $root.query.multiqs().length > 1">${ _('Group by') }</span>
       <select class="input-medium" data-bind="visible: $root.query.multiqs().length > 1, options: $root.query.multiqs, optionsValue: 'id', optionsText: 'label', value: $root.query.selectedMultiq"></select>
-      
-      <select data-bind="value: 'count'">
-        <option value="count" label="${ _('Count') }">${ _('Count') }</option>
-        <option value="unique" label="${ _('Unique Count') }">${ _('Unique Count') }</option>
-        <option value="avg" label="${ _('Average') }">${ _('Average') }</option>
-        <option value="sum" label="${ _('Sum') }">${ _('Sum') }</option>
-        <option value="min" label="${ _('Min') }">${ _('Min') }</option>
-        <option value="max" label="${ _('Max') }">${ _('Max') }</option>
-        <option value="sumsq" label="${ _('Sum of square') }">${ _('Sum of square') }</option>
-        <option value="percentile" label="${ _('Percentiles') }">${ _('Percentiles') }</option>
-      </select>      
-      
+
     </div>
     <!-- ko if: $root.collection.getFacetById($parent.id()) -->
     <div data-bind="timelineChart: {datum: {counts: counts(), extraSeries: extraSeries(), widget_id: $parent.id(), label: label()}, stacked: $root.collection.getFacetById($parent.id()).properties.stacked(), field: field, label: label(), transformer: timelineChartDataTransformer,
@@ -854,6 +856,43 @@ ${ dashboard.layout_skeleton() }
           <a href="javascript:void(0)" data-bind="click: $root.collection.rangeZoomOut"><i class="fa fa-search-minus"></i> ${ _('reset') }</a>
         </div>
       <!-- /ko -->
+      
+<div class="dimensions-header margin-bottom-10" data-bind="visible: $root.isEditing() && $data.properties.facets().length > 0">
+        <span class="muted">${ _('Selected dimensions') }</span>
+      </div>
+      <div data-bind="foreach: $data.properties.facets, visible: $root.isEditing">
+        <div class="filter-box">
+          <div class="title">
+            <a data-bind="click: function() { $root.collection.removePivotFacetValue({'pivot_facet': $parent, 'value': $data}); }" class="pull-right" href="javascript:void(0)">
+              <i class="fa fa-times"></i>
+            </a>
+            <span data-bind="text: field"></span>
+            <span data-bind="text: functionz"></span>
+            &nbsp;
+          </div>
+
+          <div class="content">
+            <div class="facet-field-cnt">
+              <span class="spinedit-cnt">
+                <span class="facet-field-label facet-field-label-fixed-width">
+                  ${ _('Limit') }
+                </span>
+                <input type="text" class="input-medium" data-bind="spinedit: limit"/>
+              </span>
+            </div>
+
+            <div class="facet-field-cnt">
+              <span class="spinedit-cnt">
+                <span class="facet-field-label facet-field-label-fixed-width">
+                  ${ _('Min Count') }
+                </span>
+                <input type="text" class="input-medium" data-bind="spinedit: mincount"/>
+              </span>
+            </div>
+          </div>
+        </div>
+      </div>
+      <div class="clearfix"></div>      
     </div>
 
     <!-- ko if: $root.collection.getFacetById($parent.id()) -->
@@ -1503,7 +1542,7 @@ var HIT_OPTIONS = [
   { value: "min", label: "${ _('Min') }" },
   { value: "max", label: "${ _('Max') }" },
   { value: "sumsq", label: "${ _('Sum of square') }" },
-  { value: "percentile", label: "${ _('Percentiles') }" }
+  { value: "median", label: "${ _('Median') }" }
 ];
 
 function getHitOption(value){

+ 7 - 3
apps/search/src/search/views.py

@@ -479,14 +479,14 @@ def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_t
     'stacked': False,
     'limit': 10,
     'mincount': 0,
-    'isDate': False
+    'isDate': False,
+    'function': 'unique' # new
   }
 
   if widget_type in ('tree-widget', 'heatmap-widget', 'map-widget'):
     facet_type = 'pivot'
   elif widget_type == 'hit-widget':
     facet_type = 'function'
-    properties['function'] = 'unique'
   else:
     solr_api = SolrApi(SOLR_URL.get(), user)
     range_properties = _new_range_facet(solr_api, collection, facet_field, widget_type)
@@ -500,12 +500,16 @@ def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_t
     else:
       #facet_type = 'field'
       facet_type = 'terms' # if 5.2+
+      # New
+      properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 10, 'function': 'count'}
+      properties['facets'] = []
+      properties['scope'] = 'stack'
 
   if widget_type in ('tree-widget', 'heatmap-widget', 'map-widget'):
     properties['mincount'] = 1
     properties['facets'] = []
     properties['stacked'] = True
-    properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 5}
+    properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 5, 'function': 'count'} # todo
 
     if widget_type == 'map-widget':
       properties['scope'] = 'world'

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

@@ -166,21 +166,27 @@ class SolrApi(object):
               'key': '%(field)s-%(id)s' % facet
           }
           props.update(facet)
-         
-          json_facets['%(key)s' % props] = {
+          print facet
+          _f = {
               'type': 'terms',
               'field': facet['field'],
               'limit': int(facet['properties'].get('limit', 10)) + (1 if facet['widgetType'] == 'facet-widget' else 0),
               'mincount': int(facet['properties']['mincount'])
           }
+          if facet['properties']['facets']: # [{u'field': u'salary_d', u'functionz': u'avg', u'limit': 10, u'mincount': 1}
+            _f['facet'] = {
+                'd2': '%(functionz)s(%(field)s)' % facet['properties']['facets'][0] 
+            }
+            
+          json_facets['%(key)s' % props] = _f
         elif facet['type'] == 'function':
           props = {
               'function': facet['properties']['function'],
               'key': '%(field)s-%(id)s' % facet
           }
           props.update(facet)
-          if facet['properties']['function'] == 'percentile':
-            props['formula'] = '%(function)s(%(field)s,25,50,75,90,99)' % props
+          if facet['properties']['function'] == 'median':
+            props['formula'] = 'percentile(%(field)s,50)' % props
           else:
             props['formula'] = '%(function)s(%(field)s)' % props          
           json_facets['%(key)s' % props] = '%(formula)s' % props