Bläddra i källkod

HUE-2578 [search] Rolling date filter skeleton

Romain Rigaux 10 år sedan
förälder
incheckning
b061cb9

+ 9 - 0
apps/search/src/search/models.py

@@ -471,6 +471,15 @@ class Collection2(object):
       props['collection']['enabled'] = True
     if 'leafletmap' not in props['collection']['template']:
       props['collection']['template']['leafletmap'] = {'latitudeField': None, 'longitudeField': None, 'labelField': None}
+    if 'timeFilter' not in props['collection']:
+      props['collection']['timeFilter'] = {
+        'field': '',
+        'type': 'rolling',
+        'value': 'all',
+        'from': '',
+        'to': 'NOW',
+        'truncate': True
+      }
 
     for facet in props['collection']['facets']:
       properties = facet['properties']

+ 4 - 0
apps/search/src/search/static/search/js/search.ko.js

@@ -402,6 +402,10 @@ var Collection = function (vm, collection) {
   self.autorefresh = ko.mapping.fromJS(collection.autorefresh);
   self.autorefreshSeconds = ko.mapping.fromJS(collection.autorefreshSeconds || 60);
   self.idField = ko.observable(collection.idField);
+  self.timeFilter = ko.mapping.fromJS(collection.timeFilter);
+  self.timeFilter.value.subscribe(function () {
+    vm.search();
+  });
   self.template = ko.mapping.fromJS(collection.template);
   self.template.fieldsSelected.subscribe(function () {
     vm.search();

+ 45 - 6
apps/search/src/search/templates/search.mako

@@ -72,12 +72,15 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
   <form data-bind="visible: $root.isEditing() && columns().length == 0">
     ${ _('Select a search index') }
     <!-- ko if: columns().length == 0 -->
-    <select data-bind="options: $root.initial.collections, value: $root.collection.name, disable: isSyncingCollections">
-    </select>
-    <label class="checkbox" style="display:inline-block; margin-left: 10px">
-      <input type="checkbox" data-bind="checked: showCores" />${ _('Show cores') }
-      <i class="fa fa-spinner fa-spin" data-bind="visible: isSyncingCollections"></i>
-    </label>
+      <select data-bind="options: $root.initial.collections, value: $root.collection.name, disable: isSyncingCollections">
+      </select>
+
+      <label class="checkbox" style="display:inline-block; margin-left: 10px">
+        <input type="checkbox" data-bind="checked: showCores" />${ _('Show cores') }
+        <i class="fa fa-spinner fa-spin" data-bind="visible: isSyncingCollections"></i>
+      </label>
+      
+      <span data-bind="template: {name: 'time-filter'}"></span>
     <!-- /ko -->
   </form>
 
@@ -104,10 +107,46 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
         <!--[if !IE]> --><i class="fa fa-spinner fa-spin" data-bind="visible: isRetrievingResults()"></i><!-- <![endif]-->
         <!--[if IE]><img src="${ static('desktop/art/spinner-inverted.gif') }" data-bind="visible: isRetrievingResults()"/><![endif]-->
       </button>
+
+      <span data-bind="template: {name: 'time-filter'}"></span>
     </div>
   </form>
 </div>
 
+
+<script type="text/html" id="time-filter">
+  <span data-bind="visible: $root.availableDateFields().length > 0">
+    <select data-bind="options: $root.availableDateFields, value: collection.timeFilter.field, optionsValue: 'name', visible: $root.isEditing" class="input-medium"></select>
+
+    <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>
+      </select>      
+    </span>
+    
+    <span data-bind="visible: collection.timeFilter.type() == 'fixed'">
+      Start date/time <input type="text" data-bind="collection.timeFilter.from"></input>
+      End date/time <input type="text" data-bind="collection.timeFilter.to"></input>
+    </span>
+
+    <button class="btn">
+      <i class="fa fa-calendar"></i>
+    </button>
+    
+    <span>  
+      <a data-bind="style: { fontWeight: collection.timeFilter.type() == 'rolling' ? 'bold' : '' }, click: function() {collection.timeFilter.type('rolling'); }">Rolling</a> |
+      <a data-bind="style: { fontWeight: collection.timeFilter.type() == 'fixed' ? 'bold' : '' }, click: function() {collection.timeFilter.type('fixed'); }">Fixed</a>
+    </span>
+  </span>
+</script>
+
+
 <%dashboard:layout_toolbar>
   <%def name="results()">
     <div data-bind="css: { 'draggable-widget': true, 'disabled': !availableDraggableResultset() },

+ 18 - 2
desktop/libs/libsolr/src/libsolr/api.py

@@ -75,9 +75,25 @@ class SolrApi(object):
     else:
       return '%(aggregate)s(%(field)s)' % props
 
-  def _get_fq(self, query):
+  def _get_range_borders(self, duration):
+    return {'from': 'NOW-%s' % duration, 'to': 'NOW'}
+
+  def _get_fq(self, collection, query):
     params = ()
 
+    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']))
+        props['field'] = collection['timeFilter']['field']
+      elif collection['timeFilter']['type'] == 'rolling' and collection['timeFilter']['from'] and collection['timeFilter']['to']:
+        props['field'] = collection['timeFilter']['field']
+        props['from'] = collection['timeFilter']['from']
+        props['to'] = collection['timeFilter']['to']
+
+      if props:
+        params += (('fq', urllib.unquote(utf_quoter('%(field)s:[%(from)s TO %(to)s}' % props))),)
+
     # Merge facets queries on same fields
     grouped_fqs = groupby(query['fqs'], lambda x: (x['type'], x['field']))
     merged_fqs = []
@@ -237,7 +253,7 @@ class SolrApi(object):
             ('json.facet', json.dumps(json_facets)),
         )
 
-    params += self._get_fq(query)
+    params += self._get_fq(collection, query)
 
     if collection['template']['fieldsSelected'] and collection['template']['isGridLayout']:
       fields = set(collection['template']['fieldsSelected'] + [collection['idField']] if collection['idField'] else [])