Переглянути джерело

[search] Apply global fixed date filter to widgets

Romain Rigaux 10 роки тому
батько
коміт
2e036ffb5b

+ 12 - 6
apps/search/src/search/api.py

@@ -33,8 +33,20 @@ LOG = logging.getLogger(__name__)
 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):
   try:
+    stats_json = solr_api.stats(collection['name'], [facet_field])
+    stat_facet = stats_json['stats']['stats_fields'][facet_field]
+
+    _compute_range_facet(widget_type, stat_facet, properties, start, end, gap)
+  except Exception, e:
+    print e
+    # stats not supported on all the fields, like text
+    pass
+  
+def _compute_range_facet(widget_type, stat_facet, properties, start=None, end=None, gap=None):
+
     if widget_type == 'pie-widget':
       SLOTS = 5
     elif widget_type == 'facet-widget':
@@ -42,8 +54,6 @@ def _guess_range_facet(widget_type, solr_api, collection, facet_field, propertie
     else:
       SLOTS = 100
 
-    stats_json = solr_api.stats(collection['name'], [facet_field])
-    stat_facet = stats_json['stats']['stats_fields'][facet_field]
     is_date = False
 
     if isinstance(stat_facet['min'], numbers.Number):
@@ -151,10 +161,6 @@ def _guess_range_facet(widget_type, solr_api, collection, facet_field, propertie
         'timelineChartType': 'bar'
       })
 
-  except Exception, e:
-    print e
-    # stats not supported on all the fields, like text
-    pass
 
 def _round_date_range(tm):
   start = tm - timedelta(seconds=tm.second, microseconds=tm.microsecond)

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

@@ -406,6 +406,12 @@ var Collection = function (vm, collection) {
   self.timeFilter.value.subscribe(function () {
     vm.search();
   });
+  self.timeFilter.from.subscribe(function () {
+    vm.search();
+  });  
+  self.timeFilter.to.subscribe(function () {
+    vm.search();
+  });
   self.timeFilter.type.subscribe(function (val) {
     if (val == 'fixed' && self.timeFilter.from().length == 0) {
       $.ajax({
@@ -421,6 +427,8 @@ var Collection = function (vm, collection) {
           self.timeFilter.to(data.properties.end);
         }
       });
+    } else {
+      vm.search();
     }
   });
   self.template = ko.mapping.fromJS(collection.template);

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

@@ -1607,7 +1607,7 @@ ${ dashboard.layout_skeleton() }
 
 
 <script type="text/html" id="time-fixed-filter">
-  <span data-bind="visible: $root.availableDateFields().length > 0" class="muted" title="${ _('Time Settings') }" rel="tooltip" data-placement="bottom" data-toggle="modal" data-target="#timeSettingsDemiModal">
+  <span data-bind="visible: $root.availableDateFields().length > 0" class="muted" title="${ _('Time Settings') }" rel="tooltip" data-placement="bottom" data-toggle="modal" data-target="#timeSettingsDemiModal" style="cursor:pointer">
     &nbsp;<span data-bind="text: moment($root.collection.timeFilter.from()).utc().format('YYYY-MM-DD HH:mm:SS')"></span>
     <i class="fa fa-long-arrow-right"></i>
     <span data-bind="text: moment($root.collection.timeFilter.to()).utc().format('YYYY-MM-DD HH:mm:SS')"></span>

+ 16 - 1
desktop/libs/libsolr/src/libsolr/api.py

@@ -30,6 +30,8 @@ from desktop.lib.rest.http_client import HttpClient, RestException
 from desktop.lib.rest import resource
 
 from search.conf import EMPTY_QUERY, SECURITY_ENABLED
+from search.api import _compute_range_facet
+import re
 
 
 LOG = logging.getLogger(__name__)
@@ -152,14 +154,27 @@ class SolrApi(object):
         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']:
+      elif collection['timeFilter']['type'] == 'fixed':
         props['field'] = collection['timeFilter']['field']
         props['from'] = collection['timeFilter']['from']
         props['to'] = collection['timeFilter']['to']
+        props['fixed'] = True
 
     return props
 
   def _get_time_filter_query(self, timeFilter, facet):
+    if 'fixed' in timeFilter:
+      props = {}
+      stat_facet = {'min': timeFilter['from'], 'max': timeFilter['to']}
+      _compute_range_facet(facet['widgetType'], stat_facet, props, stat_facet['min'], stat_facet['max'])
+      gap = props['gap']
+      unit = re.split('\d+', gap)[1]
+      return {
+        'start': '%(from)s/%(unit)s' % {'from': timeFilter['from'], 'unit': unit},
+        'end': '%(to)s/%(unit)s' % {'to': timeFilter['to'], 'unit': unit},
+        'gap': '%(gap)s' % props, # add a 'auto'
+      }
+    else:
       gap = timeFilter['gap'][facet['widgetType']]
       return {
         'start': '%(from)s/%(unit)s' % {'from': timeFilter['from'], 'unit': gap['unit']},