<commit_before>(ns navigator
  ^{:author "<NAME> <<EMAIL>>"
    :doc "This library knows how to work with competency data."}
  (:require [clojure.edn :as edn]
            [datomic.api :as d]
            [liberator.core :refer [resource]]
            [navigator.schema :as schema]
            [hatch]
            [navigator.templates :as templates]
            [navigator.data :as data]))

;; front end

(defn helmsman-def [db-conn]
  [[:get "/comp-map/" (resource :allowed-methods [:get]
                                :available-media-types ["text/html"]
                                :handle-ok (fn [ctx] (apply str (templates/view-comp-map (data/get-comp-map db-conn ctx) ctx))))]])

;; Get functions

(defn get-entity
  "Gets an entity using query and bindings. The db conn is assumed to
  be the first binding of the query and doesn't need to be included in
  the bindings part of the arguments."
  ([db-conn query & bindings]
     (d/entity (d/db db-conn) (ffirst (apply d/q query (d/db db-conn) bindings)))))

(defn get-competency
  "Get competency by shared key"
  [db-conn id-sk]
  (get-entity db-conn '[:find ?e
                     :in $ ?id-sk
                     :where [?e :comp/id-sk ?id-sk]]
              id-sk))

(defn get-competency-by-name-version
  "Get competency by name+verion"
  [db-conn name version]
  (get-entity db-conn '[:find ?e
                     :in $ ?name ?version
                     :where [?e :comp/name ?name]
                            [?e :comp/version ?version]]
              name version))
;; TODO: we probably want other ways to get competency(s)

(defn get-perf-asmt
  "Get perf-asmt by shared key"
  [db-conn id-sk]
  (get-entity db-conn '[:find ?e
                     :in $ ?id-sk
                     :where [?e :perf-asmt/id-sk ?id-sk]]
              id-sk))

(defn get-user2comp
  "Get user2comp by sis-user-id and comp"
  [db-conn sis-user-id comp-eid]
  (get-entity db-conn '[:find ?e
                     :in $ ?sis-user-id ?comp-eid
                     :where [?e :user2comp/sis-user-id ?sis-user-id]
                            [?e :user2comp/comp ?comp-eid]]))

;; Creation/update functions

(def partitions (hatch/schematode->partitions schema/schema))

(def valid-attrs (hatch/schematode->attrs schema/schema))

(def tx-entity! (partial hatch/tx-clean-entity! partitions valid-attrs))

;; queue functions

(defn task-in [db-conn message]
  (tx-entity! db-conn :task (merge {:task/id-sk (str (get-in message [:header :entity-id :task-id]))}
                                   (hatch/slam-all (get-in message [:payload :entity]) :task))))

(defn comp-in [db-conn message]
  (tx-entity! db-conn :comp (merge {:comp/id-sk (str (get-in message [:header :entity-id :comp-id]))
                                    (hatch/slam-all (get-in message [:payload :entity]) :comp)})))

(defn comp-tag-in [db-conn message]
  (tx-entity! db-conn :comp-tag (hatch/slam-all (get-in message [:payload :entity]) :comp-tag)))

(defn perf-asmt-in [db-conn message]
  (tx-entity! db-conn :perf-asmt (merge {:perf-asmt/id-sk (str (get-in message [:header :entity-id :perf-asmt-id]))
                                         (hatch/slam-all (get-in message [:paylod :entity]) :perf-asmt)})))
(defn user2comp-in [db-conn message]
  (tx-entity! db-conn :user2comp (hatch/slam-all (get-in message [:payload :entity]) :user2comp)))

(defn user2perf-asmt-in [db-conn message]
  (tx-entity! db-conn :user2perf-asmt (hatch/slam-all (get-in message [:payload :entity]) :user2perf-asmt)))

(comment

  (navigator/task-in (:db-conn nt-config/system)
                     {:payload
                      {:entity
                       {:competency-parents [1 2 3],
                        :name "tie shoes (together)",
                        :version "v3"}},
                      :header
                      {:entity-type "task", :operation "assert", :entity-id {:task-id 17}}})
<commit_msg>move the slams out of the maps<commit_after>(ns navigator
  ^{:author "<NAME> <<EMAIL>>"
    :doc "This library knows how to work with competency data."}
  (:require [clojure.edn :as edn]
            [datomic.api :as d]
            [liberator.core :refer [resource]]
            [navigator.schema :as schema]
            [hatch]
            [navigator.templates :as templates]
            [navigator.data :as data]))

;; front end

(defn helmsman-def [db-conn]
  [[:get "/comp-map/" (resource :allowed-methods [:get]
                                :available-media-types ["text/html"]
                                :handle-ok (fn [ctx] (apply str (templates/view-comp-map (data/get-comp-map db-conn ctx) ctx))))]])

;; Get functions

(defn get-entity
  "Gets an entity using query and bindings. The db conn is assumed to
  be the first binding of the query and doesn't need to be included in
  the bindings part of the arguments."
  ([db-conn query & bindings]
     (d/entity (d/db db-conn) (ffirst (apply d/q query (d/db db-conn) bindings)))))

(defn get-competency
  "Get competency by shared key"
  [db-conn id-sk]
  (get-entity db-conn '[:find ?e
                     :in $ ?id-sk
                     :where [?e :comp/id-sk ?id-sk]]
              id-sk))

(defn get-competency-by-name-version
  "Get competency by name+verion"
  [db-conn name version]
  (get-entity db-conn '[:find ?e
                     :in $ ?name ?version
                     :where [?e :comp/name ?name]
                            [?e :comp/version ?version]]
              name version))
;; TODO: we probably want other ways to get competency(s)

(defn get-perf-asmt
  "Get perf-asmt by shared key"
  [db-conn id-sk]
  (get-entity db-conn '[:find ?e
                     :in $ ?id-sk
                     :where [?e :perf-asmt/id-sk ?id-sk]]
              id-sk))

(defn get-user2comp
  "Get user2comp by sis-user-id and comp"
  [db-conn sis-user-id comp-eid]
  (get-entity db-conn '[:find ?e
                     :in $ ?sis-user-id ?comp-eid
                     :where [?e :user2comp/sis-user-id ?sis-user-id]
                            [?e :user2comp/comp ?comp-eid]]))

;; Creation/update functions

(def partitions (hatch/schematode->partitions schema/schema))

(def valid-attrs (hatch/schematode->attrs schema/schema))

(def tx-entity! (partial hatch/tx-clean-entity! partitions valid-attrs))

;; queue functions

(defn task-in [db-conn message]
  (tx-entity! db-conn :task (merge {:task/id-sk (str (get-in message [:header :entity-id :task-id]))}
                                   (hatch/slam-all (get-in message [:payload :entity]) :task))))

(defn comp-in [db-conn message]
  (tx-entity! db-conn :comp (merge {:comp/id-sk (str (get-in message [:header :entity-id :comp-id]))}
                                    (hatch/slam-all (get-in message [:payload :entity]) :comp))))

(defn comp-tag-in [db-conn message]
  (tx-entity! db-conn :comp-tag (hatch/slam-all (get-in message [:payload :entity]) :comp-tag)))

(defn perf-asmt-in [db-conn message]
  (tx-entity! db-conn :perf-asmt (merge {:perf-asmt/id-sk (str (get-in message [:header :entity-id :perf-asmt-id]))}
                                         (hatch/slam-all (get-in message [:paylod :entity]) :perf-asmt))))
(defn user2comp-in [db-conn message]
  (tx-entity! db-conn :user2comp (hatch/slam-all (get-in message [:payload :entity]) :user2comp)))

(defn user2perf-asmt-in [db-conn message]
  (tx-entity! db-conn :user2perf-asmt (hatch/slam-all (get-in message [:payload :entity]) :user2perf-asmt)))

(comment

  (navigator/task-in (:db-conn nt-config/system)
                     {:payload
                      {:entity
                       {:competency-parents [1 2 3],
                        :name "tie shoes (together)",
                        :version "v3"}},
                      :header
                      {:entity-type "task", :operation "assert", :entity-id {:task-id 17}}})

{'fraction_non_alphanumeric': 0.149879334434142, 'fraction_numerical': 0.004064524323637749, 'mean_word_length': 2.048393341076268, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '720013', 'n_tokens_mistral': 2672, 'n_tokens_neox': 2579, 'n_words': 706}