<commit_before>(ns riemann.opsgenie
  "Forwards events to OpsGenie"
  (:require [clj-http.client :as client])
  (:require [cheshire.core :as json]))

(def ^:private alerts-url
  "https://api.opsgenie.com/v1/json/alert")

(defn- post
 "Post to OpsGenie"
 [url body]
  (client/post url
                {:body body
                 :socket-timeout 5000
                 :conn-timeout 5000
                 :content-type :json
                 :accept :json
                 :throw-entire-message? true}))

(defn- message
  "Generate description based on event.
  Because service might be quite long and opsgenie limits message, it
  pulls more important info into beginning of the string"
  [event]
  (str (:host event)
       ": [" (:state event) "] "
       (:service event)))

(defn- description
  "Generate message based on event"
  [event]
  (str
   "Host: " (:host event)
   " \nService: " (:service event)
   " \nState: " (:state event)
   " \nMetric: " (:metric event)
   " \nDescription: " (:description event)))

(defn- api-alias
  "Generate OpsGenie alias based on event"
  [event]
  (hash (str (:host event) " "
       (:service event))))

(defn- create-alert
  "Create alert in OpsGenie"
  [api-key event recipients]
  (post alerts-url (json/generate-string
                    {:message (message event)
                     :description (description event)
                     :apiKey api-key
                     :alias (api-alias event)
                     :tags (clojure.string/join "," (:tags event))
                     :recipients recipients})))
(defn- close-alert
  "Close alert in OpsGenie"
  [api-key event]
  (post (str alerts-url "/close")
        (json/generate-string
          {:apiKey api-key
           :alias (api-alias event)})))

(defn opsgenie
  "Creates an OpsGenie adapter. Takes your OG service key, and returns a map of
  functions which trigger and resolve events. clojure/hash from event host and service
  will be used as the alias.

  (let [og (opsgenie \"my-service-key\" \"recipient@example.com\")]
    (changed-state
      (where (state \"ok\") (:resolve og))<commit_msg>use tags in opsgenie event alias<commit_after>(ns riemann.opsgenie
  "Forwards events to OpsGenie"
  (:require [clj-http.client :as client])
  (:require [cheshire.core :as json]))

(def ^:private alerts-url
  "https://api.opsgenie.com/v1/json/alert")

(defn- post
 "Post to OpsGenie"
 [url body]
  (client/post url
                {:body body
                 :socket-timeout 5000
                 :conn-timeout 5000
                 :content-type :json
                 :accept :json
                 :throw-entire-message? true}))

(defn- message
  "Generate description based on event.
  Because service might be quite long and opsgenie limits message, it
  pulls more important info into beginning of the string"
  [event]
  (str (:host event)
       ": [" (:state event) "] "
       (:service event)))

(defn- description
  "Generate message based on event"
  [event]
  (str
   "Host: " (:host event)
   " \nService: " (:service event)
   " \nState: " (:state event)
   " \nMetric: " (:metric event)
   " \nDescription: " (:description event)))

(defn- api-alias
  "Generate OpsGenie alias based on event"
  [event]
  (hash (str (:host event) " "
       (:service event) " "
       (apply str (:tags event)))))

(defn- create-alert
  "Create alert in OpsGenie"
  [api-key event recipients]
  (post alerts-url (json/generate-string
                    {:message (message event)
                     :description (description event)
                     :apiKey api-key
                     :alias (api-alias event)
                     :tags (clojure.string/join "," (:tags event))
                     :recipients recipients})))
(defn- close-alert
  "Close alert in OpsGenie"
  [api-key event]
  (post (str alerts-url "/close")
        (json/generate-string
          {:apiKey api-key
           :alias (api-alias event)})))

(defn opsgenie
  "Creates an OpsGenie adapter. Takes your OG service key, and returns a map of
  functions which trigger and resolve events. clojure/hash from event host, service and tags
  will be used as the alias.

  (let [og (opsgenie \"my-service-key\" \"recipient@example.com\")]
    (changed-state
      (where (state \"ok\") (:resolve og))
{'fraction_non_alphanumeric': 0.12389380530973451, 'fraction_numerical': 0.004191895668374476, 'mean_word_length': 2.2836391437308867, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2326665', 'n_tokens_mistral': 1373, 'n_tokens_neox': 1297, 'n_words': 419}