<commit_before>(ns swarmpit.event.handler
  (:require [org.httpkit.server :refer [with-channel on-close send!]]
            [clojure.walk :refer [keywordize-keys]]
            [swarmpit.handler :refer [dispatch resp-accepted resp-error resp-unauthorized]]
            [swarmpit.event.channel :as channel]
            [swarmpit.event.processor :as processor]
            [swarmpit.event.rules.predicate :refer [stats?]]
            [swarmpit.slt :as slt]))

(defn events
  [{{:keys [query]} :parameters}]
  (let [slt (:slt query)]
    (if (slt/valid? slt)
      (with-channel request channel
                    (send! channel {:status  200
                                    :headers {"Content-Type"                "text/event-stream"
                                              "Access-Control-Allow-Origin" "*"
                                              "Cache-Control"               "no-cache"
                                              "Connection"                  "keep-alive"}
                                    :body    ":ok\n\n"} false)
                    (swap! channel/hub assoc channel request)
                    (on-close channel (fn [_]
                                        (swap! channel/hub dissoc channel))))
      (resp-unauthorized "Invalid slt"))))

(defn event-push
  [{{:keys [body]} :parameters}]
  (if (some? body)
    (let [event body]
      (processor/process event)
      (if (stats? (:type event))
        (channel/broadcast-statistics)
        (channel/broadcast-memo event))
      (resp-accepted))
    (resp-error 400 "No data sent")))<commit_msg>prepare event handler for reitit #377<commit_after>(ns swarmpit.event.handler
  (:require [org.httpkit.server :refer [with-channel on-close send!]]
            [clojure.walk :refer [keywordize-keys]]
            [swarmpit.handler :refer [resp-accepted resp-error resp-unauthorized]]
            [swarmpit.event.channel :as channel]
            [swarmpit.event.processor :as processor]
            [swarmpit.event.rules.predicate :refer [stats?]]
            [clojure.tools.logging :as log]

            [swarmpit.slt :as slt]))

(defn events
  [{:keys [parameters] :as request}]
  (let [slt (get-in parameters [:query :slt])]
    (if (slt/valid? slt)
      (with-channel request channel
                    (send! channel {:status  200
                                    :headers {"Content-Type"                "text/event-stream"
                                              "Access-Control-Allow-Origin" "*"
                                              "Cache-Control"               "no-cache"
                                              "Connection"                  "keep-alive"}
                                    :body    ":ok\n\n"} false)
                    (swap! channel/hub assoc channel request)
                    (on-close channel (fn [_]
                                        (swap! channel/hub dissoc channel))))
      (resp-unauthorized "Invalid slt"))))

(defn event-push
  [{{:keys [body]} :parameters}]
  (if (some? body)
    (let [event body]
      (processor/process event)
      (if (stats? (:type event))
        (channel/broadcast-statistics)
        (channel/broadcast-memo event))
      (resp-accepted))
    (resp-error 400 "No data sent")))
{'fraction_non_alphanumeric': 0.12293577981651377, 'fraction_numerical': 0.0045871559633027525, 'mean_word_length': 1.472411186696901, 'pattern_counts': {'":': 2, '<': 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': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4021823', 'n_tokens_mistral': 912, 'n_tokens_neox': 869, 'n_words': 209}