Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Keep track of user activity #619

Merged
merged 21 commits into from
Jun 12, 2016
Merged

Keep track of user activity #619

merged 21 commits into from
Jun 12, 2016

Conversation

cyhsutw
Copy link
Contributor

@cyhsutw cyhsutw commented Jun 4, 2016

Hello guys,

This PR is related to #356, it tracks the timestamp of a user's last access to the app.

Staffs can view the timestamp using stafftools, as shown below:

last_active_at

The difference between #571 and this pull request is that whether the updated_at of User will be updated or not.

<span class="text-muted css-truncate css-truncate-target">Joined <%= local_time_ago(user.created_at) %></span><br>
<span class="text-muted css-truncate css-truncate-target">
Joined <%= local_time_ago(user.created_at) %> /
Last active: <%= user.last_active_at.present? ? local_time_ago(user.last_active_at) : 'Unknown' %>
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we set the last_active_at to created_at when the user is first created? That way we never have the case where the last_active_at is unknown.

Copy link
Contributor Author

@cyhsutw cyhsutw Jun 5, 2016

Choose a reason for hiding this comment

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

This should not be null in our main Postgres database.

I was wondering if there is a case that the indexes on Elasticsearch is corrupted/outdated, which might cause this value to be null.

@tarebyte
Copy link
Member

tarebyte commented Jun 4, 2016

@cyhsutw thanks for doing this.

What do you think about writing a data transition script so that we can set these timestamps for everyone?

The process I see here is:

  • Make a PR with just the migration, with last_active_at allowed to be null
  • Run a data transition script that uses the created_at or updated_at timestamp whatever you prefer.
  • Make a PR with another migration that enforces last_active_at to be present
  • Merge this PR with the appropriate changes.

Does that sound alright?

@cyhsutw
Copy link
Contributor Author

cyhsutw commented Jun 5, 2016

@tarebyte thanks for the feedback.

I'm totally 🆒 with the process you suggested.

Just a little question about:

Run a data transition script that uses the created_at or updated_at timestamp whatever you prefer.

If we do this in a migration like:

class SetUserLastActiveAtValue < ActiveRecord::Migration
  def change
    User.update_all('last_active_at = updated_at')
  end
end

The elasticsearch indexes will not be updated, thus we might have null values in Stafftools.
Is there any way to rebuild the indexes, or we should create a rake to scan all of the users and update index for each user instead?

@cyhsutw cyhsutw added the WIP 💭 PR's that are a 'Work In Progress' label Jun 5, 2016
@cyhsutw
Copy link
Contributor Author

cyhsutw commented Jun 5, 2016

@tarebyte I added some migration files, if there's no problem with those, I'll go ahead and open separate PRs for each of them.

Also, I am working on adding few more test for this change, so I'm marking this as WIP for now.

@cyhsutw cyhsutw changed the title Keep track of user activity [WIP] Keep track of user activity Jun 5, 2016
@cyhsutw cyhsutw self-assigned this Jun 7, 2016
@cyhsutw cyhsutw removed the WIP 💭 PR's that are a 'Work In Progress' label Jun 7, 2016
@cyhsutw
Copy link
Contributor Author

cyhsutw commented Jun 9, 2016

I added a test to the User model.

Should we add another test to check if the StafftoolsIndex::User is updated?

cc/ @tarebyte

@tarebyte
Copy link
Member

tarebyte commented Jun 9, 2016

Should we add another test to check if the StafftoolsIndex::User is updated?

I don't think that's necessary

become_active
else
auth_redirect
end
Copy link
Member

Choose a reason for hiding this comment

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

@cyhsutw What do you think about something like this?

return become_active if logged_in? && adequate_scopes?
auth_redirect

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🆒 That's much more elegant!

update_columns(last_active_at: Time.zone.now)
# but we still want indexes to work
self.class.update_index('stafftools#user') { self }
end
Copy link
Member

Choose a reason for hiding this comment

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

I think these comments are useful, but would be better placed at the top.

# This updates the `last_active_at` column without
# updating the model, but keeps the index updated.
def become_active
  update_columns(last_active_at: Time.zone.now)
  self.class.update_index('stafftools#user') { self }
end

@@ -12,6 +12,12 @@
end
end

describe '#become_active' do
it 'updates the last_active_at attribute' do
expect { user.become_active }.to change { user.last_active_at }
Copy link
Member

Choose a reason for hiding this comment

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

We should also check here that the updated_at column remains the same before and after.

it 'does not change the updated_at column' do
  updated_at = user.updated_at
  user.become_active
  expect(updated_at).to eql(user.updated_at)
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in #b3ed41b.

@tarebyte
Copy link
Member

I left a few minor comments, @cyhsutw. After that this is good to go!

@cyhsutw
Copy link
Contributor Author

cyhsutw commented Jun 12, 2016

@tarebyte Thanks for reviewing the changes, just updated the code based on your comments.

@tarebyte
Copy link
Member

Confirmed in production!

@tarebyte tarebyte merged commit 3d5e9bf into master Jun 12, 2016
@tarebyte tarebyte deleted the last-active-at branch June 12, 2016 18:39
@cyhsutw cyhsutw changed the title [WIP] Keep track of user activity Keep track of user activity Aug 18, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants