Appointment Scheduling in ruby/rails - ruby-on-rails

I am working on a call center software in rails and need to schedule appointments for agents that can handle calls for the customers. Having said the call center software, need to make sure that I schedule the appointments utilizing the entire agent's schedule as much as possible, leaving scope for least number of holes (where agent has no appointment).
Given an agent's schedule, for example 9:00AM to 5:30PM for a given day, with a lunch break of 30 minutes between 1:00PM - 1:30PM, I need to schedule appointments of varying length in duration, some 60 minutes and some 90 minutes.
And if some reason, lunch break is leaving some holes in the schedule, I should be able to move lunch break 30minutes +/-, so instead of 1:00PM - 1:30PM it could be moved between 1:30PM - 2:00PM or 12:30PM - 1:00PM.
I started off creating lunch breaks as a kind of appointment which will provide the flexibility of moving the starts_at and finishes_at attributes. And the appointments being either 60 minutes or 90 minutes, which are multiple of 30 minutes and lunch also being 30 minutes, I started off splitting agents schedule into slots of 30 minutes each.
So for a given agent on a given day, looking at his schedule I instantiated an array of slots each with a duration of 30 minutes and starts_at and finishes_at attributes be like 9:00AM - 9:30AM, 9:30AM - 10:00AM, etc.
I need some help on looping through this array of appointment slots and pull either 2 consecutive slots or 3 consecutive slots depending upon 60 or 90 minute duration appointment, keeping in mind that I should be able to move the lunch +/- 30 minutes.
Any help is much appreciated.

Looking at your problem:
Appointments are either 60 or 90 minutes long.
Lunch can vary between a 90 minute interval 12:30-2:00
And we want to minimize the amount of minutes that have no appointments.
Now, you have a time interval to fill, which is 9:00AM to 5:30pm. Assuming appointments fall between 9:00-5:30, we can use a greedy algorithm for interval scheduling based on earliest finish time (source) with your additional constraint.
Basically the algorithm is as follows (in pseudo)
Let R be the set of all appointments
Let R11 be the set of appointments from R before 12:30 that are compatible with 12:30-1:00 and R12 be the set of appointments from R after 1:00 that are compatible with 12:30-1:00
Let R21 be the set of appointments from R before 1:00 that are compatible with 1:00-1:30 and R22 be the set of appointments from R after 1:30 that are compatible with 1:00-1:30
Let R31 be the set of appointments from R before 1:30 that are compatible with 1:30-2:00 and R32 be the set of appointments from R after 2:00 that are compatible with 1:30-2:00
Let R1Comb = findSet(R11) + 12:30-1:00 + findSet(R12)
Let R2Comb = findSet(R21) + 1:00-1:30 + findSet(R22)
Let R3Comb = findSet(R31) + 1:30-2:00 + findSet(R32)
Function findSet(R)
Let A be the time interval to fill
While R is not empty
Choose a request r in R that has the smallest finishes_at
Add r to A
Remove all appointments in R that are not compatible with r
EndWhile
Return A
EndFunction
Return the R that has the smallest amount of holes in R1Comb, R2Comb, R3Comb
This algorithm makes use of a few concepts
An appointment r1 is not compatible with r2 if they overlap.
Because of #1, we know that Ri1/Ri2 for i=1,2,3 will not be conflicting with each other. Because if an appointment in Ri2 is not compatible with Ri1, then it is also not compatible with the lunch period, which is a contradiction because we took out all the non compatible appointments.
Once we split the set of appointments, then it's a matter of solving 2 scheduling problems, which can be solved greedily.
An this algorithm is still O(n log n) because you are doing the greedy algorithm 6 times (a constant), and each greedy iteration is O(n log n), and the first few lines and the last line are all O(n).
People write theses on scheduling and it's not an easy problem. I suggest you looking at http://www.asap.cs.nott.ac.uk/watt/resources/university.html to get a better understanding.
Good luck :)

Related

Time function in sheets

I have the data of 4000 employees in google sheets along with their shift timings (9 hour long shift) spread across 24 hours. I wish to use a formula to understand the most common timing these employees are available in the office (09:00 to 18:00). My results would be 09:00 to 11:00, 11:00 to 13:00, 13:00 to 15:00, 15:00 to 18:00, 18:00 to 22:00, 22:00 to 09:00.
I could have used this formula to derive to the value:
=IF(AND(TIMEVALUE(A2)>=TIMEVALUE("09:00"), TIMEVALUE(A2)<=TIMEVALUE("11:00")), "09:00 to 11:00",
IF(AND(TIMEVALUE(A2)>=TIMEVALUE("11:00"), TIMEVALUE(A2)<=TIMEVALUE("13:00")), "11:00 to 13:00",
IF(AND(TIMEVALUE(A2)>=TIMEVALUE("13:00"), TIMEVALUE(A2)<=TIMEVALUE("15:00")), "13:00 to 15:00",
IF(AND(TIMEVALUE(A2)>=TIMEVALUE("15:00"), TIMEVALUE(A2)<=TIMEVALUE("18:00")), "15:00 to 18:00",
IF(AND(TIMEVALUE(A2)>=TIMEVALUE("18:00"), TIMEVALUE(A2)<=TIMEVALUE("22:00")), "18:00 to 22:00", "22:00 to 09:00")))))
but the problem is the timings are not in the time format but they are in text format
Here's my take:
Suppose Column A has clock ins, and Column B has clock outs. Let Column D have Times starting at 00:00 and going up to 33:00 (8am next day) in 5 minute (or 30, 60 etc) increments.
Let column E be the amount of clock in and outs that an employee was in the office at the time referred to in E.
We will define E to be =COUNTIFS($A$2:$A$9999,"<="&D2,$B$2:$B$9999,">="&D2).
Next, apply some conditional formatting to highlight the most busy times.
Note that you will need only the times of day, which it sounds like you have, but you will need to convert overnight shifts to not wrap around midnight.

Googl Sheets - Time Range to Table

I want to take a time range and convert it to minutes within a table of 30 minute segments. For example, Bob works from 3:35 AM to 5:00 AM. So within the table from 3:30 AM - 4:00 AM should represent 25 minutes, because Bob was working a total of 25 minutes during that time slot. Then the time slots for 4:00 - 4:30 AM and 4:30 - 5:00 AM would both have 30 in their corresponding cells.
Apologies to leave this so broad but I honestly have no idea where to start and this forum has been immensely helpful.
https://docs.google.com/spreadsheets/d/1YpHU-UHlqXL6c8I27zSDZaRu72ViUw5W6RPru-HE3Iw/edit#gid=0
Any help is appreciated.
For each 30-minute interval, you have to check whether these two conditions are met:
The interval start time (3:30) falls between the employee start and end time (3:35 and 5:00).
The interval end time (4:00) falls between the employee start and end time.
If any of these conditions are met, the working time for that interval won't be 0. The working time will be the difference between the minimum of both end times (4:00 and 5:00) and the maximum of both start times (3:30 and 3:35).
Translated to sheets functions, you could do something like this:
=IF(MIN($D2,O$1)-MAX($C2,N$1)>0,TIMEVALUE(MIN($D2,O$1)-MAX($C2,N$1))*24*60,0)
Or, alternatively, this:
=IF(OR(AND($C2<N$1,N$1<$D2),AND($C2<O$1,O$1<$D2)),TIMEVALUE(MIN($D2,O$1)-MAX($C2,N$1))*24*60,0)

Showing hourly average (histogramm) in grafana

Given a timeseries of (electricity) marketdata with datapoints every hour, I want to show a Bar Graph with all time / time frame averages for every hour of the data, so that an analyst can easily compare actual prices to all time averages (which hour of the day is most/least expensive).
We have cratedb as backend, which is used in grafana just like a postgres source.
SELECT
extract(HOUR from start_timestamp) as "time",
avg(marketprice) as value
FROM doc.el_marketprices
GROUP BY 1
ORDER BY 1
So my data basically looks like this
time value
23.00 23.19
22.00 25.38
21.00 29.93
20.00 31.45
19.00 34.19
18.00 41.59
17.00 39.38
16.00 35.07
15.00 30.61
14.00 26.14
13.00 25.20
12.00 24.91
11.00 26.98
10.00 28.02
9.00 28.73
8.00 29.57
7.00 31.46
6.00 30.50
5.00 27.75
4.00 20.88
3.00 19.07
2.00 18.07
1.00 19.43
0 21.91
After hours of fiddling around with Bar Graphs, Histogramm Mode, Heatmap Panel und much more, I am just not able to draw a simple Hours-of-the day histogramm with this in Grafana. I would very much appreciate any advice on how to use any panel to get this accomplished.
your query doesn't return correct time series data for the Grafana - time field is not valid timestamp, so don't extract only
hour, but provide full start_timestamp (I hope it is timestamp
data type and value is in UTC)
add WHERE time condition - use Grafana's macro __timeFilter
use Grafana's macro $__timeGroupAlias for hourly groupping
SELECT
$__timeGroupAlias(start_timestamp,1h,0),
avg(marketprice) as value
FROM doc.el_marketprices
WHERE $__timeFilter(start_timestamp)
GROUP BY 1
ORDER BY 1
This will give you data for historic graph with hourly avg values.
Required histogram may be a tricky, but you can try to create metric, which will have extracted hour, e.g.
SELECT
$__timeGroupAlias(start_timestamp,1h,0),
extract(HOUR from start_timestamp) as "metric",
avg(marketprice) as value
FROM doc.el_marketprices
WHERE $__timeFilter(start_timestamp)
GROUP BY 1
ORDER BY 1
And then visualize it as histogram. Remember that Grafana is designated for time series data, so you need proper timestamp (not only extracted hours, eventually you can fake it) otherwise you will have hard time to visualize non time series data in Grafana. This 2nd query may not work properly, but it gives you at least idea.

Greedy algorithm correctness

I'm trying to solve the following problem:
CEO is checking on it's employees at a constant times every day (for
an example the number of minutes after a shift started> {5, 35,
120..}). Calculate when should employees work, so that every time the CEO checks on them they are working. Their work takes 3 minutes to
complete and it increases by another 3 minutes every time they start
working again (First time it takes 3 minutes, second time it takes 6
minutes, third time it takes 9 minutes..).
This is my algorithm in pseudocode:
int time = 2
for: every CEOcheckTime in 0 to CEOcheckTime.length
add (CEOcheckTimes-time) to results
time = time + 2
Ignoring the edge case, where the CEO checks on employees sooner that they can finish the work, is my algorithm correct? Thanks!
Ignoring the 'edge case' this seems fine except your time is being incremented by 2 rather than 3 as per the spec.
int time = 3
for: every CEOcheckTime in CEOcheckTimes
add (CEOcheckTimes-time) to results
time = time + 3
a similar just in time approach could be used here that would account for your edge case too.
for: every CEOcheckTime in CEOcheckTimes
if exists(time):
if last(results) + time < CEOcheckTime:
add (CEOcheckTimes) to results
time = time + 3
else:
add (CEOcheckTimes) to results
time = 3
edit: i have assumed that if work starts / ends at the same time as a check that this counts as working.

How do I shift Google Sheets duration value from 35:55:00 to 0:35:55?

I have pasted multiple run duration values from Garmin into a Google Sheet. The longer runs (> 1 hour) copy/paste correctly. Eg: 2:10:35. The problem is shorter (< 1 hour) runs. Eg 35:55. The latter are being shown in Google Sheets as 35:55:00. Ie Google assumes 35:55 is 35 hours and 55 mins, not 35 mins and 55 seconds. So for my shorter sub 1 hour durations I need an easy way to convert 35:55:00 to 0:35:55.
As Tom Sharpe said, there is some room for interpretation in the data you have. But assuming that the duration of your runs is always between 10 minutes and 10 hours, we can disambiguate the values as follows:
=if(A1 > 10/24, A1/60, A1)
Numerically, the duration values are measured in days, so A1 > 10/24 means "more than 10 hours". In this case the value gets divided by 60.
Depending on your workout regime you may want to replace the threshold of 10 by another number; perhaps it's safer to say that the runs are always between 5 minutes and 5 hours.

Resources