From 81348bb61a0f05fd69b446df866190826c07c344 Mon Sep 17 00:00:00 2001 From: Phillip Jensen Date: Wed, 27 Mar 2024 09:39:51 +0100 Subject: [PATCH] Update views.py --- stats-backend/api2/views.py | 65 ++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/stats-backend/api2/views.py b/stats-backend/api2/views.py index 0d786ac..3b7b510 100644 --- a/stats-backend/api2/views.py +++ b/stats-backend/api2/views.py @@ -297,20 +297,75 @@ async def golem_main_website_index(request): return HttpResponse(status=400) -async def node_wallet(request, wallet): +def node_wallet(request, wallet): """ - Returns all the nodes with the specified wallet address. + Returns all the nodes with the specified wallet address, including blacklist and taskReputation. """ if request.method == "GET": + # Fetch reputation data from the external domain + reputation_response = requests.get( + "https://reputation.dev-test.golem.network/v2/providers/scores" + ) + success_rate_mapping = {} + blacklist_provider_mapping = {} + blacklist_operator_mapping = {} + + if reputation_response.status_code == 200: + external_data = reputation_response.json() + + success_rate_mapping = { + provider["provider"]["id"]: provider["scores"]["successRate"] + for provider in external_data["testedProviders"] + } + + blacklist_provider_mapping = { + provider["provider"]["id"]: provider["reason"] + for provider in external_data["rejectedProviders"] + } + + blacklist_operator_mapping = { + operator["operator"]["walletAddress"]: operator["reason"] + for operator in external_data["rejectedOperators"] + } + + # Fetch nodes with the specified wallet address data = Node.objects.filter(wallet=wallet) - if data != None: + if data.exists(): serializer = NodeSerializer(data, many=True) + serialized_data = serializer.data + + # Attach successRate and blacklist status to each node + for node in serialized_data: + node_id = node["node_id"] + node_wallet = node.get("wallet") # Assuming 'wallet' attribute exists + + node["reputation"] = { + "blacklisted": False, + "blacklistedReason": None, + "taskReputation": None, + } + + if node_id in blacklist_provider_mapping: + node["reputation"]["blacklisted"] = True + node["reputation"]["blacklistedReason"] = ( + blacklist_provider_mapping[node_id] + ) + elif node_wallet in blacklist_operator_mapping: + node["reputation"]["blacklisted"] = True + node["reputation"]["blacklistedReason"] = ( + blacklist_operator_mapping[node_wallet] + ) + + if node_id in success_rate_mapping: + node["reputation"]["taskReputation"] = ( + success_rate_mapping[node_id] * 100 + ) + return JsonResponse( - serializer.data, safe=False, json_dumps_params={"indent": 4} + serialized_data, safe=False, json_dumps_params={"indent": 4} ) else: return HttpResponse(status=404) - else: return HttpResponse(status=400)