proposal_database.rkt 17 KB

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