(ns scheduler.matcher
  (:require [scheduler.types :as t]))

^{:line 22 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (defn has-all-capabilities? [worker required]
  ^{:line 24 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (let [worker-caps ^{:line 24 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (set ^{:line 24 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/worker-capabilities worker))]
  ^{:line 25 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (every? ^{:line 25 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [cap] ^{:line 25 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (contains? worker-caps cap)) required)))

^{:line 30 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (defn capable-workers [task workers]
  ^{:line 32 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (let [required ^{:line 32 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/task-capabilities task)
   matching ^{:line 33 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (filterv ^{:line 33 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [w] ^{:line 34 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (has-all-capabilities? w required)) workers)]
  ^{:line 36 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (sort-by ^{:line 36 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [w] ^{:line 36 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/worker-id w)) matching)))

^{:line 45 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (defn windows-overlap? [start1 end1 start2 end2]
  ^{:line 47 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (and ^{:line 47 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (< start1 end2) ^{:line 47 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (< start2 end1)))

^{:line 52 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (defn worker-available? [worker start end]
  ^{:line 53 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (not-any? ^{:line 53 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [w] ^{:line 54 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (windows-overlap? start end ^{:line 54 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/window-start w) ^{:line 54 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/window-end w))) ^{:line 55 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/worker-unavailable worker)))

^{:line 59 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (defn available-workers [workers start end]
  ^{:line 62 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (let [avail ^{:line 62 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (filterv ^{:line 62 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [w] ^{:line 63 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (worker-available? w start end)) workers)]
  ^{:line 65 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (sort-by ^{:line 65 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [w] ^{:line 65 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/worker-id w)) avail)))

^{:line 75 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (defn count-overlapping-assignments [resource-id existing task-resource-map start end]
  ^{:line 80 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (count ^{:line 80 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (filter ^{:line 80 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [a] ^{:line 81 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (let [task-resources ^{:line 81 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (get task-resource-map ^{:line 82 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/assignment-task-id a) ^{:line 82 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} [])]
  ^{:line 83 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (and ^{:line 83 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (some ^{:line 83 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [r] ^{:line 83 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (= r resource-id)) task-resources) ^{:line 85 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (windows-overlap? ^{:line 85 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/assignment-start-time a) ^{:line 86 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/assignment-end-time a) start end)))) existing)))

^{:line 93 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (defn check-resource-capacity [resource existing task-resource-map start end]
  ^{:line 98 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (let [used ^{:line 98 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (count-overlapping-assignments ^{:line 98 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/resource-id resource) existing task-resource-map start end)]
  ^{:line 100 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (< used ^{:line 100 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/resource-capacity resource))))

^{:line 104 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (defn check-all-resources [task resources existing task-resource-map start end]
  ^{:line 110 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (let [required-ids ^{:line 110 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (set ^{:line 110 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/task-resources task))
   required-resources ^{:line 111 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (filterv ^{:line 111 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [r] ^{:line 112 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (contains? required-ids ^{:line 112 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/resource-id r))) resources)]
  ^{:line 114 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (every? ^{:line 114 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [r] ^{:line 115 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (check-resource-capacity r existing task-resource-map start end)) required-resources)))

^{:line 125 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (defn worker-has-overlap? [worker-id existing start end]
  ^{:line 129 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (some? ^{:line 129 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (some ^{:line 129 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [a] ^{:line 130 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (and ^{:line 130 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (= ^{:line 130 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/assignment-worker-id a) worker-id) ^{:line 131 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (windows-overlap? ^{:line 131 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/assignment-start-time a) ^{:line 132 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/assignment-end-time a) start end))) existing)))

^{:line 147 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (defn find-eligible-workers [task workers resources existing task-resource-map start]
  ^{:line 153 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (let [end ^{:line 153 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (+ start ^{:line 153 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/task-duration task))
   cap-workers ^{:line 155 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (capable-workers task workers)
   avail-workers ^{:line 157 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (available-workers cap-workers start end)
   no-overlap ^{:line 159 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (filterv ^{:line 159 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [w] ^{:line 160 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (not ^{:line 160 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (worker-has-overlap? ^{:line 160 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/worker-id w) existing start end))) avail-workers)
   eligible ^{:line 163 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (if ^{:line 163 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (empty? ^{:line 163 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/task-resources task)) no-overlap ^{:line 165 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (filterv ^{:line 165 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [w] ^{:line 166 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (check-all-resources task resources existing task-resource-map start end)) no-overlap))]
  ^{:line 170 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (sort-by ^{:line 170 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [w] ^{:line 170 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/worker-id w)) eligible)))

^{:line 178 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (defn find-failure-reason [task workers resources existing task-resource-map start]
  ^{:line 184 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (let [end ^{:line 184 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (+ start ^{:line 184 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/task-duration task))
   cap-workers ^{:line 185 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (capable-workers task workers)]
  ^{:line 186 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (if ^{:line 186 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (empty? cap-workers) ^{:line 188 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/->NoCapableWorker ^{:line 188 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/task-capabilities task) ^{:line 189 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (mapv ^{:line 189 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [w] ^{:line 189 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/worker-capabilities w)) workers)) ^{:line 190 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (let [avail ^{:line 190 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (available-workers cap-workers start end)]
  ^{:line 191 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (if ^{:line 191 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (empty? avail) ^{:line 193 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/->WorkerUnavailable ^{:line 194 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/worker-id ^{:line 194 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (first cap-workers)) ^{:line 195 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/worker-unavailable ^{:line 195 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (first cap-workers))) ^{:line 197 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (let [required-ids ^{:line 197 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (set ^{:line 197 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/task-resources task))
   required-resources ^{:line 198 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (filterv ^{:line 198 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [r] ^{:line 199 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (contains? required-ids ^{:line 199 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/resource-id r))) resources)]
  ^{:line 201 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (let [over-cap ^{:line 201 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (first ^{:line 201 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (filterv ^{:line 202 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (fn [r] ^{:line 203 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (not ^{:line 203 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (check-resource-capacity r existing task-resource-map start end))) required-resources))]
  ^{:line 206 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (if ^{:line 206 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (some? over-cap) ^{:line 207 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (let [used ^{:line 207 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (count-overlapping-assignments ^{:line 208 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/resource-id over-cap) existing task-resource-map start end)]
  ^{:line 210 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/->ResourceOverCapacity ^{:line 210 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/resource-id over-cap) ^{:line 211 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (t/resource-capacity over-cap) ^{:line 212 :file "/home/tom/code/beagle/experiments/e16-workflow-scheduler/golden/beagle/matcher.rkt"} (+ used 1))) nil))))))))
