We have a formula for shifts and a list of people, we need to fill a calendar with them.
These are the iterations for each week “formula.txt”, each row is a week, the first value is the iteration number, the other 4 values in the row are the number for a team member, these iterations are the sequence of combinations so that people are not oncall too frequently:
1 1 2 3 4
2 5 6 7 8
3 4 3 2 1
4 8 7 6 5
5 2 1 4 3
6 6 5 8 7
7 3 4 1 2
8 7 8 5 6
the numbers for the team members are in “roster.txt”:
1 FFlinstone XXJA01
2 Jdoe XXJD02
3 OOsbourne XXHR01
4 AHitchcock XXDD02
5 ARose XXAW04
6 EAPoe XXFP01
7 ACooper XXBC06
8 BJovi XXSL01
this is “oncall.ksh”
#!/bin/bash
start_date=20120604
end_date=20120902
formula_current_row=5
formula_rows=8
get_roster()
{
for p in `grep ^$1 formula.txt | awk ‘{ print $2″ “$3” “$4” “$5 }’`;do
echo -n “`grep ^$p roster.txt|awk ‘{ print $3″ ” }’`”
done
echo “”
}
get_weekend_roster()
{
for p in `grep ^$1 formula.txt | awk ‘{ print $2″ “$3” “$2” “$3 }’`;do
echo -n “`grep ^$p roster.txt|awk ‘{ print $3″ ” }’`”
done
echo “”
}
get_oncall()
{
tmpw=0
W=`date -d “$start_date $tmpw day” +”%W”`
for ((tmpw = 0; $(date -d “$start_date $tmpw day” +%s) <= $(date -d “$end_date” +%s); tmpw += 1))
do
P=$W
W=`date -d “$start_date $tmpw day” +”%W”`
if [ “$P” != “$W” ];then
let formula_current_row+=1
if [ $formula_current_row -gt $formula_rows ];then
formula_current_row=1
fi
fi
D=`date -d “$start_date $tmpw day” +”%Y-%m-%d %a%t”`
ISWEEKEND=`echo $D|egrep -i ‘sat|sun’|wc -l`
echo -n “$D ”
if [ $ISWEEKEND -gt 0 ];then
get_weekend_roster $formula_current_row
else
get_roster $formula_current_row
fi
done
}
generate_sql()
{
echo “use OnCall;”
get_oncall | while read l;do
ISWEEKEND=`echo $l|egrep -i ‘sat|sun’|wc -l`
if [ $ISWEEKEND -gt 0 ];then
echo $l | awk ‘{ print “insert into Detail (Date,Shift_C1_Pri,Shift_C1_Sec) values (“”$1″”,””$3″”,””$4″”) on DUPLICATE KEY UPDATE Shift_C1_Pri=””$3″”,Shift_C1_Sec=””$4″”;” }’
else
echo $l | awk ‘{ print “insert into Detail (Date,Shift_A1_Pri,Shift_A1_Sec,Shift_A2_Pri,Shift_A2_Sec) values (“”$1″”,””$3″”,””$4″”,””$5″”,””$6″”) on DUPLICATE KEY UPDATE Shift_A1_Pri=””$3″”,Shift_A1_Sec=””$4″”,Shift_A2_Pri=””$5″”,Shift_A2_Sec=””$6″”;” }’
fi
done
}
get_oncall
generate_sql > from_${start_date}_to_${end_date}.sql