~drewr/baklava

e2a6ea8fde62727748653603405133835ac32655 — Drew Raines a month ago c34e3c4
WIP
1 files changed, 91 insertions(+), 14 deletions(-)

M src/baklava/workday.clj
M src/baklava/workday.clj => src/baklava/workday.clj +91 -14
@@ 14,6 14,26 @@
            [ubergraph.core :as uber]
            [ubergraph.alg :as uberalg]))

(def common-columns
  ["Employee ID"
   "Preferred Name"
   "Hire Date"
   "CF OA Band Level on Worker"
   "Management Level"
   "Cost Center - ID"
   "Manager"
   "Job Profile"
   ])

(defprotocol Employee
  (reports-to [emp] "Employee's manager")
  (reporting-chain [emp] "Every manager in this employee's line")
  (reports-to? [emp1 emp2] "Does emp1 report to emp2?"))

#_(defprotocol Manager
    (reports [emp] "Who reports to emp")
    (reports-into [emp] "Everyone who reports into emp"))

(defn employees-under
  "Example usage:



@@ 24,26 44,59 @@
  "
  [dataset management-chain-level name]
  (-> dataset
      (ds/select ["Legal Name"
                  "Preferred Name"
                  "Hire Date"
                  "Employee ID"
                  "Work Email"
                  "Manager"
                  "Manager's Email"
                  management-chain-level
                  ]
      (ds/select (conj common-columns management-chain-level)
                 :all)
      (ds/filter-column
       management-chain-level
       #(if (string? %) (.startsWith % name)))
      ds/rows))

(defn direct-reports [graph emp]
  (->> (-> gr (uber/in-edges emp))
       (map :src)
       seq))

(defn reports-into [graph emp]
  #_(locking *out*
    (prn emp))
  (let [reps (direct-reports graph emp)]
    (lazy-seq
     (when (seq reps)
       (cons (first reps)
             (apply concat (rest reps)
                    (map #(reports-into graph %) (rest reps))))))))

(defn employee [dataset prefix]
  (-> dataset
      (ds/filter-column
       "Preferred Name" #(and (string? %) (.startsWith % prefix)))
      (ds/select common-columns :all)))

(defn org-graph [dataset management-chain-level name]
  (->> (employees-under dataset management-chain-level name)
       (map (fn [x]
              [(get x "Preferred Name") (get x "Manager")]))
       (apply uber/multigraph)))
       (apply uber/digraph)))

(defn managers [graph]
  (->> graph
       uberalg/distinct-edges
       (map uber/dest)
       (into #{})))

(defn manager-emails [graph dataset]
  (for [person (managers graph)]
    (-> dataset
        (ds/filter-column "Preferred Name" person)
        (ds/select ["Work Email"] :all)
        ds/rows
        first
        (get "Work Email"))))

(defn not-managers [graph]
  (clojure.set/difference
   (->> graph uberalg/distinct-edges (map uber/src) (into #{}))
   (managers graph)))

(defn path-map [graph root]
  (let [path (fn [child]


@@ 64,7 117,27 @@
            {}
            (uber/nodes graph))))

(defn get-employee-reports [dataset name]
  (prn 'get-employee-reports name)
  (let [recs (-> dataset
                 (ds/filter-column
                  "Manager"
                  #(= % name))
                 (ds/select ["Preferred Name"
                             "Employee ID"]
                            :all)
                 ds/rows)]
    (when (pos? (count recs))
      (clojure.walk/postwalk
       #(cond
          (= % "Preferred Name") :name
          (= % "Employee ID") :id
          (integer? %) (str %)
          :else %)
       recs))))

(defn get-employee-by-name [dataset name]
  (prn 'get-employee-by-name name)
  (let [rec (-> dataset
                (ds/filter-column
                 "Preferred Name"


@@ 72,8 145,12 @@
                ds/rows
                first)]
    (when rec
      {:id (str (get rec "Employee ID"))
       :name (get rec "Preferred Name")})))
      (let [res {:id (str (get rec "Employee ID"))
                 :name (get rec "Preferred Name")}
            reports (get-employee-reports dataset name)]
        (if reports
          (assoc res :employees reports)
          res)))))

(defn load-graphql [resource]
  (-> resource


@@ 82,6 159,6 @@
      edn/read-string
      (com.walmartlabs.lacinia.util/inject-resolvers
       {:Query/employee
          (fn [ctx args value]
            (get-employee-by-name (:source ctx) (:name args)))})
        (fn [ctx args value]
          (#'get-employee-by-name (:source ctx) (:name args)))})
      com.walmartlabs.lacinia.schema/compile))