Browse Source

HUE-3228 [dashboard] Proper last N time filtering with isBigIntDate

Romain Rigaux 8 năm trước cách đây
mục cha
commit
48d0d1a

+ 17 - 15
apps/impala/src/impala/dashboard_api.py

@@ -243,22 +243,19 @@ class SQLApi():
 
       stats = list(result['data'])
       min_value, max_value = stats[0]
+      is_big_int_date = isinstance(min_value, numbers.Number)
 
-      if not isinstance(min_value, numbers.Number):
+      if not is_big_int_date:
         min_value = min_value.replace(' ', 'T') + 'Z'
         max_value = max_value.replace(' ', 'T') + 'Z'
       else:
-        print '============================================'
-        print dataset
-#         print self._get_field(dataset, fields[0])
-        min_value = datetime.fromtimestamp(1486142551).strftime('%Y-%m-%dT%H:%M:%SZ')
-        max_value = datetime.fromtimestamp(1486152551).strftime('%Y-%m-%dT%H:%M:%SZ')
-        # is field a timeline?
+        min_value = datetime.fromtimestamp(min_value).strftime('%Y-%m-%dT%H:%M:%SZ')
+        max_value = datetime.fromtimestamp(max_value).strftime('%Y-%m-%dT%H:%M:%SZ')
 
       return {
         'stats': {
           'stats_fields': {
-            fields[0]: {'min': min_value, 'max': max_value}
+            fields[0]: {'min': min_value, 'max': max_value, 'is_big_int_date': is_big_int_date}
           }
         }
       }
@@ -370,15 +367,18 @@ class SQLApi():
       field_name_to = '`%(field)s_range_to`' % facet
       order_by = '`%(field)s_range` ASC' % facet
       if facet['properties']['isDate']:
-        if False:
-          field = '`%(field)s`' % facet
-        else:
+        if facet['properties']['isBigIntDate']:
           field = 'cast(`%(field)s` AS timestamp)' % facet
+        else:
+          field = '`%(field)s`' % facet
+
         gap = facet['properties']['gap'] if 'gap' in facet['properties'] else facet['properties']['initial_gap']
+
         slot = slot_interval = re.sub('^\+\d+', '', gap).rstrip('S')
         if slot == 'MINUTE':
           slot = 'MI'
           slot_interval = 'MINUTE'
+
         select = "trunc(%(field)s, '%(slot)s') AS %(field_name)s, trunc(%(field)s, '%(slot)s') + interval 1 %(slot_interval)s AS %(field_name_to)s" % {
             'field': field,
             'slot': slot,
@@ -429,11 +429,13 @@ class SQLApi():
 
       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 +/- Proper Tz of user
+
+        if any([c['properties'].get('isBigIntDate') for c in collection['facets'] if c['field'] == time_field]):
+          props['from'] = 'cast(%(from)s AS bigint)' % props
+          props['to'] = 'cast(%(to)s AS bigint)' % props
 
-#         props['from'] = "now() - interval %(coeff)s %(unit)s" % {'coeff': coeff, 'unit': unit.strip('S')}
-#         props['to'] = 'now()' # TODO +/- Tz of user
-        props['from'] = "unix_timestamp() - %(coeff)s * 60" % {'coeff': coeff, 'unit': unit.strip('S')}
-        props['to'] = 'unix_timestamp()' # TODO +/- Tz of user
       elif collection['timeFilter']['type'] == 'fixed':
         props['from'] = collection['timeFilter'].get('from', 'now() - interval 7 DAY')
         props['to'] = collection['timeFilter'].get('to', 'now()')

+ 1 - 0
apps/search/src/search/api.py

@@ -365,6 +365,7 @@ def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_t
     'limit': 10,
     'mincount': 0,
     'isDate': False,
+    'isBigIntDate': False,
     'aggregate': {'function': 'unique', 'ops': [], 'percentiles': [{'value': 50}]}
   }
 

+ 4 - 2
apps/search/src/search/facet_builder.py

@@ -52,8 +52,9 @@ def _compute_range_facet(widget_type, stat_facet, properties, start=None, end=No
       SLOTS = 10
     else:
       SLOTS = 100
-
-    is_date = widget_type == 'timeline-widget'
+      
+    is_big_int_date = stat_facet.get('is_big_int_date')
+    is_date = is_big_int_date
 
     if isinstance(stat_facet['min'], numbers.Number):
       stats_min = int(stat_facet['min']) # Cast floats to int currently
@@ -153,6 +154,7 @@ def _compute_range_facet(widget_type, stat_facet, properties, start=None, end=No
       'gap': gap,
       'canRange': True,
       'isDate': is_date,
+      'isBigIntDate': is_big_int_date,
     })
 
     if widget_type == 'histogram-widget':

+ 1 - 1
apps/search/src/search/static/search/js/search.ko.js

@@ -1589,7 +1589,7 @@ var SearchViewModel = function (collection_json, query_json, initial_json) {
 
 
   self.availableDateFields = ko.computed(function() {
-    return $.grep(self.collection.availableFacetFields(), function(field) { return DATE_TYPES.indexOf(field.type()) != -1; });
+    return $.grep(self.collection.availableFacetFields(), function(field) { return DATE_TYPES.indexOf(field.type()) != -1 && field.name() != '_version_'; });
   });
   self.availableNumberFields = ko.computed(function() {
     return $.grep(self.collection.availableFacetFields(), function(field) { return NUMBER_TYPES.indexOf(field.type()) != -1; });

+ 3 - 1
desktop/core/src/desktop/templates/common_header.mako

@@ -14,10 +14,12 @@
 ## See the License for the specific language governing permissions and
 ## limitations under the License.
 <%!
+from django.utils.translation import ugettext as _
+
 from desktop import conf
 from desktop.conf import USE_NEW_EDITOR
 from desktop.lib.i18n import smart_unicode
-from django.utils.translation import ugettext as _
+
 from metadata.conf import has_optimizer, OPTIMIZER
 
 home_url = url('desktop.views.home')