def test_filter_slaves(): filters = {"foo": ["one", "two"], "bar": ["three", "four"]} fns = [ metastatus_lib.make_filter_slave_func(k, v) for k, v in filters.items() ] data = [ { "name": "aaa", "attributes": { "foo": "one", "bar": "three" } }, { "name": "bbb", "attributes": { "foo": "one" } }, { "name": "ccc", "attributes": { "foo": "wrong", "bar": "four" } }, ] slaves = metastatus_lib.filter_slaves(data, fns) names = [s["name"] for s in slaves] assert "aaa" in names assert "bbb" not in names assert "ccc" not in names
def resources_utilization(request): master = get_mesos_master() mesos_state = block(master.state) groupings = request.swagger_data.get('groupings', ['superregion']) # swagger actually makes the key None if it's not set if groupings is None: groupings = ['superregion'] grouping_function = metastatus_lib.key_func_for_attribute_multi(groupings) sorting_function = metastatus_lib.sort_func_for_attributes(groupings) filters = request.swagger_data.get('filter', []) filters = parse_filters(filters) filter_funcs = [ metastatus_lib.make_filter_slave_func(attr, vals) for attr, vals in filters.items() ] resource_info_dict = metastatus_lib.get_resource_utilization_by_grouping( grouping_func=grouping_function, mesos_state=mesos_state, filters=filter_funcs, sort_func=sorting_function, ) response_body = [] for k, v in resource_info_dict.items(): group = {'groupings': {}} for grouping, value in k: group['groupings'][grouping] = value for resource, value in v['total']._asdict().items(): group[resource] = {'total': value} for resource, value in v['free']._asdict().items(): group[resource]['free'] = value for resource in v['free']._fields: group[resource][ 'used'] = group[resource]['total'] - group[resource]['free'] response_body.append(group) return Response(json_body=response_body, status_code=200)