<commit_before>(ns gratefulplace.db.transform
  (:require [gratefulplace.db.query :as db]))

(declare rules transform-entity)

(def rules
  {:topic {:id :db/id
           :title :topic/title
           :first-post #(transform-entity (:post rules) (:topic/first-post %))
           :posts (fn [ent] (map #(transform-entity (:post rules) %)
                                (db/all :post/content [:post/topic (:db/id ent)])))
           :author #(transform-entity (:user rules) (:content/author %))
           :post-count #(ffirst (db/q [:find '(count ?c) :where ['?c :post/topic (:db/id %)]]))}
   :post {:id :db/id
          :content :post/content
          :author #(transform-entity (:user rules) (:content/author %))}
   
   :user {:id :db/id
          :username :user/username
          :email :user/email}})

(defn transform-entity
  ;; Given a map of transformations, apply them such that a map is
  ;; returned where the keys of the return and the transformations are
  ;; the same, and the return values are derived by applying the
  ;; values of the transformation map to the supplied entity
  [transformations entity]
  (reduce (fn [acc t]
            (conj acc [(first t) ((second t) entity)]))
          (empty transformations)
          transformations))<commit_msg>helper functions for references<commit_after>(ns gratefulplace.db.transform
  (:require [gratefulplace.db.query :as db]))

(declare rules transform-entity)

(defn one
  [ref-fn rules]
  #(transform-entity rules (ref-fn %)))

(defn many
  [remote-key rules]
  (fn [ent] (map #(transform-entity rules %)
                (db/all remote-key [remote-key (:db/id ent)]))))

(defn ref-count
  [ref-attr]
  #(ffirst (db/q [:find '(count ?c) :where ['?c ref-attr (:db/id %)]])))

(def rules
  {:topic {:id :db/id
           :title :topic/title
           :first-post (one :topic/first-post (:post rules))
           :posts (many :post/topic (:post rules))
           :author (one :content/author (:user rules))
           :post-count (ref-count :post/topic)}
   :post {:id :db/id
          :content :post/content
          :author (one :content/author (:user rules))}
   :user {:id :db/id
          :username :user/username
          :email :user/email}})

(defn transform-entity
  ;; Given a map of transformations, apply them such that a map is
  ;; returned where the keys of the return and the transformations are
  ;; the same, and the return values are derived by applying the
  ;; values of the transformation map to the supplied entity
  [transformations entity]
  (reduce (fn [acc t]
            (conj acc [(first t) ((second t) entity)]))
          (empty transformations)
          transformations))
{'fraction_non_alphanumeric': 0.14072415080253825, 'fraction_numerical': 0.0, 'mean_word_length': 2.6914600550964187, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3618611', 'n_tokens_mistral': 839, 'n_tokens_neox': 795, 'n_words': 273}