def get_resource_utilization_by_grouping(grouping_func, mesos_state, filters=[]): """ Given a function used to group slaves and mesos state, calculate resource utilization for each value of a given attribute. :grouping_func: a function that given a slave, will return the value of an attribtue to group by. :param mesos_state: the mesos state :param filters: filters to apply to the slaves in the calculation, with filtering preformed by filter_slaves :returns: a dict of {attribute_value: resource_usage}, where resource usage is the dict returned by ``calculate_resource_utilization_for_slaves`` for slaves grouped by attribute value. """ slaves = mesos_state.get('slaves', []) slaves = filter_slaves(slaves, filters) if not has_registered_slaves(mesos_state): raise ValueError("There are no slaves registered in the mesos state.") tasks = get_all_tasks_from_state(mesos_state, include_orphans=True) non_terminal_tasks = [task for task in tasks if not is_task_terminal(task)] slave_groupings = group_slaves_by_key_func(grouping_func, slaves) return { attribute_value: calculate_resource_utilization_for_slaves( slaves=slaves, tasks=filter_tasks_for_slaves(slaves, non_terminal_tasks)) for attribute_value, slaves in slave_groupings.items() }
def get_resource_utilization_by_grouping( grouping_func: _GenericNodeGroupingFunctionT, mesos_state: MesosState, filters: Sequence[_GenericNodeFilterFunctionT] = [], sort_func: _GenericNodeSortFunctionT = None, ) -> Mapping[_KeyFuncRetT, ResourceUtilizationDict]: """Given a function used to group slaves and mesos state, calculate resource utilization for each value of a given attribute. :grouping_func: a function that given a slave, will return the value of an attribute to group by. :param mesos_state: the mesos state :param filters: filters to apply to the slaves in the calculation, with filtering preformed by filter_slaves :param sort_func: a function that given a list of slaves, will return the sorted list of slaves. :returns: a dict of {attribute_value: resource_usage}, where resource usage is the dict returned by ``calculate_resource_utilization_for_slaves`` for slaves grouped by attribute value. """ slaves: Sequence[_SlaveT] = mesos_state.get("slaves", []) slaves = filter_slaves(slaves, filters) if not has_registered_slaves(mesos_state): raise ValueError("There are no slaves registered in the mesos state.") tasks = get_all_tasks_from_state(mesos_state, include_orphans=True) non_terminal_tasks = [task for task in tasks if not is_task_terminal(task)] slave_groupings = group_slaves_by_key_func(grouping_func, slaves, sort_func) return { attribute_value: calculate_resource_utilization_for_slaves( slaves=slaves, tasks=filter_tasks_for_slaves(slaves, non_terminal_tasks) ) for attribute_value, slaves in slave_groupings.items() }