made it far less busy to update running cache.

This commit is contained in:
Bryce Covert
2020-12-20 09:41:06 -08:00
parent 31e8b1e9d5
commit 8fe7f12b21
2 changed files with 27 additions and 7 deletions

View File

@@ -435,13 +435,32 @@
(sort-by (juxt first second))
(build-running-balance lookup-account))))
(def last-run-running-balance (atom nil))
(defn build-running-balance-cache []
(reduce
(fn [acc client]
(log/info "Computing running balance cache for " (:client/code client))
(assoc acc (:db/id client) (running-balance-for (:db/id client))))
{}
(d-clients/get-all)))
(let [clients-needing-refresh (if-let [last-run @last-run-running-balance]
(->> (d/query
{:query {:find ['?v]
:in ['$ '?log '?since '?till]
:where ['[(tx-ids ?log ?since ?till) [?tx ...]]
'[$ _ :journal-entry/client ?v ?tx]]}
:args [(d/history (d/db conn))
(d/log conn)
last-run
(java.util.Date.)]})
(map first)
(into #{}))
(into #{} (map :db/id (d-clients/get-all))))
starting (java.util.Date.)]
(log/info (count clients-needing-refresh) "Clients need their balance cache refreshed.")
(reduce
(fn [acc client]
(log/info "Computing running balance cache for " (:client/code (d/entity (d/db conn) client)))
(assoc acc client (running-balance-for client)))
{}
clients-needing-refresh)
(log/info "Done refreshing " (count clients-needing-refresh) " client caches")
(reset! last-run-running-balance starting)))
@@ -455,6 +474,6 @@
(mount/defstate running-balance-cache-worker
:start (scheduler/every (* 30 60 1000) refresh-running-balance-cache)
:start (scheduler/every (* 15 60 1000) refresh-running-balance-cache)
:stop (scheduler/stop running-balance-cache-worker))

View File

@@ -280,6 +280,7 @@
(d/query {:query {:find ['?e '?lia]
:in ['$]
:where ['[?e :invoice/expense-accounts ?li]
'(not [?e :invoice/total 0.0])
'[?li :invoice-expense-account/account ?lia]
'[?lia :account/name]
'(not [?lia :account/numeric-code 21000])