From a17538127a37d480cd6efd6d3c44faab4a0a7f76 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 18 Feb 2018 02:21:05 -0300 Subject: Adds some functions to manage the queue --- src/clj/queue_api/db/core.clj | 30 ++++++++++++++++++-- test/clj/queue_api/test/db/core_test.clj | 47 ++++++++++++++++++++++++-------- 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/src/clj/queue_api/db/core.clj b/src/clj/queue_api/db/core.clj index dc04187..765ce39 100644 --- a/src/clj/queue_api/db/core.clj +++ b/src/clj/queue_api/db/core.clj @@ -6,7 +6,7 @@ (def schema {:agent/id {:db/unique :db.unique/identity} :agent/primary-skillset {:db/cardinality :db.cardinality/many} :agent/secondary-skillset {:db/cardinality :db.cardinality/many} - :agent/job {:db.valueType :db.type/ref} + :agent/job {:db.valueType :db.type/ref} :job/id {:db/unique :db.unique/identity} :job/agent {:db.valueType :db.type/ref}}) @@ -23,16 +23,42 @@ (d/transact! queue-api.db.core/conn [{:job/id id :job/type type :job/urgent urgent - :job/date (time/now) + :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}]) +(defn agent-jobs [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))) + + +(defn q-job [u s t] + (let [q (->> (d/q '[:find ?d ?id + :in $ ?u ?s ?t + :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) + (sort-by first))] + (map #(d/entity @conn [:job/id (last %)]) q))) + (defn q-status [s] (d/q '[:find ?id :in $ ?status :where diff --git a/test/clj/queue_api/test/db/core_test.clj b/test/clj/queue_api/test/db/core_test.clj index 4c30ed3..7abdd56 100644 --- a/test/clj/queue_api/test/db/core_test.clj +++ b/test/clj/queue_api/test/db/core_test.clj @@ -4,26 +4,30 @@ [queue-api.db.core :refer :all] [datascript.core :as d] [clj-time.core :as time] - [mount.core :as mount]) - (:import (java.util UUID))) + [mount.core :as mount])) (def simple-schema [;;Job that will be kept :unassigned {:job/id "51ab0771-f1e4-4268-868f-9029a58f6612" - :type "rewards-question" + :job/type "rewards-question" :job/status :unassigned - :job/date (time/now) + :job/date (time/date-time 2018 2 18 1 0 2) + :job/urgent false} + {:job/id "96cf6f11-591d-4cde-9ab0-56e371acb6d2" + :job/type "rewards-question" + :job/status :unassigned + :job/date (time/date-time 2018 2 18 1 0 1) :job/urgent false} ;;Job that will be changed to processing {:job/id "b201d085-91b5-4a13-9a74-7861426e9996" - :type "rewards-question" + :job/type "purchases-question" :job/status :unassigned - :job/date (time/now) + :job/date (time/date-time 2018 2 18 1 0 4) :job/urgent true} ;;Job that will be changed to completed {:job/id "1e0d939d-494b-48d2-9247-b5ae207a519a" - :type "rewards-question" + :job/type "bills-question" :job/status :unassigned - :job/date (time/now) + :job/date (time/date-time 2018 2 18 1 0 3) :job/urgent true} ;;Agent that will be linked to jobs {:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d" @@ -71,7 +75,8 @@ (testing "test query job by status" (are [left right] (= left right) - #{["51ab0771-f1e4-4268-868f-9029a58f6612"]} (q-status :unassigned) + #{["51ab0771-f1e4-4268-868f-9029a58f6612"] + ["96cf6f11-591d-4cde-9ab0-56e371acb6d2"]} (q-status :unassigned) #{["1e0d939d-494b-48d2-9247-b5ae207a519a"]} (q-status :completed) #{["b201d085-91b5-4a13-9a74-7861426e9996"]} (q-status :processing) #{} (q-status :nil)))) @@ -80,5 +85,25 @@ (testing "test get summary of current state of the queue" (is (= {:completed ["1e0d939d-494b-48d2-9247-b5ae207a519a"] :processing ["b201d085-91b5-4a13-9a74-7861426e9996"] - :unassigned ["51ab0771-f1e4-4268-868f-9029a58f6612"]} - (sum-queue))))) \ No newline at end of file + :unassigned ["96cf6f11-591d-4cde-9ab0-56e371acb6d2" + "51ab0771-f1e4-4268-868f-9029a58f6612"]} + (sum-queue))))) + +(deftest q-job-test + (testing "test query job" + (is (= [(d/entity @conn [:job/id "96cf6f11-591d-4cde-9ab0-56e371acb6d2"]) + (d/entity @conn [:job/id "51ab0771-f1e4-4268-868f-9029a58f6612"])] + (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"])] + (q-job true :completed "bills-question"))) + (is (= [] (q-job false :completed "rewards-question"))))) + +(deftest agent-jobs-test + (testing "Test fetch relation agent job" + (is (= [(d/entity @conn [:job/id "1e0d939d-494b-48d2-9247-b5ae207a519a"])] + (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 -- cgit v1.2.3