tons of bug fixes.

This commit is contained in:
Bryce Covert
2020-07-15 08:17:39 -07:00
parent d120b7e810
commit 08f12b8107
9 changed files with 496 additions and 186 deletions

View File

@@ -0,0 +1,142 @@
(ns make-ledger-fast-again
(:require [datomic.api :as d]
[auto-ap.datomic :refer[uri]]
[auto-ap.utils :refer [by]]
[clj-time.coerce :as coerce]
))
(time
(def old-results
(map (fn [o] (update o :amount int)) (:balance_sheet_accounts (auto-ap.graphql.ledger/get-balance-sheet nil {:client_id 17592186046483
:date (coerce/to-date-time #inst "2020-09-01")} nil)))))
#_(defn roll-up-2 [jes]
)
(def new-results
(let [client-id 17592186046483
brackets [#inst "2019-01-01" #inst "2020-01-01"]
accounts (by :db/id (map first (d/query {:query {:find ['(pull ?e [:db/id :account/name
:account/numeric-code
{:account/type [:db/ident]
:account/client-overrides [:account-client-override/client :account-client-override/name]}
])]
:in ['$]
:where ['[?e :account/name]]}
:args [(d/db (d/connect uri) )]})))
bank-accounts (by :db/id (map first (d/query {:query {:find ['(pull ?e [:db/id :bank-account/name {:bank-account/type [:db/ident]}])]
:in ['$]
:where ['[?e :bank-account/name]]}
:args [(d/db (d/connect uri))]})))
overrides-by-client (->> accounts
vals
(mapcat (fn [a]
(map (fn [o]
[[[(:db/id a) (:db/id (:account-client-override/client o))]]
(:account-client-override/name o)])
(:account/client-overrides a))
) )
(into {} ))
account->name (fn [a]
(or (:bank-account/name (bank-accounts a))
(overrides-by-client [a client-id])
(:account/name (accounts a))))
account->numeric-code (fn [a]
(or (:account/numeric-code (accounts a))
(and (#{:bank-account-type/check} (:db/ident (:bank-account/type (bank-accounts a))))
11100)
(and (#{:bank-account-type/credit} (:db/ident (:bank-account/type (bank-accounts a))))
28000)))
account->type (fn [a]
(or (:db/ident (:account/type (accounts a)))
({:bank-account-type/check :account-type/asset
:bank-account-type/credit :account-type/liability}
(:db/ident (:bank-account/type (bank-accounts a))))))]
(->> (d/query
{:query {:find ['?d '?jel '?account '?location '?debit '?credit ]
:in ['$ '?client-id]
:where ['[?e :journal-entry/client ?client-id]
'[?e :journal-entry/date ?d]
'[(<= ?d #inst "2020-09-01")]
'[?e :journal-entry/line-items ?jel]
'[(get-else $ ?jel :journal-entry-line/account :account/unknown) ?account]
'[(get-else $ ?jel :journal-entry-line/debit 0.0) ?debit]
'[(get-else $ ?jel :journal-entry-line/credit 0.0) ?credit]
'[(get-else $ ?jel :journal-entry-line/location "") ?location]]
}
:args [(d/db (d/connect uri)) client-id]})
(sort-by first)
(reduce
(fn [acc [_ _ account location debit credit]]
(-> acc
(update-in [[location account] :debit] (fnil + 0.0) debit)
(update-in [[location account] :credit] (fnil + 0.0) credit)
(update-in [[location account] :count] (fnil + 0) 1))
)
{})
(reduce-kv
(fn [acc [location account] {:keys [debit credit count]}]
(let [account-type (account->type account)]
(conj acc {:name (if-not (= "A" location)
(str (account->name account) "-" location)
(account->name account))
:id (str account "-" location)
:numeric_code (account->numeric-code account)
:location (or location "")
:amount (if account-type (if (#{:account-type/asset
:account-type/dividend
:account-type/expense} account-type)
(- debit credit)
(- credit debit)
)
0.0)
:account_type account-type}))
)
[])
#_(map (juxt :name :count (comp int :amount)))
#_(sort-by first)
))
)
;; 9804
new-results
old-results
(for [[k1 a1] (by :id (map #(dissoc % :location :account_type ) (filter :numeric_code new-results)))
:let [a2 (get (by :id (map #(dissoc % :location :account_type ) (filter :numeric_code old-results)))
k1)]
:when (not= a1 a2)]
[a1 a2]
)
(clojure.data/diff (set new-results) (set old-results))
;;16275
#_(d/query
{:query {:find ['?account '?location '?debit '?credit ]
:in ['$]
:where ['[?e :journal-entry/client 17592186046483]
'[?e :journal-entry/date ?d]
'[(<= ?d #inst "2020-09-01")]
'[?e :journal-entry/line-items ?jel]
'[(get-else $ ?jel :journal-entry-line/account :account/unknown) ?account]
'[(get-else $ ?jel :journal-entry-line/debit 0.0) ?debit]
'[(get-else $ ?jel :journal-entry-line/credit 0.0) ?credit]
'[(get-else $ ?jel :journal-entry-line/location "") ?location]]
}
:args [(d/db (d/connect uri))]})

View File

@@ -0,0 +1,44 @@
(ns remove-old-yodlee)
(def account->provider-account
(->> @in-memory-cache
(reduce
(fn [acc {:keys [id accounts]}]
(reduce
(fn [acc a]
(assoc acc (:id a) id))
acc
accounts)
)
{})))
(def used-provider-accounts
(->> (datomic.api/query {:query {:find ['?y]
:in ['$]
:where ['[_ :bank-account/yodlee-account-id ?y]]}
:args [(datomic.api/db (datomic.api/connect auto-ap.datomic/uri))]})
(map first)
(map account->provider-account)
set
))
(def all-provider-accounts (set (map :id @in-memory-cache)))
(clojure.set/difference all-provider-accounts used-provider-accounts)
(->> [17891145 14187174 14264733 14274237 16681522 18612051 17796307 18643735
14187073 17754737 16153006 14565664 15941583 18621649 14187279 14187177
18652162 17892740 14187420 15613426 14187503 16604611 18611904 14180564
16153014 15942027 17574983 14186984 14180087 18651698 18645515 18472838
15735320]
(map (by :id @in-memory-cache))
(mapcat :accounts)
(mapcat :dataset)
(map :additionalStatus)
#_(map :lastUpdated)
)