Exemplo n.º 1
0
def test_base_aggregation_payloads():
    with mock.patch.object(BaseAggregationRule, 'check_matches', return_value=None) as mock_check_matches:
        rules = {'bucket_interval': {'seconds': 10},
                 'bucket_interval_timedelta': datetime.timedelta(seconds=10),
                 'buffer_time': datetime.timedelta(minutes=5),
                 'timestamp_field': '@timestamp'}

        timestamp = datetime.datetime.now()
        interval_agg = create_bucket_aggregation('interval_aggs', [{'key_as_string': '2014-01-01T00:00:00Z'}])
        rule = BaseAggregationRule(rules)

        # Payload not wrapped
        rule.add_aggregation_data({timestamp: {}})
        mock_check_matches.assert_called_once_with(timestamp, None, {})
        mock_check_matches.reset_mock()

        # Payload wrapped by date_histogram
        interval_agg_data = {timestamp: interval_agg}
        rule.add_aggregation_data(interval_agg_data)
        mock_check_matches.assert_called_once_with(ts_to_dt('2014-01-01T00:00:00Z'), None, {'key_as_string': '2014-01-01T00:00:00Z'})
        mock_check_matches.reset_mock()

        # Payload wrapped by terms
        bucket_agg_data = {timestamp: create_bucket_aggregation('bucket_aggs', [{'key': 'qk'}])}
        rule.add_aggregation_data(bucket_agg_data)
        mock_check_matches.assert_called_once_with(timestamp, 'qk', {'key': 'qk'})
        mock_check_matches.reset_mock()

        # Payload wrapped by terms and date_histogram
        bucket_interval_agg_data = {
            timestamp: create_bucket_aggregation('bucket_aggs', [{'key': 'qk', 'interval_aggs': interval_agg['interval_aggs']}])
        }
        rule.add_aggregation_data(bucket_interval_agg_data)
        mock_check_matches.assert_called_once_with(ts_to_dt('2014-01-01T00:00:00Z'), 'qk', {'key_as_string': '2014-01-01T00:00:00Z'})
        mock_check_matches.reset_mock()
Exemplo n.º 2
0
def test_base_aggregation_payloads():
    with mock.patch.object(BaseAggregationRule, 'check_matches', return_value=None) as mock_check_matches:
        rules = {'bucket_interval': {'seconds': 10},
                 'bucket_interval_timedelta': datetime.timedelta(seconds=10),
                 'buffer_time': datetime.timedelta(minutes=5),
                 'timestamp_field': '@timestamp'}

        timestamp = datetime.datetime.now()
        interval_agg = create_bucket_aggregation('interval_aggs', [{'key_as_string': '2014-01-01T00:00:00Z'}])
        rule = BaseAggregationRule(rules)

        # Payload not wrapped
        rule.add_aggregation_data({timestamp: {}})
        mock_check_matches.assert_called_once_with(timestamp, None, {})
        mock_check_matches.reset_mock()

        # Payload wrapped by date_histogram
        interval_agg_data = {timestamp: interval_agg}
        rule.add_aggregation_data(interval_agg_data)
        mock_check_matches.assert_called_once_with(ts_to_dt('2014-01-01T00:00:00Z'), None, {'key_as_string': '2014-01-01T00:00:00Z'})
        mock_check_matches.reset_mock()

        # Payload wrapped by terms
        bucket_agg_data = {timestamp: create_bucket_aggregation('bucket_aggs', [{'key': 'qk'}])}
        rule.add_aggregation_data(bucket_agg_data)
        mock_check_matches.assert_called_once_with(timestamp, 'qk', {'key': 'qk'})
        mock_check_matches.reset_mock()

        # Payload wrapped by terms and date_histogram
        bucket_interval_agg_data = {
            timestamp: create_bucket_aggregation('bucket_aggs', [{'key': 'qk', 'interval_aggs': interval_agg['interval_aggs']}])
        }
        rule.add_aggregation_data(bucket_interval_agg_data)
        mock_check_matches.assert_called_once_with(ts_to_dt('2014-01-01T00:00:00Z'), 'qk', {'key_as_string': '2014-01-01T00:00:00Z'})
        mock_check_matches.reset_mock()
Exemplo n.º 3
0
def test_base_aggregation_constructor():
    rules = {
        'bucket_interval_timedelta': datetime.timedelta(seconds=10),
        'buffer_time': datetime.timedelta(minutes=1),
        'timestamp_field': '@timestamp'
    }

    # Test time period constructor logic
    rules['bucket_interval'] = {'seconds': 10}
    rule = BaseAggregationRule(rules)
    assert rule.rules['bucket_interval_period'] == '10s'

    rules['bucket_interval'] = {'minutes': 5}
    rule = BaseAggregationRule(rules)
    assert rule.rules['bucket_interval_period'] == '5m'

    rules['bucket_interval'] = {'hours': 4}
    rule = BaseAggregationRule(rules)
    assert rule.rules['bucket_interval_period'] == '4h'

    rules['bucket_interval'] = {'days': 2}
    rule = BaseAggregationRule(rules)
    assert rule.rules['bucket_interval_period'] == '2d'

    rules['bucket_interval'] = {'weeks': 1}
    rule = BaseAggregationRule(rules)
    assert rule.rules['bucket_interval_period'] == '1w'

    # buffer_time evenly divisible by bucket_interval
    with pytest.raises(EAException):
        rules['bucket_interval_timedelta'] = datetime.timedelta(seconds=13)
        rule = BaseAggregationRule(rules)

    # run_every evenly divisible by bucket_interval
    rules['use_run_every_query_size'] = True
    rules['run_every'] = datetime.timedelta(minutes=2)
    rules['bucket_interval_timedelta'] = datetime.timedelta(seconds=10)
    rule = BaseAggregationRule(rules)

    with pytest.raises(EAException):
        rules['bucket_interval_timedelta'] = datetime.timedelta(seconds=13)
        rule = BaseAggregationRule(rules)