Skip to content

Commit

Permalink
add log support
Browse files Browse the repository at this point in the history
  • Loading branch information
PacificDou committed Oct 21, 2024
1 parent 7fe28e5 commit 01a5f30
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
12 changes: 12 additions & 0 deletions roboflow/adapters/deploymentapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ def list_machine_types(api_key):
if response.status_code != 200:
return response.status_code, response.text
return response.status_code, response.json()


def get_deployment_log(api_key, deployment_name, from_timestamp=None, to_timestamp=None):
url = f"{DEDICATED_DEPLOYMENT_URL}/get_log?api_key={api_key}&deployment_name={deployment_name}"
if from_timestamp is not None:
url += f"&from_timestamp={from_timestamp.isoformat()}"
if to_timestamp is not None:
url += f"&to_timestamp={to_timestamp.isoformat()}"
response = requests.get(url)
if response.status_code != 200:
return response.status_code, response.text
return response.status_code, response.json()
41 changes: 40 additions & 1 deletion roboflow/deployment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import time
from datetime import datetime
from datetime import datetime, timedelta

from roboflow.adapters import deploymentapi
from roboflow.config import load_roboflow_api_key
Expand All @@ -19,6 +19,7 @@ def add_deployment_parser(subparsers):
)
deployment_list_parser = deployment_subparsers.add_parser("list", help="list dedicated deployments in a workspace")
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")

deployment_machine_type_parser.set_defaults(func=list_machine_types)
deployment_machine_type_parser.add_argument("-a", "--api_key", help="api key")
Expand Down Expand Up @@ -68,6 +69,14 @@ def add_deployment_parser(subparsers):
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")

deployment_log_parser.set_defaults(func=get_deployment_log)
deployment_log_parser.add_argument("-a", "--api_key", help="api key")
deployment_log_parser.add_argument("deployment_name", help="deployment name")
deployment_log_parser.add_argument("-d", "--duration", help="duration of log (from now) in seconds", type=int, default=3600)
deployment_log_parser.add_argument(
"-f", "--follow", help="follow log output", action="store_true"
)


def list_machine_types(args):
Expand Down Expand Up @@ -149,3 +158,33 @@ def delete_deployment(args):
print(f"{status_code}: {msg}")
return
print(json.dumps(msg, indent=2))


def get_deployment_log(args):
api_key = args.api_key or load_roboflow_api_key(None)
if api_key is None:
print("Please provide an api key")
return

to_timestamp = datetime.now()
from_timestamp = (to_timestamp - timedelta(seconds = args.duration))
log_ids = set() # to avoid duplicate logs
while True:
status_code, msg = deploymentapi.get_deployment_log(api_key, args.deployment_name, from_timestamp, to_timestamp)
if status_code != 200:
print(f"{status_code}: {msg}")
return

for log in msg[::-1]: # logs are sorted by reversed timestamp
if log['insert_id'] in log_ids:
continue
log_ids.add(log['insert_id'])
print(f'[{datetime.fromisoformat(log["timestamp"]).strftime("%Y-%m-%d %H:%M:%S.%f")}] {log["payload"]}')

if not args.follow:
break

time.sleep(10)
from_timestamp = to_timestamp
to_timestamp = datetime.now()

0 comments on commit 01a5f30

Please sign in to comment.