An effective way to keep track of your login date and IP history

I am trying to track login history for stat purposes, but I don’t understand what is the best way to do this. I can have a separate table in which users and their login statistics with date will be recorded, but this table can become REALLY large. I could track some historical fields in the User model / object in the field for parsing and just update it (them) using a specific string format. for example, divide by :, get the latter, if the included date code is not today, add the element (date + account) otherwise, and then save it. At least with this second approach, it would be easy to delete old items (for example, store only 30 days of daily logins or IP addresses), since a separate table will need a task to delete old entries.

I am a big fan of instant changes. Tasks are useful, but can make things worse for maintenance reasons.

Anyone have any suggestions? I do not have external data caching yet. Any pointers are also welcome! (I searched for similar questions and answers)

Thank!

+3
source share
7 answers

I hate answering my questions, especially considering that you both gave useful answers. I think that answering my question using the approach I took can help others, combined with your answers.

Impressionist Gem ( RailStat) , , Rail MVC . "" , . ( - ), , , , , .

, Devise , Devise:: SessionController @current_member: ( , )

class TestSessionController < Devise::SessionsController
  def create
    if not @current_member.nil?
      impressionist(@current_member)
    end
    super
  end
end

. , , , TestSessionController Devise:

post 'login' => 'test_session#create', :as => :member_session

Devise , Devise, DB . , .

, , ...

+3

: , . ( , )

  def update_tracked_fields!(request)
    old_signin = self.last_sign_in_at
    super
    if self.last_sign_in_at != old_signin
      Audit.create :user => self, :action => "login", :ip => self.last_sign_in_ip
    end
  end

( https://github.com/plataformatec/devise/wiki/How-To:-Turn-off-trackable-for-admin-users)

+6

Devise.

Warden , after_set_user, . , , Login, ip, logged_in_at user_id, .

Warden::Manager.after_set_user :except => :fetch do |record, warden, options|
  Login.create!(:ip => warden.request.ip, :logged_in_at => Time.now, :user_id => record.id)
end
+4

Devise ip- :trackable. , , :

:sign_in_count,      :type => Integer, :default => 0
:current_sign_in_at, :type => Time
:last_sign_in_at,    :type => Time
:current_sign_in_ip, :type => String
:last_sign_in_ip,    :type => String

Devise::SessionsController it create, :last_sign_in_at :last_sign_in_ip before_create. .

+4

(scribd_analytics)

create_table 'page_views' do |t|
  t.column 'user_id', :integer 
  t.column 'request_url', :string, :limit => 200
  t.column 'session', :string, :limit => 32
  t.column 'ip_address', :string, :limit => 16
  t.column 'referer', :string, :limit => 200
  t.column 'user_agent', :string, :limit => 200
  t.column 'created_at', :timestamp
end

,

PageView

SQL- ActiveRecord

MySQL delayed

SQL-

'explain select', , MySQL

,

:

use two DB servers in a master/slave setup

move all the analytics queries to the slave

http://www.scribd.com/doc/49575/Scaling-Rails-Presentation-From-Scribd-Launch


- Gattica Google Analytics

+3

@user208769 answer, ​​ Devise::Models::Trackable#update_tracked_fields! update_tracked_fields . , ActiveRecord::Dirty, :

def update_tracked_fields(request)
  super

  if last_sign_in_at_changed?
    Audit.create(user: self, action: 'login', ip: last_sign_in_ip)
  end
end

( ), audits :

def update_tracked_fields(request)
  super
  audits.build(action: 'login', ip: last_sign_in_ip) if last_sign_in_at_changed?
end
+3

There is also a 'paper_trail' gem that allows you to track model changes.

0
source

All Articles