# Path: elastalert/kibana_discover.py
# def generate_kibana_discover_url(rule, match):
#     ''' Creates a link for a kibana discover app. '''
# 
#     discover_app_url = rule.get('kibana_discover_app_url')
#     if not discover_app_url:
#         logging.warning(
#             'Missing kibana_discover_app_url for rule %s' % (
#                 rule.get('name', '<MISSING NAME>')
#             )
#         )
#         return None
# 
#     kibana_version = rule.get('kibana_discover_version')
#     if not kibana_version:
#         logging.warning(
#             'Missing kibana_discover_version for rule %s' % (
#                 rule.get('name', '<MISSING NAME>')
#             )
#         )
#         return None
# 
#     index = rule.get('kibana_discover_index_pattern_id')
#     if not index:
#         logging.warning(
#             'Missing kibana_discover_index_pattern_id for rule %s' % (
#                 rule.get('name', '<MISSING NAME>')
#             )
#         )
#         return None
# 
#     columns = rule.get('kibana_discover_columns', ['_source'])
#     filters = rule.get('filter', [])
# 
#     if 'query_key' in rule:
#         query_keys = rule.get('compound_query_key', [rule['query_key']])
#     else:
#         query_keys = []
# 
#     timestamp = lookup_es_key(match, rule['timestamp_field'])
#     timeframe = rule.get('timeframe', kibana_default_timedelta)
#     from_timedelta = rule.get('kibana_discover_from_timedelta', timeframe)
#     from_time = ts_add(timestamp, -from_timedelta)
#     to_timedelta = rule.get('kibana_discover_to_timedelta', timeframe)
#     to_time = ts_add(timestamp, to_timedelta)
# 
#     if kibana_version in kibana5_kibana6_versions:
#         globalState = kibana6_disover_global_state(from_time, to_time)
#         appState = kibana_discover_app_state(index, columns, filters, query_keys, match)
# 
#     elif kibana_version in kibana7_versions:
#         globalState = kibana7_disover_global_state(from_time, to_time)
#         appState = kibana_discover_app_state(index, columns, filters, query_keys, match)
# 
#     else:
#         logging.warning(
#             'Unknown kibana discover application version %s for rule %s' % (
#                 kibana_version,
#                 rule.get('name', '<MISSING NAME>')
#             )
#         )
#         return None
# 
#     return "%s?_g=%s&_a=%s" % (
#         os.path.expandvars(discover_app_url),
#         urllib.parse.quote(globalState),
#         urllib.parse.quote(appState)
#     )


from datetime import timedelta
from elastalert.kibana_discover import generate_kibana_discover_url
import pytest
            'kibana_discover_version': '7.3',
            'kibana_discover_index_pattern_id': 'd6cabfb6-aaef-44ea-89c5-600e9a76991a',
            'timeframe': timedelta(minutes=20),
            'timestamp_field': 'timestamp'
        },
        match={
            'timestamp': '2019-09-01T04:30:00Z'
        }
    )
    expectedUrl = (
        'http://kibana:5601/#/discover'
        + '?_g=%28'  # global start
        + 'filters%3A%21%28%29%2C'
        + 'refreshInterval%3A%28pause%3A%21t%2Cvalue%3A0%29%2C'
        + 'time%3A%28'  # time start
        + 'from%3A%272019-09-01T04%3A10%3A00Z%27%2C'
        + 'to%3A%272019-09-01T04%3A50%3A00Z%27'
        + '%29'  # time end
        + '%29'  # global end
        + '&_a=%28'  # app start
        + 'columns%3A%21%28_source%29%2C'
        + 'filters%3A%21%28%29%2C'
        + 'index%3Ad6cabfb6-aaef-44ea-89c5-600e9a76991a%2C'
        + 'interval%3Aauto'
        + '%29'  # app end
    )
    assert url == expectedUrl


def test_generate_kibana_discover_url_with_custom_columns():

# Path: elastalert/kibana_discover.py
# def generate_kibana_discover_url(rule, match):
#     ''' Creates a link for a kibana discover app. '''
# 
#     discover_app_url = rule.get('kibana_discover_app_url')
#     if not discover_app_url:
#         logging.warning(
#             'Missing kibana_discover_app_url for rule %s' % (
#                 rule.get('name', '<MISSING NAME>')
#             )
#         )
#         return None
# 
#     kibana_version = rule.get('kibana_discover_version')
#     if not kibana_version:
#         logging.warning(
#             'Missing kibana_discover_version for rule %s' % (
#                 rule.get('name', '<MISSING NAME>')
#             )
#         )
#         return None
# 
#     index = rule.get('kibana_discover_index_pattern_id')
#     if not index:
#         logging.warning(
#             'Missing kibana_discover_index_pattern_id for rule %s' % (
#                 rule.get('name', '<MISSING NAME>')
#             )
#         )
#         return None
# 
#     columns = rule.get('kibana_discover_columns', ['_source'])
#     filters = rule.get('filter', [])
# 
#     if 'query_key' in rule:
#         query_keys = rule.get('compound_query_key', [rule['query_key']])
#     else:
#         query_keys = []
# 
#     timestamp = lookup_es_key(match, rule['timestamp_field'])
#     timeframe = rule.get('timeframe', kibana_default_timedelta)
#     from_timedelta = rule.get('kibana_discover_from_timedelta', timeframe)
#     from_time = ts_add(timestamp, -from_timedelta)
#     to_timedelta = rule.get('kibana_discover_to_timedelta', timeframe)
#     to_time = ts_add(timestamp, to_timedelta)
# 
#     if kibana_version in kibana5_kibana6_versions:
#         globalState = kibana6_disover_global_state(from_time, to_time)
#         appState = kibana_discover_app_state(index, columns, filters, query_keys, match)
# 
#     elif kibana_version in kibana7_versions:
#         globalState = kibana7_disover_global_state(from_time, to_time)
#         appState = kibana_discover_app_state(index, columns, filters, query_keys, match)
# 
#     else:
#         logging.warning(
#             'Unknown kibana discover application version %s for rule %s' % (
#                 kibana_version,
#                 rule.get('name', '<MISSING NAME>')
#             )
#         )
#         return None
# 
#     return "%s?_g=%s&_a=%s" % (
#         os.path.expandvars(discover_app_url),
#         urllib.parse.quote(globalState),
#         urllib.parse.quote(appState)
#     )


# Path: tests/kibana_discover_test.py
from datetime import timedelta
from elastalert.kibana_discover import generate_kibana_discover_url
import pytest

            'kibana_discover_version': '7.3',
            'kibana_discover_index_pattern_id': 'd6cabfb6-aaef-44ea-89c5-600e9a76991a',
            'timeframe': timedelta(minutes=20),
            'timestamp_field': 'timestamp'
        },
        match={
            'timestamp': '2019-09-01T04:30:00Z'
        }
    )
    expectedUrl = (
        'http://kibana:5601/#/discover'
        + '?_g=%28'  # global start
        + 'filters%3A%21%28%29%2C'
        + 'refreshInterval%3A%28pause%3A%21t%2Cvalue%3A0%29%2C'
        + 'time%3A%28'  # time start
        + 'from%3A%272019-09-01T04%3A10%3A00Z%27%2C'
        + 'to%3A%272019-09-01T04%3A50%3A00Z%27'
        + '%29'  # time end
        + '%29'  # global end
        + '&_a=%28'  # app start
        + 'columns%3A%21%28_source%29%2C'
        + 'filters%3A%21%28%29%2C'
        + 'index%3Ad6cabfb6-aaef-44ea-89c5-600e9a76991a%2C'
        + 'interval%3Aauto'
        + '%29'  # app end
    )
    assert url == expectedUrl


def test_generate_kibana_discover_url_with_custom_columns():

    url = generate_kibana_discover_url(