proposal_database.rkt 15 KB

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