update_proposals.rkt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. ; parameters
  11. ; start and end date to sub-select proposals within a given range
  12. (define start-date (make-parameter #f))
  13. (define end-date (make-parameter #f))
  14. ; if #t, use proposal type, submitting organiation, solicitation/call, and
  15. ; telescope name from the most recently submitted (i.e., highest ID) proposal
  16. (define reuse-params (make-parameter #f))
  17. ; set up command line arguments
  18. (define mode (command-line
  19. #:program "update_proposals"
  20. #:once-each
  21. [("-s" "--start-date") sd "Start of date range (YYYY-MM-DD)"
  22. (start-date sd)]
  23. [("-e" "--end-date") ed "End of date range (YYYY-MM-DD)"
  24. (end-date ed)]
  25. [("-r" "--reuse-parameters") "Reuse/auto-fill proposal type, submitting organization, solicitation/call and telescope name from the most recently added proposal."
  26. (reuse-params #t)]
  27. #:args ([updatetype "help"]) ; (add, update, list-open, list-closed, help)
  28. updatetype))
  29. (if (start-date)
  30. (println (start-date))
  31. (println "No start date provided"))
  32. ; print some help
  33. (define (printhelp)
  34. (displayln (string-append "Usage: "
  35. progname " MODE"))
  36. (newline)
  37. (displayln "Where MODE is one of:")
  38. (displayln " add\t\t - add new proposal to database.")
  39. (displayln " update\t\t - update a proposal with results.")
  40. (displayln " stats\t\t - print summary statistics.")
  41. (displayln " list-open\t - Show all submitted (but not resolved) proposals.")
  42. (displayln " list-closed\t - Show all resolved (accepted and rejected) proposals.")
  43. (displayln " list-accepted\t - Show accepted proposals.")
  44. (displayln " list-rejected\t - Show rejected proposals.")
  45. (displayln " help\t\t - Show this help message.")
  46. (newline)
  47. (displayln "Copyright 2019-2020, 2022-2023 George C. Privon"))
  48. ; set up a condensed prompt for getting information
  49. (define (getinput prompt)
  50. (write-string prompt)
  51. (write-string ": ")
  52. (read-line))
  53. ; take an input result from the SQL search and write it out nicely
  54. (define (printentry entry issub)
  55. (displayln (string-append
  56. (number->string (vector-ref entry 0))
  57. ": "
  58. (vector-ref entry 1)
  59. "("
  60. (vector-ref entry 2)
  61. "; PI: "
  62. (vector-ref entry 4)
  63. (if (not issub)
  64. (string-append "; "
  65. (vector-ref entry 5))
  66. "")
  67. ") \""
  68. (vector-ref entry 3)
  69. "\"")))
  70. (define (get-last-proposal-call conn)
  71. (println "Adopting proposal information from last submission")
  72. (last-proposal-call conn))
  73. ; get information from the most recent proposal submission
  74. (define (last-proposal-call conn)
  75. (query-exec conn "SELECT type, organization, solicitation, telescope FROM proposals ORDER BY id DESC LIMIT 1"))
  76. ; add a new proposal to the database
  77. (define (addnew conn)
  78. (displayln "Adding new proposal to database.")
  79. (define propinfo (cond [(reuse-params) (get-last-proposal-call conn)]
  80. [else (map getinput ("Proposal type" "Submitting Organization" "Solicitation/Call" "Telescope"))]))
  81. ; user inputs proposal data
  82. (cons propinfo (getinput "Proposal title"))
  83. (define pi (getinput "PI"))
  84. (define coi (getinput "CoIs"))
  85. ; assume all these proposals are submitted, don't ask the user
  86. (define status "submitted")
  87. (define submitdate (getinput "Submit date (YYYY-MM-DD)"))
  88. (define oID (getinput "Organization's proposal ID"))
  89. ; do the INSERT into the Sqlite database
  90. (query-exec conn "INSERT INTO proposals (type, organization, solicitation, telescope, title, PI, CoI, status, submitdate, orgpropID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
  91. proptype org solic tele title pi coi status submitdate oID))
  92. ; update an entry with new status (accepted, rejected, etc.)
  93. (define (update conn ID)
  94. (displayln (string-append "Updating entry " (number->string ID)))
  95. (define entry (query-maybe-row conn "SELECT * FROM proposals WHERE ID=?" ID))
  96. (cond
  97. [(eq? #f entry) (error "Invalid ID. Row not found")])
  98. (displayln (string-append "Current status is: "
  99. (vector-ref entry 9)
  100. " ("
  101. (vector-ref entry 10)
  102. ")"))
  103. (write-string "Please enter new status: ")
  104. (define newstatus (read-line))
  105. ;(write-string "Please enter date of updated status (leave blank to use current date): ")
  106. ;(define resdate (read-line))
  107. (define resdate (date->string (seconds->date (current-seconds))))
  108. ; now update that entry
  109. (query-exec conn "UPDATE proposals SET status=?, resultdate=? WHERE ID=?"
  110. newstatus
  111. resdate
  112. ID)
  113. (displayln "Entry updated."))
  114. ; retrieve and print proposals based on status
  115. (define (printprop conn
  116. #:submitted issub
  117. #:accepted [isaccept #f]
  118. #:rejected [isrej #f])
  119. (define selclause (string-append
  120. (if issub
  121. "status='submitted'"
  122. "status!='submitted'")
  123. ; find things that are "accepted" or "funded"
  124. (if isaccept
  125. " AND status LIKE '%Accepted%' OR status LIKE '%Funded%'"
  126. "")
  127. ; find things that are "rejected"
  128. (if isrej
  129. " AND status LIKE '%Rejected%'"
  130. "")))
  131. (define props (query-rows conn (string-append "SELECT ID,telescope,solicitation,title,PI,status FROM proposals WHERE "
  132. selclause)))
  133. (display (string-append (number->string (length props))))
  134. (if issub
  135. (displayln " pending proposals found.")
  136. (displayln " resolved proposals found."))
  137. (newline)
  138. ; print all the unresolved proposals to the screen
  139. (map (lambda (prop)
  140. (printentry prop issub))
  141. props))
  142. ; find proposals waiting for updates
  143. (define (findpending conn)
  144. (write-string "Updating proposals. ")
  145. (printprop conn #:submitted #t)
  146. (write-string "Please enter a proposal number to edit (enter 0 or nothing to exit): ")
  147. (define upID (read-line))
  148. (cond
  149. [(eq? (string->number upID) 0) (exit)]
  150. [(string->number upID) (update conn (string->number upID))]
  151. [else (exit)]))
  152. ; compute and print some statistics about proposals:
  153. ; - total number of proposals (since earliest date)
  154. ; - number of pending proposals
  155. ; - number of successful proposals and corresponding fraction of the total that are not pending
  156. ; - number of rejected proposals and corresponding fraction of the total that are not pending
  157. ; - do the above two for all proposals and for proposals that I PI'ed. (TODO: PI'ed separation not yet implemented)
  158. (define (proposal-stats conn)
  159. (displayln "Proposal statistics to date.\n")
  160. ; do statistics for all proposals
  161. (displayln "\tAll proposals")
  162. (let-values ([(Nprop Npending Nrejected) (get-stats conn)])
  163. (print-stats Nprop Npending Nrejected))
  164. ; do statistics for proposals as PI
  165. (displayln (string-append "\n\tPI'ed Proposals (by "
  166. PIname
  167. ")"))
  168. (let-values ([(Nprop Npending Nrejected) (get-stats conn #:selclause (string-append "PI LIKE '%"
  169. PIname
  170. "%'"))])
  171. (print-stats Nprop Npending Nrejected))
  172. )
  173. ; given numbers, format somewhat pretty output of proposal statistics
  174. (define (print-stats Nprop Npending Nrejected)
  175. (display (number->string Nprop))
  176. (display "\ttotal proposals entered (")
  177. (display (number->string (- Nprop Npending)))
  178. (display " proposals resolved; ")
  179. (display (number->string Npending))
  180. (displayln " proposals pending).")
  181. (define Naccepted (- Nprop Npending Nrejected))
  182. (display (number->string Naccepted))
  183. (display "\tproposals accepted (f=")
  184. (display (number->string (/ Naccepted
  185. (- Nprop Npending))))
  186. (displayln " of resolved proposals).")
  187. (display (number->string Nrejected))
  188. (display "\tproposals rejected (f=")
  189. (display (number->string (/ Nrejected
  190. (- Nprop Npending))))
  191. (displayln " of resolved proposals)."))
  192. ; retrieve proposal numbers from the database, for statistics
  193. (define (get-stats conn #:selclause [extrasel ""])
  194. (define mysel (if (eq? 0 (string-length extrasel))
  195. ""
  196. (string-append " AND "
  197. extrasel)))
  198. (define mysel-one (if (eq? 0 (string-length extrasel))
  199. ""
  200. (string-append " WHERE "
  201. extrasel)))
  202. (values
  203. ; total number of proposals
  204. (length (query-rows conn
  205. (string-append "SELECT ID FROM proposals"
  206. mysel-one)))
  207. ; Number of pending proposals
  208. (length (query-rows conn
  209. (string-append "SELECT ID FROM proposals WHERE status='submitted'"
  210. mysel)))
  211. ; Number of rejected proposals
  212. (length (query-rows conn
  213. (string-append "SELECT ID FROM proposals WHERE status LIKE '%rejected%'"
  214. mysel)))))
  215. ; make sure we can use the sqlite3 connection
  216. (define checkdblib
  217. (cond (not (sqlite3-available?))
  218. (error "Sqlite3 library not available.")))
  219. ; catch-all routine for when we need to access the database
  220. (define (querysys mode)
  221. ; first see if we need write access or if we can use read only
  222. (define dbmode (if (or (regexp-match "add" mode)
  223. (regexp-match "update" mode))
  224. 'read/write
  225. 'read-only))
  226. ; open the database with the specified mode
  227. (define conn (sqlite3-connect #:database dbloc
  228. #:mode dbmode))
  229. ; now handle the user's request
  230. (cond
  231. [(regexp-match "add" mode) (addnew conn)]
  232. [(regexp-match "update" mode) (findpending conn)]
  233. [(regexp-match "stats" mode) (proposal-stats conn)]
  234. [(regexp-match "list-open" mode) (printprop conn #:submitted #t)]
  235. [(regexp-match "list-closed" mode) (printprop conn #:submitted #f)]
  236. [(regexp-match "list-accepted" mode) (printprop conn #:submitted #f #:accepted #t)]
  237. [(regexp-match "list-rejected" mode) (printprop conn #:submitted #f #:rejected #t)]
  238. [else (error (string-append "Unknown mode. Try " progname " help\n\n"))])
  239. ; close the databse
  240. (disconnect conn))
  241. ; First see if the user wants help or if we need to pass to one of the other
  242. ; procedures
  243. (cond
  244. [(regexp-match "help" mode) (printhelp)]
  245. [else (querysys mode)])