aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2018-02-18 16:41:25 -0300
committerGabriel A. Giovanini <mail@gabrielgio.me>2018-02-18 16:41:25 -0300
commit48bd546122842012a65572fa436fe58cfd965ca7 (patch)
treed5e7f8accdd635ebc220cd6010ccb00280a0e145
parent7f492fb132b44917f2293b8435924de4524d3053 (diff)
downloadqueue-api-48bd546122842012a65572fa436fe58cfd965ca7.tar.gz
queue-api-48bd546122842012a65572fa436fe58cfd965ca7.tar.bz2
queue-api-48bd546122842012a65572fa436fe58cfd965ca7.zip
Adds more features and more tests
-rw-r--r--src/clj/queue_api/db/core.clj89
-rw-r--r--src/clj/queue_api/routes/services.clj4
-rw-r--r--test/clj/queue_api/test/db/core_test.clj48
3 files changed, 103 insertions, 38 deletions
diff --git a/src/clj/queue_api/db/core.clj b/src/clj/queue_api/db/core.clj
index 63446d8..40e6188 100644
--- a/src/clj/queue_api/db/core.clj
+++ b/src/clj/queue_api/db/core.clj
@@ -16,26 +16,21 @@
(defn add-agent
"Add an agent into the database"
[{:keys [id name primary-skillset secondary-skillset]}]
- (d/transact! queue-api.db.core/conn [{:agent/id id
- :agent/name name
- :agent/primary-skillset primary-skillset
- :agent/secondary-skillset secondary-skillset}]))
+ (d/transact! queue-api.db.core/conn
+ [{:agent/id id
+ :agent/name name
+ :agent/primary-skillset primary-skillset
+ :agent/secondary-skillset secondary-skillset}]))
(defn add-job
"Add a job into the database"
[{:keys [id type urgent]}]
- (d/transact! queue-api.db.core/conn [{:job/id id
- :job/type type
- :job/urgent urgent
- :job/date (time/now)
- :job/status :unassigned}]))
-
-(defn request-job [id]
- {:job_request {:job_id "Dummy" :agent_id "Dummy"}})
-
-(defn get-agent [id]
- [{:type "Dummy"
- :jobs -2}])
+ (d/transact! queue-api.db.core/conn
+ [{:job/id id
+ :job/type type
+ :job/urgent urgent
+ :job/date (time/now)
+ :job/status :unassigned}]))
(defn agent-jobs
"Get a job that has a agent bounded with given `id` and a status of `s`"
@@ -51,25 +46,26 @@
(map #(d/entity @conn [:job/id (first %)]) q)))
(defn q-job
- "Fetch job and order by date
- `u`: if it is flagged urgent
- `s` status of the job
- `t` type of the job"
- [u s t]
+ "Fetch job and sort by date
+ `u`: urgent flag
+ `s`: status of the job
+ `ts`: types of the job"
+ [u s & ts]
(let [q (->> (d/q '[:find ?d ?id
- :in $ ?u ?s ?t
+ :in $ ?u ?s ?ts
:where
[?e :job/date ?d]
[?e :job/id ?id]
[?e :job/urgent ?u]
[?e :job/status ?s]
- [?e :job/type ?t]]
- @conn u s t)
+ [?e :job/type ?t]
+ [(clojure.string/includes? ?ts ?t)]]
+ @conn u s ts)
(sort-by first))]
(map #(d/entity @conn [:job/id (last %)]) q)))
(defn q-status
- "Query job filtering only by status"
+ "Query job filtering only by `s`"
[s]
(d/q '[:find ?id :in $ ?status
:where
@@ -77,9 +73,46 @@
[?e :job/id ?id]]
@conn s))
+(defn sum-agent
+ "Get how many jobs a agent (`id`) has performed aggregated by type"
+ [id]
+ (let [jobs (agent-jobs id :completed)]
+ (->> (reduce (fn [l r]
+ (let [t (:job/type r)]
+ (if (nil? (get l t))
+ (conj l {t 1})
+ (conj l {t (inc (get l t))})))) {} jobs)
+ (map (fn [x]
+ {:type (first x)
+ :jobs (last x)})))))
+
(defn sum-queue
"Count all job aggregated by type"
[]
- {:completed (map #(first %) (q-status :completed))
- :processing (map #(first %) (q-status :processing))
- :unassigned (map #(first %) (q-status :unassigned))}) \ No newline at end of file
+ {:completed (map first (q-status :completed))
+ :processing (map first (q-status :processing))
+ :unassigned (map first (q-status :unassigned))})
+
+(defn q-skillset
+ "First query for :unassigned jobs with skillset of `s` and flagged as urgent
+ if nothing was found query for not urgent ones."
+ [s]
+ (let [ju (q-job true :unassigned s)]
+ (if (not (empty? ju))
+ (first ju)
+ (let [jn (q-job false :unassigned s)]
+ (if (not (empty? jn))
+ (first jn))))))
+
+(defn request-job
+ "Get the fittest job for a agent `id`."
+ [id]
+ (let [a (d/entity @conn [:agent/id id])]
+ (if (not (nil? a))
+ (let [p (q-skillset (:primary-skillset a))]
+ (if (not (nil? p))
+ p
+ (q-skillset (:secondary-skillset a)))))))
+
+(defn dequeue-job [id]
+ ) \ No newline at end of file
diff --git a/src/clj/queue_api/routes/services.clj b/src/clj/queue_api/routes/services.clj
index 7e9fc98..d432517 100644
--- a/src/clj/queue_api/routes/services.clj
+++ b/src/clj/queue_api/routes/services.clj
@@ -26,7 +26,7 @@
:return [{:type String :jobs s/Int}]
:body-params [agent_id :- String]
:summary "Get summary of an agent"
- (ok (db/get-agent agent_id))))
+ (ok (db/sum-agent agent_id))))
(context "/job" []
:tags ["job"]
@@ -41,7 +41,7 @@
:return {:job_request {:job_id String :agent_id String}}
:body-params [agent_id :- String]
:summary "Request a job to a given agent"
- (ok (db/request-job agent_id))))
+ (ok (db/dequeue-job agent_id))))
(context "/queue" []
:tags ["queue"]
diff --git a/test/clj/queue_api/test/db/core_test.clj b/test/clj/queue_api/test/db/core_test.clj
index 7abdd56..c1ecc6d 100644
--- a/test/clj/queue_api/test/db/core_test.clj
+++ b/test/clj/queue_api/test/db/core_test.clj
@@ -28,6 +28,16 @@
:job/type "bills-question"
:job/status :unassigned
:job/date (time/date-time 2018 2 18 1 0 3)
+ :job/urgent false}
+ {:job/id "aa327540-4e24-47f4-9e9c-81cdd5195934"
+ :job/type "bills-question"
+ :job/status :unassigned
+ :job/date (time/date-time 2018 2 18 1 0 6)
+ :job/urgent true}
+ {:job/id "3cdc52fe-b538-40a6-a9d7-92fa840c2c4b"
+ :job/type "purchases-question"
+ :job/status :unassigned
+ :job/date (time/date-time 2018 2 18 1 0 8)
:job/urgent true}
;;Agent that will be linked to jobs
{:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"
@@ -38,11 +48,17 @@
{:job/id "1e0d939d-494b-48d2-9247-b5ae207a519a"
:job/status :completed
:job/agent [:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"]}
+ {:job/id "aa327540-4e24-47f4-9e9c-81cdd5195934"
+ :job/status :completed
+ :job/agent [:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"]}
+ {:job/id "3cdc52fe-b538-40a6-a9d7-92fa840c2c4b"
+ :job/status :completed
+ :job/agent [:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"]}
;;Change job status to processing and link agent
{:job/id "b201d085-91b5-4a13-9a74-7861426e9996"
:job/status :processing
:job/agent [:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"]}
- ;;Link jog with job that it is processing
+ ;;Link agent with job that it is being processed
{:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"
:agent/job [:job/id "b201d085-91b5-4a13-9a74-7861426e9996"]}])
@@ -77,14 +93,18 @@
(= left right)
#{["51ab0771-f1e4-4268-868f-9029a58f6612"]
["96cf6f11-591d-4cde-9ab0-56e371acb6d2"]} (q-status :unassigned)
- #{["1e0d939d-494b-48d2-9247-b5ae207a519a"]} (q-status :completed)
+ #{["1e0d939d-494b-48d2-9247-b5ae207a519a"]
+ ["aa327540-4e24-47f4-9e9c-81cdd5195934"]
+ ["3cdc52fe-b538-40a6-a9d7-92fa840c2c4b"]} (q-status :completed)
#{["b201d085-91b5-4a13-9a74-7861426e9996"]} (q-status :processing)
#{} (q-status :nil))))
(deftest sum-queue-test
(testing "test get summary of current state of the queue"
- (is (= {:completed ["1e0d939d-494b-48d2-9247-b5ae207a519a"]
- :processing ["b201d085-91b5-4a13-9a74-7861426e9996"]
+ (is (= {:completed ["1e0d939d-494b-48d2-9247-b5ae207a519a"
+ "aa327540-4e24-47f4-9e9c-81cdd5195934"
+ "3cdc52fe-b538-40a6-a9d7-92fa840c2c4b"],
+ :processing ["b201d085-91b5-4a13-9a74-7861426e9996"],
:unassigned ["96cf6f11-591d-4cde-9ab0-56e371acb6d2"
"51ab0771-f1e4-4268-868f-9029a58f6612"]}
(sum-queue)))))
@@ -96,14 +116,26 @@
(q-job false :unassigned "rewards-question")))
(is (= [(d/entity @conn [:job/id "b201d085-91b5-4a13-9a74-7861426e9996"])]
(q-job true :processing "purchases-question")))
- (is (= [(d/entity @conn [:job/id "1e0d939d-494b-48d2-9247-b5ae207a519a"])]
+ (is (= [(d/entity @conn [:job/id "aa327540-4e24-47f4-9e9c-81cdd5195934"])]
(q-job true :completed "bills-question")))
- (is (= [] (q-job false :completed "rewards-question")))))
+ (is (= [] (q-job false :completed "rewards-question")))
+ (is (= [(d/entity @conn [:job/id "1e0d939d-494b-48d2-9247-b5ae207a519a"])]
+ (q-job false :completed "rewards-question" "bills-question")))
+ (is (= [(d/entity @conn [:job/id "1e0d939d-494b-48d2-9247-b5ae207a519a"])]
+ (q-job false :completed ["rewards-question" "bills-question"])))))
(deftest agent-jobs-test
(testing "Test fetch relation agent job"
- (is (= [(d/entity @conn [:job/id "1e0d939d-494b-48d2-9247-b5ae207a519a"])]
+ (is (= [(d/entity @conn [:job/id "1e0d939d-494b-48d2-9247-b5ae207a519a"])
+ (d/entity @conn [:job/id "aa327540-4e24-47f4-9e9c-81cdd5195934"])
+ (d/entity @conn [:job/id "3cdc52fe-b538-40a6-a9d7-92fa840c2c4b"])]
(agent-jobs "644be0ce-035d-48cb-867e-8e6de2714a8d" :completed)))
(is (= [(d/entity @conn [:job/id "b201d085-91b5-4a13-9a74-7861426e9996"])]
(agent-jobs "644be0ce-035d-48cb-867e-8e6de2714a8d" :processing)))
- (is (= [] (agent-jobs "644be0ce-035d-48cb-867e-8e6de2714a8d" :unassigned))))) \ No newline at end of file
+ (is (= [] (agent-jobs "644be0ce-035d-48cb-867e-8e6de2714a8d" :unassigned)))))
+
+(deftest sum-agent-test
+ (testing "Test sum-agent"
+ (is (= [{:type "bills-question", :jobs 2} {:type "purchases-question", :jobs 1}]
+ (sum-agent "644be0ce-035d-48cb-867e-8e6de2714a8d")))
+ (is (= [] (sum-agent "00000000-0000-0000-0000-000000000000"))))) \ No newline at end of file