# Path: performanceplatform/collector/ga/lib/helper.py
# def create_client(credentials):
#     if "CLIENT_SECRETS" in credentials and "STORAGE_PATH" in credentials:
#         return from_secrets_file(
#             credentials['CLIENT_SECRETS'],
#             storage_path=credentials['STORAGE_PATH'],
#             http_client=HttpWithBackoff(),
#             ga_hook=track_ga_api_usage,
#         )
#     elif "ACCOUNT_NAME" in credentials:
#         return from_private_key(
#             credentials['ACCOUNT_NAME'],
#             private_key_path=credentials['PRIVATE_KEY'],
#             storage_path=credentials['STORAGE_PATH'],
#             http_client=HttpWithBackoff(),
#             ga_hook=track_ga_api_usage,
#         )
#     else:
#         return from_credentials_db(
#             credentials['CLIENT_SECRETS']['installed'],
#             credentials['OAUTH2_CREDENTIALS'],
#             http_client=HttpWithBackoff(),
#             ga_hook=track_ga_api_usage,
#         )
# 
# Path: performanceplatform/utils/datetimeutil.py
# def to_datetime(a_date):
#     if type(a_date) == datetime:
#         return a_date.replace(tzinfo=pytz.UTC)
#     return datetime.combine(a_date, time(0)).replace(tzinfo=pytz.UTC)


import base64
from datetime import date, timedelta
from performanceplatform.collector.ga.lib.helper import create_client
from performanceplatform.client import DataSet
from performanceplatform.utils.datetimeutil \
    import to_datetime


def get_trends(data):

    for key in data:
        numerator = data[key]['week2'] - data[key]['week1']
        denominator = data[key]['week1']

        percent_change = ((float(numerator) / float(denominator)) * 100)
        data[key]['percent_change'] = percent_change

    return data


def sum_data(data, metric, collapse_key, dates, floor):

    collapsed = {}

    for row in data:

        dimensions = row['dimensions']
        k = dimensions[collapse_key]

        if k not in collapsed:
            d = {}
            for dim in dimensions:
                if dim not in ga_date_keys:
                    d[dim] = dimensions[dim]
            d['week1'] = 0
            d['week2'] = 0

# Path: performanceplatform/collector/ga/lib/helper.py
# def create_client(credentials):
#     if "CLIENT_SECRETS" in credentials and "STORAGE_PATH" in credentials:
#         return from_secrets_file(
#             credentials['CLIENT_SECRETS'],
#             storage_path=credentials['STORAGE_PATH'],
#             http_client=HttpWithBackoff(),
#             ga_hook=track_ga_api_usage,
#         )
#     elif "ACCOUNT_NAME" in credentials:
#         return from_private_key(
#             credentials['ACCOUNT_NAME'],
#             private_key_path=credentials['PRIVATE_KEY'],
#             storage_path=credentials['STORAGE_PATH'],
#             http_client=HttpWithBackoff(),
#             ga_hook=track_ga_api_usage,
#         )
#     else:
#         return from_credentials_db(
#             credentials['CLIENT_SECRETS']['installed'],
#             credentials['OAUTH2_CREDENTIALS'],
#             http_client=HttpWithBackoff(),
#             ga_hook=track_ga_api_usage,
#         )
# 
# Path: performanceplatform/utils/datetimeutil.py
# def to_datetime(a_date):
#     if type(a_date) == datetime:
#         return a_date.replace(tzinfo=pytz.UTC)
#     return datetime.combine(a_date, time(0)).replace(tzinfo=pytz.UTC)


# Path: performanceplatform/collector/ga/trending.py
import base64
from datetime import date, timedelta
from performanceplatform.collector.ga.lib.helper import create_client
from performanceplatform.client import DataSet
from performanceplatform.utils.datetimeutil \
    import to_datetime



def get_trends(data):

    for key in data:
        numerator = data[key]['week2'] - data[key]['week1']
        denominator = data[key]['week1']

        percent_change = ((float(numerator) / float(denominator)) * 100)
        data[key]['percent_change'] = percent_change

    return data


def sum_data(data, metric, collapse_key, dates, floor):

    collapsed = {}

    for row in data:

        dimensions = row['dimensions']
        k = dimensions[collapse_key]

        if k not in collapsed:
            d = {}
            for dim in dimensions:
                if dim not in ga_date_keys:
                    d[dim] = dimensions[dim]
            d['week1'] = 0
            d['week2'] = 0

            d['_timestamp'] = to_datetime(dates[0])