update_proposals.rkt 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. ; do statistics for all proposals
  140. (displayln "\tAll proposals")
  141. (let-values ([(Nprop Npending Nrejected) (get-stats conn)])
  142. (print-stats Nprop Npending Nrejected))
  143. ; do statistics for proposals as PI
  144. (displayln (string-append "\n\tPI'ed Proposals (by "
  145. PIname
  146. ")"))
  147. (let-values ([(Nprop Npending Nrejected) (get-stats conn #:selclause (string-append "PI LIKE '%"
  148. PIname
  149. "%'"))])
  150. (print-stats Nprop Npending Nrejected))
  151. )
  152. ; given numbers, format somewhat pretty output of proposal statistics
  153. (define (print-stats Nprop Npending Nrejected)
  154. (display (number->string Nprop))
  155. (display "\ttotal proposals entered (")
  156. (display (number->string (- Nprop Npending)))
  157. (display " proposals resolved; ")
  158. (display (number->string Npending))
  159. (displayln " proposals pending).")
  160. (define Naccepted (- Nprop Npending Nrejected))
  161. (display (number->string Naccepted))
  162. (display "\tproposals accepted (f=")
  163. (display (number->string (/ Naccepted
  164. (- Nprop Npending))))
  165. (displayln " of resolved proposals).")
  166. (display (number->string Nrejected))
  167. (display "\tproposals rejected (f=")
  168. (display (number->string (/ Nrejected
  169. (- Nprop Npending))))
  170. (displayln " of resolved proposals)."))
  171. ; retrieve proposal numbers from the database, for statistics
  172. (define (get-stats conn #:selclause [extrasel ""])
  173. (define mysel (if (eq? 0 (string-length extrasel))
  174. ""
  175. (string-append " AND "
  176. extrasel)))
  177. (define mysel-one (if (eq? 0 (string-length extrasel))
  178. ""
  179. (string-append " WHERE "
  180. extrasel)))
  181. (values
  182. ; total number of proposals
  183. (length (query-rows conn
  184. (string-append "SELECT ID FROM proposals"
  185. mysel-one)))
  186. ; Number of pending proposals
  187. (length (query-rows conn
  188. (string-append "SELECT ID FROM proposals WHERE status='submitted'"
  189. mysel)))
  190. ; Number of rejected proposals
  191. (length (query-rows conn
  192. (string-append "SELECT ID FROM proposals WHERE status LIKE '%rejected%'"
  193. mysel)))))
  194. ; make sure we can use the sqlite3 connection
  195. (define checkdblib
  196. (cond (not (sqlite3-available?))
  197. (error "Sqlite3 library not available.")))
  198. ; catch-all routine for when we need to access the database
  199. (define (querysys mode)
  200. ; first see if we need write access or if we can use read only
  201. (define dbmode (if (or (regexp-match "add" mode)
  202. (regexp-match "update" mode))
  203. 'read/write
  204. 'read-only))
  205. ; open the database with the specified mode
  206. (define conn (sqlite3-connect #:database dbloc
  207. #:mode dbmode))
  208. ; now handle the user's request
  209. (cond
  210. [(regexp-match "add" mode) (addnew conn)]
  211. [(regexp-match "update" mode) (findpending conn)]
  212. [(regexp-match "stats" mode) (proposal-stats conn)]
  213. [(regexp-match "list-open" mode) (printprop conn #:submitted #t)]
  214. [(regexp-match "list-closed" mode) (printprop conn #:submitted #f)]
  215. [(regexp-match "list-accepted" mode) (printprop conn #:submitted #f #:accepted #t)]
  216. [(regexp-match "list-rejected" mode) (printprop conn #:submitted #f #:rejected #t)]
  217. [else (error (string-append "Unknown mode. Try " progname " help\n\n"))])
  218. ; close the databse
  219. (disconnect conn))
  220. ; First see if the user wants help or if we need to pass to one of the other
  221. ; procedures
  222. (cond
  223. [(regexp-match "help" mode) (printhelp)]
  224. [else (querysys mode)])