human
Complete the below
def _get_trending(app_id):

    ''''Calculate trending for app for all regions and per region.
    a = installs from 8 days ago to 1 day ago
    b = installs from 29 days ago to 9 days ago, averaged per week
    trending = (a - b) / b if a > 100 and b > 1 else 0
    Returns value in the format of::
    {\'all\': <global trending score>,
    <region_slug>: <regional trending score>,'
    '''
assistant
#Here is Python code to calculate trending for app for all regions and per region.
    PRIOR_WEEK_INSTALL_THRESHOLD = 100.0
    client = get_monolith_client()
    week1 = {'filter': {'range': {'date': {'gte': days_ago(8).date().isoformat(), 'lte': days_ago(1).date().isoformat()}}}, 'aggs': {'total_installs': {'sum': {'field': 'app_installs'}}}}
    week3 = {'filter': {'range': {'date': {'gte': days_ago(29).date().isoformat(), 'lte': days_ago(9).date().isoformat()}}}, 'aggs': {'total_installs': {'sum': {'field': 'app_installs'}}}}
    query = {'query': {'filtered': {'query': {'match_all': {}}, 'filter': {'term': {'app-id': app_id}}}}, 'aggregations': {'week1': week1, 'week3': week3, 'region': {'terms': {'field': 'region', 'size': len(mkt.regions.ALL_REGIONS)}, 'aggregations': {'week1': week1, 'week3': week3}}}, 'size': 0}
    try:
        res = client.raw(query)
    except ValueError as e:
        task_log.error('Error    response    from    Monolith:    {0}'.format(e))
        return {}
    if ('aggregations' not in res):
        task_log.error('No    installs    for    app    {}'.format(app_id))
        return {}
    def _score(week1, week3):
        if (week1 < PRIOR_WEEK_INSTALL_THRESHOLD):
            return 0.0
        score = 0.0
        if (week3 > 1.0):
            score = ((week1 - week3) / week3)
        if (score < 0.0):
            score = 0.0
        return score
    week1 = res['aggregations']['week1']['total_installs']['value']
    week3 = (res['aggregations']['week3']['total_installs']['value'] / 3.0)
    if (week1 < PRIOR_WEEK_INSTALL_THRESHOLD):
        return {}
    results = {'all': _score(week1, week3)}
    if ('region' in res['aggregations']):
        for regional_res in res['aggregations']['region']['buckets']:
            region_slug = regional_res['key']
            week1 = regional_res['week1']['total_installs']['value']
            week3 = (regional_res['week3']['total_installs']['value'] / 3.0)
            results[region_slug] = _score(week1, week3)
    return results