Fix currently_online

Make currently_online aware of SCRIPT_HEARTBEAT_FREQUENCY
This commit is contained in:
Paweł Jastrzębski 2021-04-23 10:50:34 +02:00 committed by R. Miles McCain
parent da87ddb18f
commit a7548d7eba
2 changed files with 9 additions and 7 deletions

View File

@ -1,12 +1,10 @@
import json
import uuid
from django.conf import settings
from django.db import models
from django.shortcuts import reverse
from django.utils import timezone
from core.models import Service
from core.models import Service, ACTIVE_USER_TIMEDELTA
def _default_uuid():
@ -61,9 +59,7 @@ class Session(models.Model):
@property
def is_currently_active(self):
return timezone.now() - self.last_seen < timezone.timedelta(
milliseconds=settings.SCRIPT_HEARTBEAT_FREQUENCY * 2
)
return timezone.now() - self.last_seen < ACTIVE_USER_TIMEDELTA
@property
def duration(self):

View File

@ -4,6 +4,7 @@ import re
import uuid
from django.apps import apps
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.core.exceptions import ValidationError
from django.db import models
@ -13,6 +14,11 @@ from django.shortcuts import reverse
from django.utils import timezone
ACTIVE_USER_TIMEDELTA = timezone.timedelta(
milliseconds=settings.SCRIPT_HEARTBEAT_FREQUENCY * 2
)
def _default_uuid():
return str(uuid.uuid4())
@ -120,7 +126,7 @@ class Service(models.Model):
Hit = apps.get_model("analytics", "Hit")
currently_online = Session.objects.filter(
service=self, last_seen__gt=timezone.now() - timezone.timedelta(seconds=10)
service=self, last_seen__gt=timezone.now() - ACTIVE_USER_TIMEDELTA
).count()
sessions = Session.objects.filter(