Преглед изворни кода

[search] Smarter range facets for numbers

Romain Rigaux пре 11 година
родитељ
комит
7b42475abf

+ 28 - 19
apps/search/src/search/api.py

@@ -22,11 +22,11 @@ import urllib
 import numbers
 
 from datetime import datetime
+from math import log
 from time import mktime
 
 from desktop.lib.exceptions_renderable import PopupException
-from desktop.lib.rest.http_client import HttpClient, RestException
-from desktop.lib.rest import resource
+from desktop.lib.rest.http_client import RestException
 from django.utils.translation import ugettext as _
 
 from libsolr.api import SolrApi as BaseSolrApi
@@ -44,8 +44,6 @@ def utf_quoter(what):
   return urllib.quote(unicode(what).encode('utf-8'), safe='~@#$&()*!+=:;,.?/\'')
 
 def _guess_range_facet(widget_type, solr_api, collection, facet_field, properties, start=None, end=None, gap=None):
-  is_range = False
-
   try:
     if widget_type == 'pie-widget':
       SLOTS = 5
@@ -60,20 +58,22 @@ def _guess_range_facet(widget_type, solr_api, collection, facet_field, propertie
     if isinstance(stat_facet['min'], numbers.Number):
       stats_min = int(stat_facet['min']) # if field is float, cast as float isinstance(y, float)
       stats_max = int(stat_facet['max'])
-      if start is not None:
-        stats_min = max(start, stats_min)
-      if end is not None:
-        stats_max = min(end, stats_max)   
-      # TODO: check min is min of max + refactor
+      if start is None:
+        start, _ = _round_range(stats_min)
+      if end is None:
+        _, end = _round_range(stats_max)        
       
       if gap is None:
-        gap = (stats_max - stats_min) / SLOTS
+        gap = (end - start) / SLOTS
       if gap < 1:
         gap = 1
-      is_range = True
     elif 'T' in stat_facet['min']:
       stats_min = stat_facet['min']
       stats_max = stat_facet['max']
+      if start is None:
+        start = stats_min 
+      if end is None:
+        end = stats_max
       difference = (
           mktime(datetime.strptime(stats_max, '%Y-%m-%dT%H:%M:%SZ').timetuple()) - 
           mktime(datetime.strptime(stats_min, '%Y-%m-%dT%H:%M:%SZ').timetuple())
@@ -93,19 +93,28 @@ def _guess_range_facet(widget_type, solr_api, collection, facet_field, propertie
       else:
         unit = 'YEARS'
       gap = '+1' + unit      
-      is_range = True
-  except Exception, e:
-    # stats not supported on all the fields, like text
-    pass
 
-  if is_range:
     properties.update({
-      'start': stats_min,
-      'end': stats_max,
+      'min': stats_min,
+      'max': stats_max,
+      'start': start,
+      'end': end,
       'gap': gap,
       'canRange': True,
-    }) 
+    })
+  except Exception, e:
+    print e
+    # stats not supported on all the fields, like text
+    pass
 
+def _round_range(n):
+  if n <= 10:
+    return n, n
+  else:
+    i = int(log(n, 10))
+    end = round(n, -i)
+    start = end - 10 ** i
+    return start, end 
 
 def _guess_gap(solr_api, collection, facet_field, start=None, end=None):
   properties = {}

+ 1 - 1
apps/search/src/search/templates/admin_collections.mako

@@ -33,7 +33,7 @@ ${ commonheader(_('Search'), "search", user, "29px") | n,unicode }
       <i class="fa fa-plus-circle"></i> ${ _('Import Index') }
     </button>
     <a class="btn importBtn" href="${ url('indexer:collections') }">
-      <i class="fa fa-plus-circle"></i> ${ _('Create Index') }
+      <i class="fa fa-plus-circle"></i> ${ _('Indexes') }
     </a>
     </div>
   <h4>${_('Dashboards')}</h4>

+ 6 - 0
apps/search/src/search/templates/search2.mako

@@ -311,6 +311,12 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
       ${ _('End') }: <input type="text" class="input-small" data-bind="value: properties.end" />
       <br/>
       ${ _('Gap') }: <input type="text" class="input-small" data-bind="value: properties.gap" />
+      <br/>
+      ${ _('Min') }:
+      <input type="text" class="input-small" data-bind="value: properties.min" />
+      <br/>
+      ${ _('Max') }: <input type="text" class="input-small" data-bind="value: properties.max" />
+      <br/>      
     <!-- /ko -->
     <!-- ko if: type() == 'field' -->
       ${ _('Limit') }: <input type="text" class="input-small" data-bind="value: properties.limit" />