update_proposals.rkt 11 KB

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