Getting time slots when someone was online? How would you apply this feature?

Our goal is to create a time frame in which the time periods when the user was online will be indicated. (In fact, it doesn’t matter which user we are talking about and where he was online). To get information about onliners, we can call the API method, someservice.com/api/?call=whoIsOnline

The whoIsOnline method will provide us with a list of users who are currently online. But the method is not an API to get information about who is NOT online .

So, we must build our timelines using the information received from whoIsOnline. Of course, there will be a measurement error (we cannot track information in real time). Suppose we will call the whoIsOline method every 2 minutes (yes, we will run our script cron every 2 minutes).

For example, calling whoIsOnline at 08:00 will return

Peter_id Michal_id Andy_id 

calling whoIsOnline at 08:02 will return

 Michael_id Andy_id George_id 

As you can see, Peter is disabled, but we have a new onliner - George.

Available tools are Db (MySQL) / text files / keystore (Redis / memcache) ; Feel free to choose any of them (or even all).

So, we should get such information

 George_id was online... 12 May: 08:02-08:30, 12:40-12:46, 20:14-22:36 11 May: 09:10-12:30, 21:45-23:00 10 May: was not online 

And now the question is ...

  • How do you store information for such deadlines?
  • How would you query / calculate information about time periods when a user was online?

Additional Information..

  • You cannot update information about offline users, only users who are currently online.
  • The solution should be flexible: timeline information can be provided for any time zone.
  • We should only store information for the last 7 days.
  • Each user seen online automatically receives their own identifier in our database.

Uff .. it was very difficult for me to write it because my English is very poor, but I hope that my question will be clear to you.

Thanks.

+4
source share
2 answers

There are two different ways to measure online:

  • Make the assumption that when someone clicks on the page on which they are online, for some conditional interval after that, for example, 5 minutes. Therefore, if they click on the page 4:03, 4:05 and 4:09, they will have an interval between 4:03 and 4: 09-4: 14 (depending on what algorithms / assumptions you make regarding the final click ); or

  • Use Javascript "heartbeat" and / or the Flash process to track how often pages open.

(1) more common. (2) often leaves people secret about privacy. Another way to view this is that (1) is passive monitoring, while (2) is active monitoring.

There are many options (1). The interval cannot be fixed. It may vary depending on which page the user is on. This can be built on the basis of assumptions or statistical sampling or even probabilistic models. In the simplest case, "who's online?" this is just a list of users who have clicked on something in the last 5 (for example) minutes, which is easy to understand (as you record every view of the page).

+1
source

I would do something like this (pseudo code)

 UPDATE session_log SET last_online = NOW() WHERE user = ... AND last_online = '10 minutes ago' IF NOT UPDATED: INSERT INTO session_log (last_online, user) VALUES(NOW(), user) 

Typically, there should be a created at (or similar) column, but this way you can easily track sessions.

0
source

Source: https://habr.com/ru/post/1307270/


All Articles