<commit_before>(ns pc.http.doc
  (:require [clojure.tools.reader.edn :as edn]
            [clojure.java.io :as io]
            [clojure.tools.logging :as log]
            [datomic.api :as d]
            [pc.datomic :as pcd]
            [pc.models.chat-bot :as chat-bot-model]
            [pc.models.doc :as doc-model]
            [pc.models.layer :as layer-model])
  (:import [java.io PushbackReader]))

(defn read-doc [doc]
  (edn/read (PushbackReader. (io/reader (io/resource (format "docs/%s.edn" doc))))))

(defn docs* []
  {"interactive-demo" (read-doc "interactive-demo")})

(def docs (memoize docs*))

(defn duplicate-doc [document-name cust]
  (let [doc (doc-model/create-public-doc! (merge {:document/name (str "Copy of " document-name)
                                                  :document/chat-bot (rand-nth chat-bot-model/chat-bots)}
                                                 (when (:cust/uuid cust) {:document/creator (:cust/uuid cust)})))]
    (if-let [layers (get (docs) document-name)]
      @(d/transact (pcd/conn) (conj (map #(assoc %
                                            :db/id (d/tempid :db.part/user)
                                            :document/id (:db/id doc))
                                         layers)
                                    (merge {:db/id (d/tempid :db.part/tx)
                                            :document/id (:db/id doc)}
                                           (when (:cust/uuid cust)
                                             {:cust/uuid (:cust/uuid cust)}))))
      (log/infof "requested duplicate doc for non-existent doc %s" document-name))
    (:db/id doc)))

(defn save-doc
  "Helper function to save an existing doc, still needs to be committed and added to docs
   after it has been saved."
  [doc-id doc-name]
  (spit (format "resources/docs/%s.edn" doc-name)
        (pr-str (map #(dissoc % :db/id :document/id)<commit_msg>fix duplicating the interactive layers demo<commit_after>(ns pc.http.doc
  (:require [clojure.tools.reader.edn :as edn]
            [clojure.java.io :as io]
            [clojure.tools.logging :as log]
            [datomic.api :as d]
            [pc.datomic :as pcd]
            [pc.datomic.web-peer :as web-peer]
            [pc.models.chat-bot :as chat-bot-model]
            [pc.models.doc :as doc-model]
            [pc.models.layer :as layer-model])
  (:import java.io.PushbackReader
           java.util.UUID))

(defn read-doc [doc]
  (edn/read (PushbackReader. (io/reader (io/resource (format "docs/%s.edn" doc))))))

(defn docs* []
  {"interactive-demo" (read-doc "interactive-demo")})

(def docs (memoize docs*))

(defn duplicate-doc [document-name cust]
  (let [doc (doc-model/create-public-doc! (merge {:document/name (str "Copy of " document-name)
                                                  :document/chat-bot (rand-nth chat-bot-model/chat-bots)}
                                                 (when (:cust/uuid cust) {:document/creator (:cust/uuid cust)})))]
    (if-let [layers (get (docs) document-name)]
      @(d/transact (pcd/conn) (conj (map-indexed
                                     (fn [i l]
                                       (let [temp-id (d/tempid :db.part/user)]
                                         (assoc l
                                                :db/id temp-id
                                                :document/id (:db/id doc)
                                                :frontend/id (UUID. (:db/id doc) (inc i)))))
                                     layers)
                                    (merge {:db/id (d/tempid :db.part/tx)
                                            :document/id (:db/id doc)}
                                           (when (:cust/uuid cust)
                                             {:cust/uuid (:cust/uuid cust)}))))
      (log/infof "requested duplicate doc for non-existent doc %s" document-name))
    (:db/id doc)))

(defn save-doc
  "Helper function to save an existing doc, still needs to be committed and added to docs
   after it has been saved."
  [doc-id doc-name]
  (spit (format "resources/docs/%s.edn" doc-name)
        (pr-str (map #(dissoc % :db/id :document/id)
{'fraction_non_alphanumeric': 0.13558515699333967, 'fraction_numerical': 0.0, 'mean_word_length': 1.3919226393629125, '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': 41, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7107835', 'n_tokens_mistral': 1261, 'n_tokens_neox': 1215, 'n_words': 312}