Since it DEVICEstores last_sign_in_at, as soon as the user logs into the account and becomes online.
You can do something like this.
Add a table migrationto modify the user table and add a field,total_online_time
rails g migration add_total_online_time_to_users total_online_time:Float
Add a test call before the action of your application controller to verify user actions:
before_action :set_online_time, if: proc { user_signed_in? }
In this method I will get last_sign_in_at, and since I have the current time, I get the difference of these times and save in DBminutes and seconds.
Again, if he logs in, I will add this time in the old time.
I am currently saving minutes and seconds. Since I can not directly add twice.
EDIT:
, , , . .
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 - :
private
def time_diff(start_time, end_time)
(start_time - end_time) / 60
end
DB HH:MM:SS
,
Time.at(seconds).utc.strftime("%H:%M:%S")
, Time.at((current_user.total_online_time) * 60).utc.strftime("%H:%M:%S") .
:
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.
:
1) - .
2) ,
minutes = time_diff(start_time, end_time)
current_user.update_attribute(:total_online_time, minutes)
-.