proposal_database.rkt 16 KB

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