Skip to content

fix: OPTIC-1809: Adding option to only update last_activity after a significant difference #7216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions label_studio/core/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,3 +820,5 @@ def collect_versions_dummy(**kwargs):
}

LOGOUT_REDIRECT_URL = get_env('LOGOUT_REDIRECT_URL', None)

USER_LAST_ACTIVITY_CHECK_INTERVAL = get_env('USER_LAST_ACTIVITY_CHECK_INTERVAL', 60)
13 changes: 13 additions & 0 deletions label_studio/users/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""This file and its contents are licensed under the Apache License 2.0. Please see the included NOTICE for copyright information and LICENSE for a copy of the license.
"""
import datetime
import logging
from typing import Optional

from core.utils.common import load_func
Expand All @@ -18,6 +19,8 @@
from rest_framework.authtoken.models import Token
from users.functions import hash_upload

logger = logging.getLogger(__name__)

YEAR_START = 1980
YEAR_CHOICES = []
for r in range(YEAR_START, (datetime.datetime.now().year + 1)):
Expand Down Expand Up @@ -65,7 +68,17 @@ class UserLastActivityMixin(models.Model):
last_activity = models.DateTimeField(_('last activity'), default=timezone.now, editable=False)

def update_last_activity(self):
check_interval = settings.USER_LAST_ACTIVITY_CHECK_INTERVAL
self.last_activity = timezone.now()

if check_interval:
if (timezone.now() - self.last_activity).seconds < check_interval:
# Don't update last_activity because it's not necessary since it's close enough to the existing last_activity value
logger.info(
f'Not updating last_activity for {self.id} because last_activity of {self.last_activity} is within {check_interval} seconds of their existing last_activity value'
)
return

self.save(update_fields=['last_activity'])

class Meta:
Expand Down
Loading