Skip to content

Instantly share code, notes, and snippets.

@Arnauld
Last active March 29, 2018 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arnauld/343259b2cfb36356d11cfadf3d560c31 to your computer and use it in GitHub Desktop.
Save Arnauld/343259b2cfb36356d11cfadf3d560c31 to your computer and use it in GitHub Desktop.
(ns aranks.core
(:require [clojure.java.io :as io])
(:import [org.yaml.snakeyaml Yaml]))
(defn- class-loader
"Return the current classloader"
^ClassLoader []
(.getContextClassLoader (Thread/currentThread)))
(defn list-resources
"Returns a list of all resources (from a classpath point of view)
in the specified `path`
Optionaly include a custom filter (default accepts all)"
([path] (list-resources #{true}))
([path filter-fn]
(with-open [xin (.getResourceAsStream (class-loader) path)
rdr (java.io.InputStreamReader. xin)
buf (java.io.BufferedReader. rdr)]
(loop [xs []]
(let [ln (.readLine buf)]
(if ln
(if (filter-fn ln)
(recur (conj xs (str path "/" ln)))
(recur xs))
xs))))))
(defn yaml-file-filter
"Indicates whether or not the provided name
should be considered as a yaml file"
[name]
(.endsWith name ".yaml"))
(defn yaml-resource-load
"Load the resource as a yaml from the provided (classpath) path"
[path]
(let [cl (class-loader)]
(with-open [xin (.getResourceAsStream cl path)]
(.load (Yaml.) xin))))
; =========================================================
;
; Misc
;
; =========================================================
(defn thresholds [x ts]
(reduce (fn [acc t]
(if (> x t)
(inc acc)
acc)
) 0 ts))
(defn name-of-function
"Hack to retrieve the name of the function provided"
[fun]
(-> (str fun)
(clojure.string/replace #"@[0-9a-f]+" "")
(clojure.string/replace #"^[0-9a-z.]+\$" "")))
(defn evaluate
"Evaluate all functions against the provided data,
for each function the result is kept (using the function name
as key) and the overall sum is aggregated on the key `:total`."
[data fns]
(reduce (fn [acc fun]
(let [x (fun data)]
(-> acc
(assoc (name-of-function fun) x)
(update :total + x)))
) {:total 0} fns))
; =========================================================
;
; RANK
;
; =========================================================
(defn animation_meetup [interne]
(-> interne
(get "animation_meetup")
(thresholds [0 1 2 4 10])
(* 0.5)))
(defn preparation_consultant [interne]
(-> interne
(get "preparation_consultant")
(thresholds [0 2 4])
(* 0.5)))
(defn contributions [interne]
(-> interne
(get "contributions")
(* 0.5)))
(defn cours_lacombe_en_jours [interne]
(-> interne
(get "cours_lacombe_en_jours")
(thresholds [0 4 9 14 19])
(* 0.5)))
(defn animation_meetup_cours_arolla [interne]
(-> interne
(get "animation_(meetup/cours)_arolla")
(thresholds [0 1 2 3 5 10])
(* 0.5)))
(defn participation_meetup_cours_arolla [interne]
(-> interne
(get "participation_(meetup/cours)_arolla")
(thresholds [0 1 2 5 10 15])
(* 0.5)))
(defn apte_etp [interne]
(let [etp (get interne "apte_etp")]
(if (> etp 0)
1
0)))
(defn recrutements [interne]
(-> interne
(get "recrutements")
(thresholds [0 1 2 4 8 12])
(* 0.5)))
(defn cse_chst [interne]
(-> interne
(get "cse_chst")
(if 1 0)))
(defn parrain [interne]
(-> interne
(get "parrain")))
;
; EXTERNE
;
(defn tjm_ajusted [externe]
(let [tjm (-> externe
(get "tjm_ajusted"))]
(if (> tjm 800) 1 0)))
(defn nb_training [externe]
(-> externe
(get "training_clientèle_en_jours")
(thresholds [0])))
(defn mission_coaching_en_jours [externe]
(-> externe
(get "mission_coaching_en_jours")
(thresholds [5])))
(defn mission_audit_en_jours [externe]
(-> externe
(get "mission_(conseil/audit)_en_jours")
(thresholds [5])))
(defn animation_evenement [externe]
(-> externe
(get "animation_evenement")
(thresholds [0 1 5])))
(defn speaker_en_nb_talks [externe]
(-> externe
(get "speaker_en_nb_talks")
(thresholds [0 1 2 5 10])))
(defn speaker_en_nb_bbl [externe]
(-> externe
(get "speaker_en_nb_bbl")
(thresholds [0 1 2 10])))
(defn auteur_livre [externe]
(-> externe
(get "auteur_livre")
(* 2)))
(defn auteur_article [externe]
(-> externe
(get "auteur_article")
(thresholds [0 1 2 5 10])))
(defn expert_reconnu [externe]
(-> externe
(get "expert_reconnu")
(* 2)))
(defn constructeur [externe]
(-> externe
(get "constructeur_(oss,lib)")
(thresholds [0 1 2 5])))
(defn professeur [externe]
(-> externe
(get "professeur_en_heure")
(thresholds [3 6 12 60])))
(defn participation_meetup [externe]
(-> externe
(get "participation_meetup")
(thresholds [0 4])))
;
;
(defn rankify [ranks]
(let [interne (get-in ranks [:interne :total])
externe (get-in ranks [:externe :total])
th_int (>= interne 5)
th_ext (> externe 5)
ranks #{}]
{:threshold_int th_int
:threshold_ext th_ext
:rank (cond (and th_int th_ext) :compagnon
th_int :gardener
:else :crafter)
}))
;
;
(defn rank-of
[consultant]
{:interne (evaluate (get consultant "interne")
[animation_meetup
preparation_consultant
contributions
cours_lacombe_en_jours
animation_meetup_cours_arolla
participation_meetup_cours_arolla
apte_etp
recrutements
cse_chst
parrain])
:externe (evaluate (get consultant "externe")
[tjm_ajusted
nb_training
mission_coaching_en_jours
mission_audit_en_jours
animation_evenement
speaker_en_nb_talks
speaker_en_nb_bbl
auteur_livre
auteur_article
expert_reconnu
constructeur
professeur
participation_meetup])})
(defn- render-for-print [ranks]
(let [rank-eval (rankify ranks)
rank (:rank rank-eval)
th_int (:threshold_int rank-eval)
th_ext (:threshold_ext rank-eval)
]
(->
{:interne (get-in ranks [:interne :total])
:externe (get-in ranks [:externe :total])
:gardener (cond th_int "X"
th_ext "/"
:else "")
:compagnon (cond (and th_int th_ext) "X"
th_ext "/"
:else "")}
(conj (:interne ranks))
(conj (:externe ranks)))))
(defn print-all
([]
(print-all (fn [_] true)))
([filter-fn]
(let [rs (->> (list-resources "data" yaml-file-filter)
(map yaml-resource-load)
(filter filter-fn)
(map (fn [consultant]
(-> consultant
(rank-of)
(render-for-print)
(assoc
:who (str (get consultant "lastname")
" "
(get consultant "firstname")))))))
tots (reduce (fn [acc c]
(let [acc (if (and (= (:gardener c) "X")
(not= (:compagnon c) "X"))
(-> acc
(update-in [:nb-gardener] inc)
(update-in [:gardeners] conj (:who c)))
acc)
acc (if (= (:compagnon c) "X")
(-> acc
(update-in [:nb-compagnon] inc)
(update-in [:compagnons] conj (:who c)))
acc)]
acc)) {:nb-gardener 0
:nb-compagnon 0
:gardeners []
:compagnons []} rs)]
(clojure.pprint/print-table [:who :interne :externe :gardener :compagnon
"|"
"animation_meetup"
"preparation_consultant"
"contributions"
"cours_lacombe_en_jours"
"animation_meetup_cours_arolla"
"participation_meetup_cours_arolla"
"apte_etp"
"recrutements"
"cse_chst"
"parrain"
"|"
"tjm_ajusted"
"nb_training"
"mission_coaching_en_jours"
"mission_audit_en_jours"
"animation_evenement"
"speaker_en_nb_talks"
"speaker_en_nb_bbl"
"auteur_livre"
"auteur_article"
"expert_reconnu"
"constructeur"
"professeur"
"participation_meetup"]
rs)
(println)
(println "#" (count rs)
" gardeners: " (:nb-gardener tots)
" compagnons: " (:nb-compagnon tots))
(println (map #(str "'" % "'\n") (:compagnons tots)))
(println (map #(str "'" % "'\n") (:gardeners tots))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment