Since DEVICE
stores last_sign_in_at
as soon as a user login in to the account and becomes online
.
You can do something like this.
Add a migration
to change users table and add a field, total_online_time
rails g migration add_total_online_time_to_users total_online_time:Float
Add a before action callback to your application controller to check user actions:
before_action :set_online_time, if: proc { user_signed_in? }
In this method, I will get last_sign_in_at
and as I have current time, I will get the difference of these times and store in the DB
as minutes and seconds.
Again, if he login, I will add this time to old time.
Currently I am saving the minutes and seconds. Since I can't directly add two times.
EDIT:
I am saving in minutes because, if we save string or time in Database, we cannot add the last value plus the current value on the go. So it the case.
def set_online_time
start_time = current_user.last_sign_in_at.to_time;
end_time = Time.now;
if(start_time && end_time)_
minutes = time_diff(start_time, end_time)
if(current_user.total_online_time)
minutes = current_user.total_online_time + minutes
current_user.update_attribute(:total_online_time, minutes)
end
end
HEre is the method to manipulate time:
private
def time_diff(start_time, end_time)
(start_time - end_time) / 60
end
Now, retrieve the minutes from DB
and convert to HH:MM:SS
Hhere is the method for that ,
Time.at(seconds).utc.strftime("%H:%M:%S")
So, Time.at((current_user.total_online_time) * 60).utc.strftime("%H:%M:%S")
is yours.
HERE IS AN EXAMPLE:
time1 = Time.now => 2017-01-27 18:01:42 +0530
time2= Time.now - 4.hours => 2017-01-27 14:02:02 +0530
minutes = time_diff(time1,time2) => 239.66325946553334
Time.at((minutes) * 60).utc.strftime("%H:%M:%S") => "03:59:39"
So, "03:59:39" is the online time.
Note:
1) This answer keeps on incrementing the online time of the user whenever he logs in.
2) If you want only the last time, just use,
minutes = time_diff(start_time, end_time)
current_user.update_attribute(:total_online_time, minutes)
You can also take an extra field and save current online time.