Browse Source

HUE-3228 [dashboard] Add first rolling or fix global date filtering

Romain Rigaux 8 năm trước cách đây
mục cha
commit
63f4f58
2 tập tin đã thay đổi với 121 bổ sung86 xóa
  1. 33 0
      apps/impala/src/impala/dashboard_api.py
  2. 88 86
      desktop/libs/libsolr/src/libsolr/api.py

+ 33 - 0
apps/impala/src/impala/dashboard_api.py

@@ -25,10 +25,13 @@ from itertools import groupby
 
 from django.utils.html import escape
 
+from libsolr.api import GAPS
+
 from notebook.models import make_notebook
 from notebook.connectors.base import get_api, OperationTimeout
 
 from search.models import Collection2
+from search.facet_builder import _compute_range_facet
 
 
 LOG = logging.getLogger(__name__)
@@ -59,6 +62,10 @@ class SQLApi():
     filters = [q['q'] for q in query['qs'] if q['q']]
     filters.extend(self._get_fq(dashboard, query, facet))
 
+    timeFilter = self._get_time_filter_query(dashboard, query)
+    if timeFilter:
+      filters.append(timeFilter)
+
     if facet:
       if facet['type'] == 'nested':
         fields_dimensions = [self._get_dimension_field(f)['name'] for f in self._get_dimension_fields(facet)]
@@ -390,6 +397,32 @@ class SQLApi():
     return _type in ('timestamp')
 
 
+  def _get_time_filter_query(self, collection, query):
+    time_field = collection['timeFilter'].get('field')
+
+    if time_field and (collection['timeFilter']['value'] != 'all' or collection['timeFilter']['type'] == 'fixed'):
+      props = {
+        'field': collection['timeFilter']['field'],
+      }
+      # fqs overrides main time filter
+#       fq_time_ids = [fq['id'] for fq in query['fqs'] if fq['field'] == time_field]
+#       props['time_filter_overrides'] = fq_time_ids
+#       props['time_field'] = time_field
+
+      if collection['timeFilter']['type'] == 'rolling':
+        empty, coeff, unit = re.split('(\d+)', collection['timeFilter']['value'])
+
+        props['from'] = "now() - interval %(coeff)s %(unit)s" % {'coeff': coeff, 'unit': unit.strip('S')}
+        props['to'] = 'now()' # TODO +/- Tz of user
+      elif collection['timeFilter']['type'] == 'fixed':
+        props['from'] = collection['timeFilter'].get('from', 'now() - 7 DAY')
+        props['to'] = collection['timeFilter'].get('to', 'now()')
+
+      return "(`%(field)s` >= %(from)s AND `%(field)s` <= %(to)s)" %  props
+    else:
+      return {}
+
+
   def _get_database_table_names(self, name):
     if '.' in name:
       database, table_name = name.split('.', 1)

+ 88 - 86
desktop/libs/libsolr/src/libsolr/api.py

@@ -652,92 +652,6 @@ class SolrApi(object):
 
   def _get_range_borders(self, collection, query):
     props = {}
-    GAPS = {
-        '5MINUTES': {
-            'histogram-widget': {'coeff': '+3', 'unit': 'SECONDS'}, # ~100 slots
-            'timeline-widget': {'coeff': '+3', 'unit': 'SECONDS'}, # ~100 slots
-            'bucket-widget': {'coeff': '+3', 'unit': 'SECONDS'}, # ~100 slots
-            'bar-widget': {'coeff': '+3', 'unit': 'SECONDS'}, # ~100 slots
-            'facet-widget': {'coeff': '+1', 'unit': 'MINUTES'}, # ~10 slots
-        },
-        '30MINUTES': {
-            'histogram-widget': {'coeff': '+20', 'unit': 'SECONDS'},
-            'timeline-widget': {'coeff': '+20', 'unit': 'SECONDS'},
-            'bucket-widget': {'coeff': '+20', 'unit': 'SECONDS'},
-            'bar-widget': {'coeff': '+20', 'unit': 'SECONDS'},
-            'facet-widget': {'coeff': '+5', 'unit': 'MINUTES'},
-        },
-        '1HOURS': {
-            'histogram-widget': {'coeff': '+30', 'unit': 'SECONDS'},
-            'timeline-widget': {'coeff': '+30', 'unit': 'SECONDS'},
-            'bucket-widget': {'coeff': '+30', 'unit': 'SECONDS'},
-            'bar-widget': {'coeff': '+30', 'unit': 'SECONDS'},
-            'facet-widget': {'coeff': '+10', 'unit': 'MINUTES'},
-        },
-        '12HOURS': {
-            'histogram-widget': {'coeff': '+7', 'unit': 'MINUTES'},
-            'timeline-widget': {'coeff': '+7', 'unit': 'MINUTES'},
-            'bucket-widget': {'coeff': '+7', 'unit': 'MINUTES'},
-            'bar-widget': {'coeff': '+7', 'unit': 'MINUTES'},
-            'facet-widget': {'coeff': '+1', 'unit': 'HOURS'},
-        },
-        '1DAYS': {
-            'histogram-widget': {'coeff': '+15', 'unit': 'MINUTES'},
-            'timeline-widget': {'coeff': '+15', 'unit': 'MINUTES'},
-            'bucket-widget': {'coeff': '+15', 'unit': 'MINUTES'},
-            'bar-widget': {'coeff': '+15', 'unit': 'MINUTES'},
-            'facet-widget': {'coeff': '+3', 'unit': 'HOURS'},
-        },
-        '2DAYS': {
-            'histogram-widget': {'coeff': '+30', 'unit': 'MINUTES'},
-            'timeline-widget': {'coeff': '+30', 'unit': 'MINUTES'},
-            'bucket-widget': {'coeff': '+30', 'unit': 'MINUTES'},
-            'bar-widget': {'coeff': '+30', 'unit': 'MINUTES'},
-            'facet-widget': {'coeff': '+6', 'unit': 'HOURS'},
-        },
-        '7DAYS': {
-            'histogram-widget': {'coeff': '+3', 'unit': 'HOURS'},
-            'timeline-widget': {'coeff': '+3', 'unit': 'HOURS'},
-            'bucket-widget': {'coeff': '+3', 'unit': 'HOURS'},
-            'bar-widget': {'coeff': '+3', 'unit': 'HOURS'},
-            'facet-widget': {'coeff': '+1', 'unit': 'DAYS'},
-        },
-        '1MONTHS': {
-            'histogram-widget': {'coeff': '+12', 'unit': 'HOURS'},
-            'timeline-widget': {'coeff': '+12', 'unit': 'HOURS'},
-            'bucket-widget': {'coeff': '+12', 'unit': 'HOURS'},
-            'bar-widget': {'coeff': '+12', 'unit': 'HOURS'},
-            'facet-widget': {'coeff': '+5', 'unit': 'DAYS'},
-        },
-        '3MONTHS': {
-            'histogram-widget': {'coeff': '+1', 'unit': 'DAYS'},
-            'timeline-widget': {'coeff': '+1', 'unit': 'DAYS'},
-            'bucket-widget': {'coeff': '+1', 'unit': 'DAYS'},
-            'bar-widget': {'coeff': '+1', 'unit': 'DAYS'},
-            'facet-widget': {'coeff': '+30', 'unit': 'DAYS'},
-        },
-        '1YEARS': {
-            'histogram-widget': {'coeff': '+3', 'unit': 'DAYS'},
-            'timeline-widget': {'coeff': '+3', 'unit': 'DAYS'},
-            'bucket-widget': {'coeff': '+3', 'unit': 'DAYS'},
-            'bar-widget': {'coeff': '+3', 'unit': 'DAYS'},
-            'facet-widget': {'coeff': '+12', 'unit': 'MONTHS'},
-        },
-        '2YEARS': {
-            'histogram-widget': {'coeff': '+7', 'unit': 'DAYS'},
-            'timeline-widget': {'coeff': '+7', 'unit': 'DAYS'},
-            'bucket-widget': {'coeff': '+7', 'unit': 'DAYS'},
-            'bar-widget': {'coeff': '+7', 'unit': 'DAYS'},
-            'facet-widget': {'coeff': '+3', 'unit': 'MONTHS'},
-        },
-        '10YEARS': {
-            'histogram-widget': {'coeff': '+1', 'unit': 'MONTHS'},
-            'timeline-widget': {'coeff': '+1', 'unit': 'MONTHS'},
-            'bucket-widget': {'coeff': '+1', 'unit': 'MONTHS'},
-            'bar-widget': {'coeff': '+1', 'unit': 'MONTHS'},
-            'facet-widget': {'coeff': '+1', 'unit': 'YEARS'},
-        }
-    }
 
     time_field = collection['timeFilter'].get('field')
 
@@ -894,3 +808,91 @@ class SolrApi(object):
       )
     response = self._root.post('%s/update' % collection_or_core_name, contenttype=content_type, params=params, data=data)
     return self._get_json(response)
+
+
+GAPS = {
+    '5MINUTES': {
+        'histogram-widget': {'coeff': '+3', 'unit': 'SECONDS'}, # ~100 slots
+        'timeline-widget': {'coeff': '+3', 'unit': 'SECONDS'}, # ~100 slots
+        'bucket-widget': {'coeff': '+3', 'unit': 'SECONDS'}, # ~100 slots
+        'bar-widget': {'coeff': '+3', 'unit': 'SECONDS'}, # ~100 slots
+        'facet-widget': {'coeff': '+1', 'unit': 'MINUTES'}, # ~10 slots
+    },
+    '30MINUTES': {
+        'histogram-widget': {'coeff': '+20', 'unit': 'SECONDS'},
+        'timeline-widget': {'coeff': '+20', 'unit': 'SECONDS'},
+        'bucket-widget': {'coeff': '+20', 'unit': 'SECONDS'},
+        'bar-widget': {'coeff': '+20', 'unit': 'SECONDS'},
+        'facet-widget': {'coeff': '+5', 'unit': 'MINUTES'},
+    },
+    '1HOURS': {
+        'histogram-widget': {'coeff': '+30', 'unit': 'SECONDS'},
+        'timeline-widget': {'coeff': '+30', 'unit': 'SECONDS'},
+        'bucket-widget': {'coeff': '+30', 'unit': 'SECONDS'},
+        'bar-widget': {'coeff': '+30', 'unit': 'SECONDS'},
+        'facet-widget': {'coeff': '+10', 'unit': 'MINUTES'},
+    },
+    '12HOURS': {
+        'histogram-widget': {'coeff': '+7', 'unit': 'MINUTES'},
+        'timeline-widget': {'coeff': '+7', 'unit': 'MINUTES'},
+        'bucket-widget': {'coeff': '+7', 'unit': 'MINUTES'},
+        'bar-widget': {'coeff': '+7', 'unit': 'MINUTES'},
+        'facet-widget': {'coeff': '+1', 'unit': 'HOURS'},
+    },
+    '1DAYS': {
+        'histogram-widget': {'coeff': '+15', 'unit': 'MINUTES'},
+        'timeline-widget': {'coeff': '+15', 'unit': 'MINUTES'},
+        'bucket-widget': {'coeff': '+15', 'unit': 'MINUTES'},
+        'bar-widget': {'coeff': '+15', 'unit': 'MINUTES'},
+        'facet-widget': {'coeff': '+3', 'unit': 'HOURS'},
+    },
+    '2DAYS': {
+        'histogram-widget': {'coeff': '+30', 'unit': 'MINUTES'},
+        'timeline-widget': {'coeff': '+30', 'unit': 'MINUTES'},
+        'bucket-widget': {'coeff': '+30', 'unit': 'MINUTES'},
+        'bar-widget': {'coeff': '+30', 'unit': 'MINUTES'},
+        'facet-widget': {'coeff': '+6', 'unit': 'HOURS'},
+    },
+    '7DAYS': {
+        'histogram-widget': {'coeff': '+3', 'unit': 'HOURS'},
+        'timeline-widget': {'coeff': '+3', 'unit': 'HOURS'},
+        'bucket-widget': {'coeff': '+3', 'unit': 'HOURS'},
+        'bar-widget': {'coeff': '+3', 'unit': 'HOURS'},
+        'facet-widget': {'coeff': '+1', 'unit': 'DAYS'},
+    },
+    '1MONTHS': {
+        'histogram-widget': {'coeff': '+12', 'unit': 'HOURS'},
+        'timeline-widget': {'coeff': '+12', 'unit': 'HOURS'},
+        'bucket-widget': {'coeff': '+12', 'unit': 'HOURS'},
+        'bar-widget': {'coeff': '+12', 'unit': 'HOURS'},
+        'facet-widget': {'coeff': '+5', 'unit': 'DAYS'},
+    },
+    '3MONTHS': {
+        'histogram-widget': {'coeff': '+1', 'unit': 'DAYS'},
+        'timeline-widget': {'coeff': '+1', 'unit': 'DAYS'},
+        'bucket-widget': {'coeff': '+1', 'unit': 'DAYS'},
+        'bar-widget': {'coeff': '+1', 'unit': 'DAYS'},
+        'facet-widget': {'coeff': '+30', 'unit': 'DAYS'},
+    },
+    '1YEARS': {
+        'histogram-widget': {'coeff': '+3', 'unit': 'DAYS'},
+        'timeline-widget': {'coeff': '+3', 'unit': 'DAYS'},
+        'bucket-widget': {'coeff': '+3', 'unit': 'DAYS'},
+        'bar-widget': {'coeff': '+3', 'unit': 'DAYS'},
+        'facet-widget': {'coeff': '+12', 'unit': 'MONTHS'},
+    },
+    '2YEARS': {
+        'histogram-widget': {'coeff': '+7', 'unit': 'DAYS'},
+        'timeline-widget': {'coeff': '+7', 'unit': 'DAYS'},
+        'bucket-widget': {'coeff': '+7', 'unit': 'DAYS'},
+        'bar-widget': {'coeff': '+7', 'unit': 'DAYS'},
+        'facet-widget': {'coeff': '+3', 'unit': 'MONTHS'},
+    },
+    '10YEARS': {
+        'histogram-widget': {'coeff': '+1', 'unit': 'MONTHS'},
+        'timeline-widget': {'coeff': '+1', 'unit': 'MONTHS'},
+        'bucket-widget': {'coeff': '+1', 'unit': 'MONTHS'},
+        'bar-widget': {'coeff': '+1', 'unit': 'MONTHS'},
+        'facet-widget': {'coeff': '+1', 'unit': 'YEARS'},
+    }
+}