proposal_database.rkt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #lang racket/base
  2. ;; This program updates an entry in a proposals database.
  3. (require racket/cmdline
  4. racket/date
  5. racket/list
  6. racket/format
  7. db
  8. "config.rkt") ; load configuration file
  9. (define progname "proposal_database.rkt")
  10. ; give us the date in YYYY-MM-DD format
  11. (date-display-format 'iso-8601)
  12. ; parameters
  13. ; start and end date to sub-select proposals within a given range
  14. (define start-date (make-parameter #f))
  15. (define end-date (make-parameter #f))
  16. ; if #t, use proposal type, submitting organiation, solicitation/call, and
  17. ; telescope name from the most recently submitted (i.e., highest ID) proposal
  18. (define reuse-params (make-parameter #f))
  19. ; Set up the mode as a parameter to be provided by command line switches
  20. (define mode (make-parameter #f))
  21. ; set up command line arguments
  22. (command-line
  23. #:program progname
  24. #:once-each
  25. [("-s" "--start-date") sd "Start of date range (YYYY-MM-DD)"
  26. (start-date sd)]
  27. [("-e" "--end-date") ed "End of date range (YYYY-MM-DD)"
  28. (end-date ed)]
  29. [("-r" "--reuse-parameters") "Reuse/auto-fill proposal type, submitting organization, solicitation/call and telescope name from the most recently added proposal."
  30. (reuse-params #t)]
  31. #:once-any
  32. [("--create-database") "Create a new database" (mode "create-database")]
  33. [("-a" "--add") "Add a new proposal" (mode "add")]
  34. [("-u" "--update") "Update a proposal outcome" (mode "update")]
  35. [("--stats") "Calculate and display summary statistics" (mode "stats")]
  36. [("--list-open") "Show all submitted (but not resolved) proposals" (mode "list-open")]
  37. [("--list-closed") "Show all resolved (accepted and rejected) proposals" (mode "list-closed")]
  38. [("--list-accepted") "Show accepted proposals" (mode "list-accepted")]
  39. [("--list-rejected") "Show rejected proposals" (mode "list-rejected")]
  40. [("--list-open-calls") "Show calls that have submitted (but not resolved) proposals" (mode "list-open-calls")]
  41. #:ps "Copyright 2019-2020, 2022-2025 George Privon")
  42. ; set up a condensed prompt for getting information
  43. (define (getinput prompt)
  44. (write-string prompt)
  45. (write-string ": ")
  46. (read-line))
  47. (define (proposal-plurals Nprop)
  48. (cond [(= Nprop 1) "proposal"]
  49. [else "proposals"]))
  50. ; take a proposal 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. ; take a call result from the SQL search and write it out nicely
  68. (define (print-call-entry entry Nprop Nprop-PI)
  69. (displayln (string-append
  70. (vector-ref entry 1)
  71. " "
  72. (vector-ref entry 2)
  73. " ("
  74. (vector-ref entry 0)
  75. ") - "
  76. (number->string Nprop-PI)
  77. (proposal-plurals Nprop-PI)
  78. (number->string (- Nprop Nprop-PI))
  79. (proposal-plurals Nprop)
  80. "pending.")))
  81. (define (get-last-proposal-call conn)
  82. (displayln "Adopting proposal information from last submission")
  83. (last-proposal-call conn))
  84. ; get information from the most recent proposal submission
  85. (define (last-proposal-call conn)
  86. (vector->list (query-row conn "SELECT type, organization, solicitation, telescope FROM proposals ORDER BY id DESC LIMIT 1")))
  87. ; create the database and create the table
  88. (define (createdb dbloc)
  89. ; make sure we can use the sqlite3 connection
  90. (cond [(not (sqlite3-available?)) (error "Sqlite3 library not available.")])
  91. ; create the database and add the `proposals` table if it doesn't exist
  92. (cond [(file-exists? dbloc) (error "Database exists. Exiting.")])
  93. (write-string (string-append "Creating database " dbloc "\n"))
  94. (define conn (sqlite3-connect #:database dbloc
  95. #:mode 'create))
  96. (query-exec conn "CREATE TABLE proposals (ID INTEGER PRIMARY KEY,
  97. type TEXT NOT NULL,
  98. organization TEXT NOT NULL,
  99. solicitation TEXT NOT NULL,
  100. telescope TEXT DEFAULT '',
  101. orgpropID TEXT NOT NULL,
  102. PI TEXT NOT NULL,
  103. title TEXT NOT NULL,
  104. CoI TEXT NOT NULL,
  105. status TEXT NOT NULL,
  106. submitdate TEXT NOT NULL,
  107. resultdate TEXT DEFAULT '')")
  108. (disconnect conn)
  109. (write-string (string-append "Database created at " dbloc "\n")))
  110. ; check to see if we can access the database
  111. (define (checkdb conn)
  112. (cond [(connected? conn) (write-string "Database created successfully.")]
  113. [else (write-string "Could not connect to database.")]))
  114. ; add a new proposal to the database
  115. (define (addnew conn)
  116. ; full list of input fileds that we will need (these will be the prompts
  117. ; to the user)
  118. (define input-fields (list "Proposal type"
  119. "Submitting Organization"
  120. "Solicitation/Call"
  121. "Telescope"
  122. "Proposal Title"
  123. "PI"
  124. "CoIs"
  125. "Submit date (YYYY-MM-DD)"
  126. "Organization's propsal ID"))
  127. (displayln "Adding new proposal to database.")
  128. ; assume all these proposals are submitted, don't ask the user
  129. (define status "submitted")
  130. ; get the proposal information
  131. (define propinfo
  132. (cond
  133. ; if we're re-using parameters, get info from the most recent submission
  134. ; and append the user input for the remaining fields
  135. [(reuse-params) (append (get-last-proposal-call conn)
  136. (map getinput (list-tail input-fields 4)))]
  137. ; if not using previous information, ask the user for all inputs
  138. [else (map getinput input-fields)]))
  139. ; do the INSERT into the Sqlite database
  140. (let* ([add-proposal-info
  141. (prepare conn "INSERT INTO proposals (type, organization, solicitation, telescope, title, PI, CoI, submitdate, orgpropID, status) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")])
  142. (query-exec conn (bind-prepared-statement add-proposal-info
  143. (flatten (list propinfo status))))))
  144. ; update an entry with new status (accepted, rejected, etc.)
  145. (define (update conn ID)
  146. (displayln (string-append "Updating entry " (number->string ID)))
  147. (define entry (query-maybe-row conn "SELECT * FROM proposals WHERE ID=?" ID))
  148. (cond
  149. [(eq? #f entry) (error "Invalid ID. Row not found")])
  150. (displayln (string-append "Current status is: "
  151. (vector-ref entry 9)
  152. " ("
  153. (vector-ref entry 10)
  154. ")"))
  155. (write-string "Please enter new status: ")
  156. (define newstatus (read-line))
  157. ;(write-string "Please enter date of updated status (leave blank to use current date): ")
  158. ;(define resdate (read-line))
  159. (define resdate (date->string (seconds->date (current-seconds))))
  160. ; now update that entry
  161. (query-exec conn "UPDATE proposals SET status=?, resultdate=? WHERE ID=?"
  162. newstatus
  163. resdate
  164. ID)
  165. (displayln "Entry updated."))
  166. ; if the user selects a date range we need to decide which date to filter on
  167. ; If they're looking at submitted (i.e., open) proposals, use the submitted
  168. ; date.
  169. ; If they're looking at closed/resolved proposals, use the dates proposals
  170. ; were resolved.
  171. (define (date-for-selection submitted)
  172. (if submitted
  173. "submitdate"
  174. "resultdate"))
  175. ; retrieve and print proposals based on status
  176. (define (printprop conn
  177. #:submitted issub
  178. #:accepted [isaccept #f]
  179. #:rejected [isrej #f])
  180. (define selclause (string-append
  181. (if issub
  182. "status='submitted'"
  183. "status!='submitted'")
  184. ; find things that are "accepted" or "funded"
  185. (if isaccept
  186. " AND (status LIKE '%Accepted%' OR status LIKE '%Funded%')"
  187. "")
  188. ; find things that are "rejected"
  189. (if isrej
  190. " AND status LIKE '%Rejected%'"
  191. "")))
  192. ; generate a selection clause if the user requested a restricted range
  193. (define dateclause (string-append
  194. (if (or (start-date) (end-date))
  195. " AND "
  196. "")
  197. (if (start-date)
  198. (string-append
  199. " DATE("
  200. (date-for-selection issub)
  201. ") >= DATE('"
  202. (start-date)
  203. "') ")
  204. "")
  205. (if (and (start-date) (end-date))
  206. " AND "
  207. "")
  208. (if (end-date)
  209. (string-append
  210. " DATE("
  211. (date-for-selection issub)
  212. ") <= DATE('"
  213. (end-date)
  214. "') ")
  215. "")))
  216. (define props (query-rows conn (string-append "SELECT ID,telescope,solicitation,title,PI,status FROM proposals WHERE "
  217. selclause
  218. dateclause)))
  219. (display (string-append (number->string (length props))
  220. (cond [issub " pending "]
  221. [isaccept " accepted "]
  222. [isrej " rejected "])
  223. (proposal-plurals (length props))
  224. " found."))
  225. (newline)
  226. ; print all the unresolved proposals to the screen
  227. (map (lambda (prop)
  228. (printentry prop issub))
  229. props))
  230. ; retrieve and print proposal calls
  231. (define (print-open-calls conn)
  232. (define call-entries (query-rows conn (string-append "SELECT DISTINCT organization,telescope,solicitation FROM proposals WHERE status='submitted'")))
  233. (displayln (string-append (number->string (length call-entries))
  234. " pending calls found."))
  235. (newline)
  236. ; print all the unresolved proposals to the screen
  237. (map (lambda (call-entry)
  238. (define Nprop (query-value conn (string-append "SELECT COUNT(DISTINCT ID) from proposals WHERE status='submitted' AND solicitation='"
  239. (vector-ref call-entry 2)
  240. "'")))
  241. (define Nprop-PI
  242. (query-value conn (string-append "SELECT COUNT(DISTINCT ID) from proposals WHERE status='submitted' AND solicitation='"
  243. (vector-ref call-entry 2)
  244. "' AND PI LIKE '%"
  245. PIname
  246. "%'")))
  247. (print-call-entry call-entry Nprop Nprop-PI))
  248. call-entries))
  249. ; find proposals waiting for updates
  250. (define (findpending conn)
  251. (write-string "Updating proposals. ")
  252. (printprop conn #:submitted #t)
  253. (write-string "Please enter a proposal number to edit (enter 0 or nothing to exit): ")
  254. (define upID (read-line))
  255. (cond
  256. [(eq? (string->number upID) 0) (exit)]
  257. [(string->number upID) (update conn (string->number upID))]
  258. [else (exit)]))
  259. ; compute and print some statistics about proposals:
  260. ; - total number of proposals (since earliest date)
  261. ; - number of pending proposals
  262. ; - number of successful proposals and corresponding fraction of the total that are not pending
  263. ; - number of rejected proposals and corresponding fraction of the total that are not pending
  264. ; - do the above two for all proposals and for proposals that I PI'ed. (TODO: PI'ed separation not yet implemented)
  265. (define (proposal-stats conn)
  266. (displayln "Proposal statistics to date.\n")
  267. ; do statistics for all proposals
  268. (displayln "\tAll proposals")
  269. (let-values ([(Nprop Npending Nrejected) (get-stats conn)])
  270. (print-stats Nprop Npending Nrejected))
  271. ; do statistics for proposals as PI
  272. (displayln (string-append "\n\tPI'ed Proposals (by "
  273. PIname
  274. ")"))
  275. (let-values ([(Nprop Npending Nrejected) (get-stats conn #:selclause (string-append "PI LIKE '%"
  276. PIname
  277. "%'"))])
  278. (print-stats Nprop Npending Nrejected))
  279. )
  280. ; given numbers, format somewhat pretty output of proposal statistics
  281. (define (print-stats Nprop Npending Nrejected)
  282. (displayln (string-append (number->string Nprop)
  283. "\ttotal "
  284. (proposal-plurals Nprop)
  285. " entered ("
  286. (number->string (- Nprop Npending))
  287. " "
  288. (proposal-plurals (- Nprop Npending))
  289. " resolved; "
  290. (number->string Npending)
  291. " "
  292. (proposal-plurals Npending)
  293. " pending)."))
  294. (define Naccepted (- Nprop Npending Nrejected))
  295. (displayln (string-append (number->string Naccepted)
  296. "\t"
  297. (proposal-plurals Naccepted)
  298. " accepted (f="
  299. (~r (/ Naccepted
  300. (- Nprop Npending))
  301. #:precision `(= 3))
  302. " of resolved proposals)."))
  303. (displayln (string-append (number->string Nrejected)
  304. "\t"
  305. (proposal-plurals Nrejected)
  306. " accepted (f="
  307. (~r (/ Nrejected
  308. (- Nprop Npending))
  309. #:precision `(= 3))
  310. " of resolved proposals).")))
  311. ; retrieve proposal numbers from the database, for statistics
  312. (define (get-stats conn #:selclause [extrasel ""])
  313. (define mysel (if (eq? 0 (string-length extrasel))
  314. ""
  315. (string-append " AND "
  316. extrasel)))
  317. (define mysel-one (if (eq? 0 (string-length extrasel))
  318. ""
  319. (string-append " WHERE "
  320. extrasel)))
  321. (values
  322. ; total number of proposals
  323. (query-value conn
  324. (string-append "SELECT COUNT(DISTINCT ID) FROM proposals"
  325. mysel-one))
  326. ; Number of pending proposals
  327. (query-value conn
  328. (string-append "SELECT COUNT(DISTINCT ID) FROM proposals WHERE status='submitted'"
  329. mysel))
  330. ; Number of rejected proposals
  331. (query-value conn
  332. (string-append "SELECT COUNT(DISTINCT ID) FROM proposals WHERE status LIKE '%rejected%'"
  333. mysel))))
  334. ; make sure we can use the sqlite3 connection
  335. (define checkdblib
  336. (cond (not (sqlite3-available?))
  337. (error "Sqlite3 library not available.")))
  338. ; catch-all routine for when we need to access the database
  339. (define (querysys mode)
  340. ; check if the user would like to create the database or not
  341. (cond
  342. [(string=? "create-database" mode) (createdb dbloc)])
  343. ; see if we need write access or if we can use read only
  344. (define dbmode (if (or (string=? "add" mode)
  345. (string=? "update" mode))
  346. 'read/write
  347. 'read-only))
  348. ; open the database with the specified mode
  349. (define conn (sqlite3-connect #:database dbloc
  350. #:mode dbmode))
  351. ; now handle the user's request
  352. (cond
  353. [(string=? "create-database" mode) (checkdb conn)]
  354. [(string=? "add" mode) (addnew conn)]
  355. [(string=? "update" mode) (findpending conn)]
  356. [(string=? "stats" mode) (proposal-stats conn)]
  357. [(string=? "list-open-calls" mode) (print-open-calls conn)]
  358. [(string=? "list-open" mode) (printprop conn #:submitted #t)]
  359. [(string=? "list-closed" mode) (printprop conn #:submitted #f)]
  360. [(string=? "list-accepted" mode) (printprop conn #:submitted #f #:accepted #t)]
  361. [(string=? "list-rejected" mode) (printprop conn #:submitted #f #:rejected #t)]
  362. [else (error (string-append "Unknown mode. Try " progname " help\n\n"))])
  363. ; close the databse
  364. (disconnect conn))
  365. (querysys (mode))