update_proposals.rkt 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #lang racket/base
  2. ;; This program updates an entry in a proposals database.
  3. (require racket/cmdline
  4. racket/date
  5. db
  6. "config.rkt") ; load configuration file
  7. (define progname "update_proposals.rkt")
  8. ; give us the date in YYYY-MM-DD format
  9. (date-display-format 'iso-8601)
  10. ; set up command line arguments
  11. (define mode (command-line
  12. #:program "update_proposals"
  13. #:args ([updatetype "help"]) ; (add, update, list-open, list-closed, help)
  14. updatetype))
  15. ; print some help
  16. (define (printhelp)
  17. (displayln (string-append "Usage: "
  18. progname " MODE"))
  19. (newline)
  20. (displayln "Where MODE is one of:")
  21. (displayln " add\t\t - add new proposal to database.")
  22. (displayln " update\t\t - update a proposal with results.")
  23. (displayln " stats\t\t - print summary statistics.")
  24. (displayln " list-open\t - Show all submitted (but not resolved) proposals.")
  25. (displayln " list-closed\t - Show all resolved proposals.")
  26. (displayln " list-accepted\t - Show accepted proposals.")
  27. (displayln " list-rejected\t - Show rejected proposals.")
  28. (displayln " help\t\t - Show this help message.")
  29. (newline)
  30. (displayln "Copyright 2019-2020, 2022 George C. Privon"))
  31. ; set up a condensed prompt for getting information
  32. (define (getinput prompt)
  33. (write-string prompt)
  34. (write-string ": ")
  35. (read-line))
  36. ; take an input result from the SQL search and write it out nicely
  37. (define (printentry entry issub)
  38. (displayln (string-append
  39. (number->string (vector-ref entry 0))
  40. ": "
  41. (vector-ref entry 1)
  42. "("
  43. (vector-ref entry 2)
  44. "; PI: "
  45. (vector-ref entry 4)
  46. (if (not issub)
  47. (string-append "; "
  48. (vector-ref entry 5))
  49. "")
  50. ") \""
  51. (vector-ref entry 3)
  52. "\"")))
  53. ; add a new proposal to the database
  54. (define (addnew conn)
  55. (displayln "Adding new proposal to database.")
  56. ; user inputs proposal data
  57. (define proptype (getinput "Proposal type"))
  58. (define org (getinput "Submitting organization"))
  59. (define solic (getinput "Solitation/call"))
  60. (define tele (getinput "Telescope"))
  61. (define title (getinput "Proposal title"))
  62. (define pi (getinput "PI"))
  63. (define coi (getinput "CoIs"))
  64. ; assume all these proposals are submitted, don't ask the user
  65. (define status "submitted")
  66. (define submitdate (getinput "Submit date (YYYY-MM-DD)"))
  67. (define oID (getinput "Organization's proposal ID"))
  68. ; do the INSERT into the Sqlite database
  69. (query-exec conn "INSERT INTO proposals (type, organization, solicitation, telescope, PI, title, CoI, status, submitdate, orgpropID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
  70. proptype org solic tele pi title coi status submitdate oID))
  71. ; update an entry with new status (accepted, rejected, etc.)
  72. (define (update conn ID)
  73. (displayln (string-append "Updating entry " (number->string ID)))
  74. (define entry (query-maybe-row conn "SELECT * FROM proposals WHERE ID=?" ID))
  75. (cond
  76. [(eq? #f entry) (error "Invalid ID. Row not found")])
  77. (displayln (string-append "Current status is: "
  78. (vector-ref entry 9)
  79. " ("
  80. (vector-ref entry 10)
  81. ")"))
  82. (write-string "Please enter new status: ")
  83. (define newstatus (read-line))
  84. ;(write-string "Please enter date of updated status (leave blank to use current date): ")
  85. ;(define resdate (read-line))
  86. (define resdate (date->string (seconds->date (current-seconds))))
  87. ; now update that entry
  88. (query-exec conn "UPDATE proposals SET status=?, resultdate=? WHERE ID=?"
  89. newstatus
  90. resdate
  91. ID)
  92. (displayln "Entry updated."))
  93. ; retrieve and print proposals based on status
  94. (define (printprop conn
  95. #:submitted issub
  96. #:accepted [isaccept #f]
  97. #:rejected [isrej #f])
  98. (define selclause (string-append
  99. (if issub
  100. "status='submitted'"
  101. "status!='submitted'")
  102. ; find things that are "accepted" or "funded"
  103. (if isaccept
  104. " AND status LIKE '%Accepted%' OR status LIKE '%Funded%'"
  105. "")
  106. ; find things that are "rejected"
  107. (if isrej
  108. " AND status LIKE '%Rejected%'"
  109. "")))
  110. (define props (query-rows conn (string-append "SELECT ID,telescope,solicitation,title,PI,status FROM proposals WHERE "
  111. selclause)))
  112. (display (string-append (number->string (length props))))
  113. (if issub
  114. (displayln " pending proposals found.")
  115. (displayln " resolved proposals found."))
  116. (newline)
  117. ; print all the unresolved proposals to the screen
  118. (map (lambda (prop)
  119. (printentry prop issub))
  120. props))
  121. ; find proposals waiting for updates
  122. (define (findpending conn)
  123. (write-string "Updating proposals. ")
  124. (printprop conn #:submitted #t)
  125. (write-string "Please enter a proposal number to edit (enter 0 or nothing to exit): ")
  126. (define upID (read-line))
  127. (cond
  128. [(eq? (string->number upID) 0) (exit)]
  129. [(string->number upID) (update conn (string->number upID))]
  130. [else (exit)]))
  131. ; compute and print some statistics about proposals:
  132. ; - total number of proposals (since earliest date)
  133. ; - number of pending proposals
  134. ; - number of successful proposals and corresponding fraction of the total that are not pending
  135. ; - number of rejected proposals and corresponding fraction of the total that are not pending
  136. ; - do the above two for all proposals and for proposals that I PI'ed. (TODO: PI'ed separation not yet implemented)
  137. (define (proposal-stats conn)
  138. (displayln "Proposal statistics to date.\n")
  139. (displayln "\tAll proposals")
  140. (define Nprop (length (query-rows conn
  141. "SELECT ID FROM proposals;")))
  142. (display (number->string Nprop))
  143. (display "\ttotal proposals entered (")
  144. (define Npending (length (query-rows conn
  145. "SELECT ID FROM proposals WHERE status='submitted'")))
  146. (display (number->string (- Nprop Npending)))
  147. (display " proposals resolved; ")
  148. (display (number->string Npending))
  149. (displayln " proposals pending).")
  150. (define Nrejected (length (query-rows conn
  151. "SELECT ID FROM proposals WHERE status LIKE '%rejected%'")))
  152. (define Naccepted (- Nprop Npending Nrejected))
  153. (display (number->string Naccepted))
  154. (display "\tproposals accepted (f=")
  155. (display (number->string (/ Naccepted
  156. (- Nprop Npending))))
  157. (displayln " of resolved proposals).")
  158. (display (number->string Nrejected))
  159. (display "\tproposals rejected (f=")
  160. (display (number->string (/ Nrejected
  161. (- Nprop Npending))))
  162. (displayln " of resolved proposals)."))
  163. ; make sure we can use the sqlite3 connection
  164. (define checkdblib
  165. (cond (not (sqlite3-available?))
  166. (error "Sqlite3 library not available.")))
  167. ; catch-all routine for when we need to access the database
  168. (define (querysys mode)
  169. ; first see if we need write access or if we can use read only
  170. (define dbmode (if (or (regexp-match "add" mode)
  171. (regexp-match "update" mode))
  172. 'read/write
  173. 'read-only))
  174. ; open the database with the specified mode
  175. (define conn (sqlite3-connect #:database dbloc
  176. #:mode dbmode))
  177. ; now handle the user's request
  178. (cond
  179. [(regexp-match "add" mode) (addnew conn)]
  180. [(regexp-match "update" mode) (findpending conn)]
  181. [(regexp-match "stats" mode) (proposal-stats conn)]
  182. [(regexp-match "list-open" mode) (printprop conn #:submitted #t)]
  183. [(regexp-match "list-closed" mode) (printprop conn #:submitted #f)]
  184. [(regexp-match "list-accepted" mode) (printprop conn #:submitted #f #:accepted #t)]
  185. [(regexp-match "list-rejected" mode) (printprop conn #:submitted #f #:rejected #t)]
  186. [else (error (string-append "Unknown mode. Try " progname " help\n\n"))])
  187. ; close the databse
  188. (disconnect conn))
  189. ; First see if the user wants help or if we need to pass to one of the other
  190. ; procedures
  191. (cond
  192. [(regexp-match "help" mode) (printhelp)]
  193. [else (querysys mode)])