From 1467bfae9c7971ba04429a1741d6d8549f5acbe2 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Tue, 20 Feb 2018 13:22:08 -0300 Subject: Adds more test and doc --- src/clj/queue_api/db/core.clj | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'src/clj/queue_api') diff --git a/src/clj/queue_api/db/core.clj b/src/clj/queue_api/db/core.clj index 0381e1e..3210a6c 100644 --- a/src/clj/queue_api/db/core.clj +++ b/src/clj/queue_api/db/core.clj @@ -36,18 +36,18 @@ (defn agent-jobs "Get a job that has a agent bounded with given `id` and a status of `s`" [id s] - (let [q (d/q '[:find ?jid - :in $ ?id ?s - :where - [?e :agent/id ?id] - [?x :job/id ?jid] - [?x :job/status ?s] - [?x :job/agent ?e]] - @conn id s)] - (map #(d/entity @conn [:job/id (first %)]) q))) + (->> (d/q '[:find ?jid + :in $ ?id ?s + :where + [?e :agent/id ?id] + [?x :job/id ?jid] + [?x :job/status ?s] + [?x :job/agent ?e]] + @conn id s) + (map #(d/entity @conn [:job/id (first %)])))) (defn q-job - "Fetch job and sort by date + "Query jobs and sort them by date `u`: urgent flag `s`: status of the job `ts`: types of the job" @@ -66,7 +66,7 @@ (map #(d/entity @conn [:job/id (last %)])))) (defn q-status - "Query job filtering only by `s`" + "Query job by status `s`" [s] (d/q '[:find ?id :in $ ?status :where @@ -75,7 +75,7 @@ @conn s)) (defn sum-agent - "Get how many jobs a agent (`id`) has performed aggregated by type" + "Get how many jobs a agent `id` has performed aggregated by type" [id] (let [jobs (agent-jobs id :completed)] (reduce (fn [l r] @@ -85,7 +85,7 @@ (conj l {t (inc (get l t))})))) {} jobs))) (defn sum-queue - "Count all job aggregated by type" + "Count all jobs aggregated by type" [] {:completed (map first (q-status :completed)) :processing (map first (q-status :processing)) -- cgit v1.2.3 From b8f65dd00187023edf33ea462b92f50b01e32b00 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Tue, 20 Feb 2018 13:34:57 -0300 Subject: Improve code idiomaticness (thanks to kibit) --- src/clj/queue_api/db/core.clj | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'src/clj/queue_api') diff --git a/src/clj/queue_api/db/core.clj b/src/clj/queue_api/db/core.clj index 3210a6c..00af5cb 100644 --- a/src/clj/queue_api/db/core.clj +++ b/src/clj/queue_api/db/core.clj @@ -36,15 +36,15 @@ (defn agent-jobs "Get a job that has a agent bounded with given `id` and a status of `s`" [id s] - (->> (d/q '[:find ?jid + (map #(d/entity @conn [:job/id (first %)]) + (d/q '[:find ?jid :in $ ?id ?s :where [?e :agent/id ?id] [?x :job/id ?jid] [?x :job/status ?s] [?x :job/agent ?e]] - @conn id s) - (map #(d/entity @conn [:job/id (first %)])))) + @conn id s))) (defn q-job "Query jobs and sort them by date @@ -96,10 +96,10 @@ if nothing was found query for not urgent ones." [s] (let [ju (q-job true :unassigned s)] - (if (not (empty? ju)) + (if (seq ju) (first ju) (let [jn (q-job false :unassigned s)] - (if (not (empty? jn)) + (if (seq jn) (first jn)))))) (defn request-job @@ -108,7 +108,7 @@ (let [a (d/entity @conn [:agent/id id])] (if (not (nil? a)) (let [p (q-skillset (:agent/primary-skillset a))] - (if (not (nil? p)) + (if-not (nil? p) p (q-skillset (:agent/secondary-skillset a))))))) @@ -139,17 +139,14 @@ "Change status of a job `j` to :processing and bind it to an agent `a`" [^String a ^String j] - (if (not (nil? j)) - (do - (t-job j :processing a) - (bind-agent a j)))) + (when-not (nil? j) (t-job j :processing a) (bind-agent a j))) (defn dequeue-job "Dequeue a job to a agent `id`" [^String id] (let [a (d/entity @conn [:agent/id id])] (if (not (nil? a)) - (let [jid (-> (request-job id) :job/id)] + (let [jid (:job/id (request-job id))] (end-job a) (start-job id jid) jid)))) \ No newline at end of file -- cgit v1.2.3