From 2a1ad083b293f957750d738d89b9e3308db98492 Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Thu, 5 Oct 2023 16:48:29 +0200 Subject: [PATCH 01/17] Add new error --- rest/openeoerrors.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rest/openeoerrors.py b/rest/openeoerrors.py index 5117f689..2a525e6b 100644 --- a/rest/openeoerrors.py +++ b/rest/openeoerrors.py @@ -153,3 +153,9 @@ def __init__(self, width, height) -> None: error_code = "ImageDimensionInvalid" http_code = 400 + + +class InsufficientCredits(SHOpenEOError): + error_code = "InsufficientCredits" + http_code = 402 + message = "You do not have sufficient credits to perform this request. Please visit https://portal.terrascope.be/pages/pricing to find more information on how to buy additional credits." \ No newline at end of file From 00e6b70254f8a64450433cb8e01810a2eb262c18 Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Thu, 5 Oct 2023 16:50:20 +0200 Subject: [PATCH 02/17] Create function for getting credits from reporting service --- rest/authentication/user.py | 6 ++++++ rest/usage_reporting/report_usage.py | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/rest/authentication/user.py b/rest/authentication/user.py index 410e1570..fdd577d5 100644 --- a/rest/authentication/user.py +++ b/rest/authentication/user.py @@ -24,6 +24,9 @@ def get_user_info(self): if self.default_plan: user_info["default_plan"] = self.default_plan.name return user_info + + def get_leftover_credits(self): + pass def report_usage(self, pu_spent, job_id=None): pass @@ -59,6 +62,9 @@ def get_user_info(self): user_info = super().get_user_info() user_info["info"] = {"oidc_userinfo": self.oidc_userinfo} return user_info + + def get_leftover_credits(self): + return usageReporting.get_leftover_credits() def report_usage(self, pu_spent, job_id=None): usageReporting.report_usage(self.user_id, pu_spent, job_id) diff --git a/rest/usage_reporting/report_usage.py b/rest/usage_reporting/report_usage.py index b86bb0e7..d9710d3f 100644 --- a/rest/usage_reporting/report_usage.py +++ b/rest/usage_reporting/report_usage.py @@ -57,6 +57,18 @@ def reporting_check_health(self): content = r.json() return r.status_code == 200 and content["status"] == "ok" + + def get_leftover_credits(self): + user_url = f"{self.base_url}user" + reporting_token = self.get_token() + + headers = {"Authorization": f"Bearer {reporting_token['access_token']}"} + + r = requests.get(user_url, headers=headers) + content = r.json() + credits = content["credits"] + + return credits def report_usage(self, user_id, pu_spent, job_id=None, max_tries=5): reporting_token = self.get_token() From 05a37554cdfbb91bdbc7ad4a104dd053410809b7 Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Thu, 5 Oct 2023 16:59:36 +0200 Subject: [PATCH 03/17] Improve function for fetching credits --- rest/usage_reporting/report_usage.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/rest/usage_reporting/report_usage.py b/rest/usage_reporting/report_usage.py index d9710d3f..f8200f1d 100644 --- a/rest/usage_reporting/report_usage.py +++ b/rest/usage_reporting/report_usage.py @@ -58,17 +58,30 @@ def reporting_check_health(self): return r.status_code == 200 and content["status"] == "ok" - def get_leftover_credits(self): + def get_leftover_credits(self, max_tries=5): user_url = f"{self.base_url}user" reporting_token = self.get_token() headers = {"Authorization": f"Bearer {reporting_token['access_token']}"} - r = requests.get(user_url, headers=headers) - content = r.json() - credits = content["credits"] + if not self.reporting_check_health(): + log(ERROR, "Services for usage reporting are not healthy") + raise Internal("Services for usage reporting are not healthy") + - return credits + for try_number in range(max_tries): + r = requests.get(user_url, headers=headers) + + if r.status_code == 200: + content = r.json() + credits = content["credits"] + + return credits + else: + log(ERROR, f"Error fetching leftover credits on try #{try_number+1}: {r.status_code} {r.text}") + raise Internal(f"Problems during fetching leftover credits on try #{try_number+1}: {r.status_code} {r.text}") + + raise Internal(f"Out of retries. Fetching leftover credits failed: {r.status_code} {r.text}") def report_usage(self, user_id, pu_spent, job_id=None, max_tries=5): reporting_token = self.get_token() From 9e7b924a783334fbb5981e5bc01e4624ec27882c Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Thu, 5 Oct 2023 17:08:35 +0200 Subject: [PATCH 04/17] Prepare part of code --- rest/processing/processing.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rest/processing/processing.py b/rest/processing/processing.py index 5a2aa096..e9d3dfb5 100644 --- a/rest/processing/processing.py +++ b/rest/processing/processing.py @@ -54,6 +54,10 @@ def create_batch_job(process): def start_new_batch_job(sentinel_hub, process, job_id): + # add check here (after merge MR !318) and raise error if needed + # leftover_credits = g.user.get_leftover_credits() + # if leftover_credits < estimated_pu: + # raise InsufficientCredits() new_batch_request_id, deployment_endpoint = create_batch_job(process) estimated_pu, _ = get_batch_job_estimate(new_batch_request_id, process, deployment_endpoint) sentinel_hub.start_batch_job(new_batch_request_id) @@ -85,6 +89,10 @@ def start_batch_job(batch_request_id, process, deployment_endpoint, job_id): if batch_request_info is None: return start_new_batch_job(sentinel_hub, process, job_id) elif batch_request_info.status in [BatchRequestStatus.CREATED, BatchRequestStatus.ANALYSIS_DONE]: + # add check here (after merge MR !318) and raise error if needed + # leftover_credits = g.user.get_leftover_credits() + # if leftover_credits < estimated_pu: + # raise InsufficientCredits() estimated_pu, _ = get_batch_job_estimate(batch_request_id, process, deployment_endpoint) sentinel_hub.start_batch_job(batch_request_id) g.user.report_usage(estimated_pu, job_id) From a977a4e99cb211db49aba712ea52ffd851144a00 Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Thu, 5 Oct 2023 17:16:14 +0200 Subject: [PATCH 05/17] run linting --- rest/authentication/user.py | 4 ++-- rest/openeoerrors.py | 2 +- rest/processing/processing.py | 4 ++-- rest/usage_reporting/report_usage.py | 9 +++++---- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/rest/authentication/user.py b/rest/authentication/user.py index fdd577d5..2b6e300d 100644 --- a/rest/authentication/user.py +++ b/rest/authentication/user.py @@ -24,7 +24,7 @@ def get_user_info(self): if self.default_plan: user_info["default_plan"] = self.default_plan.name return user_info - + def get_leftover_credits(self): pass @@ -62,7 +62,7 @@ def get_user_info(self): user_info = super().get_user_info() user_info["info"] = {"oidc_userinfo": self.oidc_userinfo} return user_info - + def get_leftover_credits(self): return usageReporting.get_leftover_credits() diff --git a/rest/openeoerrors.py b/rest/openeoerrors.py index 2a525e6b..e04b74af 100644 --- a/rest/openeoerrors.py +++ b/rest/openeoerrors.py @@ -158,4 +158,4 @@ def __init__(self, width, height) -> None: class InsufficientCredits(SHOpenEOError): error_code = "InsufficientCredits" http_code = 402 - message = "You do not have sufficient credits to perform this request. Please visit https://portal.terrascope.be/pages/pricing to find more information on how to buy additional credits." \ No newline at end of file + message = "You do not have sufficient credits to perform this request. Please visit https://portal.terrascope.be/pages/pricing to find more information on how to buy additional credits." diff --git a/rest/processing/processing.py b/rest/processing/processing.py index e9d3dfb5..29c229b8 100644 --- a/rest/processing/processing.py +++ b/rest/processing/processing.py @@ -57,7 +57,7 @@ def start_new_batch_job(sentinel_hub, process, job_id): # add check here (after merge MR !318) and raise error if needed # leftover_credits = g.user.get_leftover_credits() # if leftover_credits < estimated_pu: - # raise InsufficientCredits() + # raise InsufficientCredits() new_batch_request_id, deployment_endpoint = create_batch_job(process) estimated_pu, _ = get_batch_job_estimate(new_batch_request_id, process, deployment_endpoint) sentinel_hub.start_batch_job(new_batch_request_id) @@ -92,7 +92,7 @@ def start_batch_job(batch_request_id, process, deployment_endpoint, job_id): # add check here (after merge MR !318) and raise error if needed # leftover_credits = g.user.get_leftover_credits() # if leftover_credits < estimated_pu: - # raise InsufficientCredits() + # raise InsufficientCredits() estimated_pu, _ = get_batch_job_estimate(batch_request_id, process, deployment_endpoint) sentinel_hub.start_batch_job(batch_request_id) g.user.report_usage(estimated_pu, job_id) diff --git a/rest/usage_reporting/report_usage.py b/rest/usage_reporting/report_usage.py index f8200f1d..1aaea4de 100644 --- a/rest/usage_reporting/report_usage.py +++ b/rest/usage_reporting/report_usage.py @@ -57,7 +57,7 @@ def reporting_check_health(self): content = r.json() return r.status_code == 200 and content["status"] == "ok" - + def get_leftover_credits(self, max_tries=5): user_url = f"{self.base_url}user" reporting_token = self.get_token() @@ -68,7 +68,6 @@ def get_leftover_credits(self, max_tries=5): log(ERROR, "Services for usage reporting are not healthy") raise Internal("Services for usage reporting are not healthy") - for try_number in range(max_tries): r = requests.get(user_url, headers=headers) @@ -79,8 +78,10 @@ def get_leftover_credits(self, max_tries=5): return credits else: log(ERROR, f"Error fetching leftover credits on try #{try_number+1}: {r.status_code} {r.text}") - raise Internal(f"Problems during fetching leftover credits on try #{try_number+1}: {r.status_code} {r.text}") - + raise Internal( + f"Problems during fetching leftover credits on try #{try_number+1}: {r.status_code} {r.text}" + ) + raise Internal(f"Out of retries. Fetching leftover credits failed: {r.status_code} {r.text}") def report_usage(self, user_id, pu_spent, job_id=None, max_tries=5): From 1308a23158b7ec442324a97375862fa16c056758 Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Mon, 9 Oct 2023 10:42:49 +0200 Subject: [PATCH 06/17] Add check and raise error if not enough credits --- rest/processing/processing.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/rest/processing/processing.py b/rest/processing/processing.py index 29c229b8..07bb8795 100644 --- a/rest/processing/processing.py +++ b/rest/processing/processing.py @@ -10,7 +10,7 @@ from processing.partially_supported_processes import partially_supported_processes from dynamodb.utils import get_user_defined_processes_graphs from const import openEOBatchJobStatus -from openeoerrors import Timeout +from openeoerrors import InsufficientCredits, Timeout def check_process_graph_conversion_validity(process_graph): @@ -54,12 +54,13 @@ def create_batch_job(process): def start_new_batch_job(sentinel_hub, process, job_id): - # add check here (after merge MR !318) and raise error if needed - # leftover_credits = g.user.get_leftover_credits() - # if leftover_credits < estimated_pu: - # raise InsufficientCredits() new_batch_request_id, deployment_endpoint = create_batch_job(process) estimated_pu, _ = get_batch_job_estimate(new_batch_request_id, process, deployment_endpoint) + + leftover_credits = g.user.get_leftover_credits() + if leftover_credits < estimated_pu: + raise InsufficientCredits() + sentinel_hub.start_batch_job(new_batch_request_id) g.user.report_usage(estimated_pu, job_id) return new_batch_request_id @@ -89,11 +90,12 @@ def start_batch_job(batch_request_id, process, deployment_endpoint, job_id): if batch_request_info is None: return start_new_batch_job(sentinel_hub, process, job_id) elif batch_request_info.status in [BatchRequestStatus.CREATED, BatchRequestStatus.ANALYSIS_DONE]: - # add check here (after merge MR !318) and raise error if needed - # leftover_credits = g.user.get_leftover_credits() - # if leftover_credits < estimated_pu: - # raise InsufficientCredits() estimated_pu, _ = get_batch_job_estimate(batch_request_id, process, deployment_endpoint) + + leftover_credits = g.user.get_leftover_credits() + if leftover_credits < estimated_pu: + raise InsufficientCredits() + sentinel_hub.start_batch_job(batch_request_id) g.user.report_usage(estimated_pu, job_id) elif batch_request_info.status == BatchRequestStatus.PARTIAL: From 142d8d189a395083b2a79c2b756f4aa34c8542c4 Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Wed, 11 Oct 2023 14:06:04 +0200 Subject: [PATCH 07/17] add missing import --- rest/authentication/authentication.py | 1 + 1 file changed, 1 insertion(+) diff --git a/rest/authentication/authentication.py b/rest/authentication/authentication.py index e4f40d90..a0d69fe1 100644 --- a/rest/authentication/authentication.py +++ b/rest/authentication/authentication.py @@ -15,6 +15,7 @@ Internal, CredentialsInvalid, BillingPlanInvalid, + TokenInvalid, ) from authentication.oidc_providers import oidc_providers from authentication.user import OIDCUser, SHUser From 7729e0892ba9758f5d8a6eef07e13627e2dcc2eb Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Wed, 11 Oct 2023 14:06:20 +0200 Subject: [PATCH 08/17] add header for content type --- rest/usage_reporting/report_usage.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest/usage_reporting/report_usage.py b/rest/usage_reporting/report_usage.py index 1aaea4de..d3739abe 100644 --- a/rest/usage_reporting/report_usage.py +++ b/rest/usage_reporting/report_usage.py @@ -62,7 +62,7 @@ def get_leftover_credits(self, max_tries=5): user_url = f"{self.base_url}user" reporting_token = self.get_token() - headers = {"Authorization": f"Bearer {reporting_token['access_token']}"} + headers = {"content-type": "application/json", "Authorization": f"Bearer {reporting_token['access_token']}"} if not self.reporting_check_health(): log(ERROR, "Services for usage reporting are not healthy") @@ -73,7 +73,7 @@ def get_leftover_credits(self, max_tries=5): if r.status_code == 200: content = r.json() - credits = content["credits"] + credits = content.get("credits") return credits else: From e103825c53faf293c903b622f49d5d4bf88bacf8 Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Fri, 13 Oct 2023 14:41:59 +0200 Subject: [PATCH 09/17] only try once to get credits --- rest/usage_reporting/report_usage.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/rest/usage_reporting/report_usage.py b/rest/usage_reporting/report_usage.py index d3739abe..017d3735 100644 --- a/rest/usage_reporting/report_usage.py +++ b/rest/usage_reporting/report_usage.py @@ -58,7 +58,7 @@ def reporting_check_health(self): return r.status_code == 200 and content["status"] == "ok" - def get_leftover_credits(self, max_tries=5): + def get_leftover_credits(self): user_url = f"{self.base_url}user" reporting_token = self.get_token() @@ -68,21 +68,19 @@ def get_leftover_credits(self, max_tries=5): log(ERROR, "Services for usage reporting are not healthy") raise Internal("Services for usage reporting are not healthy") - for try_number in range(max_tries): - r = requests.get(user_url, headers=headers) + r = requests.get(user_url, headers=headers) - if r.status_code == 200: - content = r.json() - credits = content.get("credits") + if r.status_code == 200: + content = r.json() + credits = content.get("credits") - return credits - else: - log(ERROR, f"Error fetching leftover credits on try #{try_number+1}: {r.status_code} {r.text}") - raise Internal( - f"Problems during fetching leftover credits on try #{try_number+1}: {r.status_code} {r.text}" + return credits + else: + log(ERROR, f"Error fetching leftover credits: {r.status_code} {r.text}") + raise Internal( + f"Problems during fetching leftover credits: {r.status_code} {r.text}" ) - raise Internal(f"Out of retries. Fetching leftover credits failed: {r.status_code} {r.text}") def report_usage(self, user_id, pu_spent, job_id=None, max_tries=5): reporting_token = self.get_token() From 854b564b9d9a3449a7dbc9a8881c7895c7b5ba11 Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Thu, 19 Oct 2023 10:00:27 +0200 Subject: [PATCH 10/17] add vito access token to oidc user for fetching leftover credits --- rest/authentication/authentication.py | 2 +- rest/authentication/user.py | 5 +++-- rest/usage_reporting/report_usage.py | 5 ++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rest/authentication/authentication.py b/rest/authentication/authentication.py index a0d69fe1..c9f75c97 100644 --- a/rest/authentication/authentication.py +++ b/rest/authentication/authentication.py @@ -61,7 +61,7 @@ def authenticate_user_oidc(self, access_token, oidc_provider_id): user_id = userinfo["sub"] try: - user = OIDCUser(user_id, oidc_userinfo=userinfo) + user = OIDCUser(user_id, oidc_userinfo=userinfo, access_token=access_token) except BillingPlanInvalid: return None diff --git a/rest/authentication/user.py b/rest/authentication/user.py index 2b6e300d..95faebc4 100644 --- a/rest/authentication/user.py +++ b/rest/authentication/user.py @@ -33,7 +33,7 @@ def report_usage(self, pu_spent, job_id=None): class OIDCUser(User): - def __init__(self, user_id=None, oidc_userinfo={}): + def __init__(self, user_id=None, oidc_userinfo={}, access_token=None): super().__init__(user_id) self.entitlements = [ self.convert_entitlement(entitlement) for entitlement in oidc_userinfo.get("eduperson_entitlement", []) @@ -41,6 +41,7 @@ def __init__(self, user_id=None, oidc_userinfo={}): self.oidc_userinfo = oidc_userinfo self.default_plan = OpenEOPBillingPlan.get_billing_plan(self.entitlements) self.session = central_user_sentinelhub_session + self.access_token = access_token def __str__(self): return f"{self.__class__.__name__}: {self.user_id}" @@ -64,7 +65,7 @@ def get_user_info(self): return user_info def get_leftover_credits(self): - return usageReporting.get_leftover_credits() + return usageReporting.get_leftover_credits_for_user(self.access_token) def report_usage(self, pu_spent, job_id=None): usageReporting.report_usage(self.user_id, pu_spent, job_id) diff --git a/rest/usage_reporting/report_usage.py b/rest/usage_reporting/report_usage.py index 017d3735..912405f5 100644 --- a/rest/usage_reporting/report_usage.py +++ b/rest/usage_reporting/report_usage.py @@ -58,11 +58,10 @@ def reporting_check_health(self): return r.status_code == 200 and content["status"] == "ok" - def get_leftover_credits(self): + def get_leftover_credits_for_user(self, user_access_token): user_url = f"{self.base_url}user" - reporting_token = self.get_token() - headers = {"content-type": "application/json", "Authorization": f"Bearer {reporting_token['access_token']}"} + headers = {"content-type": "application/json", "Authorization": f"Bearer {user_access_token}"} if not self.reporting_check_health(): log(ERROR, "Services for usage reporting are not healthy") From 871942eb801b04c5be5950f48721069191b4c848 Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Thu, 19 Oct 2023 10:23:08 +0200 Subject: [PATCH 11/17] run linting --- rest/usage_reporting/report_usage.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/rest/usage_reporting/report_usage.py b/rest/usage_reporting/report_usage.py index 912405f5..977d836a 100644 --- a/rest/usage_reporting/report_usage.py +++ b/rest/usage_reporting/report_usage.py @@ -76,10 +76,7 @@ def get_leftover_credits_for_user(self, user_access_token): return credits else: log(ERROR, f"Error fetching leftover credits: {r.status_code} {r.text}") - raise Internal( - f"Problems during fetching leftover credits: {r.status_code} {r.text}" - ) - + raise Internal(f"Problems during fetching leftover credits: {r.status_code} {r.text}") def report_usage(self, user_id, pu_spent, job_id=None, max_tries=5): reporting_token = self.get_token() From ffc5219e5c83f9c13bf82696763fa1ac7f5eeb17 Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Thu, 19 Oct 2023 11:47:26 +0200 Subject: [PATCH 12/17] Add additional check if leftover_Credits even exist since SH user don't have it --- rest/processing/processing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest/processing/processing.py b/rest/processing/processing.py index 07bb8795..ffc1df5c 100644 --- a/rest/processing/processing.py +++ b/rest/processing/processing.py @@ -58,7 +58,7 @@ def start_new_batch_job(sentinel_hub, process, job_id): estimated_pu, _ = get_batch_job_estimate(new_batch_request_id, process, deployment_endpoint) leftover_credits = g.user.get_leftover_credits() - if leftover_credits < estimated_pu: + if leftover_credits is not None and leftover_credits < estimated_pu: raise InsufficientCredits() sentinel_hub.start_batch_job(new_batch_request_id) @@ -93,7 +93,7 @@ def start_batch_job(batch_request_id, process, deployment_endpoint, job_id): estimated_pu, _ = get_batch_job_estimate(batch_request_id, process, deployment_endpoint) leftover_credits = g.user.get_leftover_credits() - if leftover_credits < estimated_pu: + if leftover_credits is not None and leftover_credits < estimated_pu: raise InsufficientCredits() sentinel_hub.start_batch_job(batch_request_id) From c4d4fb7c003f36832581413de0a1a39bab7c39c9 Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Thu, 19 Oct 2023 11:48:35 +0200 Subject: [PATCH 13/17] convert from SH PU's to platform credits before performing check if there is enough resources --- rest/processing/processing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest/processing/processing.py b/rest/processing/processing.py index ffc1df5c..cf38b66a 100644 --- a/rest/processing/processing.py +++ b/rest/processing/processing.py @@ -58,7 +58,7 @@ def start_new_batch_job(sentinel_hub, process, job_id): estimated_pu, _ = get_batch_job_estimate(new_batch_request_id, process, deployment_endpoint) leftover_credits = g.user.get_leftover_credits() - if leftover_credits is not None and leftover_credits < estimated_pu: + if leftover_credits is not None and leftover_credits < estimated_pu * 0.15: raise InsufficientCredits() sentinel_hub.start_batch_job(new_batch_request_id) @@ -93,7 +93,7 @@ def start_batch_job(batch_request_id, process, deployment_endpoint, job_id): estimated_pu, _ = get_batch_job_estimate(batch_request_id, process, deployment_endpoint) leftover_credits = g.user.get_leftover_credits() - if leftover_credits is not None and leftover_credits < estimated_pu: + if leftover_credits is not None and leftover_credits < estimated_pu * 0.15: raise InsufficientCredits() sentinel_hub.start_batch_job(batch_request_id) From f7aefa51860cd982f1b2bc4c8e25e8688d0643e6 Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Tue, 24 Oct 2023 10:19:21 +0200 Subject: [PATCH 14/17] run linting --- rest/processing/processing.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rest/processing/processing.py b/rest/processing/processing.py index 26a0d850..2ad9e2bb 100644 --- a/rest/processing/processing.py +++ b/rest/processing/processing.py @@ -63,11 +63,11 @@ def start_new_batch_job(sentinel_hub, process, job_id): raise JobNotFound() estimated_sentinelhub_pu, _, _ = create_or_get_estimate_values_from_db(job, new_batch_request_id) - + leftover_credits = g.user.get_leftover_credits() if leftover_credits is not None and leftover_credits < estimated_sentinelhub_pu * 0.15: raise InsufficientCredits() - + JobsPersistence.update_key( job["id"], "sum_costs", str(round(float(job.get("sum_costs", 0)) + estimated_sentinelhub_pu, 3)) ) @@ -105,7 +105,7 @@ def start_batch_job(batch_request_id, process, deployment_endpoint, job_id): raise JobNotFound() estimated_sentinelhub_pu, _, _ = create_or_get_estimate_values_from_db(job, job["batch_request_id"]) - + leftover_credits = g.user.get_leftover_credits() if leftover_credits is not None and leftover_credits < estimated_sentinelhub_pu * 0.15: raise InsufficientCredits() From bf4a67edf6b07bcc8add41d8ffb6022173c086ce Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Tue, 24 Oct 2023 14:59:38 +0200 Subject: [PATCH 15/17] extract duplicate code to function --- rest/processing/processing.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/rest/processing/processing.py b/rest/processing/processing.py index 2ad9e2bb..ae5c7327 100644 --- a/rest/processing/processing.py +++ b/rest/processing/processing.py @@ -64,9 +64,7 @@ def start_new_batch_job(sentinel_hub, process, job_id): estimated_sentinelhub_pu, _, _ = create_or_get_estimate_values_from_db(job, new_batch_request_id) - leftover_credits = g.user.get_leftover_credits() - if leftover_credits is not None and leftover_credits < estimated_sentinelhub_pu * 0.15: - raise InsufficientCredits() + check_leftover_credits(estimated_sentinelhub_pu) JobsPersistence.update_key( job["id"], "sum_costs", str(round(float(job.get("sum_costs", 0)) + estimated_sentinelhub_pu, 3)) @@ -106,9 +104,7 @@ def start_batch_job(batch_request_id, process, deployment_endpoint, job_id): estimated_sentinelhub_pu, _, _ = create_or_get_estimate_values_from_db(job, job["batch_request_id"]) - leftover_credits = g.user.get_leftover_credits() - if leftover_credits is not None and leftover_credits < estimated_sentinelhub_pu * 0.15: - raise InsufficientCredits() + check_leftover_credits(estimated_sentinelhub_pu) JobsPersistence.update_key( job["id"], "sum_costs", str(round(float(job.get("sum_costs", 0)) + estimated_sentinelhub_pu, 3)) @@ -232,3 +228,10 @@ def create_or_get_estimate_values_from_db(job, batch_request_id): estimated_file_size = float(job.get("estimated_file_size", 0)) return estimated_sentinelhub_pu, estimated_platform_credits, estimated_file_size + + +def check_leftover_credits(estimated_pu): + leftover_credits = g.user.get_leftover_credits() + estimated_pu_as_credits = estimated_pu * 0.15 # platform credits === SH PU's * 0.15 + if leftover_credits is not None and leftover_credits < estimated_pu_as_credits: + raise InsufficientCredits() \ No newline at end of file From 2e74c7cfbf20a540ea106d13f613052e9eff68cc Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Tue, 24 Oct 2023 15:01:56 +0200 Subject: [PATCH 16/17] rename credits to not use python's built-in constant for credits --- rest/usage_reporting/report_usage.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest/usage_reporting/report_usage.py b/rest/usage_reporting/report_usage.py index 6160a66a..e85cbf7f 100644 --- a/rest/usage_reporting/report_usage.py +++ b/rest/usage_reporting/report_usage.py @@ -87,9 +87,9 @@ def get_leftover_credits_for_user(self, user_access_token): if r.status_code == 200: content = r.json() - credits = content.get("credits") + platform_credits = content.get("credits") - return credits + return platform_credits else: log(ERROR, f"Error fetching leftover credits: {r.status_code} {r.text}") raise Internal(f"Problems during fetching leftover credits: {r.status_code} {r.text}") From 189521daca98da04ac8b05be48ebdb4fd61dead1 Mon Sep 17 00:00:00 2001 From: Zan Pecovnik Date: Tue, 24 Oct 2023 15:06:32 +0200 Subject: [PATCH 17/17] run linting --- rest/processing/processing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest/processing/processing.py b/rest/processing/processing.py index ae5c7327..3a49e136 100644 --- a/rest/processing/processing.py +++ b/rest/processing/processing.py @@ -232,6 +232,6 @@ def create_or_get_estimate_values_from_db(job, batch_request_id): def check_leftover_credits(estimated_pu): leftover_credits = g.user.get_leftover_credits() - estimated_pu_as_credits = estimated_pu * 0.15 # platform credits === SH PU's * 0.15 + estimated_pu_as_credits = estimated_pu * 0.15 # platform credits === SH PU's * 0.15 if leftover_credits is not None and leftover_credits < estimated_pu_as_credits: - raise InsufficientCredits() \ No newline at end of file + raise InsufficientCredits()