def query_long_term_data(config, filter_server_failures=True):
    now = datetime.utcnow()
    then = (now - timedelta(days=(config.get('long term', 90) + 1)))
    es = Elasticsearch(**config['elasticsearch'])
    battles_query['query']['bool']['must'][(- 1)]['range']['date']['gte'] = then.strftime('%Y-%m-%d')
    battles_query['query']['bool']['must'][(- 1)]['range']['date']['lte'] = now.strftime('%Y-%m-%d')
    players_query['query']['bool']['must'][(- 1)]['range']['date']['gte'] = then.strftime('%Y-%m-%d')
    players_query['query']['bool']['must'][(- 1)]['range']['date']['lte'] = now.strftime('%Y-%m-%d')
    players = es.search(index=config['battle index'], body=players_query)
    battles = es.search(index=config['battle index'], body=battles_query)
    players_buckets = {'xbox': OrderedDict(), 'ps': OrderedDict(), 'all': OrderedDict()}
    battles_buckets = {'xbox': OrderedDict(), 'ps': OrderedDict(), 'all': OrderedDict()}
    average_battles_per_day_buckets = {'xbox': OrderedDict(), 'ps': OrderedDict(), 'all': OrderedDict()}
    for bucket in players['aggregations']['2']['buckets']:
        key = bucket['key_as_string'].split('T')[0]
        players_buckets['xbox'][key] = 0
        players_buckets['ps'][key] = 0
        players_buckets['all'][key] = 0
        if (not bucket['3']['buckets']):
            continue
        for subbucket in bucket['3']['buckets']:
            players_buckets[subbucket['key']][key] = subbucket['doc_count']
        players_buckets['all'][key] = (players_buckets['xbox'][key] + players_buckets['ps'][key])
    for bucket in battles['aggregations']['2']['buckets']:
        key = bucket['key_as_string'].split('T')[0]
        battles_buckets['xbox'][key] = 0
        battles_buckets['ps'][key] = 0
        battles_buckets['all'][key] = 0
        if (not bucket['3']['buckets']):
            continue
        for subbucket in bucket['3']['buckets']:
            battles_buckets[subbucket['key']][key] = subbucket['1']['value']
        battles_buckets['all'][key] = (battles_buckets['xbox'][key] + battles_buckets['ps'][key])
    if filter_server_failures:
        skip_next = False
        for (key, value) in players_buckets['ps'].items():
            if (value < 20000):
                players_buckets['xbox'][key] = None
                players_buckets['ps'][key] = None
                players_buckets['all'][key] = None
                battles_buckets['xbox'][key] = None
                battles_buckets['ps'][key] = None
                battles_buckets['all'][key] = None
                skip_next = True
            elif skip_next:
                players_buckets['xbox'][key] = None
                players_buckets['ps'][key] = None
                players_buckets['all'][key] = None
                battles_buckets['xbox'][key] = None
                battles_buckets['ps'][key] = None
                battles_buckets['all'][key] = None
                skip_next = False
    for key in players_buckets['all'].keys():
        if (players_buckets['xbox'][key] is None):
            average_battles_per_day_buckets['all'][key] = None
            average_battles_per_day_buckets['xbox'][key] = None
            average_battles_per_day_buckets['ps'][key] = None
        else:
            average_battles_per_day_buckets['xbox'][key] = (battles_buckets['xbox'][key] / players_buckets['xbox'][key])
            average_battles_per_day_buckets['ps'][key] = (battles_buckets['ps'][key] / players_buckets['ps'][key])
            average_battles_per_day_buckets['all'][key] = ((battles_buckets['xbox'][key] + battles_buckets['ps'][key]) / (players_buckets['xbox'][key] + players_buckets['ps'][key]))
    delkey = list(players_buckets['all'].keys())[0]
    del players_buckets['all'][key]
    del players_buckets['xbox'][key]
    del players_buckets['ps'][key]
    del battles_buckets['all'][key]
    del battles_buckets['xbox'][key]
    del battles_buckets['ps'][key]
    del average_battles_per_day_buckets['xbox'][key]
    del average_battles_per_day_buckets['ps'][key]
    del average_battles_per_day_buckets['all'][key]
    return (players_buckets, battles_buckets, average_battles_per_day_buckets)
def query_long_term_data(config, filter_server_failures=True):
    now = datetime.utcnow()
    then = (now - timedelta(days=(config.get('long term', 90) + 1)))
    es = Elasticsearch(**config['elasticsearch'])
    battles_query['query']['bool']['must'][(- 1)]['range']['date']['gte'] = then.strftime('%Y-%m-%d')
    battles_query['query']['bool']['must'][(- 1)]['range']['date']['lte'] = now.strftime('%Y-%m-%d')
    players_query['query']['bool']['must'][(- 1)]['range']['date']['gte'] = then.strftime('%Y-%m-%d')
    players_query['query']['bool']['must'][(- 1)]['range']['date']['lte'] = now.strftime('%Y-%m-%d')
    players = es.search(index=config['battle index'], body=players_query)
    battles = es.search(index=config['battle index'], body=battles_query)
    players_buckets = {'xbox': OrderedDict(), 'ps': OrderedDict(), 'all': OrderedDict()}
    battles_buckets = {'xbox': OrderedDict(), 'ps': OrderedDict(), 'all': OrderedDict()}
    average_battles_per_day_buckets = {'xbox': OrderedDict(), 'ps': OrderedDict(), 'all': OrderedDict()}
    for bucket in players['aggregations']['2']['buckets']:
        key = bucket['key_as_string'].split('T')[0]
        players_buckets['xbox'][key] = 0
        players_buckets['ps'][key] = 0
        players_buckets['all'][key] = 0
        if (not bucket['3']['buckets']):
            continue
        for subbucket in bucket['3']['buckets']:
            players_buckets[subbucket['key']][key] = subbucket['doc_count']
        players_buckets['all'][key] = (players_buckets['xbox'][key] + players_buckets['ps'][key])
    for bucket in battles['aggregations']['2']['buckets']:
        key = bucket['key_as_string'].split('T')[0]
        battles_buckets['xbox'][key] = 0
        battles_buckets['ps'][key] = 0
        battles_buckets['all'][key] = 0
        if (not bucket['3']['buckets']):
            continue
        for subbucket in bucket['3']['buckets']:
            battles_buckets[subbucket['key']][key] = subbucket['1']['value']
        battles_buckets['all'][key] = (battles_buckets['xbox'][key] + battles_buckets['ps'][key])
    if filter_server_failures:
        skip_next = False
        for (key, value) in players_buckets['ps'].items():
            if (value < 20000):
                players_buckets['xbox'][key] = None
                players_buckets['ps'][key] = None
                players_buckets['all'][key] = None
                battles_buckets['xbox'][key] = None
                battles_buckets['ps'][key] = None
                battles_buckets['all'][key] = None
                skip_next = True
            elif skip_next:
                players_buckets['xbox'][key] = None
                players_buckets['ps'][key] = None
                players_buckets['all'][key] = None
                battles_buckets['xbox'][key] = None
                battles_buckets['ps'][key] = None
                battles_buckets['all'][key] = None
                skip_next = False
    for key in players_buckets['all'].keys():
        if (players_buckets['xbox'][key] is None):
            average_battles_per_day_buckets['all'][key] = None
            average_battles_per_day_buckets['xbox'][key] = None
            average_battles_per_day_buckets['ps'][key] = None
        else:
            average_battles_per_day_buckets['xbox'][key] = (battles_buckets['xbox'][key] / players_buckets['xbox'][key])
            average_battles_per_day_buckets['ps'][key] = (battles_buckets['ps'][key] / players_buckets['ps'][key])
            average_battles_per_day_buckets['all'][key] = ((battles_buckets['xbox'][key] + battles_buckets['ps'][key]) / (players_buckets['xbox'][key] + players_buckets['ps'][key]))
    delkey = list(players_buckets['all'].keys())[0]
    del players_buckets['all'][key]
    del players_buckets['xbox'][key]
    del players_buckets['ps'][key]
    del battles_buckets['all'][key]
    del battles_buckets['xbox'][key]
    del battles_buckets['ps'][key]
    del average_battles_per_day_buckets['xbox'][key]
    del average_battles_per_day_buckets['ps'][key]
    del average_battles_per_day_buckets['all'][key]
    return (players_buckets, battles_buckets, average_battles_per_day_buckets)