Enable companies, the ability to know if a member of their team were in contact with a person with covid is an amazing tool for avoiding future contact.
Actually, COVID is no more a hazard for people but definitely we as humanity will face another future pandemic.
Here we'll learn how to create an algorithm for allowing all members of a company, university, or other organization that has an authentication schema when they work in their offices or use their phones as their identification; doesn't matter what identification method it's required what we focus is how to create a tracing tool.
Using devise gem as our baseline for the User models had the following model.
Actually, COVID is no more a hazard for people but definitely we as humanity will face another future pandemic.
Here we'll learn how to create an algorithm for allowing all members of a company, university, or other organization that has an authentication schema when they work in their offices or use their phones as their identification; doesn't matter what identification method it's required what we focus is how to create a tracing tool.
Using devise gem as our baseline for the User models had the following model.
- User model for identifying each person in the office or building.
User model - The window model for recording all tracing information, the information required is the user that was in contact and the time between the users. The approach for saving all time of contact is the following to split the 24 hours of the day on 10 minutes windows (that's the reason the model is named window), so we'll have 144 windows in one day, ie:
Windows samplesWindow model
0 => 0:00 - 0:10 1 => 0:10 - 0:20 . . 48 => 8:00 - 8:10 . . . 143 => 23:40 - 23:50How calculate the current window?
To calculate the current window must use the following algorithm:
- Get the number of minutes elapsed from the beginning of the day
- Divide the number of minutes by 10
- The result is the current window
Sample: 9 y 50 minutes of the morning:
Current window = (9 * 60) + 50 minutes = 590 minutes / 10 = 59
Current window = (9 * 60) + 50 minutes = 590 minutes / 10 = 59
How register our contact between users?
POST /api/window
{ users_ids: [1,2,3] }Create window method:
def create
params[:user_ids].each do |user_id|
window = Window.today.
where(user_id: current_user.id, contact_user_id: user_id)
window = Window.new(user_id: current_user.id,
window_date: Time.zone.now.to_date,
contact_user_id: user_id) if window.blank?
window.windows = window.windows + [Window.current_window]
end
endHow do find contact between users?
For searching contact between users, we use the window model, searching all windows on the date range that share the time between the
the user that we are using for base contact.
The algorithm is the following:
the user that we are using for base contact.
The algorithm is the following:
- Search all windows on date range (or specific date)
- Search all windows for other users on date range (or specific date)
- Search shared time between users
#Get window of current user
user_windows = current_user.windows.on_date_range(params[:start_date] ,params[:end_date])
#Windows not assigned to current user on same range
contact_windows = Window.on_date_range(params[:start_date], params[:end_date]).
where("user_id != ?", current_user.id)
shared_users = []
user_windows.group_by{ |w| w.window_date }.each do |window_date, items|
shared_windows = items.map(&:windows).uniq.flatten
#Get shared time between users and get shared time
contact_windows.where(window_date: window_date).
on_windows(shared_windows).group_by{ |w| w.user }.each do |user, _items|
shared_users << [user.email, _items.sum(&:elapsed_time)]
end
end