Skip to content

Commit

Permalink
Update views.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptobench committed Mar 27, 2024
1 parent be8e671 commit 81348bb
Showing 1 changed file with 60 additions and 5 deletions.
65 changes: 60 additions & 5 deletions stats-backend/api2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 81348bb

Please sign in to comment.