def insertDateRangeFilter(query):
    """
    Add a date range filter on the '@timestamp' field either as 
    a 'filter' or 'post_filter'. If both 'filter' and 'post_filter'
    are already defined then an RuntimeError is raised as the 
    date filter cannot be inserted into the query.

    The date range filter will look like either

        "filter" : {"range" : { "@timestamp" : { "gte" : "start-date",
            "lt" : "end-date"} } }
    or

        "post_filter" : {"range" : { "@timestamp" : { "gte" : "start-date",
            "lt" : "end-date"} } }

    where 'start-date' and 'end-date' literals will be replaced by 
    the actual timestamps in the query. 
    """

    dates = {'gte' : 'start-date', 'lt' : 'end-date'}
    timestamp = {'@timestamp' : dates}
    range_ = {'range' : timestamp}
    
    if not 'filter' in query:
        query['filter'] = range_
    elif not 'post_filter' in query:
        query['post_filter'] = range_
    else:
        raise RuntimeError("Cannot add a 'filter' or 'post_filter' \
date range to the query")

    return query