<commit_before>(ns merlion.etcd
  (:require
   [clojure.test :as test :refer [is deftest with-test]]
   [ring.util.codec]
   [clojure.core.async :as async :refer [<! go chan >!]]
   [clojure.string :as str]
   [aleph.http :as http]
   [byte-streams :as bs]
   [cheshire.core :as json]
   [clojure.edn :as edn]))


(defn value-from-etcd-node [nodename node]
  (if (get node "dir")
    (reduce (fn [m e]
              (assoc
               m
               (keyword (subs (str/replace-first (get e "key") nodename "") 1))
               (value-from-etcd-node (get e "key") e)))
            {}
            (get node "nodes"))
    (get node "value")))

(defn map-from-etcd-response [prefix response-body]
  (value-from-etcd-node prefix
                        (get (json/decode response-body) "node")))

(deftest map-from-etcd-response-test
  (let [body (slurp "test/fixtures/etcd-response.json")
        m (map-from-etcd-response  "/conf/dev.booksh.lv" body)]
    (is (= (keys m) [:auth]))
    (is (= (keys (:auth m)) [:google :facebook :yahoo]))
    (is (= (-> m :auth :google :client-id)
           "608620955125-9plvti1kpi8vjacjo3ssmei9m43r1qo9.apps.googleusercontent.com"))))

(def get-http (comp slurp :body deref http/get))
(def put-http (comp slurp :body deref http/put))
(def etcd-endpoint "http://localhost:2379/v2/keys")

(defn get-prefix [prefix]
  (->> (str etcd-endpoint prefix "/?recursive=1")
       get-http
       (map-from-etcd-response prefix)))

(defn watch-prefix [prefix]
  (let [ch (chan)]
    (go
      (loop []<commit_msg>etcd: work on empty nodes<commit_after>(ns merlion.etcd
  (:require
   [clojure.test :as test :refer [is deftest with-test]]
   [ring.util.codec]
   [clojure.core.async :as async :refer [<! go chan >!]]
   [clojure.string :as str]
   [aleph.http :as http]
   [byte-streams :as bs]
   [cheshire.core :as json]
   [clojure.edn :as edn]))


(defn value-from-etcd-node [nodename node]
  (if (get node "dir")
    (reduce (fn [m e]
              (assoc
               m
               (keyword (subs (str/replace-first (get e "key") nodename "") 1))
               (value-from-etcd-node (get e "key") e)))
            {}
            (get node "nodes"))
    (get node "value")))

(defn map-from-etcd-response [prefix response-body]
  (let [j (json/decode response-body)]
    (if-let [node (get j "node")]
      (value-from-etcd-node prefix node)
      (with-meta {} {:error j}))))

(deftest map-from-etcd-response-test
  (let [body (slurp "test/fixtures/etcd-response.json")
        m (map-from-etcd-response  "/conf/dev.booksh.lv" body)]
    (is (= (keys m) [:auth]))
    (is (= (keys (:auth m)) [:google :facebook :yahoo]))
    (is (= (-> m :auth :google :client-id)
           "608620955125-9plvti1kpi8vjacjo3ssmei9m43r1qo9.apps.googleusercontent.com"))))

(def get-http
  (comp slurp :body deref #(http/get % {:throw-exceptions false})))
(def put-http
  (comp slurp :body deref #(http/get % {:throw-exceptions false})))
(def etcd-endpoint "http://localhost:2379/v2/keys")

(defn get-prefix [prefix]
  (->> (str etcd-endpoint prefix "/?recursive=1")
       get-http
       (map-from-etcd-response prefix)))

(defn watch-prefix [prefix]
  (let [ch (chan)]
    (go
      (loop []
{'fraction_non_alphanumeric': 0.17407522536524714, 'fraction_numerical': 0.017407522536524712, 'mean_word_length': 2.669327251995439, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '6846945', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1214, 'n_words': 291}