proposal_database.rkt 17 KB

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