Пример #1
0
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
Пример #2
0
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