update_proposals.rkt 11 KB

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