aboutsummaryrefslogtreecommitdiff
path: root/src/clj/queue_api/routes/services.clj
blob: dee949aea05afdf4f29bdfbdd1e2743958419bb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(ns queue-api.routes.services
  (:require [ring.util.http-response :refer :all]
            [compojure.api.sweet :refer :all]
            [schema.core :as s]
            [queue-api.db.core :as db]))

(defapi service-routes
  {:swagger {:ui   "/swagger-ui"
             :spec "/swagger.json"
             :data {:info {:version     "1.0.0"
                           :title       "Job Queue API"
                           :description "Manages agent resources"}}}}

  (context "/agent" []
    :tags ["agent"]
    (PUT "/" []
      :body-params [id :- String, name :- String, primary_skillset :- [String], secondary_skillset :- [String]]
      :summary "Add a new agent"
      (db/add-agent
        {:id                 id
         :name               name
         :primary-skillset   primary_skillset
         :secondary-skillset secondary_skillset})
      (ok))
    (POST "/" []
      :return [{:type String :jobs s/Int}]
      :body-params [agent_id :- String]
      :summary "Get summary of an agent"
      (let [jobs (db/sum-agent agent_id)]
        (map (fn [x]
               {:type (first x)
                :jobs (last x)}) jobs))))

  (context "/job" []
    :tags ["job"]
    (PUT "/" []
      :body-params [id :- String, type :- String, urgent :- Boolean]
      :summary "Add a new job"
      (db/add-job {:id     id
                   :type   type
                   :urgent urgent})
      (ok))
    (POST "/" []
      :return {:job_request {:job_id s/Any :agent_id String}}
      :body-params [agent_id :- String]
      :summary "Request a job to a given agent"
      (let [j (db/dequeue-job agent_id)]
        (if (nil? j)
          (bad-request {:message "Agent does not exist"})
          (ok {:job_request {:job_id j :agent_id agent_id}})))))

  (context "/queue" []
    :tags ["queue"]
    (GET "/" []
      :return {:completed  [String]
               :processing [String]
               :unassigned [String]}
      :summary "Get a summary of the queue"
      (ok (db/sum-queue)))))