Skip to content

Commit 39913e4

Browse files
committed
simplify, just be a small web api; build on top of this with a pretty page
1 parent 97d62da commit 39913e4

File tree

3 files changed

+27
-55
lines changed

3 files changed

+27
-55
lines changed

project.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
:url "https://gitub.com/derwolfe/firedamp"
44
:license {:name "Eclipse Public License"
55
:url "http://www.eclipse.org/legal/epl-v10.html"}
6-
:dependencies [[org.clojure/clojure "1.8.0"]
6+
:dependencies [;; backend
7+
[org.clojure/clojure "1.8.0"]
78
[org.clojure/core.match "0.3.0-alpha4"]
89
[environ "1.0.3"]
910
[com.taoensso/timbre "4.5.1"]

src/firedamp/core.clj

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
[compojure.route :as cjr]
88
[clj-time.core :as time]
99
[clj-time.coerce :as time-coerce]
10+
[clj-time.format :as time-format]
1011
[clojure.core.match :as cmatch]
1112
[clojure.core.reducers :as r]
1213
[environ.core :as env]
1314
[manifold.deferred :as md]
1415
[manifold.time :as mt]
1516
[taoensso.timbre :as timbre]
16-
[twitter.oauth :as tw-auth]
17-
[twitter.api.restful :as tw-api]
1817
[prometheus.core :as prometheus])
1918
(:import
2019
[java.util.concurrent TimeoutException])
@@ -32,6 +31,7 @@
3231
(def timeout-after (mt/seconds 10))
3332

3433
(def state (atom {:alarm-state ::good
34+
:statuses nil
3535
:last-update (time/now)}))
3636

3737
(def store (atom nil))
@@ -83,7 +83,7 @@
8383
:travis travis-status
8484
:github github-status})))
8585

86-
(defn red-alert?
86+
(defn down?
8787
[{:keys [github codecov travis]}]
8888
(if (and (= status-io-good codecov)
8989
(= status-io-good travis)
@@ -99,48 +99,15 @@
9999
[::bad ::bad] ::dark
100100
[::bad ::good] ::brightening))
101101

102-
(defn setup-twitter
103-
[env]
104-
(let [{:keys [api-key api-secret access-token access-token-secret]} env
105-
token (tw-auth/make-oauth-creds api-key
106-
api-secret
107-
access-token
108-
access-token-secret)]
109-
token))
110-
111-
(defn tweet!
112-
[message]
113-
(timbre/info "tweeting" message)
114-
(let [token (setup-twitter env/env)]
115-
(->
116-
(md/chain
117-
;; this should also be able to timeout
118-
(md/future (tw-api/statuses-update :oauth-creds token :params {:status message}))
119-
(fn [_] (timbre/info "tweeted")))
120-
(md/catch
121-
Exception
122-
(fn [exc] (timbre/warn "exception while tweeting:" exc))))))
123-
124-
(defn tweet-alert!
125-
[status]
126-
(timbre/info "tweet alert" status)
127-
(condp = status
128-
::darkening (tweet! "expect problems @chriswwolfe")
129-
::brightening (tweet! "should be back to normal @chriswwolfe")
130-
;; else
131-
(md/success-deferred ::no-tweet)))
132-
133102
(defn alert!
134103
[ctx statuses]
135104
(let [{s0 :alarm-state} ctx
136-
s1 (red-alert? statuses)
137-
tweet-status (get-next-state s0 s1)]
138-
(md/chain
139-
(tweet-alert! tweet-status)
140-
(fn [arg]
141-
(-> ctx
142-
(assoc :alarm-state s1)
143-
(assoc :last-update (time/now)))))))
105+
s1 (down? statuses)
106+
status (get-next-state s0 s1)]
107+
(-> ctx
108+
(assoc :alarm-state s1)
109+
(assoc :statuses statuses)
110+
(assoc :last-update (time/now)))))
144111

145112
(defn run-world!
146113
[]
@@ -149,6 +116,7 @@
149116
(get-parse-statuses!)
150117
#(alert! @state %)
151118
(fn [new-world]
119+
(timbre/info (:statuses new-world))
152120
(reset! state new-world)
153121
(timbre/infof "s0=%s, s1=%s" (:alarm-state old-state) (:alarm-state @state))))))
154122

@@ -179,13 +147,28 @@
179147
(register-metrics)
180148
(reset! store)))
181149

150+
(def built-in-formatter (time-format/formatters :basic-date-time))
151+
152+
(defn status-handler
153+
[_req]
154+
(let [{:keys [statuses last-update alarm-state]} @state
155+
body {:statuses statuses
156+
:alarm-state alarm-state
157+
:last-update (time-coerce/to-date last-update)}
158+
as-json (json/generate-string body {:pretty true})]
159+
{:status 200
160+
:headers {"content-type" "text/plain"}
161+
:body as-json}))
162+
182163
(cjc/defroutes app
164+
(cjc/GET "/" [] status-handler)
183165
(cjc/GET "/metrics" [] metrics-handler)
184166
(cjr/not-found "404: Not Found"))
185167

186168
(defn -main
187169
[& args]
188170
(let [port (Integer/parseInt (first args))]
171+
(prn port)
189172
(init-metrics!)
190173
(http/start-server app {:port port :host "0.0.0.0"})
191174
(keep-checking (mt/minutes 2))

test/firedamp/core_test.clj

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,6 @@
118118
@result))
119119
(is (= 0 (count @calls)))))))))
120120

121-
(deftest tweet-alert!
122-
(let [toot (atom {})
123-
fake-tweet (fn [message]
124-
(reset! toot {:message message}))]
125-
(with-redefs [firedamp.core/tweet! fake-tweet]
126-
(testing "tweets when problem"
127-
(core/tweet-alert! :firedamp.core/darkening)
128-
(is (= "expect problems @chriswwolfe" (:message @toot))))
129-
(testing "tweets when repaired"
130-
(core/tweet-alert! :firedamp.core/brightening)
131-
(is (= "should be back to normal @chriswwolfe" (:message @toot)))))))
132-
133121
;; (deftest alert-tests
134122
;; (testing "alerts "
135123
;; (let [fake-tweet (fn [msg token] (md/success-deferred ::done))]

0 commit comments

Comments
 (0)