Skip to content
This repository was archived by the owner on Sep 22, 2023. It is now read-only.

feat: Add command to get available resources #165

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions changes/165.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add command to get available resources
51 changes: 50 additions & 1 deletion src/ai/backend/client/cli/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

from . import main
from .interaction import ask_yn
from .pretty import print_done, print_error, print_fail, print_info, print_wait
from .pretty import print_done, print_error, print_fail, print_info, print_wait, print_warn
from ..session import Session
from .config import get_config


@main.group()
Expand Down Expand Up @@ -204,3 +205,51 @@ def exclude_agents(agent_ids):
except Exception as e:
print_error(e)
sys.exit(1)


@main.command()
@click.argument('scaling_group', metavar='SCALING_GROUP', default='default')
@click.argument('group', metavar='GROUP', default='default')
@click.option('-a', '--all', is_flag=True,
help='Get all resources of group.')
def get_resources(scaling_group, group, all):
'''
Get available resources from the scaling groups.
'''
config = get_config()
if config.endpoint_type != 'session':
print_warn('To use get-resources, your endpoint type must be "session".')
raise click.Abort()

with Session() as session:
ret = session.Resource.get_available_resources(scaling_group, group)
print(f'Total remaining resources of scaling group [{scaling_group}]:')
print(' CPU:', ret['scaling_group_remaining']['cpu'])
print(' Memory:', ret['scaling_group_remaining']['mem'])
Copy link
Member

@achimnol achimnol Jul 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be more resource slot types.
Please use session.Resource.get_resource_slots() to retrieve the exact list of the currently configured resource slots, and then iterate over it when displaying the remainings.
Note that you may need to have an extra mapping from the resource slot name to a human-readable name with a fallback to the raw slot name.

print('Each resources of scaling groups:')
if not all:
print(f' [{scaling_group}]')
print(' Using:')
print(' CPU:', ret['scaling_groups'][scaling_group]['using']['cpu'])
print(' Memory:', ret['scaling_groups'][scaling_group]['using']['mem'])
print(' Remaining:')
print(' CPU:', ret['scaling_groups'][scaling_group]['remaining']['cpu'])
print(' Memory:', ret['scaling_groups'][scaling_group]['remaining']['mem'])
else:
for x in ret['scaling_groups'].keys():
print(f' [{x}]')
print(' Using:')
print(' CPU:', ret['scaling_groups'][x]['using']['cpu'])
print(' Memory:', ret['scaling_groups'][x]['using']['mem'])
print(' Remaining:')
print(' CPU:', ret['scaling_groups'][x]['remaining']['cpu'])
print(' Memory:', ret['scaling_groups'][x]['remaining']['mem'])
print('Group limits:')
print(' CPU:', ret['group_limits']['cpu'])
print(' Memory:', ret['group_limits']['mem'])
print('Group using:')
print(' CPU:', ret['group_using']['cpu'])
print(' Memory:', ret['group_using']['mem'])
print('Group remaining:')
print(' CPU:', ret['group_remaining']['cpu'])
print(' Memory:', ret['group_remaining']['mem'])
10 changes: 10 additions & 0 deletions src/ai/backend/client/func/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ async def check_presets(cls):
async with rqst.fetch() as resp:
return await resp.json()

@api_function
@classmethod
async def get_available_resources(cls, scaling_group: str, group: str):
"""
Lists available resources from the scaling groups.
"""
rqst = Request('GET', '/resource/available-resources')
async with rqst.fetch() as resp:
return await resp.json()

@api_function
@classmethod
async def get_docker_registries(cls):
Expand Down