<commit_before>(ns yetibot.core.monitoring
  (:require [clojure.spec.alpha :as s]
            [metrics.reporters.riemann :as riemann]
            [taoensso.timbre :as log]
            [yetibot.core.config :as config]
            [yetibot.core.spec :as yspec])
  (:import [java.util.concurrent TimeUnit]
           [com.codahale.metrics MetricFilter]))

(s/def ::host ::yspec/non-blank-string)

(s/def ::port pos-int?)

(s/def ::interval pos-int?)

(s/def ::riemann (s/keys :req-un [::host]
                         :opt-un [::port ::interval]))

(s/def ::config (s/keys :req-un [::riemann]))

(defonce riemann-reporter (atom nil))

(defn start
  []
  (if-let [{:keys [host port interval]} (config/get-config ::config [:monitoring])]
    (let [reporter (-> (riemann/make-riemann host (or port 5555))
                       (riemann/reporter {:rate-unit TimeUnit/SECONDS
                                          :duration-unit TimeUnit/MILLISECONDS
                                          :filter MetricFilter/ALL}))]
      (log/info "starting monitoring")
      (reset! riemann-reporter reporter)
      (riemann/start reporter (or interval 1)))
    (log/debug "monitoring is not configured")))

(defn stop
  []
  (when-let [r @riemann-reporter]
    (riemann/stop r)<commit_msg>Fix bug in monitoring where it attempted to start even when not configured<commit_after>(ns yetibot.core.monitoring
  (:require [clojure.spec.alpha :as s]
            [metrics.reporters.riemann :as riemann]
            [taoensso.timbre :as log]
            [yetibot.core.config :as config]
            [yetibot.core.spec :as yspec])
  (:import [java.util.concurrent TimeUnit]
           [com.codahale.metrics MetricFilter]))

(s/def ::host ::yspec/non-blank-string)

(s/def ::port pos-int?)

(s/def ::interval pos-int?)

(s/def ::riemann (s/keys :req-un [::host]
                         :opt-un [::port ::interval]))

(s/def ::config (s/keys :req-un [::riemann]))

(defonce riemann-reporter (atom nil))

(defn start
  []
  (let [{:keys [host port interval]} (config/get-config ::config [:monitoring])]
    (if (and host port interval)
      (let [reporter (-> (riemann/make-riemann host (or port 5555))
                         (riemann/reporter {:rate-unit TimeUnit/SECONDS
                                            :duration-unit TimeUnit/MILLISECONDS
                                            :filter MetricFilter/ALL}))]
        (log/info "starting monitoring")
        (reset! riemann-reporter reporter)
        (riemann/start reporter (or interval 1)))
      (log/debug "monitoring is not configured"))))

(defn stop
  []
  (when-let [r @riemann-reporter]
    (riemann/stop r)
{'fraction_non_alphanumeric': 0.1444111027756939, 'fraction_numerical': 0.0037509377344336083, 'mean_word_length': 2.27239263803681, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7035773', 'n_tokens_mistral': 897, 'n_tokens_neox': 845, 'n_words': 208}