Skip to content

Commit

Permalink
New search structure
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptobench committed Apr 27, 2024
1 parent cf16365 commit 2f77e9c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
22 changes: 12 additions & 10 deletions stats-backend/api2/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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))
25 changes: 24 additions & 1 deletion stats-backend/api2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 2f77e9c

Please sign in to comment.