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, 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_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