Skip to content

Commit

Permalink
Update scoring.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptobench committed Aug 23, 2024
1 parent b579b3c commit 0c16374
Showing 1 changed file with 22 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,31 @@ def penalty_weight(deviation):
else:
return 0.4 # Larger penalty

from django.db.models import Sum, Case, When, F
from django.utils import timezone
def calculate_uptime(provider_id):

provider = Provider.objects.get(node_id=provider_id)

# Calculate total online duration using database aggregation
online_duration = NodeStatusHistory.objects.filter(
provider=provider,
is_online=True
).aggregate(
total_duration=Sum(
Case(
When(
id__in=NodeStatusHistory.objects.filter(
provider=provider,
is_online=False
).values('id'),
then=F('timestamp') - F('provider__nodestatushistory__timestamp')
),
default=timezone.now() - F('timestamp')
)
)
)['total_duration'] or timedelta(0)

total_duration = timezone.now() - provider.created_at
uptime_percentage = (online_duration.total_seconds() / total_duration.total_seconds()) * 100

def calculate_uptime(node_id, node=None):
if node is None:
node = Provider.objects.get(node_id=node_id)
statuses = NodeStatusHistory.objects.filter(provider=node).order_by("timestamp")

online_duration = timedelta(0)
last_online_time = None

for status in statuses:
if status.is_online:
last_online_time = status.timestamp
elif last_online_time:
online_duration += status.timestamp - last_online_time
last_online_time = None

# Check if the node is currently online and add the duration
if last_online_time is not None:
online_duration += timezone.now() - last_online_time

total_duration = timezone.now() - node.created_at
uptime_percentage = (
online_duration.total_seconds() / total_duration.total_seconds()
) * 100
return uptime_percentage


Expand Down

0 comments on commit 0c16374

Please sign in to comment.