proposal_database.rkt 15 KB

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