From f874d56e5e84cceea6b3f6fd6c27bd860aec36cb Mon Sep 17 00:00:00 2001 From: Wesley Lima Date: Wed, 12 Mar 2025 15:54:06 -0400 Subject: [PATCH 1/4] fix: OPTIC: 1809: Adding option to only update last_activiy after a significant difference --- label_studio/core/settings/base.py | 2 ++ label_studio/users/models.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/label_studio/core/settings/base.py b/label_studio/core/settings/base.py index 344d541d6494..dc97aeb623dd 100644 --- a/label_studio/core/settings/base.py +++ b/label_studio/core/settings/base.py @@ -822,3 +822,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) diff --git a/label_studio/users/models.py b/label_studio/users/models.py index 97fa37b41b88..d0e697b9a4b3 100644 --- a/label_studio/users/models.py +++ b/label_studio/users/models.py @@ -65,6 +65,12 @@ class UserLastActivityMixin(models.Model): last_activity = models.DateTimeField(_('last activity'), default=timezone.now, editable=False) def update_last_activity(self): + check_interval = getattr(settings, 'USER_LAST_ACTIVITY_CHECK_INTERVAL', None) + if check_interval: + if timezone.now() - self.last_activity < check_interval: + # Don't update last_activity because it's not necessary since it's close enough to the existing last_activity value + return + self.last_activity = timezone.now() self.save(update_fields=['last_activity']) From 35079fa22da0bfb8f592dfff5cc96cc5e8c75683 Mon Sep 17 00:00:00 2001 From: Wesley Lima Date: Mon, 17 Mar 2025 10:10:10 -0400 Subject: [PATCH 2/4] Implementing logging and better getting of setting --- label_studio/users/models.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/label_studio/users/models.py b/label_studio/users/models.py index d0e697b9a4b3..e1e65a770162 100644 --- a/label_studio/users/models.py +++ b/label_studio/users/models.py @@ -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 @@ -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)): @@ -65,13 +68,17 @@ class UserLastActivityMixin(models.Model): last_activity = models.DateTimeField(_('last activity'), default=timezone.now, editable=False) def update_last_activity(self): - check_interval = getattr(settings, 'USER_LAST_ACTIVITY_CHECK_INTERVAL', None) + check_interval = settings.USER_LAST_ACTIVITY_CHECK_INTERVAL + self.last_activity = timezone.now() + if check_interval: if timezone.now() - self.last_activity < 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.last_activity = timezone.now() self.save(update_fields=['last_activity']) class Meta: From 944a37ec1d01e8368fb025af279a769af6b1eea5 Mon Sep 17 00:00:00 2001 From: Wesley Lima Date: Wed, 19 Mar 2025 17:45:17 -0400 Subject: [PATCH 3/4] Making sure comparison works --- label_studio/users/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/label_studio/users/models.py b/label_studio/users/models.py index e1e65a770162..0c5d3a10165b 100644 --- a/label_studio/users/models.py +++ b/label_studio/users/models.py @@ -72,7 +72,7 @@ def update_last_activity(self): self.last_activity = timezone.now() if check_interval: - if timezone.now() - self.last_activity < check_interval: + if (timezone.now() - self.last_activity) < 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' From df98ec560662cb3ca9027ba0c37d1c828a207c12 Mon Sep 17 00:00:00 2001 From: Wesley Lima Date: Wed, 19 Mar 2025 18:07:03 -0400 Subject: [PATCH 4/4] fixing comparison --- label_studio/users/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/label_studio/users/models.py b/label_studio/users/models.py index 0c5d3a10165b..2b8f6cc10770 100644 --- a/label_studio/users/models.py +++ b/label_studio/users/models.py @@ -72,7 +72,7 @@ def update_last_activity(self): self.last_activity = timezone.now() if check_interval: - if (timezone.now() - self.last_activity) < 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'