示例#1
0
    def _get_histogram_aggregation(self, interval, args, aggregations):
        params = args.get('params') or {}
        lt, gte, time_zone = self._es_get_date_filters(params)

        # If the interval is weekly, then offset the buckets by the
        # starting day of the week, based on app.config['START_OF_WEEK']
        offset = 0 if interval != 'week' else get_weekstart_offset_hr()

        aggs = {
            'dates': {
                'date_histogram': {
                    'field': self.histogram_source_field,
                    'interval': interval,
                    'time_zone': time_zone,
                    'min_doc_count': 0,
                    'offset': '{}h'.format(offset),
                    'extended_bounds': {
                        'min': gte,
                        'max': lt
                    }
                },
                'aggs': aggregations
            }
        }

        if get_elastic_version().startswith('1.'):
            aggs['dates']['date_histogram'][
                'pre_zone_adjust_large_interval'] = True

        return aggs
示例#2
0
    def _get_histogram_aggregation(self, interval, args, aggregations):
        params = args.get('params') or {}
        lt, gte, time_zone = self._es_get_date_filters(params)

        # If the interval is weekly, then offset the buckets by the
        # starting day of the week, based on app.config['START_OF_WEEK']
        offset = 0 if interval != 'week' else get_weekstart_offset_hr()

        if lt.startswith('now'):
            extended_bounds = {
                'max': relative_to_absolute_datetime(lt, '%Y-%m-%dT%H:%M:%S'),
                'min': relative_to_absolute_datetime(gte, '%Y-%m-%dT%H:%M:%S')
            }
        else:
            extended_bounds = {
                'min': gte[:-5],  # remove timezone part
                'max': lt[:-5]  # remove timezone part
            }

        # dates.extended_bounds.min & dates.extended_bounds.max should use dates.format

        aggs = {
            'dates': {
                'date_histogram': {
                    'field': self.histogram_source_field,
                    'interval': interval,
                    'time_zone': time_zone,
                    'min_doc_count': 0,
                    'offset': '{}h'.format(offset),
                    'extended_bounds': extended_bounds,
                    'format': 'yyyy-MM-dd\'T\'HH:mm:ss'
                },
                'aggs': aggregations
            }
        }

        if get_elastic_version().startswith('1.'):
            aggs['dates']['date_histogram'][
                'pre_zone_adjust_large_interval'] = True

        return aggs
示例#3
0
    def test_weekstart_offset_hr(self):
        self.app.config['START_OF_WEEK'] = 0
        self.assertEqual(get_weekstart_offset_hr(), -24)

        self.app.config['START_OF_WEEK'] = 1
        self.assertEqual(get_weekstart_offset_hr(), 0)

        self.app.config['START_OF_WEEK'] = 2
        self.assertEqual(get_weekstart_offset_hr(), 24)

        self.app.config['START_OF_WEEK'] = 3
        self.assertEqual(get_weekstart_offset_hr(), 48)

        self.app.config['START_OF_WEEK'] = 4
        self.assertEqual(get_weekstart_offset_hr(), 72)

        self.app.config['START_OF_WEEK'] = 5
        self.assertEqual(get_weekstart_offset_hr(), 96)

        self.app.config['START_OF_WEEK'] = 6
        self.assertEqual(get_weekstart_offset_hr(), 120)