(cloud) big performance improvements for page load.

This commit is contained in:
2023-04-06 14:38:22 -07:00
parent 2a0f736af7
commit 5561861d3d
10 changed files with 349 additions and 272 deletions

View File

@@ -1,10 +1,46 @@
(ns auto-ap.datomic.clients
(:require
[auto-ap.datomic :refer [conn]]
[auto-ap.datomic :refer [add-sorter-fields
apply-pagination
apply-sort-3
conn
merge-query
pull-many]]
[auto-ap.graphql.utils :refer [can-see-client? limited-clients]]
[auto-ap.search :as search]
[clj-time.coerce :as coerce]
[clojure.string :as str]
[datomic.client.api :as dc]))
[datomic.client.api :as dc]
[com.brunobonacci.mulog :as mu]))
(def full-read '[*
{:client/square-integration-status [:integration-status/message
:integration-status/last-attempt
:integration-status/last-updated
:db/id
{:integration-status/state [:db/ident]}]}
{:client/address [*]}
{:client/square-locations [:square-location/square-id
:square-location/name
:square-location/client-location
:db/id]}
{:client/ezcater-locations [{:ezcater-location/caterer [:ezcater-caterer/name :db/id]}
:ezcater-location/location
:db/id]}
{:client/bank-accounts [* {:bank-account/type [*]
:bank-account/yodlee-account [:yodlee-account/name :yodlee-account/id :yodlee-account/number]
:bank-account/plaid-account [:plaid-account/name :db/id :plaid-account/number :plaid-account/balance]
:bank-account/intuit-bank-account [:intuit-bank-account/name :intuit-bank-account/external-id :db/id]
:bank-account/integration-status [:integration-status/message
:db/id
:integration-status/last-attempt
:integration-status/last-updated
{:integration-status/state [:db/ident]}]}
]}
{:yodlee-provider-account/_client [*]}
{:plaid-item/_client [*]}
{:client/emails [:db/id :email-contact/email :email-contact/description]}])
(defn cleanse [e]
(-> e
@@ -29,52 +65,25 @@
(update :bank-account/sort-order (fn [so] (or so i)))))
(range) bas)))))
(defn get-all []
(->> (dc/q '[:find (pull ?e [*
{:client/square-integration-status [:integration-status/message
:integration-status/last-attempt
:integration-status/last-updated
:db/id
{:integration-status/state [:db/ident]}]}
{:client/address [*]}
{:client/square-locations [:square-location/square-id
:square-location/name
:square-location/client-location
:db/id]}
{:client/ezcater-locations [{:ezcater-location/caterer [:ezcater-caterer/name :db/id]}
:ezcater-location/location
:db/id]}
{:client/bank-accounts [* {:bank-account/type [*]
:bank-account/yodlee-account [:yodlee-account/name :yodlee-account/id :yodlee-account/number]
:bank-account/plaid-account [:plaid-account/name :db/id :plaid-account/number :plaid-account/balance]
:bank-account/intuit-bank-account [:intuit-bank-account/name :intuit-bank-account/external-id :db/id]
:bank-account/integration-status [:integration-status/message
:db/id
:integration-status/last-attempt
:integration-status/last-updated
{:integration-status/state [:db/ident]}]}
]}
{:yodlee-provider-account/_client [*]}
{:plaid-item/_client [*]}
{:client/emails [:db/id :email-contact/email :email-contact/description]}])
(->> (dc/q '[:find (pull ?e ?r)
:in $ ?r
:where [?e :client/name]]
(dc/db conn))
(dc/db conn)
full-read)
(map first)
(map cleanse)))
(defn get-minimal []
(->> (dc/q '[:find (pull ?e [:client/name :client/code :client/locations :db/id {:client/bank-accounts [{:bank-account/type [:db/ident]} :bank-account/name :bank-account/code :db/id]}])
:where [?e :client/name]]
(dc/db conn))
(map first)
(map cleanse)))
(defn get-by-id [id]
(->>
(dc/pull (dc/db conn )
'[* {:client/bank-accounts [* {:bank-account/type [*]
:bank-account/yodlee-account [:yodlee-account/name :yodlee-account/id :yodlee-account/number]
:bank-account/intuit-bank-account [:intuit-bank-account/name :intuit-bank-account/external-id :db/id]
:bank-account/plaid-account [:plaid-account/name :db/id :plaid-account/number :plaid-account/balance]}]
:client/emails [:db/id :email-contact/email :email-contact/description]}
{:client/ezcater-locations [{:ezcater-location/caterer [:ezcater-caterer/name :db/id]}
:ezcater-location/location
:db/id]}
{:yodlee-provider-account/_client [*]}
{:plaid-item/_client [*]}]
full-read
id)
(cleanse)))
@@ -105,3 +114,54 @@
:text match
:exact-match (str/upper-case match)})
"client"))
(defn raw-graphql-ids [db args]
(let [query (cond-> {:query {:find []
:in ['$ ]
:where []}
:args [db]}
(not (str/blank? (:code args)))
(merge-query {:query {:in ['?client-code]
:where ['[?e :client/code ?client-code]]}
:args [(:code args)]})
(not (str/blank? (:name-like args)))
(merge-query {:query {:in ['?name-like]
:where ['[?e :client/name ?name]
'[(clojure.string/includes? ?name ?name-like)]]}
:args [(:name-like args)]})
(limited-clients (:id args))
(merge-query {:query {:in ['[?e ...]]
:where []}
:args [(set (map :db/id (limited-clients (:id args))))]})
(:sort args) (add-sorter-fields {"name" ['[?e :client/name ?sort-name]]}
args)
true
(merge-query {:query {:find ['?sort-default '?e] :where ['[?e :client/name ?sort-default]]}}))]
(mu/log ::q
:query query)
(->> query
(dc/q)
(apply-sort-3 (update args :sort conj {:sort-key "default-2" :asc true}))
(apply-pagination args))))
(defn graphql-results [ids db args]
(let [results (->> (pull-many db full-read
ids)
(map cleanse))]
results))
(defn get-graphql-page [args]
(let [db (dc/db conn)
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]
[(->> (graphql-results ids-to-retrieve db args))
matching-count]))

View File

@@ -9,11 +9,10 @@
pull-many
visible-clients
conn]]
[auto-ap.graphql.utils :refer [limited-clients]]
[iol-ion.query]
[clj-time.coerce :as c]
[clj-time.core :as time]
[clojure.set :as set]
[clojure.tools.logging :as log]
[datomic.client.api :as dc]
[com.brunobonacci.mulog :as mu]
))

View File

@@ -699,15 +699,14 @@
(- (+ total credit)
debit))
0.0
(dc/q {:query {:find '[?debit ?credit]
:in '[$ ?client]
:where ['[?j :journal-entry/client ?client]
'[?j :journal-entry/line-items ?je]
'[?je :journal-entry-line/account ?ba]
'[?ba :bank-account/type :bank-account-type/check]
'[(get-else $ ?je :journal-entry-line/debit 0.0) ?debit]
'[(get-else $ ?je :journal-entry-line/credit 0.0) ?credit]]}
:args [(dc/db conn) client_id]}))
(dc/q {:query {:find '[?debit ?credit]
:in '[$ ?client]
:where ['[?client :client/bank-accounts ?ba]
'[?ba :bank-account/type :bank-account-type/check]
'[?je :journal-entry-line/account ?ba]
'[(get-else $ ?je :journal-entry-line/debit 0.0) ?debit]
'[(get-else $ ?je :journal-entry-line/credit 0.0) ?credit]]}
:args [(dc/db conn) client_id]}))
bills-due-soon (dc/q {:query {:find '[?due ?outstanding ?invoice-number ?vendor-id ?vendor-name]
:in '[$ ?client ?due-before]
:where ['[?i :invoice/client ?client]
@@ -734,8 +733,8 @@
recent-fulfillments (dc/q {:query {:find '[?f ?d]
:in '[$ ?client ?min-date]
:where ['[?t :transaction/forecast-match ?f]
'[?t :transaction/date ?d]
'[?t :transaction/client ?client]
'[?t :transaction/date ?d]
'[(>= ?d ?min-date)]]}
:args [(dc/db conn) client_id (coerce/to-date (t/plus (time/local-now) (t/months -2)))]})
forecasted-transactions (for [{:forecasted-transaction/keys [amount identifier day-of-month]

View File

@@ -3,12 +3,13 @@
[amazonica.aws.s3 :as s3]
[auto-ap.datomic :refer [audit-transact conn]]
[auto-ap.datomic.clients :as d-clients]
[iol-ion.tx :refer [upsert-entity]]
[auto-ap.graphql.utils
:refer [->graphql
assert-admin
attach-tracing-resolvers
can-see-client?
<-graphql
result->page
is-admin?]]
[auto-ap.routes.queries :as q]
[auto-ap.square.core :as square]
@@ -18,12 +19,12 @@
[clojure.set :as set]
[clojure.string :as str]
[clojure.tools.logging :as log]
[com.brunobonacci.mulog :as mu]
[datomic.client.api :as dc]
[unilog.context :as lc]
[iol-ion.tx :refer [random-tempid upsert-entity]]
[mount.core :as mount]
[yang.scheduler :as scheduler]
[iol-ion.tx :refer [random-tempid]]
[com.brunobonacci.mulog :as mu])
[unilog.context :as lc]
[yang.scheduler :as scheduler])
(:import
(java.util UUID)
(org.apache.commons.codec.binary Base64)))
@@ -200,23 +201,39 @@
(defn get-client [context _ _]
(->graphql
(->> (d-clients/get-all)
(filter #(can-see-client? (:id context) %))
(map (fn [c]
(update c :client/bank-accounts (fn [bas]
(map #(set/rename-keys % {:bank-account/use-date-instead-of-post-date? :use-date-instead-of-post-date}) bas)))))
(map (fn [c]
(if (is-admin? (:id context))
c
(-> c
(dissoc :client/yodlee-provider-accounts)
(dissoc :client/plaid-items)))))
(map (fn [c]
(update c :client/bank-accounts
(fn [bank-accounts]
(mapv (fn [ba]
(assoc ba :bank-account/yodlee-balance-old nil))
bank-accounts))))))))
(->> (d-clients/get-minimal)
(filter #(can-see-client? (:id context) %)))))
(defn get-admin-client [context {:keys [id]} _]
(assert-admin (:id context))
(->graphql
(-> (d-clients/get-by-id id)
(update :client/bank-accounts (fn [bas]
(map #(set/rename-keys % {:bank-account/use-date-instead-of-post-date? :use-date-instead-of-post-date}) bas))))))
(defn get-client-page [context args _]
(assert-admin (:id context))
(let [args (assoc args :id (:id context))
[clients clients-count] (d-clients/get-graphql-page (assoc (<-graphql (:filters args))
:id (:id context)))
clients (->> clients
(map (fn [c]
(update c :client/bank-accounts (fn [bas]
(map #(set/rename-keys % {:bank-account/use-date-instead-of-post-date? :use-date-instead-of-post-date}) bas)))))
(map (fn [c]
(if (is-admin? (:id context))
c
(-> c
(dissoc :client/yodlee-provider-accounts)
(dissoc :client/plaid-items)))))
(map (fn [c]
(update c :client/bank-accounts
(fn [bank-accounts]
(mapv (fn [ba]
(assoc ba :bank-account/yodlee-balance-old nil))
bank-accounts))))))]
(result->page clients clients-count :clients (:filters args))))
(def sales-summary-query
"[:find ?d4 (sum ?total) (sum ?tax) (sum ?tip) (sum ?service-charge) (sum ?discount) (sum ?returns)
@@ -437,6 +454,15 @@
:yodlee_provider_accounts {:type '(list :yodlee_provider_account)}
:plaid_items {:type '(list :plaid_item)}}}
:client_page
{:fields {:clients {:type '(list :client)}
:count {:type 'Int}
:total {:type 'Int}
:start {:type 'Int}
:end {:type 'Int}}}
:bank_account
{:fields {:id {:type :id}
:integration_status {:type :integration_status}
@@ -471,7 +497,14 @@
(def queries
{:client {:type '(list :client)
:resolve :get-client}})
:resolve :get-client}
:admin_client {:type :client
:args {:id {:type :id}}
:resolve :get-admin-client}
:client_page {:type :client_page
:args {:filters {:type :client_filters}}
:resolve :get-client-page}})
(def mutations
{:edit_client {:type :client
@@ -486,6 +519,13 @@
:match {:type 'String}
:id {:type :id}}}
:client_filters
{:fields {:code {:type 'String}
:name_like {:type 'String}
:start {:type 'Int}
:per_page {:type 'Int}
:sort {:type '(list :sort_item)}}}
:edit_square_location {:fields {:client_location {:type 'String}
:id {:type :id}}}
@@ -551,6 +591,8 @@
(def resolvers
{:get-client get-client
:get-admin-client get-admin-client
:get-client-page get-client-page
:mutation/edit-client edit-client
:mutation/setup-sales-queries setup-sales-queries})