Skip to content

Added middleware to track last_activity of a user #9554

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

bsekachev
Copy link
Member

@bsekachev bsekachev commented Jun 20, 2025

Motivation and context

We need a robust way to define when user used CVAT service.

It seems last_login field in User model may not get updated for more than a year if user works every day and do not do any logins. It does not seem reliable. Also, different auth methods (basic, social, sso) and a lot of code around make it difficult to guarantee that this field gets updated always.

Timestamps from analytics are not reliable also:

  1. User may send client events with future timestamps (probably it is a bug, but currently we already have some). Thus his account always have "recent" activity from the future.
  2. When user account scheduled for deletion, just opening CVAT account does not guarantee analytics will receive any logs from client (e.g. if browser closed immediately, or other issues in client code). Also, user may be authentificated via SDK/CLI what probably will not be displayed in analytics.

Finally, currently to find non-active users it is necessary to make many queries to the clickhouse database, so, usage report generation is much slower than with just postgres.

For me middleware with registering last activity once a day, seems the most reliable way.

How has this been tested?

Checklist

  • I submit my changes into the develop branch
  • I have created a changelog fragment
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • I have linked related issues (see GitHub docs)

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

Copy link

@@ -1172,6 +1172,7 @@ class TrackedShapeAttributeVal(AttributeVal):
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
rating = models.FloatField(default=0.0)
last_activity = models.DateTimeField(null=True, blank=True, default=None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for consistency with other similar fields, it would be better to call this last_activity_date.

@@ -787,3 +787,5 @@ class CVAT_QUEUES(Enum):
"cron_string": "0 8 * * *",
}
)

USER_LAST_ACTIVITY_UPDATEP_PERIOD = timedelta(days=1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
USER_LAST_ACTIVITY_UPDATEP_PERIOD = timedelta(days=1)
USER_LAST_ACTIVITY_UPDATE_PERIOD = timedelta(days=1)

Also, I'm doubtful about using "period" here, because we don't update it periodically. Maybe something like MIN_INTERVAL would be better?

@@ -24,3 +27,22 @@ def __call__(self, request):
response.headers["X-Request-Id"] = request.uuid

return response


class LastActivityMiddleware:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you forgot to add this to the middleware list.

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 40.00000% with 9 lines in your changes missing coverage. Please review.

Project coverage is 73.78%. Comparing base (d7a9f97) to head (7877f7d).
Report is 2 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #9554      +/-   ##
===========================================
+ Coverage    73.76%   73.78%   +0.01%     
===========================================
  Files          448      448              
  Lines        46695    46710      +15     
  Branches      3942     3942              
===========================================
+ Hits         34446    34463      +17     
+ Misses       12249    12247       -2     
Components Coverage Δ
cvat-ui 77.77% <ø> (+0.05%) ⬆️
cvat-server 70.67% <40.00%> (-0.02%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants