update_proposals.rkt 11 KB

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