From 8fe7f12b21a3f895ecd4c13fe292850c84e695a8 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Sun, 20 Dec 2020 09:41:06 -0800 Subject: [PATCH] made it far less busy to update running cache. --- src/clj/auto_ap/graphql/ledger.clj | 33 +++++++++++++++++++++++------- src/clj/auto_ap/ledger.clj | 1 + 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/clj/auto_ap/graphql/ledger.clj b/src/clj/auto_ap/graphql/ledger.clj index 0f08684a..c9ea15e3 100644 --- a/src/clj/auto_ap/graphql/ledger.clj +++ b/src/clj/auto_ap/graphql/ledger.clj @@ -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)) diff --git a/src/clj/auto_ap/ledger.clj b/src/clj/auto_ap/ledger.clj index 509ef9d2..bf598efc 100644 --- a/src/clj/auto_ap/ledger.clj +++ b/src/clj/auto_ap/ledger.clj @@ -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])