<commit_before>(ns buildviz.handler
  (:use compojure.core
        ring.middleware.json
        ring.middleware.resource
        ring.middleware.content-type
        ring.middleware.not-modified)
  (:require [compojure.handler :as handler]
             [buildviz.jobinfo :as jobinfo]))

(def builds (atom {}))

(defn- job-entry [job]
  (if (contains? @builds job)
    (@builds job)
    {}))

(defn- store-build! [job build buildData]
  (let [entry (job-entry job)
        updatedEntry (assoc entry build buildData)]
    (swap! builds assoc job updatedEntry)
    {:body buildData}))

(defn- get-build [job build]
  (if (contains? @builds job)
    (if-let [buildData ((@builds job) build)]
      {:body buildData}
      {:status 404})
    {:status 404}))

(defn- avg [series]
  (/ (reduce + series) (count series)))

(defn- duration-for [build]
  (if (and (contains? build :end) (contains? build :end))
    (- (build :end) (build :start))))

(defn- average-runtime-for [summary buildDataEntries]
  (let [runtimes (filter (complement nil?) (map duration-for buildDataEntries))]
    (if (not (empty? runtimes))
      (assoc summary :averageRuntime (avg runtimes))
      summary)))

(defn- total-count-for [summary buildDataEntries]
  (assoc summary :totalCount (count buildDataEntries)))

(defn- builds-with-outcome [build-data-entries]
  (filter #(contains? % :outcome) build-data-entries))

(defn- error-count-for [summary build-data-entries]
  (if-let [builds (seq (builds-with-outcome build-data-entries))]
    (assoc summary :failedCount (count (filter #(= "fail" (:outcome %)) builds)))
    summary))

(defn- flaky-runs-for [summary build-data-entries]
  (if-let [builds (seq (builds-with-outcome build-data-entries))]
    (assoc summary :flakyCount (jobinfo/flaky-build-count builds))
    summary))

(defn- summary-for [job]
  (let [buildDataEntries (vals (@builds job))]
    (-> {}
        (average-runtime-for buildDataEntries)
        (total-count-for buildDataEntries)
        (error-count-for buildDataEntries)
        (flaky-runs-for buildDataEntries))))

(defn- get-pipeline []
  (let [jobNames (keys @builds)
        buildSummaries (map summary-for jobNames)
        buildSummary (zipmap jobNames buildSummaries)]
    {:body buildSummary}))

(defroutes app-routes
  (PUT "/builds/:job/:build" [job build :as {body :body}] (store-build! job build body))
  (GET "/builds/:job/:build" [job build] (get-build job build))

  (GET "/pipeline" [] (get-pipeline)))<commit_msg>Naming<commit_after>(ns buildviz.handler
  (:use compojure.core
        ring.middleware.json
        ring.middleware.resource
        ring.middleware.content-type
        ring.middleware.not-modified)
  (:require [compojure.handler :as handler]
             [buildviz.jobinfo :as jobinfo]))

(def builds (atom {}))

(defn- job-entry [job]
  (if (contains? @builds job)
    (@builds job)
    {}))

(defn- store-build! [job build build-data]
  (let [entry (job-entry job)
        updated-entry (assoc entry build build-data)]
    (swap! builds assoc job updated-entry)
    {:body build-data}))

(defn- get-build [job build]
  (if (contains? @builds job)
    (if-let [build-data ((@builds job) build)]
      {:body build-data}
      {:status 404})
    {:status 404}))

(defn- avg [series]
  (/ (reduce + series) (count series)))

(defn- duration-for [build]
  (if (and (contains? build :end) (contains? build :end))
    (- (build :end) (build :start))))

(defn- average-runtime-for [summary build-data-entries]
  (let [runtimes (filter (complement nil?) (map duration-for build-data-entries))]
    (if (not (empty? runtimes))
      (assoc summary :averageRuntime (avg runtimes))
      summary)))

(defn- total-count-for [summary build-data-entries]
  (assoc summary :totalCount (count build-data-entries)))

(defn- builds-with-outcome [build-data-entries]
  (filter #(contains? % :outcome) build-data-entries))

(defn- error-count-for [summary build-data-entries]
  (if-let [builds (seq (builds-with-outcome build-data-entries))]
    (assoc summary :failedCount (count (filter #(= "fail" (:outcome %)) builds)))
    summary))

(defn- flaky-runs-for [summary build-data-entries]
  (if-let [builds (seq (builds-with-outcome build-data-entries))]
    (assoc summary :flakyCount (jobinfo/flaky-build-count builds))
    summary))

(defn- summary-for [job]
  (let [build-data-entries (vals (@builds job))]
    (-> {}
        (average-runtime-for build-data-entries)
        (total-count-for build-data-entries)
        (error-count-for build-data-entries)
        (flaky-runs-for build-data-entries))))

(defn- get-pipeline []
  (let [jobNames (keys @builds)
        buildSummaries (map summary-for jobNames)
        buildSummary (zipmap jobNames buildSummaries)]
    {:body buildSummary}))

(defroutes app-routes
  (PUT "/builds/:job/:build" [job build :as {body :body}] (store-build! job build body))
  (GET "/builds/:job/:build" [job build] (get-build job build))

  (GET "/pipeline" [] (get-pipeline)))
{'fraction_non_alphanumeric': 0.15856109324758844, 'fraction_numerical': 0.002411575562700965, 'mean_word_length': 3.4596774193548385, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4005195', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1708, 'n_words': 429}