{"id":694,"date":"2012-05-18T07:18:46","date_gmt":"2012-05-18T07:18:46","guid":{"rendered":"http:\/\/kraftek.com\/blog\/?p=694"},"modified":"2012-05-18T07:18:46","modified_gmt":"2012-05-18T07:18:46","slug":"oncall-rotation-bash-script","status":"publish","type":"post","link":"\/blog\/index.php\/oncall-rotation-bash-script\/","title":{"rendered":"Oncall rotation bash script"},"content":{"rendered":"<p>We have a formula for shifts and a list of people, we need to fill a calendar with them.<\/p>\n<p>These are the iterations for each week &#8220;formula.txt&#8221;, 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:<\/p>\n<p style=\"padding-left: 30px;\">1 1 2 3 4<br \/>\n2 5 6 7 8<br \/>\n3 4 3 2 1<br \/>\n4 8 7 6 5<br \/>\n5 2 1 4 3<br \/>\n6 6 5 8 7<br \/>\n7 3 4 1 2<br \/>\n8 7 8 5 6<\/p>\n<p>the numbers for the team members are in &#8220;roster.txt&#8221;:<\/p>\n<p style=\"padding-left: 30px;\">1 FFlinstone \u00a0\u00a0 XXJA01<br \/>\n2 Jdoe \u00a0\u00a0 XXJD02<br \/>\n3 OOsbourne \u00a0\u00a0 XXHR01<br \/>\n4 AHitchcock \u00a0\u00a0 XXDD02<br \/>\n5 ARose \u00a0\u00a0 XXAW04<br \/>\n6 EAPoe \u00a0\u00a0 XXFP01<br \/>\n7 ACooper \u00a0\u00a0 XXBC06<br \/>\n8 BJovi \u00a0\u00a0 XXSL01<\/p>\n<p>this is &#8220;oncall.ksh&#8221;<\/p>\n<p style=\"padding-left: 30px;\">#!\/bin\/bash<\/p>\n<p>start_date=20120604<br \/>\nend_date=20120902<br \/>\nformula_current_row=5<\/p>\n<p>formula_rows=8<\/p>\n<p>get_roster()<br \/>\n{<br \/>\nfor p in `grep ^$1 formula.txt | awk &#8216;{ print $2&#8243; &#8220;$3&#8221; &#8220;$4&#8221; &#8220;$5 }&#8217;`;do<br \/>\necho -n &#8220;`grep ^$p roster.txt|awk &#8216;{ print $3&#8243; &#8221; }&#8217;`&#8221;<br \/>\ndone<br \/>\necho &#8220;&#8221;<br \/>\n}<\/p>\n<p>get_weekend_roster()<br \/>\n{<br \/>\nfor p in `grep ^$1 formula.txt | awk &#8216;{ print $2&#8243; &#8220;$3&#8221; &#8220;$2&#8221; &#8220;$3 }&#8217;`;do<br \/>\necho -n &#8220;`grep ^$p roster.txt|awk &#8216;{ print $3&#8243; &#8221; }&#8217;`&#8221;<br \/>\ndone<br \/>\necho &#8220;&#8221;<br \/>\n}<\/p>\n<p>get_oncall()<br \/>\n{<br \/>\ntmpw=0<br \/>\nW=`date -d &#8220;$start_date $tmpw day&#8221; +&#8221;%W&#8221;`<br \/>\nfor ((tmpw = 0; $(date -d &#8220;$start_date $tmpw day&#8221; +%s) &lt;= $(date -d &#8220;$end_date&#8221; +%s); tmpw += 1))<br \/>\ndo<br \/>\nP=$W<br \/>\nW=`date -d &#8220;$start_date $tmpw day&#8221; +&#8221;%W&#8221;`<br \/>\nif [ &#8220;$P&#8221; != &#8220;$W&#8221; ];then<br \/>\nlet formula_current_row+=1<br \/>\nif [ $formula_current_row -gt $formula_rows ];then<br \/>\nformula_current_row=1<br \/>\nfi<br \/>\nfi<br \/>\nD=`date -d &#8220;$start_date $tmpw day&#8221; +&#8221;%Y-%m-%d\u00a0 %a%t&#8221;`<br \/>\nISWEEKEND=`echo $D|egrep -i &#8216;sat|sun&#8217;|wc -l`<br \/>\necho -n &#8220;$D &#8221;<br \/>\nif [ $ISWEEKEND -gt 0 ];then<br \/>\nget_weekend_roster $formula_current_row<br \/>\nelse<br \/>\nget_roster $formula_current_row<br \/>\nfi<br \/>\ndone<br \/>\n}<\/p>\n<p>generate_sql()<br \/>\n{<br \/>\necho &#8220;use OnCall;&#8221;<br \/>\nget_oncall | while read l;do<br \/>\nISWEEKEND=`echo $l|egrep -i &#8216;sat|sun&#8217;|wc -l`<br \/>\nif [ $ISWEEKEND -gt 0 ];then<br \/>\necho $l | awk &#8216;{ print &#8220;insert into Detail (Date,Shift_C1_Pri,Shift_C1_Sec) values (&#8220;&#8221;$1&#8243;&#8221;,&#8221;&#8221;$3&#8243;&#8221;,&#8221;&#8221;$4&#8243;&#8221;) on DUPLICATE KEY UPDATE Shift_C1_Pri=&#8221;&#8221;$3&#8243;&#8221;,Shift_C1_Sec=&#8221;&#8221;$4&#8243;&#8221;;&#8221; }&#8217;<br \/>\nelse<br \/>\necho $l | awk &#8216;{ print &#8220;insert into Detail (Date,Shift_A1_Pri,Shift_A1_Sec,Shift_A2_Pri,Shift_A2_Sec) values (&#8220;&#8221;$1&#8243;&#8221;,&#8221;&#8221;$3&#8243;&#8221;,&#8221;&#8221;$4&#8243;&#8221;,&#8221;&#8221;$5&#8243;&#8221;,&#8221;&#8221;$6&#8243;&#8221;) on DUPLICATE KEY UPDATE Shift_A1_Pri=&#8221;&#8221;$3&#8243;&#8221;,Shift_A1_Sec=&#8221;&#8221;$4&#8243;&#8221;,Shift_A2_Pri=&#8221;&#8221;$5&#8243;&#8221;,Shift_A2_Sec=&#8221;&#8221;$6&#8243;&#8221;;&#8221; }&#8217;<br \/>\nfi<br \/>\ndone<br \/>\n}<\/p>\n<p>get_oncall<br \/>\ngenerate_sql &gt; from_${start_date}_to_${end_date}.sql<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8220;formula.txt&#8221;, 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-694","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts\/694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=694"}],"version-history":[{"count":0,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts\/694\/revisions"}],"wp:attachment":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=694"},{"taxonomy":"post_tag","embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}