diff --git a/stats-backend/api2/tasks.py b/stats-backend/api2/tasks.py index 95709d1..d755b6b 100644 --- a/stats-backend/api2/tasks.py +++ b/stats-backend/api2/tasks.py @@ -1787,10 +1787,11 @@ def computing_total_over_time(): def extract_wallets_and_ids(): from itertools import chain from collections import defaultdict + import json from django.db.models import Q offers = Offer.objects.prefetch_related("provider").all() - wallets_list = [] + wallets_dict = defaultdict(set) providers_dict = defaultdict(list) for offer in offers: @@ -1799,26 +1800,27 @@ def extract_wallets_and_ids(): properties = offer.properties provider_id = properties.get("id", "") provider_name = properties.get("golem.node.id.name", "") - wallets = [ - v - for k, v in properties.items() - if k.startswith("golem.com.payment.platform") and v - ] - # Update wallets list - wallets_list.extend(wallets) + # Extract wallet addresses and associate with provider ID + for k, v in properties.items(): + if k.startswith("golem.com.payment.platform") and v: + wallets_dict[v].add(provider_id) # Update providers dictionary if provider_id and provider_name: providers_dict[(provider_id, provider_name)].append(offer.provider.node_id) - # Deduplicate wallets - wallets_list = list(set(wallets_list)) + # Convert wallet sets to counts of unique providers using each wallet + wallets_list = [ + {"address": wallet, "provider_count": len(providers)} + for wallet, providers in wallets_dict.items() + ] # Convert providers to list of dictionaries providers_list = [ {"provider_name": name, "id": _id} for (_id, name), nodes in providers_dict.items() ] + data = {"wallets": wallets_list, "providers": providers_list} r.set("wallets_and_ids", json.dumps(data)) diff --git a/stats-backend/api2/views.py b/stats-backend/api2/views.py index 94ad2df..d1d2d3e 100644 --- a/stats-backend/api2/views.py +++ b/stats-backend/api2/views.py @@ -835,15 +835,38 @@ async def computing_total_over_time(request): else: return HttpResponse(status=400) + +from django.http import JsonResponse, HttpResponse +import json +import aioredis + + async def wallets_and_ids(request): if request.method == "GET": + query = request.GET.get( + "query", "" + ).lower() # Get the query parameter from the request pool = aioredis.ConnectionPool.from_url( "redis://redis:6379/0", decode_responses=True ) r = aioredis.Redis(connection_pool=pool) content = await r.get("wallets_and_ids") data = json.loads(content) + + # Filter logic based on query + filtered_data = {"wallets": [], "providers": []} + for item in data.get("wallets", []): + if query in item.get("address", "").lower(): + filtered_data["wallets"].append(item) + + for item in data.get("providers", []): + if ( + query in item.get("provider_name", "").lower() + or query in str(item.get("id", "")).lower() + ): + filtered_data["providers"].append(item) + pool.disconnect() - return JsonResponse(data, safe=False, json_dumps_params={"indent": 4}) + return JsonResponse(filtered_data, safe=False, json_dumps_params={"indent": 4}) else: return HttpResponse(status=400)