浏览代码

[search] Date selection overrides timeline selection

Romain Rigaux 10 年之前
父节点
当前提交
4339fc6
共有 2 个文件被更改,包括 60 次插入21 次删除
  1. 22 8
      apps/search/src/search/templates/search.mako
  2. 38 13
      desktop/libs/libsolr/src/libsolr/api.py

+ 22 - 8
apps/search/src/search/templates/search.mako

@@ -121,12 +121,15 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
     <span data-bind="visible: collection.timeFilter.type() == 'rolling'">
       <select data-bind="value: collection.timeFilter.value" class="input-small"  style="margin-left:10px">
         <option value="all">${ _('All') }</option>
-        <option value="5MINUTES">${ _('Last 5 Minutes') }</option>
-        <option value="15MINUTES">Last 15 Minutes</option>
-        <option value="1HOURS">Last 1 Hour</option>
-        <option value="6MONTHS">Last 6 Months</option>
-        <option value="1YEARS">Last Year</option>
-        <option value="2YEARS">Last 2 Years</option>
+        <option value="5MINUTES/MINUTES">${ _('Last 5 Minutes') }</option>
+        <option value="15MINUTES/MINUTES">Last 15 Minutes</option>
+        <option value="1HOURS/HOURS">Last 1 Hour</option>
+        <option value="1DAYS/DAYS">Yesterday</option>
+        <option value="2DAYS/DAYS">Last 2 days</option>
+        <option value="1WEEKS/WEEKS">Last Week</option>
+        <option value="6MONTHS/MONTHS">Last 6 Months</option>
+        <option value="1YEARS/YEARS">Last Year</option>
+        <option value="2YEARS/YEARS">Last 2 Years</option>
       </select>      
     </span>
     
@@ -296,6 +299,7 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
       </%def>
 </%dashboard:layout_toolbar>
 
+
 ${ dashboard.layout_skeleton() }
 
 
@@ -351,7 +355,17 @@ ${ dashboard.layout_skeleton() }
       </div>
     <!-- /ko -->
 
-    <!-- ko if: type() == 'range' || type() == 'range-up' || (type() == 'nested' && typeof properties.min != "undefined")-->
+    <!-- ko if: type() == 'range' || type() == 'range-up' || (type() == 'nested' && typeof properties.min != "undefined") -->
+      ## <input type="checkbox" data-bind="checkedValue: name, checked: $root.collection.template.fieldsSelected" style="margin: 0" />      
+      <div class="facet-field-cnt">
+        <span class="spinedit-cnt">
+          <span class="facet-field-label facet-field-label-fixed-width">
+            ${ _('Custom range') }
+          </span>
+          <input type="checkbox"/>
+        </span>
+      </div>
+
       <!-- ko ifnot: properties.isDate() -->
         <div class="slider-cnt" data-bind="slider: {start: properties.min, end: properties.max, gap: properties.initial_gap, min: properties.initial_start, max: properties.initial_end, properties: properties, labels: SLIDER_LABELS}"></div>
       <!-- /ko -->
@@ -848,7 +862,7 @@ ${ dashboard.layout_skeleton() }
   <div class="row-fluid" data-bind="with: $root.getFacetFromQuery(id())">
     <div data-bind="visible: $root.isEditing, with: $root.collection.getFacetById($parent.id())" style="margin-bottom: 20px">
       <span data-bind="template: { name: 'facet-toggle' }">
-      </span>
+      </span>      
     </div>
 
     <div style="padding-bottom: 10px; text-align: right; padding-right: 20px" data-bind="visible: counts().length > 0">

+ 38 - 13
desktop/libs/libsolr/src/libsolr/api.py

@@ -75,24 +75,36 @@ class SolrApi(object):
     else:
       return '%(aggregate)s(%(field)s)' % props
 
-  def _get_range_borders(self, duration):
-    return {'from': 'NOW-%s' % duration, 'to': 'NOW'}
-
-  def _get_fq(self, collection, query):
-    params = ()
-
+  def _get_range_borders(self, collection):
+    props = {}
+    GAPS = {
+        '5MINUTES/MINUTES': '+3SECONDS',
+        '15MINUTES/MINUTES': '+10SECONDS',
+        '1HOURS/HOURS': '+36SECONDS',
+        '1DAYS/DAYS': '+15MINUTES',
+        '2DAYS/DAYS': '+30MINUTES',
+        '1WEEKS/WEEKS': '+3HOURS',        
+    }
+    
     if collection['timeFilter'].get('field'):
-      props = {}
-      if collection['timeFilter']['type'] == 'rolling' and collection['timeFilter']['value'] != 'all':
-        props.update(self._get_range_borders(collection['timeFilter']['value']))
+      if collection['timeFilter']['type'] == 'rolling' and collection['timeFilter']['value'] != 'all': # todo all for chart range? guess based on min
         props['field'] = collection['timeFilter']['field']
-      elif collection['timeFilter']['type'] == 'rolling' and collection['timeFilter']['from'] and collection['timeFilter']['to']:
+        props['from'] = 'NOW-%s' % collection['timeFilter']['value']
+        props['to'] = 'NOW'
+        props['gap'] = GAPS.get(collection['timeFilter']['value'])
+      elif collection['timeFilter']['type'] == 'fixed' and collection['timeFilter']['from'] and collection['timeFilter']['to']:
         props['field'] = collection['timeFilter']['field']
         props['from'] = collection['timeFilter']['from']
         props['to'] = collection['timeFilter']['to']
+            
+    return props
 
-      if props:
-        params += (('fq', urllib.unquote(utf_quoter('%(field)s:[%(from)s TO %(to)s}' % props))),)
+  def _get_fq(self, collection, query):
+    params = ()
+
+    timeFilter = self._get_range_borders(collection)
+    if timeFilter:
+      params += (('fq', urllib.unquote(utf_quoter('%(field)s:[%(from)s TO %(to)s}' % timeFilter))),)
 
     # Merge facets queries on same fields
     grouped_fqs = groupby(query['fqs'], lambda x: (x['type'], x['field']))
@@ -159,6 +171,8 @@ class SolrApi(object):
       )
       json_facets = {}
 
+      timeFilter = self._get_range_borders(collection)
+
       for facet in collection['facets']:
         if facet['type'] == 'query':
           params += (('facet.query', '%s' % facet['field']),)
@@ -169,9 +183,20 @@ class SolrApi(object):
               'key': '%(field)s-%(id)s' % facet,
               'start': facet['properties']['start'],
               'end': facet['properties']['end'],
-              'gap': facet['properties']['gap'],
+              'gap': facet['properties']['gap'],              
               'mincount': int(facet['properties']['mincount'])
           }
+          
+          # todo round by gap? 
+          
+          if timeFilter:
+            if facet['widgetType'] == 'histogram-widget':
+              keys.update({
+                'start': timeFilter['from'],
+                'end': timeFilter['to'],
+                'gap': timeFilter['gap'],
+              })
+
           params += (
              ('facet.range', '{!key=%(key)s ex=%(id)s f.%(field)s.facet.range.start=%(start)s f.%(field)s.facet.range.end=%(end)s f.%(field)s.facet.range.gap=%(gap)s f.%(field)s.facet.mincount=%(mincount)s}%(field)s' % keys),
           )