def get_project_list_data():
    project_list = dict()
    status = 200
    project_names = []

    try:
        log.info('In Project List Data ----')

        projects = get_distinct_projects()
        cost_center_projects = set_global_cost_center_list()

        for (project) in projects:
            for center in cost_center_projects:
                center_data = json.loads(json.dumps(center, cls=AlchemyEncoder))
                if project[0] == center_data['project_id']:
                    project_list[project[0]] = center_data['project_name']
                elif project[0] not in project_list:
                    project_list[project[0]] = project[0]

        for (id, names) in project_list.iteritems():
            project_names.append(names)
    except Exception as e:
        log_error(e[0])
        status = 500
        project_list['message'] = str(e[0])

    response = dict(data=project_names, status=status)

    return response
def get_costs_per_center_year(year, center):
    data = {}
    log.info('get_costs_per_CENTER_YEAR_month == {0} --{1}'.format(year,center))
    log_output(year)
    log_output(center)
    try:
        project_list_local = project_list_per_center(center)['list']
        project_ids = project_list_per_center(center)['ids']

        log_output('Project_list_local')
        log_output(project_list_local)

        query_data = get_billing_data_per_year_per_center(str(year), project_ids)
        log_output(query_data)

        log.info('get_billing_data_per_year_per_center == {0}'.format(query_data))
        usage_data = get_per_month_cost(query_data)

        data = {
            'usage_data': usage_data,
            'project_list': project_list_local
        }
    except Exception as e:
        log_error(e)
        log.info(e)
    return data
def get_project_list_data():
    project_list = dict()
    status = 200
    project_names = []

    try:
        log.info('In Project List Data ----')

        projects = get_distinct_projects()
        cost_center_projects = set_global_cost_center_list()

        for (project) in projects:
            for center in cost_center_projects:
                center_data = json.loads(json.dumps(center, cls=AlchemyEncoder))
                if project[0] == center_data['project_id']:
                    project_list[project[0]] = center_data['project_name']
                elif project[0] not in project_list:
                    project_list[project[0]] = project[0]

        for (id, names) in project_list.iteritems():
            project_names.append(names)
    except Exception as e:
        log_error(e[0])
        status = 500
        project_list['message'] = str(e[0])

    response = dict(data=project_names, status=status)

    return response
def get_costs_per_resource_month_center(year, month, center, project):
    project_json = {}

    try:
        project_data = []
        resource_list = []
        query_data = get_billing_data_per_resource_month_center(str(year), str(month), str(project))
        log_output(query_data)

        for (resource, cost) in query_data:
            each_project = {'name': resource, 'cost': float(cost)}
            resource_list.append(resource)
            project_data.append(each_project)

        resource_list_local = resource_list_per_project(center, project)

        project_list_local = project_list_per_center(center)['list']

        project_json = {
            'usage_data': project_data,
            'resource_list': resource_list_local,
            'project_list': project_list_local
        }
    except Exception as e:
        log_error(e)
    return project_json
def get_costs_per_resource_all_project_per_day_week(year, week, center, resource, output_type):
    log.info('get_costs_per_resource_all_project_per_day_week == {0}--{1} --{2} --{3}--{4} '.format(year, week, center,
                                                                                                    resource,
                                                                                                    output_type))
    resource_json = dict()
    day_data = dict()
    status = 200

    try:
        project_list_local = project_list_per_center(center)['list']
        project_list_ids = project_list_per_center(center)['ids']
        resource_list_local = resource_list_per_project(center, None)

        query_data = get_billing_data_per_resource_all_project_per_day_week(str(year), str(week), project_list_ids,
                                                                            str(resource), output_type)
        log_output(query_data)

        if output_type == 'day':
            day_data = get_per_day_data(query_data)
        elif output_type == 'week':
            day_data['usage_data'] = get_week_data(query_data, year, None)
            day_data['d3_json'] = []

        resource_json = {'usage_data': day_data['usage_data'], 'd3_json': day_data['d3_json'],
                         'project_list': project_list_local,
                         'resource_list': resource_list_local}

    except Exception as e:
        log_error(e[0])
        status = 500
        resource_json['message'] = str(e[0])

    response = dict(data=resource_json, status=status)

    return response
def get_costs_year(year, output_type):
    data = dict()
    usage_data = []
    status = 200

    log.info(' In get_costs_year == {0} -- {1}'.format(year, output_type))
    try:
        query_data = get_billing_data_per_year(str(year), output_type)
        log_output(query_data)
        data = {
            'usage_data': usage_data
        }

        if output_type == 'month':
            usage_data = get_per_month_cost(query_data, None, year)
            data['usage_data'] = usage_data
        elif output_type == 'week' or output_type == 'quarter':
            usage_data = get_usage_data(query_data)
            data['usage_data'] = usage_data
        elif output_type == 'day':
            day_data = get_per_day_data(query_data)
            data['usage_data'] = day_data['usage_data']
            data['d3_json'] = day_data['d3_json']

    except Exception as e:
        log_error(e[0])
        status = 500
        data['message'] = str(e[0])

    response = dict(data=data, status=status)

    return response
def get_costs_year(year, output_type):
    data = dict()
    usage_data = []
    status = 200

    log.info(' In get_costs_year == {0} -- {1}'.format(year, output_type))
    try:
        query_data = get_billing_data_per_year(str(year), output_type)
        log_output(query_data)
        data = {
            'usage_data': usage_data
        }

        if output_type == 'month':
            usage_data = get_per_month_cost(query_data, None, year)
            data['usage_data'] = usage_data
        elif output_type == 'week' or output_type == 'quarter':
            usage_data = get_usage_data(query_data)
            data['usage_data'] = usage_data
        elif output_type == 'day':
            day_data = get_per_day_data(query_data)
            data['usage_data'] = day_data['usage_data']
            data['d3_json'] = day_data['d3_json']

    except Exception as e:
        log_error(e[0])
        status = 500
        data['message'] = str(e[0])

    response = dict(data=data, status=status)

    return response
def get_costs_per_resource_all_project_per_day_week(year, week, center, resource, output_type):
    log.info('get_costs_per_resource_all_project_per_day_week == {0}--{1} --{2} --{3}--{4} '.format(year, week, center,
                                                                                                    resource,
                                                                                                    output_type))
    resource_json = dict()
    day_data = dict()
    status = 200

    try:
        project_list_local = project_list_per_center(center)['list']
        project_list_ids = project_list_per_center(center)['ids']
        resource_list_local = resource_list_per_project(center, None)

        query_data = get_billing_data_per_resource_all_project_per_day_week(str(year), str(week), project_list_ids,
                                                                            str(resource), output_type)
        log_output(query_data)

        if output_type == 'day':
            day_data = get_per_day_data(query_data)
        elif output_type == 'week':
            day_data['usage_data'] = get_week_data(query_data, year, None)
            day_data['d3_json'] = []

        resource_json = {'usage_data': day_data['usage_data'], 'd3_json': day_data['d3_json'],
                         'project_list': project_list_local,
                         'resource_list': resource_list_local}

    except Exception as e:
        log_error(e[0])
        status = 500
        resource_json['message'] = str(e[0])

    response = dict(data=resource_json, status=status)

    return response
def get_costs_per_resource_quarter_center(year, quarter, center, project, output_type):
    log.info(
        'get_costs_per_resource_month_center == {0}--{1} --{2} --{3} -- {4}'.format(year, quarter, center,
                                                                                    project, output_type))

    project_json = dict()
    usage_data = []
    status = 200

    try:
        project_list_local = project_list_per_center(center)['list']
        resource_list_local = resource_list_per_project(center, project)

        project_json = {
            'usage_data': usage_data,
            'resource_list': resource_list_local,
            'project_list': project_list_local
        }

        if output_type == 'month':
            query_data = get_billing_data_per_year_quarter_week_day(str(year), str(quarter), str(project),
                                                                    output_type)
            log_output(query_data)

            usage_data = get_per_month_cost(query_data, quarter, year)
            project_json['usage_data'] = usage_data

        elif output_type == 'week':
            query_data = get_billing_data_per_year_quarter_week_day(str(year), str(quarter),
                                                                    str(project),
                                                                    output_type)
            log_output(query_data)

            usage_data = get_week_data(query_data, year, None)

            project_json['usage_data'] = usage_data

        elif output_type == 'day':
            query_data = get_billing_data_per_year_quarter_week_day(str(year), str(quarter),
                                                                    str(project),
                                                                    output_type)
            log_output(query_data)

            day_data = get_per_day_data(query_data)
            project_json['usage_data'] = day_data['usage_data']
            project_json['d3_json'] = day_data['d3_json']

    except Exception as e:
        log_error(e[0])
        status = 500
        project_json['message'] = str(e[0])

    response = dict(data=project_json, status=status)

    return response
def get_costs_per_resource_quarter_center(year, quarter, center, project, output_type):
    log.info(
        'get_costs_per_resource_month_center == {0}--{1} --{2} --{3} -- {4}'.format(year, quarter, center,
                                                                                    project, output_type))

    project_json = dict()
    usage_data = []
    status = 200

    try:
        project_list_local = project_list_per_center(center)['list']
        resource_list_local = resource_list_per_project(center, project)

        project_json = {
            'usage_data': usage_data,
            'resource_list': resource_list_local,
            'project_list': project_list_local
        }

        if output_type == 'month':
            query_data = get_billing_data_per_year_quarter_week_day(str(year), str(quarter), str(project),
                                                                    output_type)
            log_output(query_data)

            usage_data = get_per_month_cost(query_data, quarter, year)
            project_json['usage_data'] = usage_data

        elif output_type == 'week':
            query_data = get_billing_data_per_year_quarter_week_day(str(year), str(quarter),
                                                                    str(project),
                                                                    output_type)
            log_output(query_data)

            usage_data = get_week_data(query_data, year, None)

            project_json['usage_data'] = usage_data

        elif output_type == 'day':
            query_data = get_billing_data_per_year_quarter_week_day(str(year), str(quarter),
                                                                    str(project),
                                                                    output_type)
            log_output(query_data)

            day_data = get_per_day_data(query_data)
            project_json['usage_data'] = day_data['usage_data']
            project_json['d3_json'] = day_data['d3_json']

    except Exception as e:
        log_error(e[0])
        status = 500
        project_json['message'] = str(e[0])

    response = dict(data=project_json, status=status)

    return response
def get_costs_per_cost_month(year, value_to_match, output_type):
    log.info('get_costs_per_cost_month == {0}--{1} --{2}'.format(year, value_to_match, output_type))
    month_json = dict()
    month_data = []
    status = 200
    try:
        cost_center_list = set_global_cost_center_list()
        new_dict = dict()
        projects_list = []
        month_json = {
            'usage_data': month_data
        }

        for project_info in cost_center_list:
            projects_list.append(str(project_info['project_id']))

        query_data = get_billing_data_per_year_month(str(year), str(value_to_match), str(output_type))
        log_output(query_data)

        if query_data is not None:

            for (project, cost) in query_data:
                for project_info in cost_center_list:
                    cost_center = str(project_info['cost_center'])
                    project_id = str(project_info['project_id'])
                    owner = str(project_info['owner'])

                    if project == project_id:
                        new_dict[cost_center] = new_dict.get(cost_center, {})
                        new_dict[cost_center]['owner'] = owner
                        new_dict[cost_center]['cost'] = new_dict[cost_center].get('cost', 0.0)
                        new_dict[cost_center]['project'] = new_dict[cost_center].get('project', [])
                        new_dict[cost_center]['project'].append(str(project))
                        new_dict[cost_center]['cost'] += cost

            for key, value in new_dict.items():
                each_month = dict(name=value['owner'], cost=value['cost'], id=key)
                month_data.append(each_month)

        month_json['usage_data'] = month_data

        month_json = {
            'usage_data': month_data
        }

    except Exception as e:

        log_error(e[0])
        status = 500
        month_json['message'] = str(e[0])

    response = dict(data=month_json, status=status)

    return response
def get_costs_per_cost_month(year, month):
    log.info('get_costs_per_cost_month == {0}--{1}'.format(year,month))
    month_json = {}

    cost_center_list = get_center_list(False)
    log.info('COST_CENTER_LIST == {0}'.format(cost_center_list))

    log_output('In cost per month for a year')
    log_output(year)
    log_output(month)

    try:
        month_data = []
        query_data = get_billing_data_per_year_per_month(str(year), str(month))
        log_output(query_data)

        new_dict = dict()
        projects_list = []

        if query_data is not None:
            for project_info in cost_center_list:
                projects_list.append(str(project_info['project_id']))

            log.info('Project_Cost_center_list_global {0}'.format(cost_center_list))
            log.info('Project_Cost_center_list {0}'.format(projects_list))

            for (project, cost) in query_data:
                for project_info in cost_center_list:
                    cost_center = str(project_info['cost_center'])
                    project_id = str(project_info['project_id'])

                    if project == project_id:
                        new_dict[cost_center] = new_dict.get(cost_center, {})
                        new_dict[cost_center]['cost'] = new_dict[cost_center].get('cost', 0.0)
                        new_dict[cost_center]['project'] = new_dict[cost_center].get('project', [])
                        new_dict[cost_center]['project'].append(str(project))
                        new_dict[cost_center]['cost'] += cost

            log.info('new_dic --1 {0}'.format(new_dict))

            for key, value in new_dict.items():
                each_month = dict(name=key, cost=value['cost'])
                month_data.append(each_month)

            log.info('MONTH_DATA {0}'.format(month_data))

        month_json = {
            'usage_data': month_data
        }

    except Exception as e:

        log_error(e)
    return month_json
def get_costs_per_cost_month(year, value_to_match, output_type):
    log.info('get_costs_per_cost_month == {0}--{1} --{2}'.format(year, value_to_match, output_type))
    month_json = dict()
    month_data = []
    status = 200
    try:
        cost_center_list = set_global_cost_center_list()
        new_dict = dict()
        projects_list = []
        month_json = {
            'usage_data': month_data
        }

        for project_info in cost_center_list:
            projects_list.append(str(project_info['project_id']))

        query_data = get_billing_data_per_year_month(str(year), str(value_to_match), str(output_type))
        log_output(query_data)

        if query_data is not None:

            for (project, cost) in query_data:
                for project_info in cost_center_list:
                    cost_center = str(project_info['cost_center'])
                    project_id = str(project_info['project_id'])
                    director = str(project_info['director'])

                    if project == project_id:
                        new_dict[cost_center] = new_dict.get(cost_center, {})
                        new_dict[cost_center]['director'] = director
                        new_dict[cost_center]['cost'] = new_dict[cost_center].get('cost', 0.0)
                        new_dict[cost_center]['project'] = new_dict[cost_center].get('project', [])
                        new_dict[cost_center]['project'].append(str(project))
                        new_dict[cost_center]['cost'] += cost

            for key, value in new_dict.items():
                each_month = dict(name=value['director'], cost=value['cost'], id=key)
                month_data.append(each_month)

        month_json['usage_data'] = month_data

        month_json = {
            'usage_data': month_data
        }

    except Exception as e:

        log_error(e[0])
        status = 500
        month_json['message'] = str(e[0])

    response = dict(data=month_json, status=status)

    return response
def get_costs_per_center_week(year, week, center, output_type):
    log.info(
        'get_costs_per_center_week == {0}--{1} --{2} --{3}'.format(year, week, center, output_type))
    center_json = dict()
    status = 200
    try:


        cost_center_list = set_global_cost_center_list()
        project_list_local = project_list_per_center(center)['list']
        project_id_local = project_list_per_center(center)['ids']
        resource_list_local = resource_list_per_project(center, None)
        cost_center_projects_id = []
        cost_center_projects_name = []

        for project_info in cost_center_list:
            if project_info['cost_center'] == center:
                cost_center_projects_id.append(project_info['project_id'])
                cost_center_projects_name.append(project_info['project_name'])

        week_output = []
        center_json = {
            'usage_data': week_output,
            'project_list': project_list_local,
            'resource_list': resource_list_local

        }
        if output_type == 'week':
            query_data = get_billing_data_per_year_week_day(str(year), str(week), str(output_type), project_id_local)
            log_output(query_data)

            center_json['usage_data'] = get_week_data(query_data, year, cost_center_projects_id)

        elif output_type == 'day':
            query_data = get_billing_data_per_year_week_day(str(year), str(week), str(output_type), project_id_local)
            log_output(query_data)

            day_data = get_per_day_data(query_data)
            center_json['usage_data'] = day_data['usage_data']
            center_json['d3_json'] = day_data['d3_json']

    except Exception as e:
        log_error(e[0])
        status = 500
        center_json['message'] = str(e[0])

    response = dict(data=center_json, status=status)

    return response
def get_project_list_data():
    log.info('In Project List Data ----')
    data = dict()
    project_list = dict()
    try:
        projects = get_distinct_projects()

        for (project) in projects:
            project_list[project[0]] = project[0]

        log_output('PROJECT LIST')
        log_output(project_list)
        for (project) in project_list:
            log.info('INSIDE LOOP')
            # Request an access token from the metadata server.
            token_data = get_access_token()
            resp_access = token_data['resp_access']
            content_access = token_data['content_access']

            if resp_access.status == 200:

                # Extract the access token from the response.
                d = json.loads(content_access)
                access_token = d['access_token']  # Save the access token
                # log.debug('access_token -- {0}'.format(access_token))
                # Construct the request to Google Cloud Storage
                if project != 'Not Available':
                    project_id = project.split('-')[1]
                else:
                    project_id = 'Not Available'

                project_data = get_project_data(project_id, access_token)
                resp = project_data['resp']
                content = project_data['content']
                if resp.status == 200:
                    # log.debug('Project_data {0} -- {1}'.format(project_id, content))
                    data = json.loads(content)
                    project_list[project] = data['name']
                else:
                    log.error('Project_data  Error {0} -- {1}'.format(project_id, resp.status))


            else:
                log.error('Access Token Error {0}'.format(resp_access.status))

    except Exception as e:
        log_error(e)

    return project_list
def get_costs_per_center_week(year, week, center, output_type):
    log.info(
        'get_costs_per_center_week == {0}--{1} --{2} --{3}'.format(year, week, center, output_type))
    center_json = dict()
    status = 200
    try:


        cost_center_list = set_global_cost_center_list()
        project_list_local = project_list_per_center(center)['list']
        project_id_local = project_list_per_center(center)['ids']
        resource_list_local = resource_list_per_project(center, None)
        cost_center_projects_id = []
        cost_center_projects_name = []

        for project_info in cost_center_list:
            if project_info['cost_center'] == center:
                cost_center_projects_id.append(project_info['project_id'])
                cost_center_projects_name.append(project_info['project_name'])

        week_output = []
        center_json = {
            'usage_data': week_output,
            'project_list': project_list_local,
            'resource_list': resource_list_local

        }
        if output_type == 'week':
            query_data = get_billing_data_per_year_week_day(str(year), str(week), str(output_type), project_id_local)
            log_output(query_data)

            center_json['usage_data'] = get_week_data(query_data, year, cost_center_projects_id)

        elif output_type == 'day':
            query_data = get_billing_data_per_year_week_day(str(year), str(week), str(output_type), project_id_local)
            log_output(query_data)

            day_data = get_per_day_data(query_data)
            center_json['usage_data'] = day_data['usage_data']
            center_json['d3_json'] = day_data['d3_json']

    except Exception as e:
        log_error(e[0])
        status = 500
        center_json['message'] = str(e[0])

    response = dict(data=center_json, status=status)

    return response
def get_costs_per_center_month(year, month, center):
    center_json = {}
    cost_center_list = get_center_list(False)
    try:

        cost_center_projects_id = []
        cost_center_projects_name = []
        cost_center_projects_all_id = []
        cost_center_projects_all_name = []

        for project_info in cost_center_list:
            cost_center_projects_all_id.append(project_info['project_id'])
            cost_center_projects_all_name.append(project_info['project_name'])
            if project_info['cost_center'] == center:
                cost_center_projects_id.append(project_info['project_id'])
                cost_center_projects_name.append(project_info['project_name'])


        month_data = []

        query_data = get_billing_data_per_year_per_month(str(year), str(month))
        log_output(query_data)

        for (project, cost) in query_data:
            if project in cost_center_projects_id:
                if cost_center_projects_name[cost_center_projects_id.index(project)].lower() == 'none':
                    name = project
                else:
                    name = cost_center_projects_name[cost_center_projects_id.index(project)]
                each_month = {'name': name,
                              'cost': float(cost)}
                month_data.append(each_month)

        project_list_local = project_list_per_center(center)['list']

        log.info('MonthData {0}'.format(month_data))
        log.info('Project List {0}'.format(project_list_local))

        center_json = {
            'usage_data': month_data,
            'project_list': project_list_local

        }
    except Exception as e:
        log_error(e)
    return center_json
def get_costs_per_month(year):
    data = {}
    log.info(' In get_costs_per_month == {0}'.format(year))
    try:

        query_data = get_billing_data_per_year(str(year))
        log_output(query_data)

        usage_data = get_per_month_cost(query_data)

        log.info(' get_costs_per_month  DATA == {0}'.format(usage_data))
        data = {
            'usage_data': usage_data
        }
    except Exception as e:
        log_error(e)
    return data
def get_costs_per_resource_all_project_per_day(year, month, center, resource):
    resource_json = {}
    try:
        project_list_local = project_list_per_center(center)['list']
        project_list_ids = project_list_per_center(center)['ids']

        month_int = int(month)
        days = calendar.monthrange(year, month_int)[1] + 1
        each_day = []

        query_data = get_billing_data_per_resource_all_project_per_day(str(year), str(month), project_list_ids,
                                                                       str(resource))
        log_output(query_data)

        for x in range(1, days):
            each_day.append({'name': x, 'cost': 0, 'usage': 0, 'unit': ''})

        d3_json = [{'key': 'Cost', 'values': [], 'bar': True}, {'key': 'Usage', 'values': []}]

        for (day, cost, use, unit) in query_data:
            for val in each_day:
                if val['name'] == day:
                    val['cost'] = float(cost)
                    val['usage'] = float(use)
                    val['unit'] = str(unit)

            value = []
            value.append(day)
            value.append(float(cost))
            value.append('$')
            d3_json[0]['values'].append(value)
            value = []
            value.append(day)
            value.append(float(use))
            value.append(str(unit))
            d3_json[1]['values'].append(value)

        resource_list_local = resource_list_per_project(center, None)


        resource_json = {'usage_data': each_day, 'd3_json': d3_json, 'project_list': project_list_local,
                         'resource_list': resource_list_local}
    except Exception as e:
        log_error(e)
    return resource_json
def get_costs_per_resource_per_project_per_day_quarter(year, value_to_match, center, project, resource, output_type):
    log.info('get_costs_per_resource_per_project_per_day_quarter == {0}--{1} --{2} --{3}--{4}--{5} '.format(year,
                                                                                                            value_to_match,
                                                                                                            center,
                                                                                                            project,
                                                                                                            resource,
                                                                                                            output_type))

    resource_json = dict()
    day_data = dict()
    status = 200

    try:
        project_list_local = project_list_per_center(center)['list']
        resource_list_local = resource_list_per_project(center, project)

        query_data = get_billing_data_per_resource_per_project_per_quarter(str(year), str(value_to_match),
                                                                           str(project),
                                                                           str(resource), output_type)
        log_output(query_data)

        if output_type == 'month':
            day_data['usage_data'] = get_per_month_cost(query_data, value_to_match, year)
            day_data['d3_json'] = []
        elif output_type == 'day':
            day_data = get_per_day_data(query_data)
        else:
            day_data['usage_data'] = get_week_data(query_data, year, None)
            day_data['d3_json'] = []

        resource_json = {'usage_data': day_data['usage_data'], 'd3_json': day_data['d3_json'],
                         'project_list': project_list_local,
                         'resource_list': resource_list_local}

    except Exception as e:
        log_error(e[0])
        status = 500
        resource_json['message'] = str(e[0])

    response = dict(data=resource_json, status=status)

    return response
def get_costs_per_resource_per_project_per_day_quarter(year, value_to_match, center, project, resource, output_type):
    log.info('get_costs_per_resource_per_project_per_day_quarter == {0}--{1} --{2} --{3}--{4}--{5} '.format(year,
                                                                                                            value_to_match,
                                                                                                            center,
                                                                                                            project,
                                                                                                            resource,
                                                                                                            output_type))

    resource_json = dict()
    day_data = dict()
    status = 200

    try:
        project_list_local = project_list_per_center(center)['list']
        resource_list_local = resource_list_per_project(center, project)

        query_data = get_billing_data_per_resource_per_project_per_quarter(str(year), str(value_to_match),
                                                                           str(project),
                                                                           str(resource), output_type)
        log_output(query_data)

        if output_type == 'month':
            day_data['usage_data'] = get_per_month_cost(query_data, value_to_match, year)
            day_data['d3_json'] = []
        elif output_type == 'day':
            day_data = get_per_day_data(query_data)
        else:
            day_data['usage_data'] = get_week_data(query_data, year, None)
            day_data['d3_json'] = []

        resource_json = {'usage_data': day_data['usage_data'], 'd3_json': day_data['d3_json'],
                         'project_list': project_list_local,
                         'resource_list': resource_list_local}

    except Exception as e:
        log_error(e[0])
        status = 500
        resource_json['message'] = str(e[0])

    response = dict(data=resource_json, status=status)

    return response
def get_costs_per_resource_per_project(year, center, project, resource):
    resource_json = {}

    project_list_local = project_list_per_center(center)['list']
    resource_list_local = resource_list_per_project(center, project)

    try:
        query_data = get_billing_data_per_resource_per_project(str(year), str(project), str(resource))
        log_output(query_data)

        usage_data = get_per_month_cost(query_data)
        resource_json = {
            'usage_data': usage_data,
            'project_list': project_list_local,
            'resource_list': resource_list_local

        }
    except Exception as e:
        log_error(e)
    return resource_json
def get_costs_per_project_year(year, center, project):
    month_json = {}

    project_list_local = project_list_per_center(center)['list']

    try:

        month_data = []
        query_data = get_billing_data_per_project_year(str(year), str(project))
        log_output(query_data)

        for (project, cost) in query_data:
            each_month = {'name': project, 'cost': float(cost)}
            month_data.append(each_month)
        month_json = {
            'usage_data': month_data,
            'project_list': project_list_local
        }
    except Exception as e:
        log_error(e)
    return month_json
def get_costs_per_center_month(year, month, center, output_type):
    log.info(
        'get_costs_per_center_month == {0}--{1} --{2} --{3}'.format(year, month, center, output_type))
    center_json = dict()
    status = 200

    try:


        cost_center_list = set_global_cost_center_list()
        project_list_local = project_list_per_center(center)['list']
        project_id_local = project_list_per_center(center)['ids']
        resource_list_local = resource_list_per_project(center, None)
        cost_center_projects_id = []
        cost_center_projects_name = []

        for project_info in cost_center_list:
            if project_info['cost_center'] == center:
                cost_center_projects_id.append(project_info['project_id'])
                cost_center_projects_name.append(project_info['project_name'])

        month_data = []
        center_json = {
            'usage_data': month_data,
            'project_list': project_list_local,
            'resource_list': resource_list_local

        }
        if output_type == 'month':


            query_data = get_billing_data_per_year_month(str(year), str(month), str(output_type))
            log_output(query_data)

            for (project, cost) in query_data:
                if project in cost_center_projects_id:
                    if cost_center_projects_name[cost_center_projects_id.index(project)].lower() == 'none':
                        name = project
                    else:
                        name = cost_center_projects_name[cost_center_projects_id.index(project)]
                    each_month = {'name': name,
                                  'cost': float(cost)}
                    month_data.append(each_month)
            center_json['usage_data'] = month_data

        elif output_type == 'week':
            query_data = get_billing_data_per_year_month_week_day(str(year), str(month), str(output_type),
                                                                  project_id_local)
            log_output(query_data)

            center_json['usage_data'] = get_week_data(query_data, year, cost_center_projects_id)

        elif output_type == 'day':
            query_data = get_billing_data_per_year_month_week_day(str(year), str(month), str(output_type),
                                                                  project_id_local)
            log_output(query_data)
            day_data = get_per_day_data(query_data)
            center_json['usage_data'] = day_data['usage_data']
            center_json['d3_json'] = day_data['d3_json']

    except Exception as e:
        log_error(e[0])
        status = 500
        center_json['message'] = str(e[0])

    response = dict(data=center_json, status=status)

    return response
def get_costs_per_center_month(year, month, center, output_type):
    log.info(
        'get_costs_per_center_month == {0}--{1} --{2} --{3}'.format(year, month, center, output_type))
    center_json = dict()
    status = 200

    try:


        cost_center_list = set_global_cost_center_list()
        project_list_local = project_list_per_center(center)['list']
        project_id_local = project_list_per_center(center)['ids']
        resource_list_local = resource_list_per_project(center, None)
        cost_center_projects_id = []
        cost_center_projects_name = []

        for project_info in cost_center_list:
            if project_info['cost_center'] == center:
                cost_center_projects_id.append(project_info['project_id'])
                cost_center_projects_name.append(project_info['project_name'])

        month_data = []
        center_json = {
            'usage_data': month_data,
            'project_list': project_list_local,
            'resource_list': resource_list_local

        }
        if output_type == 'month':


            query_data = get_billing_data_per_year_month(str(year), str(month), str(output_type))
            log_output(query_data)

            for (project, cost) in query_data:
                if project in cost_center_projects_id:
                    if cost_center_projects_name[cost_center_projects_id.index(project)].lower() == 'none':
                        name = project
                    else:
                        name = cost_center_projects_name[cost_center_projects_id.index(project)]
                    each_month = {'name': name,
                                  'cost': float(cost)}
                    month_data.append(each_month)
            center_json['usage_data'] = month_data

        elif output_type == 'week':
            query_data = get_billing_data_per_year_month_week_day(str(year), str(month), str(output_type),
                                                                  project_id_local)
            log_output(query_data)

            center_json['usage_data'] = get_week_data(query_data, year, cost_center_projects_id)

        elif output_type == 'day':
            query_data = get_billing_data_per_year_month_week_day(str(year), str(month), str(output_type),
                                                                  project_id_local)
            log_output(query_data)
            day_data = get_per_day_data(query_data)
            center_json['usage_data'] = day_data['usage_data']
            center_json['d3_json'] = day_data['d3_json']

    except Exception as e:
        log_error(e[0])
        status = 500
        center_json['message'] = str(e[0])

    response = dict(data=center_json, status=status)

    return response