def project_list_per_center(center): log.info(' IN PROJECT LIST PER CENTER __ {0}'.format(center)) project_list_center = '' project_list_center_arr = [] project_list_center_arr_id = [] project_center_all_list = [] project_list_local = [] status = 200 try: cost_center_list = set_global_cost_center_list() for project in cost_center_list: project_center_all_list.append(project['project_id']) if project['cost_center'] == center: project_list_center += ",'" + project['project_id'] + "'" if center.lower() == 'other' and project['project_name'] == project['project_id']: project_list_center_arr.append(project['project_id']) else: project_list_center_arr.append(project['project_name']) project_list_center_arr_id.append(project['project_id']) project_list_local = project_list_center_arr except Exception as e: status = 500 return {'list': project_list_local, 'ids': project_list_center_arr_id, 'data': project_list_center_arr, 'status': status}
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 resource_list_per_project(center, project): resource_list = [] project_list_local = {} log.info('IN RESOURCE_LIST_PER_PROJECT -- {0} -- {1}'.format(center, project)) cost_center_list = set_global_cost_center_list() for project_info in cost_center_list: project_list_local[project_info['project_id']] = project_info['project_name'] if project is not None: for project_id in project_list_local: if project_list_local[project_id] == project: project = str(project_id) query_data = get_resource_list_per_project([project]) else: project_ids = project_list_per_center(center)['ids'] query_data = get_resource_list_per_project(project_ids) log_output('In resource List') log_output(query_data) for (resource) in query_data: resource_list.append(resource[0]) return resource_list
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_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_center_list(unique): log_output('in get center list ------') cost_center_list = [] if unique: center_list = get_cost_centers(unique) log_output(center_list) for (cost_center) in center_list: cost_center_list.append(cost_center[0]) if 'other' not in cost_center_list: cost_center_list.append('other') else: cost_center_list = set_global_cost_center_list() return cost_center_list
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