# 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 flatten_data_and_assign_ids(data):

    flattened = []

    for key in data:
        data[key]['_id'] = encode_id(data[key]['pagePath'])
        flattened.append(data[key])

    return flattened


def query_ga(client, end, query, start):
    ga_query = parse_query(query)

    return client.query.get(
        ga_query['id'],
        start,
        end,
        [ga_query['metric']],
        ga_query['dimensions'],
        ga_query['filters'] if 'filters' in ga_query else None,
        None,
        None,
        ga_query.get('segment')
    )


def main(credentials, data_set_config, query, options, start_at, end_at):

    credentials = credentials

# 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 flatten_data_and_assign_ids(data):

    flattened = []

    for key in data:
        data[key]['_id'] = encode_id(data[key]['pagePath'])
        flattened.append(data[key])

    return flattened


def query_ga(client, end, query, start):
    ga_query = parse_query(query)

    return client.query.get(
        ga_query['id'],
        start,
        end,
        [ga_query['metric']],
        ga_query['dimensions'],
        ga_query['filters'] if 'filters' in ga_query else None,
        None,
        None,
        ga_query.get('segment')
    )


def main(credentials, data_set_config, query, options, start_at, end_at):

    credentials = credentials

    client = create_client(credentials)