Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lucywyman/4c1e893844db1b7dd0e5e0de1467a455 to your computer and use it in GitHub Desktop.
Save lucywyman/4c1e893844db1b7dd0e5e0de1467a455 to your computer and use it in GitHub Desktop.
(defn get-plan-job-status
"Returns a list of states for a plan job"
[db plan-job-id]
{:pre [(some? db)]}
(sql/with-db-transaction [conn db]
(let [query {:select [:jobs.id
[(h/call :json_agg
(h/call :json_build_object
"state" :job_statuses.state
"enter_time" :job_statuses.enter_time
"exit_time" :job_statuses.exit_time)) :result]]
:from [:job_statuses :jobs]
:inner-join [:jobs [:= :job_statuses.job_id :jobs.id]]
:where [:= :jobs.plan_job_id plan-job-id]
:group-by [:jobs.id]}
result (sql/query conn (h/format query))
; result is ({:id 1, :result #object[org.postgresql.util.PGobject 0x2fb34336 [{"state" : "ready", "enter_time" : "2018-05-17T14:49:22.356292", "exit_time" : "2018-05-17T14:49:22.366159"}]})
formatted (map (fn [m] {(:id m) (keywordize-keys
(parse-jsonb-object
(:result m)))}) result)
; Map returns a collection of element which is our hash of statuses,
; but we just need the hash of statuses
statuses (first formatted)]
(if (nil? statuses) {} statuses))))
@lucywyman
Copy link
Author

Prints

({:id 1, :result #object[org.postgresql.util.PGobject 0x2fb34336 [{"state" : "ready", "enter_time" : "2018-05-17T14:49:22.356292", "exit_time" : "2018-05-17T14:49:22.366159"}, {"state" : "ready", "enter_time" : "2018-05-17T14:49:22.403872", "exit_time" : "2018-05-17T14:49:22.413278"}, {"state" : "running", "enter_time" : "2018-05-17T14:49:22.366159", "exit_time" : "2018-05-17T14:49:22.409824"}, {"state" : "running", "enter_time" : "2018-05-17T14:49:22.413278", "exit_time" : null}, {"state" : "failed", "enter_time" : "2018-05-17T14:49:22.409824", "exit_time" : null}]]} {:id 2, :result #object[org.postgresql.util.PGobject 0x2a45121c [{"state" : "ready", "enter_time" : "2018-05-17T14:49:22.356292", "exit_time" : "2018-05-17T14:49:22.366159"}, {"state" : "ready", "enter_time" : "2018-05-17T14:49:22.403872", "exit_time" : "2018-05-17T14:49:22.413278"}, {"state" : "running", "enter_time" : "2018-05-17T14:49:22.366159", "exit_time" : "2018-05-17T14:49:22.409824"}, {"state" : "running", "enter_time" : "2018-05-17T14:49:22.413278", "exit_time" : null}, {"state" : "failed", "enter_time" : "2018-05-17T14:49:22.409824", "exit_time" : null}]]})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment