update_proposals.rkt 11 KB

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