diff --git a/src/buildviz/controllers/status.clj b/src/buildviz/controllers/status.clj index 0f420206..7e713ec5 100644 --- a/src/buildviz/controllers/status.clj +++ b/src/buildviz/controllers/status.clj @@ -1,15 +1,33 @@ (ns buildviz.controllers.status (:require [buildviz.data.results :as results] - [buildviz.util.http :as http])) + [buildviz.util.http :as http] + [clj-time + [coerce :as tc] + [core :as t]])) + +;; We manually calculate this period to be in sync with the JS implementation +(def ^:private two-months (* 2 30 24 60 60 1000)) + +(defn- recent-jobs [all-builds] + (let [two-months-ago (- (tc/to-long (t/now)) two-months)] + (filter #(< two-months-ago (:start %)) + all-builds))) + +(defn- build-starts [all-builds] + (->> all-builds + (map :start) + (remove nil?))) (defn get-status [build-results pipeline-name] (let [all-builds (results/all-builds build-results) total-build-count (count all-builds) - build-starts (->> all-builds - (map :start) - (remove nil?) - seq)] + build-starts (seq (build-starts all-builds)) + recent-job-names (->> (recent-jobs all-builds) + (map :job) + distinct + seq)] (http/respond-with-json (cond-> {:totalBuildCount total-build-count} pipeline-name (assoc :pipelineName pipeline-name) build-starts (assoc :latestBuildStart (apply max build-starts) - :earliestBuildStart (apply min build-starts)))))) + :earliestBuildStart (apply min build-starts)) + recent-job-names (assoc :recentJobNames recent-job-names))))) diff --git a/test/buildviz/controllers/status_test.clj b/test/buildviz/controllers/status_test.clj index 8cdcc6d4..bfea6205 100644 --- a/test/buildviz/controllers/status_test.clj +++ b/test/buildviz/controllers/status_test.clj @@ -48,4 +48,15 @@ pipeline-name) body (json-body (json-get-request (the-app) "/status"))] (is (= pipeline-name - (get body "pipelineName")))))) + (get body "pipelineName"))))) + + (testing "should return names of recent jobs" + (let [today (tc/to-long (t/now)) + app (the-app {"aBuild" {"1" {:start today} + "2" {:start (- today (* 60 a-day))}} + "anotherBuild" {"3" {:start (- today (* 60 a-day))}}} + {})] + (is (= ["aBuild"] + (-> (json-get-request app "/status") + json-body + (get "recentJobNames")))))))