(ns queue-api.db.core (:require [datascript.core :as d] [mount.core :as mount])) (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/jobs {:db.valueType :db.type/ref} :job/id {:db/unique :db.unique/identity} :job/agent {:db.valueType :db.type/ref}}) (mount/defstate conn :start (d/create-conn schema)) (defn add-agent [{: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}])) (defn add-job [{:keys [id type urgent]}] (d/transact! queue-api.db.core/conn [{:job/id id :job/type type :job/urgent urgent :job/status :unassigned}])) (defn request-job [id] {:job_request {:job_id "Dummy" :agent_id "Dummy"}}) (defn get-agent [id] [{:type "Dummy" :jobs -2}]) (defn q-agent [id] (d/q '[:find ?type :in $ ?id :where [?e :agent/id ?id] [?x :job/type ?type] [?x :job/agent ?e]])) (defn q-status [s] (d/q '[:find ?id :in $ ?status :where [?e :job/status ?status] [?e :job/id ?id]] @conn s)) (defn get-queue [] {:completed (map #(first %) (q-status :completed)) :processing (map #(first %) (q-status :processing)) :unassigned (map #(first %) (q-status :unassigned))})