From 81f155df4864d9b7caad0e511bb83347944f132d Mon Sep 17 00:00:00 2001 From: PacificDou Date: Mon, 18 Nov 2024 11:23:46 +0000 Subject: [PATCH 1/4] use local timezone --- roboflow/adapters/deploymentapi.py | 11 ++++++----- roboflow/deployment.py | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/roboflow/adapters/deploymentapi.py b/roboflow/adapters/deploymentapi.py index 1fef3745..98e9347f 100644 --- a/roboflow/adapters/deploymentapi.py +++ b/roboflow/adapters/deploymentapi.py @@ -1,5 +1,5 @@ import requests - +import urllib from roboflow.config import DEDICATED_DEPLOYMENT_URL @@ -58,13 +58,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 diff --git a/roboflow/deployment.py b/roboflow/deployment.py index 97c77ffd..d8f75231 100644 --- a/roboflow/deployment.py +++ b/roboflow/deployment.py @@ -169,7 +169,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 @@ -183,7 +183,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"]) @@ -195,5 +195,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 From 3c39c8bbe1029f2b670efd8b8d705ea51303bdbe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 18 Nov 2024 11:26:26 +0000 Subject: [PATCH 2/4] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20?= =?UTF-8?q?format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- roboflow/adapters/deploymentapi.py | 12 +++++++----- roboflow/deployment.py | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/roboflow/adapters/deploymentapi.py b/roboflow/adapters/deploymentapi.py index 98e9347f..a6420d9d 100644 --- a/roboflow/adapters/deploymentapi.py +++ b/roboflow/adapters/deploymentapi.py @@ -1,5 +1,7 @@ -import requests import urllib + +import requests + from roboflow.config import DEDICATED_DEPLOYMENT_URL @@ -58,13 +60,13 @@ def list_machine_types(api_key): def get_deployment_log(api_key, deployment_name, from_timestamp=None, to_timestamp=None, max_entries=-1): - params = {'api_key': api_key, 'deployment_name': deployment_name} + params = {"api_key": api_key, "deployment_name": deployment_name} if from_timestamp is not None: - params['from_timestamp'] = from_timestamp.isoformat() # may contain + sign + params["from_timestamp"] = from_timestamp.isoformat() # may contain + sign if to_timestamp is not None: - params['to_timestamp'] = to_timestamp.isoformat() # may contain + sign + params["to_timestamp"] = to_timestamp.isoformat() # may contain + sign if max_entries > 0: - params['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: diff --git a/roboflow/deployment.py b/roboflow/deployment.py index d8f75231..08530bc2 100644 --- a/roboflow/deployment.py +++ b/roboflow/deployment.py @@ -169,7 +169,7 @@ def get_deployment_log(args): print("Please provide an api key") exit(1) - to_timestamp = datetime.now().astimezone() # local timezone + 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 @@ -195,5 +195,5 @@ def get_deployment_log(args): time.sleep(10) from_timestamp = last_log_timestamp - to_timestamp = datetime.now().astimezone() # local timezone + to_timestamp = datetime.now().astimezone() # local timezone max_entries = 300 # only set max_entries for the first request From 3eee5b3f6954895bab19605442d879fee430dae2 Mon Sep 17 00:00:00 2001 From: PacificDou Date: Tue, 26 Nov 2024 11:03:30 +0000 Subject: [PATCH 3/4] set local timezone --- roboflow/adapters/deploymentapi.py | 14 ++++++++++++-- roboflow/deployment.py | 10 +++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/roboflow/adapters/deploymentapi.py b/roboflow/adapters/deploymentapi.py index 7735e02f..a63b0643 100644 --- a/roboflow/adapters/deploymentapi.py +++ b/roboflow/adapters/deploymentapi.py @@ -44,7 +44,12 @@ 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 @@ -52,7 +57,12 @@ def get_workspace_usage(api_key, from_timestamp, to_timestamp): 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 diff --git a/roboflow/deployment.py b/roboflow/deployment.py index 1bb4b831..61d9454e 100644 --- a/roboflow/deployment.py +++ b/roboflow/deployment.py @@ -23,19 +23,19 @@ def check_from_to_timestamp(from_timestamp, to_timestamp, default_timedelta): print("Please provide a valid to_timestamp in ISO8601 format") 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) From 54412a8803b24906593c589025b30aec4d595d03 Mon Sep 17 00:00:00 2001 From: PacificDou Date: Thu, 28 Nov 2024 16:55:04 +0000 Subject: [PATCH 4/4] add example of timestamp --- roboflow/deployment.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roboflow/deployment.py b/roboflow/deployment.py index 61d9454e..84fa792e 100644 --- a/roboflow/deployment.py +++ b/roboflow/deployment.py @@ -16,11 +16,11 @@ 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().astimezone() # local timezone