Skip to content

Commit 7fca4bb

Browse files
authored
Merge pull request #341 from roboflow/bugfix-dedicated-deployment-log
Use local timezone when query logs & usages for dedicated deployment
2 parents d369d43 + 54412a8 commit 7fca4bb

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

roboflow/adapters/deploymentapi.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import urllib
2+
13
import requests
24

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

4345

4446
def get_workspace_usage(api_key, from_timestamp, to_timestamp):
45-
url = f"{DEDICATED_DEPLOYMENT_URL}/usage_workspace?api_key={api_key}&from_timestamp={from_timestamp.isoformat()}&to_timestamp={to_timestamp.isoformat()}"
47+
params = {"api_key": api_key}
48+
if from_timestamp is not None:
49+
params["from_timestamp"] = from_timestamp.isoformat() # may contain + sign
50+
if to_timestamp is not None:
51+
params["to_timestamp"] = to_timestamp.isoformat() # may contain + sign
52+
url = f"{DEDICATED_DEPLOYMENT_URL}/usage_workspace?{urllib.parse.urlencode(params)}"
4653
response = requests.get(url)
4754
if response.status_code != 200:
4855
return response.status_code, response.text
4956
return response.status_code, response.json()
5057

5158

5259
def get_deployment_usage(api_key, deployment_name, from_timestamp, to_timestamp):
53-
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()}"
60+
params = {"api_key": api_key, "deployment_name": deployment_name}
61+
if from_timestamp is not None:
62+
params["from_timestamp"] = from_timestamp.isoformat() # may contain + sign
63+
if to_timestamp is not None:
64+
params["to_timestamp"] = to_timestamp.isoformat() # may contain + sign
65+
url = f"{DEDICATED_DEPLOYMENT_URL}/usage_deployment?{urllib.parse.urlencode(params)}"
5466
response = requests.get(url)
5567
if response.status_code != 200:
5668
return response.status_code, response.text
@@ -74,13 +86,14 @@ def list_machine_types(api_key):
7486

7587

7688
def get_deployment_log(api_key, deployment_name, from_timestamp=None, to_timestamp=None, max_entries=-1):
77-
url = f"{DEDICATED_DEPLOYMENT_URL}/get_log?api_key={api_key}&deployment_name={deployment_name}"
89+
params = {"api_key": api_key, "deployment_name": deployment_name}
7890
if from_timestamp is not None:
79-
url += f"&from_timestamp={from_timestamp.isoformat()}"
91+
params["from_timestamp"] = from_timestamp.isoformat() # may contain + sign
8092
if to_timestamp is not None:
81-
url += f"&to_timestamp={to_timestamp.isoformat()}"
93+
params["to_timestamp"] = to_timestamp.isoformat() # may contain + sign
8294
if max_entries > 0:
83-
url += f"&max_entries={max_entries}"
95+
params["max_entries"] = max_entries
96+
url = f"{DEDICATED_DEPLOYMENT_URL}/get_log?{urllib.parse.urlencode(params)}"
8497
response = requests.get(url)
8598
if response.status_code != 200:
8699
return response.status_code, response.text

roboflow/deployment.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@ def is_valid_ISO8601_timestamp(ts):
1616

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

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

26-
time_now = datetime.now().replace(tzinfo=None)
26+
time_now = datetime.now().astimezone() # local timezone
2727
if from_timestamp is None and to_timestamp is None:
2828
from_timestamp = time_now - default_timedelta
2929
to_timestamp = time_now
3030
elif from_timestamp is not None and to_timestamp is None:
31-
from_timestamp = datetime.fromisoformat(from_timestamp).replace(tzinfo=None)
31+
from_timestamp = datetime.fromisoformat(from_timestamp).astimezone()
3232
to_timestamp = from_timestamp + default_timedelta
3333
elif from_timestamp is None and to_timestamp is not None:
34-
to_timestamp = datetime.fromisoformat(to_timestamp).replace(tzinfo=None)
34+
to_timestamp = datetime.fromisoformat(to_timestamp).astimezone()
3535
from_timestamp = to_timestamp - default_timedelta
3636
else:
37-
from_timestamp = datetime.fromisoformat(from_timestamp).replace(tzinfo=None)
38-
to_timestamp = datetime.fromisoformat(to_timestamp).replace(tzinfo=None)
37+
from_timestamp = datetime.fromisoformat(from_timestamp).astimezone()
38+
to_timestamp = datetime.fromisoformat(to_timestamp).astimezone()
3939
if from_timestamp >= to_timestamp:
4040
print("from_timestamp should be earlier than to_timestamp")
4141
exit(1)
@@ -259,7 +259,7 @@ def get_deployment_log(args):
259259
print("Please provide an api key")
260260
exit(1)
261261

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

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

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

0 commit comments

Comments
 (0)