Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature for querying dedicated deployment usage (per workspace) #337

Merged
merged 7 commits into from
Nov 26, 2024
20 changes: 20 additions & 0 deletions roboflow/adapters/deploymentapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ def list_deployment(api_key):
return response.status_code, response.json()


def get_workspace_usage(api_key, target_month):
url = f"{DEDICATED_DEPLOYMENT_URL}/usage_workspace?api_key={api_key}"
if target_month is not None:
url += f"&target_month={target_month}"
response = requests.get(url)
if response.status_code != 200:
return response.status_code, response.text
return response.status_code, response.json()


def get_deployment_usage(api_key, deployment_name, target_month):
url = f"{DEDICATED_DEPLOYMENT_URL}/usage_deployment?api_key={api_key}&deployment_name={deployment_name}"
if target_month is not None:
url += f"&target_month={target_month}"
response = requests.get(url)
if response.status_code != 200:
return response.status_code, response.text
return response.status_code, response.json()


def delete_deployment(api_key, deployment_name):
url = f"{DEDICATED_DEPLOYMENT_URL}/delete"
response = requests.post(url, json={"api_key": api_key, "deployment_name": deployment_name})
Expand Down
39 changes: 39 additions & 0 deletions roboflow/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ def add_deployment_parser(subparsers):
"get", help="show detailed info for a dedicated deployment"
)
deployment_list_parser = deployment_subparsers.add_parser("list", help="list dedicated deployments in a workspace")
deployment_usage_workspace_parser = deployment_subparsers.add_parser(
"usage_workspace", help="get all dedicated deployments usage in a workspace"
)
deployment_usage_deployment_parser = deployment_subparsers.add_parser(
"usage_deployment", help="get usage of a specific dedicated deployments"
)
deployment_delete_parser = deployment_subparsers.add_parser("delete", help="delete a dedicated deployment")
deployment_log_parser = deployment_subparsers.add_parser("log", help="show log info for a dedicated deployment")

Expand Down Expand Up @@ -66,6 +72,15 @@ def add_deployment_parser(subparsers):
deployment_list_parser.set_defaults(func=list_deployment)
deployment_list_parser.add_argument("-a", "--api_key", help="api key")

deployment_usage_workspace_parser.set_defaults(func=get_workspace_usage)
deployment_usage_workspace_parser.add_argument("-a", "--api_key", help="api key")
deployment_usage_workspace_parser.add_argument("target_month", help="target month (format: YYYYMM)", nargs="?")

deployment_usage_deployment_parser.set_defaults(func=get_deployment_usage)
deployment_usage_deployment_parser.add_argument("-a", "--api_key", help="api key")
deployment_usage_deployment_parser.add_argument("deployment_name", help="deployment name")
deployment_usage_deployment_parser.add_argument("target_month", help="target month (format: YYYYMM)", nargs="?")

deployment_delete_parser.set_defaults(func=delete_deployment)
deployment_delete_parser.add_argument("-a", "--api_key", help="api key")
deployment_delete_parser.add_argument("deployment_name", help="deployment name")
Expand Down Expand Up @@ -151,6 +166,30 @@ def list_deployment(args):
print(json.dumps(msg, indent=2))


def get_workspace_usage(args):
api_key = args.api_key or load_roboflow_api_key(None)
if api_key is None:
print("Please provide an api key")
exit(1)
status_code, msg = deploymentapi.get_workspace_usage(api_key, args.target_month)
if status_code != 200:
print(f"{status_code}: {msg}")
exit(status_code)
print(json.dumps(msg, indent=2))


def get_deployment_usage(args):
api_key = args.api_key or load_roboflow_api_key(None)
if api_key is None:
print("Please provide an api key")
exit(1)
status_code, msg = deploymentapi.get_deployment_usage(api_key, args.deployment_name, args.target_month)
if status_code != 200:
print(f"{status_code}: {msg}")
exit(status_code)
print(json.dumps(msg, indent=2))


def delete_deployment(args):
api_key = args.api_key or load_roboflow_api_key(None)
if api_key is None:
Expand Down