From ff69f08e64ad890255fb0f9e957be4e00b1c404f Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 18 Feb 2018 21:26:07 -0300 Subject: Improves code clarity --- src/clj/queue_api/db/core.clj | 43 +++++++++++++++++++++-------------- src/clj/queue_api/routes/services.clj | 6 ++--- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/clj/queue_api/db/core.clj b/src/clj/queue_api/db/core.clj index 35a87fd..88e6eed 100644 --- a/src/clj/queue_api/db/core.clj +++ b/src/clj/queue_api/db/core.clj @@ -1,7 +1,8 @@ (ns queue-api.db.core (:require [datascript.core :as d] [mount.core :as mount] - [clj-time.core :as time])) + [clj-time.core :as time]) + (:import (clojure.lang Keyword))) (def schema {:agent/id {:db/unique :db.unique/identity} :agent/primary-skillset {:db/cardinality :db.cardinality/many} @@ -106,7 +107,7 @@ (defn request-job "Get the fittest job for a agent `id`." - [id] + [^String id] (let [a (d/entity @conn [:agent/id id])] (if (not (nil? a)) (let [p (q-skillset (:agent/primary-skillset a))] @@ -115,34 +116,42 @@ (q-skillset (:agent/secondary-skillset a))))))) (defn t-job - ([id s] + ([^String id ^Keyword s] (d/transact! conn [{:job/id id :job/status s}])) - ([id s a] + ([^String id ^Keyword s ^String a] (d/transact! conn [{:job/id id :job/status s :job/agent [:agent/id a]}]))) -(defn t-agent [id j] - (d/transact! conn [{:agent/id id +(defn bind-agent + "Bind an agent `a` to a job `j`" + [^String a ^String j] + (d/transact! conn [{:agent/id a :agent/job [:job/id j]}])) -(defn end-job [a] + +(defn end-job + "Change job's status to :completed from a job bound to an agent `a`" + [^String a] (let [jid (-> a :agent/job :job/id)] (if (not (nil? jid)) (t-job jid :completed)))) -(defn t-agent-job-status - [a j] +(defn start-job + "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) - (t-agent a j) - {:job_request {:job_id j :agent_id a}}) - {:job_request {:job_id nil :agent_id a}})) + (bind-agent a j)))) -(defn dequeue-job [id] - (let [j (request-job id) - a (d/entity @conn [:agent/id id])] +(defn dequeue-job + "Dequeue a job from a agent `id`" + [^String id] + (let [a (d/entity @conn [:agent/id id])] (if (not (nil? a)) - (do (end-job a) - (t-agent-job-status (-> a :agent/id) (-> j :job/id)))))) \ No newline at end of file + (let [jid (-> (request-job id) :job/id)] + (end-job a) + (start-job id jid) + {:job_id jid :agent_id 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 e8f03f9..694f0d8 100644 --- a/src/clj/queue_api/routes/services.clj +++ b/src/clj/queue_api/routes/services.clj @@ -44,13 +44,13 @@ (let [j (db/dequeue-job agent_id)] (if (nil? j) (bad-request {:message "Agent does not exist"}) - (ok j))))) + (ok {:job_request j}))))) (context "/queue" [] :tags ["queue"] (GET "/" [] - :return {:completed [String] - :processing[String] + :return {:completed [String] + :processing [String] :unassigned [String]} :summary "Get a summary of the queue" (ok (db/sum-queue))))) -- cgit v1.2.3