Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[5690] User last active#6466

Merged
amercader merged 17 commits into
ckan:masterfrom
TomeCirun:5690-user-last-active
Feb 18, 2022
Merged

[5690] User last active#6466
amercader merged 17 commits into
ckan:masterfrom
TomeCirun:5690-user-last-active

Conversation

@TomeCirun
Copy link
Copy Markdown
Contributor

@TomeCirun TomeCirun commented Oct 12, 2021

Fixes #5690

Proposed fixes:

I added:
-last_active column to user table
-set_user_last_login instance method
-helper function
-migration file

Features:

  • includes tests covering changes
  • includes updated documentation
  • includes user-visible changes
  • includes API changes
  • includes bugfix for possible backport

Please [X] all the boxes above that apply

@TomeCirun TomeCirun marked this pull request as draft October 12, 2021 13:23
@tino097 tino097 changed the title 5690 user last login [5690] User last login Oct 12, 2021
@TomeCirun TomeCirun marked this pull request as ready for review October 13, 2021 07:21
@amercader amercader self-assigned this Oct 14, 2021
@smotornyuk
Copy link
Copy Markdown
Member

@amercader , can we use successful login signal from the UsernamePasswordAuthenicator here? Or, if doing it inside login-view is better, maybe successful login signal also should be moved there

Comment thread ckan/migration/versions/101_d111f446733b_add_last_active_column_in_user_table.py Outdated
Comment thread ckan/lib/helpers.py Outdated
@amercader
Copy link
Copy Markdown
Member

@TomeCirun @smotornyuk as I mentioned on the issue I think that rather than tracking logins we should track the last time a user has been active, ie identified in a request. Otherwise we lose all API access. So basically have a last_active field instead of last_login and hook it to identify_user.

This does mean a DB write on each request so maybe we can store the current last_active value in the session and only write it if its bigger than X mins (which can be configurable).

If all this seems to complicated and we want to stick with actual UI logins I'm fine with it.
But then @smotornyuk the signal you suggest would probably miss logins from alternative authentication methods like SAML2, OAuth etc right?

@smotornyuk
Copy link
Copy Markdown
Member

signal you suggest would probably miss logins from alternative authentication methods

Yes, that's right. When I added this signal, it decided that Authenticator is the right place to notify about successful logins. And other authenticators probably should also emit this signal when it's appropriate. I didn't put this signal to the logged_in view, because it cannot see the difference in cases when the user just signed in, and when it already has an active session and just visited /user/logged_in URL accidentally.

current last_active value in the session and only write it if its bigger than X mins

This means that we have to collect all the sessions periodically and check them. Which in turn requires two decisions:

  • should we use Redis instead of sessions? It is fast, much more straightforward in terms of iterations over records, and even allows us to build some sort of admin page with an activity table for users, with a button "FlushIntoDB" (just as an example, do not consider it seriously). The obvious problem is a data loss when Redis reloaded.
  • should we use background jobs/cron jobs with CLI command / other triggers that will write data to the database?

Another option(really unreliable) is to modify the user object and do not call Session.commit. Eventually, some other action (update/create/follow/etc.) will commit the session and our changes will be stored. But there are some serious problems here:

  • session rollbacks will erase uncommitted changes
  • If there will be no writes for quite a long period of time, we can aggregate thousands of pending changes. It can slow down the following commit.

@TomeCirun TomeCirun changed the title [5690] User last login [5690] User last active Oct 17, 2021
@TomeCirun
Copy link
Copy Markdown
Contributor Author

TomeCirun commented Oct 22, 2021

Hey, @amercader do these changes seem sensible?
And please if you could tell me why the test fails?

Copy link
Copy Markdown
Member

@amercader amercader left a comment

Choose a reason for hiding this comment

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

This is looking great @TomeCirun , but we need a test to make sure that the functionality works as expected. Add a test where the user does a request to see if last_active gets set, and maybe another one to check that ckan.user.last_active works. Let me know if you need pointers on how to write those.

Comment thread doc/maintaining/configuration.rst Outdated
Comment thread ckan/model/user.py
Comment thread doc/maintaining/configuration.rst Outdated
@TomeCirun TomeCirun requested a review from amercader November 2, 2021 16:01
@TomeCirun
Copy link
Copy Markdown
Contributor Author

@amercader do you have any idea why this happens?
Thanks

@amercader
Copy link
Copy Markdown
Member

@TomeCirun I don't know about these failures, but they are unrelated to your changes. I've created an issue to track them if you want to investigate in a separate PR: #6537

Thanks for your PR, we'll review it as soon as we have availability

@TomeCirun
Copy link
Copy Markdown
Contributor Author

Hey, @amercader is there anything else that can be done in this PR?
Thanks

@TomeCirun
Copy link
Copy Markdown
Contributor Author

@amercader is there anything preventing us to merge this PR?

amercader added a commit that referenced this pull request Feb 18, 2022
@amercader amercader merged commit a1f6c3b into ckan:master Feb 18, 2022
@amercader
Copy link
Copy Markdown
Member

is there anything preventing us to merge this PR?

Only time, and the relentless onslaught of the world against our attention.

Thanks for this, it's a very valuable contribution

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.

There is no way to find out when a user last logged in

3 participants