update_proposals.rkt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. selclause
  154. dateclause)))
  155. (display (string-append (number->string (length props))))
  156. (if issub
  157. (displayln " pending proposals found.")
  158. (displayln " resolved proposals found."))
  159. (newline)
  160. ; print all the unresolved proposals to the screen
  161. (map (lambda (prop)
  162. (printentry prop issub))
  163. props))
  164. ; find proposals waiting for updates
  165. (define (findpending conn)
  166. (write-string "Updating proposals. ")
  167. (printprop conn #:submitted #t)
  168. (write-string "Please enter a proposal number to edit (enter 0 or nothing to exit): ")
  169. (define upID (read-line))
  170. (cond
  171. [(eq? (string->number upID) 0) (exit)]
  172. [(string->number upID) (update conn (string->number upID))]
  173. [else (exit)]))
  174. ; compute and print some statistics about proposals:
  175. ; - total number of proposals (since earliest date)
  176. ; - number of pending proposals
  177. ; - number of successful proposals and corresponding fraction of the total that are not pending
  178. ; - number of rejected proposals and corresponding fraction of the total that are not pending
  179. ; - do the above two for all proposals and for proposals that I PI'ed. (TODO: PI'ed separation not yet implemented)
  180. (define (proposal-stats conn)
  181. (displayln "Proposal statistics to date.\n")
  182. ; do statistics for all proposals
  183. (displayln "\tAll proposals")
  184. (let-values ([(Nprop Npending Nrejected) (get-stats conn)])
  185. (print-stats Nprop Npending Nrejected))
  186. ; do statistics for proposals as PI
  187. (displayln (string-append "\n\tPI'ed Proposals (by "
  188. PIname
  189. ")"))
  190. (let-values ([(Nprop Npending Nrejected) (get-stats conn #:selclause (string-append "PI LIKE '%"
  191. PIname
  192. "%'"))])
  193. (print-stats Nprop Npending Nrejected))
  194. )
  195. ; given numbers, format somewhat pretty output of proposal statistics
  196. (define (print-stats Nprop Npending Nrejected)
  197. (display (number->string Nprop))
  198. (display "\ttotal proposals entered (")
  199. (display (number->string (- Nprop Npending)))
  200. (display " proposals resolved; ")
  201. (display (number->string Npending))
  202. (displayln " proposals pending).")
  203. (define Naccepted (- Nprop Npending Nrejected))
  204. (display (number->string Naccepted))
  205. (display "\tproposals accepted (f=")
  206. (display (number->string (/ Naccepted
  207. (- Nprop Npending))))
  208. (displayln " of resolved proposals).")
  209. (display (number->string Nrejected))
  210. (display "\tproposals rejected (f=")
  211. (display (number->string (/ Nrejected
  212. (- Nprop Npending))))
  213. (displayln " of resolved proposals)."))
  214. ; retrieve proposal numbers from the database, for statistics
  215. (define (get-stats conn #:selclause [extrasel ""])
  216. (define mysel (if (eq? 0 (string-length extrasel))
  217. ""
  218. (string-append " AND "
  219. extrasel)))
  220. (define mysel-one (if (eq? 0 (string-length extrasel))
  221. ""
  222. (string-append " WHERE "
  223. extrasel)))
  224. (values
  225. ; total number of proposals
  226. (length (query-rows conn
  227. (string-append "SELECT ID FROM proposals"
  228. mysel-one)))
  229. ; Number of pending proposals
  230. (length (query-rows conn
  231. (string-append "SELECT ID FROM proposals WHERE status='submitted'"
  232. mysel)))
  233. ; Number of rejected proposals
  234. (length (query-rows conn
  235. (string-append "SELECT ID FROM proposals WHERE status LIKE '%rejected%'"
  236. mysel)))))
  237. ; make sure we can use the sqlite3 connection
  238. (define checkdblib
  239. (cond (not (sqlite3-available?))
  240. (error "Sqlite3 library not available.")))
  241. ; catch-all routine for when we need to access the database
  242. (define (querysys mode)
  243. ; first see if we need write access or if we can use read only
  244. (define dbmode (if (or (regexp-match "add" mode)
  245. (regexp-match "update" mode))
  246. 'read/write
  247. 'read-only))
  248. ; open the database with the specified mode
  249. (define conn (sqlite3-connect #:database dbloc
  250. #:mode dbmode))
  251. ; now handle the user's request
  252. (cond
  253. [(regexp-match "add" mode) (addnew conn)]
  254. [(regexp-match "update" mode) (findpending conn)]
  255. [(regexp-match "stats" mode) (proposal-stats conn)]
  256. [(regexp-match "list-open" mode) (printprop conn #:submitted #t)]
  257. [(regexp-match "list-closed" mode) (printprop conn #:submitted #f)]
  258. [(regexp-match "list-accepted" mode) (printprop conn #:submitted #f #:accepted #t)]
  259. [(regexp-match "list-rejected" mode) (printprop conn #:submitted #f #:rejected #t)]
  260. [else (error (string-append "Unknown mode. Try " progname " help\n\n"))])
  261. ; close the databse
  262. (disconnect conn))
  263. ; First see if the user wants help or if we need to pass to one of the other
  264. ; procedures
  265. (cond
  266. [(regexp-match "help" mode) (printhelp)]
  267. [else (querysys mode)])