Browse Source

HUE-2173 [dashboard] Port the range & up next text facet

Romain Rigaux 8 years ago
parent
commit
6169990d8c

+ 53 - 6
desktop/libs/dashboard/src/dashboard/models.py

@@ -355,6 +355,52 @@ def range_pair(field, cat, fq_filter, iterable, end, collection_facet):
   return pairs
   return pairs
 
 
 
 
+def range_pair2(facet_field, cat, fq_filter, iterable, end, facet):
+  # e.g. counts":["0",17430,"1000",1949,"2000",671,"3000",404,"4000",243,"5000",165],"gap":1000,"start":0,"end":6000}
+  pairs = []
+  selected_values = [f['value'] for f in fq_filter]
+  print facet
+  is_single_unit_gap = re.match('^[\+\-]?1[A-Za-z]*$', str(facet['gap'])) is not None
+  is_up = facet['sort'] == 'asc'
+
+  if facet['sort'] == 'asc' and facet['type'] == 'range-up':
+    prev = None
+    n = []
+    for e in iterable:
+      if prev is not None:
+        n.append(e)
+        n.append(prev)
+        prev = None
+      else:
+        prev = e
+    iterable = n
+    iterable.reverse()
+
+  a, to = itertools.tee(iterable)
+  next(to, None)
+  counts = iterable[1::2]
+  total_counts = counts.pop(0) if facet['sort'] == 'asc' else 0
+
+  for element in a:
+    next(to, None)
+    to_value = next(to, end)
+    count = next(a)
+
+    pairs.append({
+        'field': facet_field, 'from': element, 'value': count, 'to': to_value, 'selected': element in selected_values,
+        'exclude': all([f['exclude'] for f in fq_filter if f['value'] == element]),
+        'is_single_unit_gap': is_single_unit_gap,
+        'total_counts': total_counts,
+        'is_up': is_up
+    })
+    total_counts += counts.pop(0) if counts else 0
+
+  if facet['sort'] == 'asc' and facet['type'] != 'range-up':
+    pairs.reverse()
+
+  return pairs
+
+
 def augment_solr_response(response, collection, query):
 def augment_solr_response(response, collection, query):
   augmented = response
   augmented = response
   augmented['normalized_facets'] = []
   augmented['normalized_facets'] = []
@@ -460,11 +506,12 @@ def augment_solr_response(response, collection, query):
           cols.append(SolrApi._get_aggregate_function(f))
           cols.append(SolrApi._get_aggregate_function(f))
         rows = []
         rows = []
 
 
-        # For dim in dimensions
-
+        facet_one = collection_facet['properties']['facets'][0]
+        
         # Number or Date range
         # Number or Date range
-        if collection_facet['properties']['canRange'] and not facet['properties'].get('type') == 'field':
-          dimension = 3 if collection_facet['properties']['isDate'] else 1
+        if facet_one['canRange'] and not facet_one['type'] == 'field':
+          dimension = 3 if facet_one['isDate'] else 1
+
           # Single dimension or dimension 2 with analytics
           # Single dimension or dimension 2 with analytics
           if len(collection_facet['properties']['facets']) == 1 or len(collection_facet['properties']['facets']) == 2 and collection_facet['properties']['facets'][1]['aggregate']['function'] != 'count':
           if len(collection_facet['properties']['facets']) == 1 or len(collection_facet['properties']['facets']) == 2 and collection_facet['properties']['facets'][1]['aggregate']['function'] != 'count':
             column = 'count'
             column = 'count'
@@ -479,7 +526,7 @@ def augment_solr_response(response, collection, query):
             _augment_stats_2d(name, facet, counts, selected_values, agg_keys, rows)
             _augment_stats_2d(name, facet, counts, selected_values, agg_keys, rows)
 
 
             counts = [_v for _f in counts for _v in (_f['val'], _f[column])]
             counts = [_v for _f in counts for _v in (_f['val'], _f[column])]
-            counts = range_pair(facet['field'], name, selected_values.get(facet['id'], []), counts, 1, collection_facet)
+            counts = range_pair2(facet['field'], name, selected_values.get(facet['id'], []), counts, 1, collection_facet['properties']['facets'][0])
           else:
           else:
             # Dimension 1 with counts and 2 with analytics
             # Dimension 1 with counts and 2 with analytics
             agg_keys = [key for key, value in counts[0].items() if key.lower().startswith('agg_') or key.lower().startswith('dim_')]
             agg_keys = [key for key, value in counts[0].items() if key.lower().startswith('agg_') or key.lower().startswith('dim_')]
@@ -501,7 +548,7 @@ def augment_solr_response(response, collection, query):
                   _series[legend].append(cell)
                   _series[legend].append(cell)
 
 
             for _name, val in _series.iteritems():
             for _name, val in _series.iteritems():
-              _c = range_pair(facet['field'], _name, selected_values.get(facet['id'], []), val, 1, collection_facet)
+              _c = range_pair2(facet['field'], _name, selected_values.get(facet['id'], []), val, 1, collection_facet['properties']['facets'][0])
               extraSeries.append({'counts': _c, 'label': _name})
               extraSeries.append({'counts': _c, 'label': _name})
             counts = []
             counts = []
         elif collection_facet['properties'].get('isOldPivot'):
         elif collection_facet['properties'].get('isOldPivot'):

+ 3 - 3
desktop/libs/dashboard/src/dashboard/templates/common_search.mako

@@ -731,7 +731,7 @@ ${ dashboard.layout_skeleton(suffix='search') }
     </div>
     </div>
     <div class="clearfix"></div>
     <div class="clearfix"></div>
     <div data-bind="with: $root.collection.getFacetById($parent.id())">
     <div data-bind="with: $root.collection.getFacetById($parent.id())">
-      <!-- ko if: properties.type() == 'field' -->
+      <!-- ko if: properties.facets()[0].type() == 'field' -->
         <div data-bind="foreach: $parent.counts">
         <div data-bind="foreach: $parent.counts">
           <div class="trigger-exclude">
           <div class="trigger-exclude">
               <!-- ko if: $index() < $parent.properties.limit() -->
               <!-- ko if: $index() < $parent.properties.limit() -->
@@ -771,7 +771,7 @@ ${ dashboard.layout_skeleton(suffix='search') }
           </div>
           </div>
         </div>
         </div>
       <!-- /ko -->
       <!-- /ko -->
-      <!-- ko if: properties.type() == 'range' -->
+      <!-- ko if: properties.facets()[0].type() == 'range' -->
         <div data-bind="foreach: $parent.counts">
         <div data-bind="foreach: $parent.counts">
           <div class="trigger-exclude">
           <div class="trigger-exclude">
               <!-- ko if: ! selected -->
               <!-- ko if: ! selected -->
@@ -793,7 +793,7 @@ ${ dashboard.layout_skeleton(suffix='search') }
           </div>
           </div>
         </div>
         </div>
       <!-- /ko -->
       <!-- /ko -->
-      <!-- ko if: properties.type() == 'range-up' -->
+      <!-- ko if: properties.facets()[0].type() == 'range-up' -->
         <div data-bind="foreach: $parent.counts">
         <div data-bind="foreach: $parent.counts">
           <div class="trigger-exclude">
           <div class="trigger-exclude">
               <!-- ko if: ! selected -->
               <!-- ko if: ! selected -->