-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewriting_history.clj
189 lines (162 loc) · 6.99 KB
/
rewriting_history.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
(ns no.nsd.rewriting-history
(:require [no.nsd.rewriting-history.impl :as impl]
[no.nsd.rewriting-history.replay-impl :as replay]
[no.nsd.rewriting-history.schedule-impl :as schedule]
[no.nsd.rewriting-history.patch :as patch]
[no.nsd.rewriting-history.wipe :as wipe]
[no.nsd.rewriting-history.rollback :as rollback]
[no.nsd.rewriting-history.dbfns.schema :as fns]
[clojure.string :as str]
[clojure.tools.logging :as log]
[datomic.api :as d]
[clojure.pprint :as pprint])
(:import (java.util Date)))
; public API
(def schema impl/schema)
(declare pull-flat-history)
(declare pending-replacements)
(defn init! [conn]
@(d/transact conn schema)
(doseq [f fns/fns]
(let [db-fn (fns/read-dbfn @f)
db-ident (:db/ident db-fn)]
(when-let [e (d/q '[:find ?e .
:in $ ?db-ident
:where
[?e :db/ident ?db-ident]
[?e :db/fn ?f]]
(d/db conn)
db-ident)]
@(d/transact conn [[:db/retractEntity e]])
@(d/transact conn [db-fn])))))
(defn schedule-replacement!
"Schedule a replacement of ^String match with ^String replacement
for lookup-ref.
Creates or updates a rewrite-job."
[conn lookup-ref match replacement]
(assert (string? match))
(assert (string? replacement))
(assert (some? (impl/resolve-lookup-ref conn lookup-ref))
(str "Expected to find lookup-ref " lookup-ref))
(let [curr-history (pull-flat-history conn lookup-ref)
found-match-in-history? (->> curr-history
(map (fn [[e a v t o]] v))
(filter string?)
(into #{})
(reduce (fn [_ v]
(when (str/includes? v match)
(reduced true)))
false))]
(cond (and (empty? match)
(not-empty replacement))
(do (log/warn "empty match given and non-empty replacement given, ignoring")
(pending-replacements conn lookup-ref))
found-match-in-history? (schedule/schedule-replacement! conn lookup-ref match replacement)
:else (pending-replacements conn lookup-ref))))
(defn schedule-patch!
[conn lookup-ref tag org-history new-history]
(patch/schedule-patch! conn lookup-ref tag org-history new-history))
(defn cancel-patch!
[conn lookup-ref tag org-history new-history]
(patch/cancel-patch! conn lookup-ref org-history new-history))
(defn cancel-patch-tag!
[conn tag]
(patch/cancel-patch-tag! conn tag))
(defn all-pending-patches [conn]
(patch/all-pending-patches conn))
(defn cancel-replacement! [conn lookup-ref match replacement]
"Cancel a scheduled replacement for lookup-ref. Updates an existing rewrite-job."
(schedule/cancel-replacement! conn lookup-ref match replacement))
(defn pending-replacements [conn lookup-ref]
"Get pending replacements for lookup-ref."
(schedule/pending-replacements conn lookup-ref))
(defn all-pending-replacements [conn]
"Get all pending replacements in the form of
[{:attr attr
:ref ref
:match ...
:replacement ...}
...]"
(schedule/all-pending-replacements conn))
(defn rewrite-scheduled! [conn]
"Process all rewrite-jobs that are scheduled."
(replay/process-state! conn :scheduled))
(defn excise-old-rewrite-jobs! [conn older-than-days]
"Excise all rewrite-job metadata that are older than older-than-days days."
(wipe/wipe-old-rewrite-jobs! conn (Date.) older-than-days))
(defn available-rollback-times [conn lookup-ref]
"Get available rollback times for a lookup ref. Returns a set of java.util.Dates."
(rollback/available-times conn lookup-ref))
(defn rollback! [conn lookup-ref ^Date t]
"Rollback lookup-ref to a previous ^java.util.Date t."
(rollback/rollback! conn lookup-ref t))
(defn assoc-lookup-ref [eavtos [attr idval] & kvs]
(assert (vector? eavtos))
(let [new-vals (into {} (mapv vec (partition 2 kvs)))]
(if-let [eid (->> eavtos
(filter (fn [[_e a v _t o]]
(and (= a attr)
(= v idval)
o)))
(ffirst))]
(let [[_e _a _v start-tx _] (->> eavtos
(filter (fn [[e a v t o]]
(and (= a attr)
(= v idval)
o)))
(first))
old-eavtos (->> eavtos
(filter (fn [[e a _v t _o]]
(and (<= t start-tx)
(= e eid)
(contains? (into #{} (keys new-vals)) a))))
(group-by second)
(vals)
(mapcat (fn [v]
(let [[_e _a _v _t o] (first v)]
(assert (true? o) "Expected first tx to be assert"))
(take 2 v)))
(vec))
new-eavtos (->> old-eavtos
(mapv (fn [[e a _v t o]]
[e a (get new-vals a) t o])))
change (zipmap old-eavtos new-eavtos)]
(->> eavtos
(mapv (fn [eavto] (get change eavto eavto)))))
eavtos)))
; convenience methods
(defn pull-flat-history
"Returns a vector of all the EAVTOs that are reachable from lookup-ref,
i.e. the full history of lookup-ref, with normalized values for E and T."
[db lookup-ref]
(impl/pull-flat-history-simple db lookup-ref))
(defn get-org-history [db lookup-ref]
"Get the original flat history of lookup-ref as it was before rewriting started."
(impl/get-org-history db lookup-ref))
(defn get-new-history [db lookup-ref]
"Get the expected flat history of lookup-ref before rewriting started."
(impl/get-new-history db lookup-ref))
(defn get-job-state [conn lookup-ref]
(impl/job-state conn lookup-ref))
(defn get-state-count [conn]
(let [db (impl/to-db conn)]
(->> (d/q '[:find ?state ?e
:in $
:where
[?e :rh/lookup-ref ?lref]
[?e :rh/state ?state]]
db)
(map (fn [[state _]] {state 1}))
(reduce (partial merge-with +) (sorted-map)))))
(defn healthy? [db]
(if-let [cnt (d/q '[:find (count ?e) .
:in $
:where
[?e :rh/state :error]]
db)]
(if (pos-int? cnt)
false
true)
true))
(defn unhealthy? [db]
(false? (healthy? db)))