Skip to content

Commit

Permalink
Fetch RC versions
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptobench committed Apr 11, 2024
1 parent 87b1dd3 commit 5603f09
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 15 deletions.
98 changes: 84 additions & 14 deletions stats-backend/collector/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,26 +455,97 @@ def network_versions_to_redis():
versions_nonsorted = []
versions = []
data = content[0]["data"]["result"]
# Append to array so we can sort
for obj in data:
versions_nonsorted.append(
{"version": int(obj["metric"]["version"]), "count": obj["values"][0][1]}
)

versions_nonsorted.sort(key=lambda x: x["version"], reverse=False)

# Function to fetch all pages of releases
def fetch_all_releases(url, access_token):
headers = {"Authorization": f"token {access_token}"}
releases = []
while url:
response = requests.get(url, headers=headers)
page_releases = response.json()
if page_releases:
releases.extend(page_releases)
if "Link" in response.headers:
links = response.headers["Link"].split(", ")
next_link = [link for link in links if 'rel="next"' in link]
if next_link:
url = next_link[0][
next_link[0].find("<") + 1 : next_link[0].find(">")
]
else:
url = None
else:
url = None
else:
break
return releases

releases_data = fetch_all_releases(
"https://api.github.com/repos/golemfactory/yagna/releases",
os.environ.get("GITHUB_AUTH_TOKEN_NON_PRIVILEDGED"),
)

def find_release_by_tag(tag, releases_data):
# Check for an exact match first
for release in releases_data:
if release["tag_name"].lower() == tag.lower():
return "official"

# If no exact match is found, check for partial matches where "prerelease": true
for release in releases_data:
if (
tag.lower() in release["tag_name"].lower()
and release["prerelease"] == True
):
return "rc"
return "rc"

versions_nonsorted.sort(key=lambda x: x["version"], reverse=False)

# Calculate the total count for all versions
total_count = sum(int(obj["count"]) for obj in versions_nonsorted)

# Function definitions for fetch_all_releases and find_release_by_tag remain the same...

for obj in versions_nonsorted:
version = str(obj["version"])
count = obj["count"]
if len(version) == 2:
concatinated = "0." + version[0] + "." + version[1]
elif len(version) == 3:
concatinated = "0." + version[0] + version[1] + "." + version[2]
versions.append(
{
"version": concatinated,
"count": count,
}
)

tag_to_search = "v" + concatinated
release_status = find_release_by_tag(tag_to_search, releases_data)

# Calculate the percentage of the total for this version
percentage_of_total = round((int(count) / total_count) * 100, 2)

if release_status == "official":
versions.append(
{
"version": concatinated,
"count": count,
"rc": False,
"percentage": percentage_of_total,
}
)
elif release_status == "rc":
versions.append(
{
"version": "RC-" + concatinated,
"count": count,
"rc": True,
"percentage": percentage_of_total,
}
)

serialized = json.dumps(versions)
r.set("network_versions", serialized)

Expand Down Expand Up @@ -544,10 +615,12 @@ def network_total_earnings():
)
update_total_earnings(domain)


from api2.models import GolemTransactions
from django.db.models import Sum
from django.utils.timezone import now


@app.task
def network_earnings_overview_new():
time_frames = [6, 24, 168, 720, 2160]
Expand All @@ -568,19 +641,19 @@ def network_earnings_overview_new():
}

all_time_earnings = (
GolemTransactions.objects.filter(tx_from_golem=True).aggregate(
Sum("amount")
)["amount__sum"]
GolemTransactions.objects.filter(tx_from_golem=True).aggregate(Sum("amount"))[
"amount__sum"
]
or 0.0
)


response_data["network_total_earnings"] = {
"total_earnings": float(all_time_earnings)
}

r.set("network_earnings_overview_new", json.dumps(response_data))


def update_total_earnings(domain):
data = get_stats_data(domain)
if data[1] == 200 and data[0]["data"]["result"]:
Expand Down Expand Up @@ -681,9 +754,6 @@ def provider_accepted_invoices_1h():
import urllib.parse





def get_earnings_for_node_on_platform(user_node_id, platform):
now = round(time.time())
domain = (
Expand Down
2 changes: 1 addition & 1 deletion stats-backend/core/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def setup_periodic_tasks(sender, **kwargs):
options={"queue": "default", "routing_key": "default"},
)
sender.add_periodic_task(
10.0,
30.0,
network_versions_to_redis.s(),
queue="default",
options={"queue": "default", "routing_key": "default"},
Expand Down

0 comments on commit 5603f09

Please sign in to comment.