Skip to content

Commit

Permalink
Use new state protocol and make code more readable in the process #11
Browse files Browse the repository at this point in the history
  • Loading branch information
flosell committed Nov 29, 2016
1 parent 1ce6a2a commit 2ab67a3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 48 deletions.
97 changes: 52 additions & 45 deletions src/lambdacd_cctray/core.clj
Original file line number Diff line number Diff line change
@@ -1,61 +1,69 @@
(ns lambdacd-cctray.core
(:require [clojure.data.xml :as xml]
[lambdacd.presentation.pipeline-structure :as lp]
[lambdacd.internal.pipeline-state :as pipeline-state]
[lambdacd.state.core :as state]
[clojure.string :as s]
[clj-time.format :as f]))

(defn- has-step-id [step-id [_ steps]]
(get steps step-id))
(defn- not-nil? [x]
(not (nil? x)))

(defn- state-for [step-id [k steps]]
(let [build-number k
status (get steps step-id)
activity (if (= (:status status) :running) "Building" "Sleeping")]
(defn- activity-for [step-result]
(if (= (:status step-result) :running)
"Building"
"Sleeping"))

(defn- state-for [ctx build-number step-id]
(if-let [step-result (state/get-step-result ctx build-number step-id)]
{:build-number build-number
:activity activity
:result status}))
:activity (activity-for step-result)
:result step-result}))

(defn- first-updated [build-state]
(:first-updated-at (:result build-state)))

(defn- states-for [step-id ctx]
(->> (state/all-build-numbers ctx)
(map #(state-for ctx % step-id))
(filter not-nil?)
(sort-by first-updated)
(reverse)))

(defn- first-updated [step-id]
(fn [[_ steps]]
(:first-updated-at (get steps step-id))))
(defn- cctray-status-for [step-result]
(case (:status step-result)
:success "Success"
:failure "Failure"
"Unknown"))

(defn- states-for [step-id state]
(let [builds-with-step-id (filter #(has-step-id step-id %) (seq state))
by-most-recent (reverse (sort-by (first-updated step-id) builds-with-step-id))]
(map #(state-for step-id %) by-most-recent)))
(defn- current-build-active? [states-for-step]
(let [cur-build-status (:status (:result (first states-for-step)))]
(or (= cur-build-status :running)
(= cur-build-status :waiting))))

(defn- last-build-status-for [states-for-step]
(let [current-build (:result (first states-for-step))
prev-build (:result (second states-for-step))
cur-build-status (:status current-build)
build-to-use (if (or (= cur-build-status :running) (= cur-build-status :waiting))
prev-build
current-build)
status-to-use (:status build-to-use)]
(case status-to-use
:success "Success"
:failure "Failure"
"Unknown")))
(let [build-to-use (if (current-build-active? states-for-step)
(second states-for-step)
(first states-for-step))]
(cctray-status-for (:result build-to-use))))

(defn- last-build-time-for [step]
(let [most-recent-update (:most-recent-update-at (:result step))
formatted (f/unparse (f/formatters :date-time) most-recent-update)]
formatted (f/unparse (f/formatters :date-time) most-recent-update)]
formatted))

(defn- project-for [state config fallback-base-url step-info]
(let [step-id (:step-id step-info)
(defn- project-for [context config fallback-base-url step-info]
(let [step-id (:step-id step-info)
formatted-step-id (s/join "-" step-id)
states-for-step (states-for step-id state)
state-for-step (first states-for-step)
states-for-step (states-for step-id context)
state-for-step (first states-for-step)
last-build-number (:build-number state-for-step)
pipeline-name (:name config)
add-prefix (get config :cctray-add-prefix true)
step-name (:name step-info)
name (if (and add-prefix pipeline-name)
(str pipeline-name " :: " step-name)
step-name)
base-url (or (:ui-url config) fallback-base-url)]
pipeline-name (:name config)
add-prefix (get config :cctray-add-prefix true)
step-name (:name step-info)
name (if (and add-prefix pipeline-name)
(str pipeline-name " :: " step-name)
step-name)
base-url (or (:ui-url config) fallback-base-url)]
(xml/element :Project {:name name
:activity (:activity state-for-step)
:lastBuildStatus (last-build-status-for states-for-step)
Expand All @@ -68,13 +76,12 @@
(concat pipeline-representation children-reps)))

(defn- projects-for [pipeline fallback-base-url]
(let [def (:pipeline-def pipeline)
context (:context pipeline)
config (:config context)
state-component (:pipeline-state-component context)
state (pipeline-state/get-all state-component)
(let [def (:pipeline-def pipeline)
context (:context pipeline)
config (:config context)
state-component (:pipeline-state-component context)
pipeline-representation (flatten-pipeline (lp/pipeline-display-representation def))]
(map (partial project-for state config fallback-base-url) pipeline-representation)))
(map (partial project-for context config fallback-base-url) pipeline-representation)))

(defn cctray-xml-for
([pipeline]
Expand Down
13 changes: 10 additions & 3 deletions test/lambdacd_cctray/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[clj-cctray.core :as parser]
[clj-time.core :as t]
[lambdacd.steps.control-flow :as control-flow]
[lambdacd.internal.pipeline-state :as pipeline-state]
[lambdacd.state.protocols :as protocols]
[lambdacd-cctray.core :refer :all]))

(defn some-name [& _])
Expand Down Expand Up @@ -34,9 +34,16 @@
:first-updated-at (t/date-time 2015 1 2 3 40 2)
:most-recent-update-at (t/date-time 2015 1 2 3 40 2)}}})

(defrecord MockStateComponent [state]
protocols/QueryAllBuildNumbersSource
(all-build-numbers [self]
(keys state))
protocols/QueryStepResultsSource
(get-step-results [self build-number]
(get state build-number))) ; TODO: also implement pipeline structure

(defn mock-state-component [state]
(reify pipeline-state/PipelineStateComponent
(get-all [self] state)))
(->MockStateComponent state))

(defn mock-pipeline [state config]
{:pipeline-def pipeline-def
Expand Down

0 comments on commit 2ab67a3

Please sign in to comment.