Skip to content

Commit

Permalink
Merge pull request #341 from roboflow/bugfix-dedicated-deployment-log
Browse files Browse the repository at this point in the history
Use local timezone when query logs & usages for dedicated deployment
  • Loading branch information
PacificDou authored Nov 28, 2024
2 parents d369d43 + 54412a8 commit 7fca4bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
25 changes: 19 additions & 6 deletions roboflow/adapters/deploymentapi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import urllib

import requests

from roboflow.config import DEDICATED_DEPLOYMENT_URL
Expand Down Expand Up @@ -42,15 +44,25 @@ def list_deployment(api_key):


def get_workspace_usage(api_key, from_timestamp, to_timestamp):
url = f"{DEDICATED_DEPLOYMENT_URL}/usage_workspace?api_key={api_key}&from_timestamp={from_timestamp.isoformat()}&to_timestamp={to_timestamp.isoformat()}"
params = {"api_key": api_key}
if from_timestamp is not None:
params["from_timestamp"] = from_timestamp.isoformat() # may contain + sign
if to_timestamp is not None:
params["to_timestamp"] = to_timestamp.isoformat() # may contain + sign
url = f"{DEDICATED_DEPLOYMENT_URL}/usage_workspace?{urllib.parse.urlencode(params)}"
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, from_timestamp, to_timestamp):
url = f"{DEDICATED_DEPLOYMENT_URL}/usage_deployment?api_key={api_key}&deployment_name={deployment_name}&from_timestamp={from_timestamp.isoformat()}&to_timestamp={to_timestamp.isoformat()}"
params = {"api_key": api_key, "deployment_name": deployment_name}
if from_timestamp is not None:
params["from_timestamp"] = from_timestamp.isoformat() # may contain + sign
if to_timestamp is not None:
params["to_timestamp"] = to_timestamp.isoformat() # may contain + sign
url = f"{DEDICATED_DEPLOYMENT_URL}/usage_deployment?{urllib.parse.urlencode(params)}"
response = requests.get(url)
if response.status_code != 200:
return response.status_code, response.text
Expand All @@ -74,13 +86,14 @@ def list_machine_types(api_key):


def get_deployment_log(api_key, deployment_name, from_timestamp=None, to_timestamp=None, max_entries=-1):
url = f"{DEDICATED_DEPLOYMENT_URL}/get_log?api_key={api_key}&deployment_name={deployment_name}"
params = {"api_key": api_key, "deployment_name": deployment_name}
if from_timestamp is not None:
url += f"&from_timestamp={from_timestamp.isoformat()}"
params["from_timestamp"] = from_timestamp.isoformat() # may contain + sign
if to_timestamp is not None:
url += f"&to_timestamp={to_timestamp.isoformat()}"
params["to_timestamp"] = to_timestamp.isoformat() # may contain + sign
if max_entries > 0:
url += f"&max_entries={max_entries}"
params["max_entries"] = max_entries
url = f"{DEDICATED_DEPLOYMENT_URL}/get_log?{urllib.parse.urlencode(params)}"
response = requests.get(url)
if response.status_code != 200:
return response.status_code, response.text
Expand Down
20 changes: 10 additions & 10 deletions roboflow/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ def is_valid_ISO8601_timestamp(ts):

def check_from_to_timestamp(from_timestamp, to_timestamp, default_timedelta):
if from_timestamp and not is_valid_ISO8601_timestamp(from_timestamp):
print("Please provide a valid from_timestamp in ISO8601 format")
print("Please provide a valid from_timestamp in ISO8601 format (YYYY-MM-DD HH:MM:SS)")
exit(1)

if to_timestamp and not is_valid_ISO8601_timestamp(to_timestamp):
print("Please provide a valid to_timestamp in ISO8601 format")
print("Please provide a valid to_timestamp in ISO8601 format (YYYY-MM-DD HH:MM:SS)")
exit(1)

time_now = datetime.now().replace(tzinfo=None)
time_now = datetime.now().astimezone() # local timezone
if from_timestamp is None and to_timestamp is None:
from_timestamp = time_now - default_timedelta
to_timestamp = time_now
elif from_timestamp is not None and to_timestamp is None:
from_timestamp = datetime.fromisoformat(from_timestamp).replace(tzinfo=None)
from_timestamp = datetime.fromisoformat(from_timestamp).astimezone()
to_timestamp = from_timestamp + default_timedelta
elif from_timestamp is None and to_timestamp is not None:
to_timestamp = datetime.fromisoformat(to_timestamp).replace(tzinfo=None)
to_timestamp = datetime.fromisoformat(to_timestamp).astimezone()
from_timestamp = to_timestamp - default_timedelta
else:
from_timestamp = datetime.fromisoformat(from_timestamp).replace(tzinfo=None)
to_timestamp = datetime.fromisoformat(to_timestamp).replace(tzinfo=None)
from_timestamp = datetime.fromisoformat(from_timestamp).astimezone()
to_timestamp = datetime.fromisoformat(to_timestamp).astimezone()
if from_timestamp >= to_timestamp:
print("from_timestamp should be earlier than to_timestamp")
exit(1)
Expand Down Expand Up @@ -259,7 +259,7 @@ def get_deployment_log(args):
print("Please provide an api key")
exit(1)

to_timestamp = datetime.now()
to_timestamp = datetime.now().astimezone() # local timezone
from_timestamp = to_timestamp - timedelta(seconds=args.duration)
last_log_timestamp = from_timestamp
log_ids = set() # to avoid duplicate logs
Expand All @@ -273,7 +273,7 @@ def get_deployment_log(args):
exit(status_code)

for log in msg[::-1]: # logs are sorted by reversed timestamp
log_timestamp = datetime.fromisoformat(log["timestamp"]).replace(tzinfo=None)
log_timestamp = datetime.fromisoformat(log["timestamp"]).astimezone() # local timezone
if (log["insert_id"] in log_ids) or (log_timestamp < last_log_timestamp):
continue
log_ids.add(log["insert_id"])
Expand All @@ -285,5 +285,5 @@ def get_deployment_log(args):

time.sleep(10)
from_timestamp = last_log_timestamp
to_timestamp = datetime.now()
to_timestamp = datetime.now().astimezone() # local timezone
max_entries = 300 # only set max_entries for the first request

0 comments on commit 7fca4bb

Please sign in to comment.