query_functions 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # Modalità "query"
  2. # Variabili e funzioni di supporto
  3. # a modalità query
  4. # Controlla che l'id in ingresso esista
  5. # e lo converte nel nome dell'operatore corrispondente
  6. # (se non esiste, ferma il programma esce con un errore)
  7. id_to_name_check(){
  8. case "$1" in
  9. 0)
  10. echo "$ID_0_150"
  11. ;;
  12. 1)
  13. echo "$ID_1_150"
  14. ;;
  15. 2)
  16. echo "$ID_2_150"
  17. ;;
  18. *)
  19. echoerr "Illegal operator ID specified. Choose between 0,1,2."
  20. esac
  21. return 0
  22. }
  23. # Prendendo in ingresso l'id di un operatore
  24. # restituisce il numero totale di ore di lavoro
  25. get_operator_total(){
  26. OPERATOR_NAME=$(id_to_name_check "$1") || exit 1
  27. awk -F\; -v pattern="$OPERATOR_NAME" '
  28. BEGIN { counter=0 }
  29. $0 ~ pattern { counter+=$3; }
  30. END{ print counter }' "$SHIFT_FILE"
  31. return 0
  32. }
  33. # Grand total mode
  34. print_gtotal(){
  35. echo -e "\nHere it comes the operators' grand total:"
  36. for i in {0..2}
  37. do
  38. printf "%-30s%10s/120h\n" "$(id_to_name_check $i):" "$(get_operator_total $i)"
  39. done
  40. }
  41. # Prendendo in stdin una lista di entrate
  42. # restituisce le date corrispondente.
  43. get_dates(){
  44. cut -f1 -d';' | cut -f1 -d':' 2>/dev/null
  45. return 0
  46. }
  47. # Prendendo come primo argomento il nome di un
  48. # operatore restituisce una lista delle date
  49. # nelle quali un suo turno è stato registrato.
  50. get_operator_dates(){
  51. grep "$1" "$SHIFT_FILE" | get_dates | uniq 2>/dev/null
  52. return 0
  53. }
  54. # Mostra a schermo le date relativi ai turni
  55. # dell'operatore richiesto in linea di comando
  56. # se questo esiste
  57. show_operator_dates(){
  58. check_operator "$QUERY_OPERATOR" ||\
  59. echoerr "\"$QUERY_OPERATOR\" is not an LCM operator."
  60. local OPERATOR_DATES="$(get_operator_dates "$QUERY_OPERATOR")"
  61. [ -n "$OPERATOR_DATES" ] ||\
  62. { echo -e "\nNo dates for \"$QUERY_OPERATOR\"."; exit 0; }
  63. ifpaginate "Shifts dates for \"$QUERY_OPERATOR\":\n\n$OPERATOR_DATES"
  64. return 0
  65. }
  66. # Controlla che il primo argomento sia
  67. # una data e nel caso l'assegna alla variabile
  68. # corretta
  69. ifformat_date(){
  70. local DATE="$(check_date "$1")"
  71. if [ -n "$DATE" ]
  72. then
  73. QUERY_DATE="$DATE"
  74. else
  75. return 1
  76. fi
  77. return 0
  78. }
  79. # Prende come primo argomento una data e printa
  80. # la lista delle entrate per la stessa, se presenti.
  81. get_entries_by_date(){
  82. grep "$1" "$SHIFT_FILE" 2>/dev/null ||\
  83. return 1
  84. return 0
  85. }
  86. # Prendendo come primo argomento un'entrata
  87. # restituisce l'operatore corrispondente.
  88. get_operator_from_entry(){
  89. cut -f2 -d';' <<< "$1" 2>/dev/null
  90. return 0
  91. }
  92. # Prendendo come primo argomento un'entrata
  93. # restituisce la durata del turno corrispondente.
  94. get_shift_duration_from_entry(){
  95. cut -f3 -d';' <<< "$1" 2>/dev/null
  96. return 0
  97. }
  98. # Prendendo come primo argomento un'entrata
  99. # restituisce il commento corrispondente.
  100. get_comment_from_entry(){
  101. local COMMENT="$(cut -f4 -d';' <<< "$1" 2>/dev/null)"
  102. if [ -z "$COMMENT" ]
  103. then
  104. COMMENT="[No comment]"
  105. else
  106. COMMENT="\"$(base64 -d <<<"$COMMENT")\""
  107. fi
  108. echo "$COMMENT"
  109. return 0
  110. }
  111. # Prende un'entrata come primo argomento
  112. # e la printa in formato leggibile per
  113. # esseri umani.
  114. entry_to_human(){
  115. echo "operator: $(get_operator_from_entry "$1")"
  116. echo "shift duration: $(get_shift_duration_from_entry "$1")h"
  117. echo -e "comment: $(get_comment_from_entry "$1")\n"
  118. return 0
  119. }
  120. # Mostra a schermo le entrate relative alla data
  121. # richiesta se presenti.
  122. show_entries_by_date(){
  123. ifformat_date "$QUERY_DATE" ||\
  124. echoerr "\"$QUERY_DATE\" is not a valid date."
  125. local DATE_ENTRIES="$(get_entries_by_date "$QUERY_DATE")"
  126. [ -n "$DATE_ENTRIES" ] ||\
  127. { echo -e "\nNo entries for \"$QUERY_DATE\"."; exit 0; }
  128. local DATE_ENTRIES_HUMAN="$(for i in $DATE_ENTRIES; do entry_to_human "$i"; done)"
  129. ifpaginate "Entries for \"$QUERY_DATE\":\n\n$DATE_ENTRIES_HUMAN"
  130. return 0
  131. }