Skip to content

Commit

Permalink
Recent job names from status resource
Browse files Browse the repository at this point in the history
  • Loading branch information
cburgmer committed Apr 18, 2016
1 parent 21d1c2b commit ae105b0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
30 changes: 24 additions & 6 deletions src/buildviz/controllers/status.clj
Original file line number Diff line number Diff line change
@@ -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)))))
13 changes: 12 additions & 1 deletion test/buildviz/controllers/status_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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")))))))

0 comments on commit ae105b0

Please sign in to comment.