<commit_before>(ns emotions-demo.webapp
  (:require [clojure.core.async :as async
             :refer [<! >! <!! timeout chan alt! put! go go-loop]]
            [ring.util.response :refer [response]]
            [chord.http-kit :refer [with-channel]]
            [nomad :refer [defconfig]]
            [clojure.java.io :as io]
            [compojure.core :refer [defroutes GET routes]]
            [compojure.handler :refer [api]]
            [compojure.route :refer [resources]]
            [hiccup.page :refer [html5 include-js include-css]]
            [hiccup.element :refer [javascript-tag]]
            [emotions-demo.core :refer :all]))

(defconfig my-config (io/resource "config/demo.edn"))

(def remote-clients-atom (atom {}))

; would prefer not to have these as globals, need to think of a
; cleaner way to share them with ws-handler
(def state-chan (chan))

(def percept-chan (chan))

(def next-id!
  (let [counter (java.util.concurrent.atomic.AtomicLong.)]
    (fn [] (.incrementAndGet counter))))

(defn add-client
  [clients-atom client-id listener-chan]
  (swap! clients-atom assoc client-id listener-chan))

(defn remote-state-emitter
  [state-chan clients-atom]
  (go-loop [state (<! state-chan)]
           (when state
             (doseq [[client-id client-chan] @clients-atom]
               (>! client-chan (pr-str state)))
             (recur (<! state-chan)))))

(defn index-page []
  (html5
   [:head
    [:title "Clojure Emotional model demo"]]
    [:body
     [:div#app]
      (include-css "/css/demo.css")
      (include-js "//fb.me/react-0.8.0.js") ; only required in dev build
      (include-js "/out/goog/base.js") ; only required in dev build
      (include-js "/js/demo.js")
      (javascript-tag "goog.require('emotions_demo.client');") ; only required in dev build
      ]))

(defn ws-handler [req]
  (with-channel req ws
    (println "Opened connection from" (:remote-addr req))
    (let [client-id (next-id!)]
      (add-client remote-clients-atom client-id ws))
    (go-loop []
      (when-let [{:keys [message]} (<! ws)]
        (println "Message received:" message)
        (let [[msg-type msg-body] (read-string message)]
          (condp = msg-type
            :connect (start-robot msg-body percept-chan state-chan)
            (println "Unexpected message" msg-type msg-body)))
        (recur)))))

(defn app-routes []
  (routes
    (GET "/" [] (response (index-page)))<commit_msg>added D3 include to page<commit_after>(ns emotions-demo.webapp
  (:require [clojure.core.async :as async
             :refer [<! >! <!! timeout chan alt! put! go go-loop]]
            [ring.util.response :refer [response]]
            [chord.http-kit :refer [with-channel]]
            [nomad :refer [defconfig]]
            [clojure.java.io :as io]
            [compojure.core :refer [defroutes GET routes]]
            [compojure.handler :refer [api]]
            [compojure.route :refer [resources]]
            [hiccup.page :refer [html5 include-js include-css]]
            [hiccup.element :refer [javascript-tag]]
            [emotions-demo.core :refer :all]))

(defconfig my-config (io/resource "config/demo.edn"))

(def remote-clients-atom (atom {}))

; would prefer not to have these as globals, need to think of a
; cleaner way to share them with ws-handler
(def state-chan (chan))

(def percept-chan (chan))

(def next-id!
  (let [counter (java.util.concurrent.atomic.AtomicLong.)]
    (fn [] (.incrementAndGet counter))))

(defn add-client
  [clients-atom client-id listener-chan]
  (swap! clients-atom assoc client-id listener-chan))

(defn remote-state-emitter
  [state-chan clients-atom]
  (go-loop [state (<! state-chan)]
           (when state
             (doseq [[client-id client-chan] @clients-atom]
               (>! client-chan (pr-str state)))
             (recur (<! state-chan)))))

(defn index-page []
  (html5
   [:head
    [:title "Clojure Emotional model demo"]]
    [:body
     [:div#app]
      (include-css "/css/demo.css")
      (include-js "//fb.me/react-0.8.0.js") ; only required in dev build
      (include-js "/out/goog/base.js") ; only required in dev build
      (include-js "/jslib/d3/d3.min.js")
      (include-js "/js/demo.js")
      (javascript-tag "goog.require('emotions_demo.client');") ; only required in dev build
      ]))

(defn ws-handler [req]
  (with-channel req ws
    (println "Opened connection from" (:remote-addr req))
    (let [client-id (next-id!)]
      (add-client remote-clients-atom client-id ws))
    (go-loop []
      (when-let [{:keys [message]} (<! ws)]
        (println "Message received:" message)
        (let [[msg-type msg-body] (read-string message)]
          (condp = msg-type
            :connect (start-robot msg-body percept-chan state-chan)
            (println "Unexpected message" msg-type msg-body)))
        (recur)))))

(defn app-routes []
  (routes
    (GET "/" [] (response (index-page)))
{'fraction_non_alphanumeric': 0.15789473684210525, 'fraction_numerical': 0.0026519787841697267, 'mean_word_length': 2.524802300503235, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, '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': '7643920', 'n_tokens_mistral': 1655, 'n_tokens_neox': 1620, 'n_words': 413}