aboutsummaryrefslogtreecommitdiff
path: root/src/clj/queue_api/db/core.clj
diff options
context:
space:
mode:
Diffstat (limited to 'src/clj/queue_api/db/core.clj')
-rw-r--r--src/clj/queue_api/db/core.clj89
1 files changed, 61 insertions, 28 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