-
Notifications
You must be signed in to change notification settings - Fork 2
/
results.clj
85 lines (71 loc) · 2.71 KB
/
results.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(ns salutem.core.results
"Provides constructors and predicates for check results."
(:require
[tick.core :as t]))
(defn result
"Constructs a result with the provided `status`.
The optional map of extra data is stored with the result for future use.
Unless overridden in the extra data map, an `:salutem/evaluated-at` field is
added to the result, set to the current date time in the system default time
zone."
([status] (result status {}))
([status {:keys [salutem/evaluated-at]
:or {evaluated-at (t/now)}
:as extra-data}]
(merge extra-data
{:salutem/status status
:salutem/evaluated-at evaluated-at})))
(defn healthy
"Constructs a healthy result.
The optional map of extra data is stored with the result for future use.
Unless overridden in the extra data map, an `:salutem/evaluated-at` field is
added to the result, set to the current date time in the system default time
zone."
([] (healthy {}))
([extra-data]
(result :healthy extra-data)))
(defn unhealthy
"Constructs an unhealthy result.
The optional map of extra data is stored with the result for future use.
Unless overridden in the extra data map, an `:salutem/evaluated-at` field is
added to the result, set to the current date time in the system default time
zone."
([] (unhealthy {}))
([extra-date]
(result :unhealthy extra-date)))
(defn prepend
"Adds each entry from the provided map of extra data to the result if no entry
already exists in the result for the key."
[result extra-data]
(merge extra-data result))
(defn status
"Returns the status of the provided result."
[result]
(:salutem/status result))
(defn healthy?
"Returns `true` if the result has a `:healthy` status, `false`
otherwise."
[result]
(= (status result) :healthy))
(defn unhealthy?
"Returns `true` if the result has an `:unhealthy` status, `false`
otherwise."
[result]
(= (status result) :unhealthy))
(defn outdated?
"Returns `true` if the result of the check is outdated, `false`
otherwise.
For a realtime check, a result is always considered outdated.
For a background check, a result is considered outdated if the
time to re-evaluation of the check has passed, i.e., if its evaluation date
time is before the current date time minus the check's time to re-evaluation.
If `relative-to` is provided, the calculation is performed relative to that
date time rather than to the current date time."
([result check]
(outdated? result check (t/now)))
([result check relative-to]
(or
(= (:salutem/type check) :realtime)
(nil? result)
(t/< (:salutem/evaluated-at result)
(t/<< relative-to (:salutem/time-to-re-evaluation check))))))