much more background process tracking

This commit is contained in:
2022-06-22 10:43:37 -07:00
parent 480066b202
commit 1db8d7a52c
15 changed files with 379 additions and 486 deletions

View File

@@ -1,37 +1,32 @@
(ns auto-ap.background.invoices (ns auto-ap.background.invoices
(:require [auto-ap.datomic.invoices :as d-invoices] (:require
[auto-ap.datomic :refer [uri conn]] [auto-ap.datomic :refer [conn]]
[datomic.api :as d] [auto-ap.time :as time]
[auto-ap.time :as time] [auto-ap.utils :refer [heartbeat]]
[clj-time.coerce :as coerce] [clj-time.coerce :as coerce]
[mount.core :as mount] [clojure.tools.logging :as log]
[yang.scheduler :as scheduler] [datomic.api :as d]
[unilog.context :as lc] [mount.core :as mount]
[clojure.tools.logging :as log])) [yang.scheduler :as scheduler]))
(defn close-auto-invoices [] (defn close-auto-invoices []
(lc/with-context {:source "close-auto-invoices"} (let [invoices-to-close (d/query {:query {:find ['?e]
(try :in ['$ '?today]
:where ['[?e :invoice/scheduled-payment ?d]
(let [invoices-to-close (d/query {:query {:find ['?e] '[?e :invoice/status :invoice-status/unpaid]
:in ['$ '?today] '[(<= ?d ?today)]]}
:where ['[?e :invoice/scheduled-payment ?d] :args [(d/db conn) (coerce/to-date (time/local-now))]})]
'[?e :invoice/status :invoice-status/unpaid] (log/info "Closing " (count invoices-to-close) "scheduled invoices")
'[(<= ?d ?today)]]} (some->> invoices-to-close
:args [(d/db conn) (coerce/to-date (time/local-now))]})] seq
(log/info "Closing " (count invoices-to-close) "scheduled invoices")
(some->> invoices-to-close
seq
(mapv (fn [[i]] {:db/id i (mapv (fn [[i]] {:db/id i
:invoice/outstanding-balance 0.0 :invoice/outstanding-balance 0.0
:invoice/status :invoice-status/paid})) :invoice/status :invoice-status/paid}))
(d/transact conn) (d/transact conn)
deref) deref)
(log/info "Closed " (count invoices-to-close) "scheduled invoices")) (log/info "Closed " (count invoices-to-close) "scheduled invoices")))
(catch Exception e
(log/error e)))))
(mount/defstate close-auto-invoices-worker (mount/defstate close-auto-invoices-worker
:start (scheduler/every 60000 close-auto-invoices) :start (scheduler/every 60000 (heartbeat close-auto-invoices "close-auto-invoices"))
:stop (scheduler/stop close-auto-invoices-worker)) :stop (scheduler/stop close-auto-invoices-worker))

View File

@@ -1,22 +1,24 @@
(ns auto-ap.background.mail (ns auto-ap.background.mail
(:require [amazonica.aws.s3 :as s3] (:require
[amazonica.aws.sqs :as sqs] [amazonica.aws.s3 :as s3]
[amazonica.aws.simpleemail :as ses] [amazonica.aws.simpleemail :as ses]
[auto-ap.parse :as parse] [amazonica.aws.sqs :as sqs]
[auto-ap.routes.invoices :as invoices] [auto-ap.parse :as parse]
[clojure-mail.message :as message] [auto-ap.routes.invoices :as invoices]
[clojure.data.json :as json] [auto-ap.utils :refer [heartbeat]]
[clojure.java.io :as io] [clojure-mail.message :as message]
[clojure.string :as str] [clojure.data.json :as json]
[config.core :refer [env]] [clojure.java.io :as io]
[clojure.tools.logging :as log] [clojure.string :as str]
[unilog.context :as lc] [clojure.tools.logging :as log]
[mount.core :as mount] [config.core :refer [env]]
[yang.scheduler :as scheduler]) [mount.core :as mount]
(:import (java.util Properties UUID) [unilog.context :as lc]
(javax.mail Session) [yang.scheduler :as scheduler])
(javax.mail.internet MimeMessage))) (:import
(java.util Properties UUID)
(javax.mail Session)
(javax.mail.internet MimeMessage)))
(defn send-email-about-failed-message [mail-bucket mail-key] (defn send-email-about-failed-message [mail-bucket mail-key]
(let [target-key (str "failed-emails/" mail-key ".eml") (let [target-key (str "failed-emails/" mail-key ".eml")
@@ -33,54 +35,49 @@
(defn process-sqs [] (defn process-sqs []
(lc/with-context {:source "import-uploaded-invoices"} (log/info "Fetching messages from sqs...")
(doseq [message (:messages (sqs/receive-message {:queue-url (:invoice-import-queue-url env)
(try :wait-time-seconds 5
(log/info "Fetching messages from sqs...") :max-number-of-messages 10
(doseq [message (:messages (sqs/receive-message {:queue-url (:invoice-import-queue-url env) #_#_:attribute-names ["All"]}))]
:wait-time-seconds 5 (let [message-body (json/read-str (:body message)
:max-number-of-messages 10 :key-fn keyword)]
#_#_:attribute-names ["All"]}))] (doseq [r (:Records message-body)]
(let [message-body (json/read-str (:body message) (log/info "Processing record " r)
:key-fn keyword)] (let [props (Session/getDefaultInstance (Properties.))
(doseq [r (:Records message-body)] message-stream (-> (s3/get-object {:key (-> r :s3 :object :key)
(log/info "Processing record " r) :bucket-name (-> r :s3 :bucket :name)})
(let [props (Session/getDefaultInstance (Properties.)) :input-stream)
message-stream (-> (s3/get-object {:key (-> r :s3 :object :key) mail (message/read-message (MimeMessage. props message-stream))]
:bucket-name (-> r :s3 :bucket :name)}) (log/info "reading mail" (->> mail :body (filter :content-type) (map :body) ))
:input-stream) (doseq [pdf-stream (->> (-> mail :body)
mail (message/read-message (MimeMessage. props message-stream))] (filter :content-type)
(log/info "reading mail" (->> mail :body (filter :content-type) (map :body) )) #_(filter #(re-find #"application/pdf" (:content-type %)) ))
(doseq [pdf-stream (->> (-> mail :body) :let [filename (str "/tmp/" (UUID/randomUUID) ".pdf")]]
(filter :content-type) (try
#_(filter #(re-find #"application/pdf" (:content-type %)) )) (let [
:let [filename (str "/tmp/" (UUID/randomUUID) ".pdf")]] _ (io/copy (:body pdf-stream) (io/file filename))
(try extension (last (str/split (.getName (io/file filename)) #"\."))
(let [ s3-location (str "invoice-files/" (str (UUID/randomUUID)) "." extension)
_ (io/copy (:body pdf-stream) (io/file filename)) _ (s3/put-object :bucket-name (:data-bucket env)
extension (last (str/split (.getName (io/file filename)) #"\.")) :key s3-location
s3-location (str "invoice-files/" (str (UUID/randomUUID)) "." extension) :input-stream (io/input-stream filename)
_ (s3/put-object :bucket-name (:data-bucket env) :metadata {:content-type "application/pdf"})
:key s3-location imports (->> (parse/parse-file filename filename)
:input-stream (io/input-stream filename) (map #(assoc %
:metadata {:content-type "application/pdf"}) :source-url (str "http://" (:data-bucket env)
imports (->> (parse/parse-file filename filename) ".s3-website-us-east-1.amazonaws.com/"
(map #(assoc % s3-location)
:source-url (str "http://" (:data-bucket env) :import-status :import-status/approved)))]
".s3-website-us-east-1.amazonaws.com/" (log/info "Found imports" imports)
s3-location) (invoices/import-uploaded-invoice {:user/role "admin"} imports ))
:import-status :import-status/approved)))] (catch Exception e
(log/info "Found imports" imports) (log/warn e)
(invoices/import-uploaded-invoice {:user/role "admin"} imports )) (send-email-about-failed-message (-> r :s3 :bucket :name) (-> r :s3 :object :key)))
(catch Exception e (finally
(log/warn e) (io/delete-file filename)))))))
(send-email-about-failed-message (-> r :s3 :bucket :name) (-> r :s3 :object :key))) (sqs/delete-message (assoc message :queue-url (:invoice-import-queue-url env) ))))
(finally
(io/delete-file filename)))))))
(sqs/delete-message (assoc message :queue-url (:invoice-import-queue-url env) )))
(catch Exception e
(log/error e)))))
(mount/defstate import-invoices (mount/defstate import-invoices
:start (scheduler/every (* 60 5000) process-sqs) :start (scheduler/every (* 60 5000) (heartbeat process-sqs "import-uploaded-invoices"))
:stop (scheduler/stop import-invoices)) :stop (scheduler/stop import-invoices))

View File

@@ -1,62 +1,44 @@
(ns auto-ap.background.requests (ns auto-ap.background.requests
(:require (:require
[amazonica.aws.sqs :as sqs] [amazonica.aws.sqs :as sqs]
[config.core :refer [env]]
[mount.core :as mount]
[yang.scheduler :as scheduler]
[auto-ap.yodlee.core2 :as client2]
[clojure.tools.logging :as log]
[auto-ap.import.intuit :as i] [auto-ap.import.intuit :as i]
[auto-ap.import.plaid :as p] [auto-ap.import.plaid :as p]
[unilog.context :as lc]
[auto-ap.import.yodlee :as y] [auto-ap.import.yodlee :as y]
[auto-ap.import.yodlee2 :as y2] [auto-ap.import.yodlee2 :as y2]
)) [auto-ap.utils :refer [heartbeat]]
[clojure.tools.logging :as log]
[config.core :refer [env]]
[mount.core :as mount]
[yang.scheduler :as scheduler]))
(def queue-url (:requests-queue-url env)) (def queue-url (:requests-queue-url env))
(defn process-1 [] (defn process-1 []
(lc/with-context {:source "Request poller"} (let [[{:keys [message-id receipt-handle body]}] (:messages (sqs/receive-message {:queue-url queue-url
(log/info "Checking SQS...") :wait-time-seconds 1
(let [[{:keys [message-id receipt-handle body]}] (:messages (sqs/receive-message {:queue-url queue-url :count 1}))]
:wait-time-seconds 1
:count 1}))] (when message-id
(sqs/delete-message {:queue-url queue-url
(when message-id :receipt-handle receipt-handle} )
(sqs/delete-message {:queue-url queue-url (log/infof "processing message %s with body %s" message-id body )
:receipt-handle receipt-handle} ) (cond
(log/infof "processing message %s with body %s" message-id body ) (= ":intuit" body)
(cond (i/import-intuit)
(= ":intuit" body)
(try
(i/import-intuit)
(catch Exception e
(log/error e)))
(= ":yodlee" body) (= ":yodlee" body)
(try (y/import-yodlee)
(y/import-yodlee)
(catch Exception e
(log/error e)))
(= ":yodlee2" body) (= ":yodlee2" body)
(try (y2/import-yodlee2)
(client2/upsert-accounts)
(y2/import-yodlee2)
(catch Exception e
(log/error e)))
(= ":plaid" body) (= ":plaid" body)
(try (p/import-plaid)))))
(p/import-plaid)
(catch Exception e
(log/error e))))
))))
(defn fake-message [] (defn fake-message []
(sqs/send-message {:queue-url (:requests-queue-url env) (sqs/send-message {:queue-url (:requests-queue-url env)
:message-body ":intuit"} )) :message-body ":intuit"} ))
(mount/defstate request-listener (mount/defstate request-listener
:start (scheduler/every (* 1000 30) process-1) :start (scheduler/every (* 1000 30) (heartbeat process-1 "request-poller"))
:stop (scheduler/stop request-listener)) :stop (scheduler/stop request-listener))

View File

@@ -6,6 +6,7 @@
[auto-ap.datomic.invoices :refer [code-invoice]] [auto-ap.datomic.invoices :refer [code-invoice]]
[auto-ap.parse :as parse] [auto-ap.parse :as parse]
[auto-ap.time :as t] [auto-ap.time :as t]
[auto-ap.utils :refer [heartbeat]]
[clj-time.coerce :as coerce] [clj-time.coerce :as coerce]
[clojure.data.csv :as csv] [clojure.data.csv :as csv]
[clojure.java.io :as io] [clojure.java.io :as io]
@@ -15,8 +16,6 @@
[config.core :refer [env]] [config.core :refer [env]]
[datomic.api :as d] [datomic.api :as d]
[mount.core :as mount] [mount.core :as mount]
[unilog.context :as lc]
#_{:clj-kondo/ignore [:unused-namespace]}
[yang.scheduler :as scheduler]) [yang.scheduler :as scheduler])
(:import (:import
(java.util UUID))) (java.util UUID)))
@@ -111,54 +110,53 @@
(defn import-sysco [] (defn import-sysco []
(lc/with-context {:source "sysco-importer"} (let [sysco-vendor (get-sysco-vendor)
(let [sysco-vendor (get-sysco-vendor) clients (d-clients/get-all)
clients (d-clients/get-all) keys (->> (s3/list-objects-v2 {:bucket-name bucket-name
keys (->> (s3/list-objects-v2 {:bucket-name bucket-name :prefix "sysco/pending"})
:prefix "sysco/pending"}) :object-summaries
:object-summaries (map :key))]
(map :key))]
(statsd/event {:title "Sysco import started" (statsd/event {:title "Sysco import started"
:text (format "Found %d sysco invoice to import: %s" (count keys) (pr-str keys)) :text (format "Found %d sysco invoice to import: %s" (count keys) (pr-str keys))
:priority :low} :priority :low}
nil) nil)
(log/infof "Found %d sysco invoice to import: %s" (count keys) (pr-str keys)) (log/infof "Found %d sysco invoice to import: %s" (count keys) (pr-str keys))
(let [transaction (->> keys (let [transaction (->> keys
(mapcat (fn [k] (mapcat (fn [k]
(try (try
(let [invoice-key (str "invoice-files/" (UUID/randomUUID) ".csv") ; (let [invoice-key (str "invoice-files/" (UUID/randomUUID) ".csv") ;
invoice-url (str "http://" (:data-bucket env) ".s3-website-us-east-1.amazonaws.com/" invoice-key)] invoice-url (str "http://" (:data-bucket env) ".s3-website-us-east-1.amazonaws.com/" invoice-key)]
(s3/copy-object {:source-bucket-name (:data-bucket env) (s3/copy-object {:source-bucket-name (:data-bucket env)
:destination-bucket-name (:data-bucket env)
:source-key k
:destination-key invoice-key})
[[:propose-invoice
(-> k
read-sysco-csv
(extract-invoice-details clients sysco-vendor)
(assoc :invoice/source-url invoice-url))]])
(catch Exception e
(log/error (str "Cannot load file " k) e)
(log/info
(s3/copy-object {:source-bucket-name (:data-bucket env)
:destination-bucket-name (:data-bucket env) :destination-bucket-name (:data-bucket env)
:source-key k :source-key k
:destination-key invoice-key}) :destination-key (doto (str "sysco/error/"
[[:propose-invoice (.getName (io/file k)))
(-> k println)}))
read-sysco-csv [])))))
(extract-invoice-details clients sysco-vendor) result @(d/transact conn transaction)]
(assoc :invoice/source-url invoice-url))]]) (log/infof "Imported %d invoices" (/ (count (:tempids result)) 2)))
(catch Exception e (doseq [k keys]
(log/error (str "Cannot load file " k) e) (mark-key k))
(log/info (statsd/event {:title "Sysco import ended"
(s3/copy-object {:source-bucket-name (:data-bucket env) :text "Sysco completed"
:destination-bucket-name (:data-bucket env) :priority :low} nil)))
:source-key k
:destination-key (doto (str "sysco/error/"
(.getName (io/file k)))
println)}))
[])))))
result @(d/transact conn transaction)]
(log/infof "Imported %d invoices" (/ (count (:tempids result)) 2)))
(doseq [k keys]
(mark-key k))
(statsd/event {:title "Sysco import ended"
:text "Sysco completed"
:priority :low} nil))))
(mount/defstate sysco-invoice-importer (mount/defstate sysco-invoice-importer
:start (scheduler/every (* 1000 60 60) import-sysco) :start (scheduler/every (* 1000 60 60) (heartbeat import-sysco "sysco-importer"))
:stop (scheduler/stop sysco-invoice-importer)) :stop (scheduler/stop sysco-invoice-importer))

View File

@@ -1,44 +1,35 @@
(ns auto-ap.background.vendor (ns auto-ap.background.vendor
(:require [auto-ap.datomic.invoices :as d-invoices] (:require
[auto-ap.datomic :refer [uri conn]] [auto-ap.datomic :refer [conn]]
[datomic.api :as d] [auto-ap.utils :refer [heartbeat]]
[auto-ap.time :as time] [datomic.api :as d]
[clj-time.coerce :as coerce] [mount.core :as mount]
[mount.core :as mount] [yang.scheduler :as scheduler]))
[yang.scheduler :as scheduler]
[unilog.context :as lc]
[clojure.tools.logging :as log]))
(defn refresh-vendor-usages [] (defn refresh-vendor-usages []
(lc/with-context {:source "refreshing vendor-usages"} (->> {:query {:find ['?v '?c '(count ?e)]
:in ['$]
(try :where ['[?v :vendor/name]
(->> {:query {:find ['?v '?c '(count ?e)] '(or-join [?v ?c ?e]
:in ['$] (and
:where ['[?v :vendor/name] [?e :invoice/vendor ?v]
'(or-join [?v ?c ?e] [?e :invoice/client ?c])
(and (and
[?e :invoice/vendor ?v] [?e :transaction/vendor ?v]
[?e :invoice/client ?c]) [?e :transaction/client ?c])
(and (and
[?e :transaction/vendor ?v] [?e :journal-entry/vendor ?v]
[?e :transaction/client ?c]) [?e :journal-entry/client ?c]))]}
(and :args [(d/db conn)]}
[?e :journal-entry/vendor ?v] (d/query)
[?e :journal-entry/client ?c]))]} (map (fn [[v c cnt]]
:args [(d/db conn)]} #:vendor-usage {:vendor v
(d/query) :client c
(map (fn [[v c cnt]] :key (str v "-" c)
#:vendor-usage {:vendor v :count cnt}))
:client c (d/transact conn)
:key (str v "-" c) deref))
:count cnt}))
(d/transact conn)
deref)
(log/info "updated vendor usage")
(catch Exception e
(log/error e)))))
(mount/defstate refresh-vendor-usages-worker (mount/defstate refresh-vendor-usages-worker
:start (scheduler/every (* 60 60 1000) refresh-vendor-usages) :start (scheduler/every (* 60 60 1000) (heartbeat refresh-vendor-usages "vendor-usages"))
:stop (scheduler/stop refresh-vendor-usages)) :stop (scheduler/stop refresh-vendor-usages))

View File

@@ -1,24 +1,26 @@
(ns auto-ap.graphql.clients (ns auto-ap.graphql.clients
(:require [auto-ap.datomic :refer [audit-transact conn remove-nils]] (:require
[auto-ap.datomic.clients :as d-clients] [amazonica.aws.s3 :as s3]
[auto-ap.graphql.utils :refer [->graphql assert-admin can-see-client? is-admin?]] [auto-ap.datomic :refer [audit-transact conn remove-nils]]
[auto-ap.utils :refer [by]] [auto-ap.datomic.clients :as d-clients]
[auto-ap.yodlee.core :refer [in-memory-cache]] [auto-ap.graphql.utils
[auto-ap.routes.queries :as q] :refer [->graphql assert-admin can-see-client? is-admin?]]
[auto-ap.square.core :as square] [auto-ap.routes.queries :as q]
[com.walmartlabs.lacinia.util :refer [attach-resolvers]] [auto-ap.square.core :as square]
[clj-time.coerce :as coerce] [auto-ap.utils :refer [by heartbeat]]
[clojure.string :as str] [auto-ap.yodlee.core :refer [in-memory-cache]]
[unilog.context :as lc] [clj-time.coerce :as coerce]
[clojure.tools.logging :as log] [clojure.java.io :as io]
[datomic.api :as d] [clojure.string :as str]
[clojure.java.io :as io] [clojure.tools.logging :as log]
[amazonica.aws.s3 :as s3] [com.walmartlabs.lacinia.util :refer [attach-resolvers]]
#_{:clj-kondo/ignore [:unused-namespace]} [datomic.api :as d]
[yang.scheduler :as scheduler] [mount.core :as mount]
[mount.core :as mount]) [unilog.context :as lc]
(:import [org.apache.commons.codec.binary Base64] [yang.scheduler :as scheduler])
java.util.UUID)) (:import
(java.util UUID)
(org.apache.commons.codec.binary Base64)))
(defn assert-client-code-is-unique [code] (defn assert-client-code-is-unique [code]
(when (seq (d/query {:query {:find '[?id] (when (seq (d/query {:query {:find '[?id]
@@ -245,15 +247,10 @@
(map first))))) (map first)))))
(defn refresh-current-balance [] (defn refresh-current-balance []
(lc/with-context {:source "current-balance-cache"} (build-current-balance (bank-accounts-needing-refresh)))
(try
(log/info "Refreshing running balance cache")
(build-current-balance (bank-accounts-needing-refresh))
(catch Exception e
(log/error e)))))
(mount/defstate current-balance-worker (mount/defstate current-balance-worker
:start (scheduler/every (* 17 60 1000) refresh-current-balance) :start (scheduler/every (* 17 60 1000) (heartbeat refresh-current-balance "current-balance-cache"))
:stop (scheduler/stop current-balance-worker)) :stop (scheduler/stop current-balance-worker))
(defn get-client [context _ _] (defn get-client [context _ _]

View File

@@ -8,16 +8,16 @@
[auto-ap.graphql.utils [auto-ap.graphql.utils
:refer [->graphql <-graphql assert-admin assert-can-see-client result->page]] :refer [->graphql <-graphql assert-admin assert-can-see-client result->page]]
[auto-ap.parse.util :as parse] [auto-ap.parse.util :as parse]
[auto-ap.utils :refer [by dollars=]] [auto-ap.pdf.ledger :refer [print-balance-sheet print-pnl]]
[auto-ap.pdf.ledger :refer [print-pnl print-balance-sheet]] [auto-ap.utils :refer [by dollars= heartbeat]]
[clj-time.coerce :as coerce] [clj-time.coerce :as coerce]
[clj-time.core :as t]
[clojure.tools.logging :as log] [clojure.tools.logging :as log]
[com.walmartlabs.lacinia.util :refer [attach-resolvers]] [com.walmartlabs.lacinia.util :refer [attach-resolvers]]
[datomic.api :as d] [datomic.api :as d]
[mount.core :as mount] [mount.core :as mount]
[unilog.context :as lc] [unilog.context :as lc]
[yang.scheduler :as scheduler] [yang.scheduler :as scheduler]))
[clj-time.core :as t]))
(mount/defstate running-balance-cache (mount/defstate running-balance-cache
:start (atom {})) :start (atom {}))
@@ -525,16 +525,11 @@
(defn refresh-running-balance-cache [] (defn refresh-running-balance-cache []
(lc/with-context {:source "running-balance-cache"} (build-running-balance-cache))
(try
(log/info "Refreshing running balance cache")
(build-running-balance-cache)
(catch Exception e
(log/error e)))))
(mount/defstate running-balance-cache-worker (mount/defstate running-balance-cache-worker
:start (scheduler/every (* 15 60 1000) refresh-running-balance-cache) :start (scheduler/every (* 15 60 1000) (heartbeat refresh-running-balance-cache "running-balance-cache"))
:stop (scheduler/stop running-balance-cache-worker)) :stop (scheduler/stop running-balance-cache-worker))

View File

@@ -2,18 +2,18 @@
(:require (:require
[auto-ap.datomic :refer [conn]] [auto-ap.datomic :refer [conn]]
[auto-ap.import.transactions :as t] [auto-ap.import.transactions :as t]
[auto-ap.time :as atime]
[auto-ap.intuit.core :as i] [auto-ap.intuit.core :as i]
[auto-ap.utils :refer [allow-once]] [auto-ap.time :as atime]
[auto-ap.utils :refer [allow-once heartbeat]]
[clj-time.coerce :as coerce] [clj-time.coerce :as coerce]
[clj-time.core :as time] [clj-time.core :as time]
[clojure.string :as str]
[clojure.tools.logging :as log]
[com.unbounce.dogstatsd.core :as statsd] [com.unbounce.dogstatsd.core :as statsd]
[datomic.api :as d] [datomic.api :as d]
[mount.core :as mount] [mount.core :as mount]
[unilog.context :as lc] [unilog.context :as lc]
[yang.scheduler :as scheduler] [yang.scheduler :as scheduler]))
[clojure.string :as str]
[clojure.tools.logging :as log]))
(defn get-intuit-bank-accounts [db] (defn get-intuit-bank-accounts [db]
(d/q '[:find ?external-id ?ba ?c (d/q '[:find ?external-id ?ba ?c
@@ -47,34 +47,33 @@
(t/apply-synthetic-ids))) (t/apply-synthetic-ids)))
(defn import-intuit [] (defn import-intuit []
(lc/with-context {:source "Import intuit transactions"} (statsd/event {:title "Intuit import started"
(statsd/event {:title "Intuit import started" :text "Starting"
:text "Starting" :priority :low}
:priority :low} nil)
nil) (let [import-batch (t/start-import-batch :import-source/intuit "Automated intuit user")
(let [import-batch (t/start-import-batch :import-source/intuit "Automated intuit user") db (d/db conn)
db (d/db conn) end (auto-ap.time/local-now)
end (auto-ap.time/local-now) start (time/plus end (time/days -30))]
start (time/plus end (time/days -30))] (try
(try (doseq [[external-id bank-account-id client-id] (get-intuit-bank-accounts db)
(doseq [[external-id bank-account-id client-id] (get-intuit-bank-accounts db) transaction (-> (i/get-transactions (auto-ap.time/unparse start auto-ap.time/iso-date)
transaction (-> (i/get-transactions (auto-ap.time/unparse start auto-ap.time/iso-date) (auto-ap.time/unparse end auto-ap.time/iso-date)
(auto-ap.time/unparse end auto-ap.time/iso-date) external-id)
external-id) (intuits->transactions bank-account-id client-id))]
(intuits->transactions bank-account-id client-id))] (t/import-transaction! import-batch transaction))
(t/import-transaction! import-batch transaction)) (t/finish! import-batch)
(t/finish! import-batch) (statsd/event {:title "Intuit import Finished"
(statsd/event {:title "Intuit import Finished" :text (pr-str (t/get-stats import-batch))
:text (pr-str (t/get-stats import-batch)) :priority :low}
:priority :low} nil)
nil) (catch Exception e
(catch Exception e (t/fail! import-batch e)
(t/fail! import-batch e) (statsd/event {:title "Intuit import failed"
(statsd/event {:title "Intuit import failed" :text (str e)
:text (str e) :alert-type :warning
:alert-type :warning :priority :normal}
:priority :normal} nil)))))
nil))))))
(def upsert-transactions (allow-once upsert-transactions)) (def upsert-transactions (allow-once upsert-transactions))
@@ -88,7 +87,7 @@
bank-accounts)))) bank-accounts))))
(mount/defstate import-worker (mount/defstate import-worker
:start (scheduler/every (* 1000 60 60 24) import-intuit) :start (scheduler/every (* 1000 60 60 24) (heartbeat import-intuit "import-intuit"))
:stop (scheduler/stop import-worker)) :stop (scheduler/stop import-worker))
(mount/defstate account-worker (mount/defstate account-worker

View File

@@ -1,17 +1,15 @@
(ns auto-ap.import.plaid (ns auto-ap.import.plaid
(:require (:require
[auto-ap.datomic :refer [conn]] [auto-ap.datomic :refer [conn]]
[auto-ap.plaid.core :as p]
[auto-ap.utils :refer [allow-once by]]
[auto-ap.import.transactions :as t] [auto-ap.import.transactions :as t]
[auto-ap.plaid.core :as p]
[auto-ap.utils :refer [allow-once by heartbeat]]
[clj-time.coerce :as coerce]
[clj-time.core :as time] [clj-time.core :as time]
[clojure.tools.logging :as log]
[datomic.api :as d] [datomic.api :as d]
[mount.core :as mount] [mount.core :as mount]
[unilog.context :as lc] [unilog.context :as lc]
[yang.scheduler :as scheduler] [yang.scheduler :as scheduler]))
[clj-time.coerce :as coerce]))
(defn get-plaid-accounts [db] (defn get-plaid-accounts [db]
(-> (d/q '[:find ?ba ?c ?external-id ?t (-> (d/q '[:find ?ba ?c ?external-id ?t
@@ -59,7 +57,7 @@
(def import-plaid (allow-once import-plaid)) (def import-plaid (allow-once import-plaid))
(mount/defstate import-worker (mount/defstate import-worker
:start (scheduler/every (* 1000 60 60 3) import-plaid) :start (scheduler/every (* 1000 60 60 3) (heartbeat import-plaid "import-plaid"))
:stop (scheduler/stop import-worker)) :stop (scheduler/stop import-worker))

View File

@@ -3,7 +3,7 @@
[auto-ap.datomic :refer [conn]] [auto-ap.datomic :refer [conn]]
[auto-ap.import.transactions :as t] [auto-ap.import.transactions :as t]
[auto-ap.time :as atime] [auto-ap.time :as atime]
[auto-ap.utils :refer [allow-once]] [auto-ap.utils :refer [allow-once heartbeat]]
[auto-ap.yodlee.core :as client] [auto-ap.yodlee.core :as client]
[clj-time.coerce :as coerce] [clj-time.coerce :as coerce]
[clojure.string :as str] [clojure.string :as str]
@@ -45,43 +45,42 @@
:status status})) :status status}))
(defn import-yodlee [] (defn import-yodlee []
(lc/with-context {:source "Import yodlee transactions"} (statsd/event {:title "Yodlee import started"
(statsd/event {:title "Yodlee import started" :text "Starting"
:text "Starting" :priority :low}
:priority :low} nil)
nil) (let [import-batch (t/start-import-batch :import-source/yodlee "Automated yodlee user")]
(let [import-batch (t/start-import-batch :import-source/yodlee "Automated yodlee user")] (try
(try (let [account-lookup (d/q '[:find ?ya ?ba ?c
(let [account-lookup (d/q '[:find ?ya ?ba ?c :in $
:in $ :where [?ba :bank-account/yodlee-account-id ?ya]
:where [?ba :bank-account/yodlee-account-id ?ya] [?c :client/bank-accounts ?ba]]
[?c :client/bank-accounts ?ba]] (d/db conn))]
(d/db conn))] (doseq [[yodlee-account bank-account client-id] account-lookup
(doseq [[yodlee-account bank-account client-id] account-lookup transaction (try
transaction (try (client/get-specific-transactions yodlee-account (client/get-auth-header))
(client/get-specific-transactions yodlee-account (client/get-auth-header)) (catch Exception e
(catch Exception e (log/warn e)
(log/warn e) []))]
[]))] (t/import-transaction! import-batch (assoc (yodlee->transaction transaction)
(t/import-transaction! import-batch (assoc (yodlee->transaction transaction)
:transaction/bank-account bank-account :transaction/bank-account bank-account
:transaction/client client-id))) :transaction/client client-id)))
(t/finish! import-batch)) (t/finish! import-batch))
(statsd/event {:title "Yodlee import Finished" (statsd/event {:title "Yodlee import Finished"
:text (pr-str (t/get-stats import-batch)) :text (pr-str (t/get-stats import-batch))
:priority :low} :priority :low}
nil) nil)
(catch Exception e (catch Exception e
(t/fail! import-batch e) (t/fail! import-batch e)
(statsd/event {:title "Yodlee import failed" (statsd/event {:title "Yodlee import failed"
:text (str e) :text (str e)
:alert-type :warning :alert-type :warning
:priority :normal} :priority :normal}
nil)))))) nil)))))
(def import-yodlee (allow-once import-yodlee)) (def import-yodlee (allow-once import-yodlee))
(mount/defstate import-worker (mount/defstate import-worker
:start (scheduler/every (* 1000 60 60 8) import-yodlee) :start (scheduler/every (* 1000 60 60 8) (heartbeat import-yodlee "import-yodlee"))
:stop (scheduler/stop import-worker)) :stop (scheduler/stop import-worker))

View File

@@ -3,7 +3,7 @@
[auto-ap.datomic :refer [conn]] [auto-ap.datomic :refer [conn]]
[auto-ap.import.transactions :as t] [auto-ap.import.transactions :as t]
[auto-ap.import.yodlee :as y] [auto-ap.import.yodlee :as y]
[auto-ap.utils :refer [allow-once]] [auto-ap.utils :refer [allow-once heartbeat]]
[auto-ap.yodlee.core2 :as client2] [auto-ap.yodlee.core2 :as client2]
[com.unbounce.dogstatsd.core :as statsd] [com.unbounce.dogstatsd.core :as statsd]
[datomic.api :as d] [datomic.api :as d]
@@ -53,9 +53,9 @@
(mount/defstate import-worker (mount/defstate import-worker
:start (scheduler/every (* 1000 60 60 4) import-yodlee2) :start (scheduler/every (* 1000 60 60 4) (heartbeat import-yodlee2 "import-yodlee"))
:stop (scheduler/stop import-worker)) :stop (scheduler/stop import-worker))
(mount/defstate account-worker (mount/defstate account-worker
:start (scheduler/every (* 5 60 1000) client2/upsert-accounts) :start (scheduler/every (* 5 60 1000) (heartbeat client2/upsert-accounts "upsert-yodlee2-accounts"))
:stop (scheduler/stop account-worker)) :stop (scheduler/stop account-worker))

View File

@@ -196,47 +196,41 @@
:stop (-> process-txes-worker :running? (reset! false))) :stop (-> process-txes-worker :running? (reset! false)))
(defn reconcile-ledger [] (defn reconcile-ledger []
(lc/with-context {:source "reconcile-ledger"} (let [txes-missing-ledger-entries (->> (d/query {:query {:find ['?t ]
(try :in ['$]
(log/info "Attempting to reconcile the ledger") :where ['[?t :transaction/date]
(let [txes-missing-ledger-entries (->> (d/query {:query {:find ['?t ] '[?t :transaction/amount ?amt]
:in ['$] '[(not= 0.0 ?amt)]
:where ['[?t :transaction/date] '(not [?t :transaction/approval-status :transaction-approval-status/excluded])
'[?t :transaction/amount ?amt] '(not [?t :transaction/approval-status :transaction-approval-status/suppressed])
'(not-join [?t] [?e :journal-entry/original-entity ?t])]}
:args [(d/db conn)]})
(map first)
(mapv #(entity-change->ledger (d/db conn) [:transaction %])))
invoices-missing-ledger-entries (->> (d/query {:query {:find ['?t ]
:in ['$]
:where ['[?t :invoice/date]
'[?t :invoice/total ?amt]
'[(not= 0.0 ?amt)] '[(not= 0.0 ?amt)]
'(not [?t :transaction/approval-status :transaction-approval-status/excluded]) '(not [?t :invoice/status :invoice-status/voided])
'(not [?t :transaction/approval-status :transaction-approval-status/suppressed]) '(not [?t :invoice/import-status :import-status/pending])
'(not [?t :invoice/exclude-from-ledger true])
'(not-join [?t] [?e :journal-entry/original-entity ?t])]} '(not-join [?t] [?e :journal-entry/original-entity ?t])]}
:args [(d/db conn)]}) :args [(d/db conn)]})
(map first) (map first)
(mapv #(entity-change->ledger (d/db conn) [:transaction %]))) (mapv #(entity-change->ledger (d/db conn) [:invoice %])))
repairs (vec (concat txes-missing-ledger-entries invoices-missing-ledger-entries))]
(when (seq repairs)
invoices-missing-ledger-entries (->> (d/query {:query {:find ['?t ] (log/info (take 3 repairs))
:in ['$] (log/warn "repairing " (count txes-missing-ledger-entries) " missing transactions, " (count invoices-missing-ledger-entries) " missing invoices that were missing ledger entries")
:where ['[?t :invoice/date] @(d/transact conn repairs))))
'[?t :invoice/total ?amt]
'[(not= 0.0 ?amt)]
'(not [?t :invoice/status :invoice-status/voided])
'(not [?t :invoice/import-status :import-status/pending])
'(not [?t :invoice/exclude-from-ledger true])
'(not-join [?t] [?e :journal-entry/original-entity ?t])]}
:args [(d/db conn)]})
(map first)
(mapv #(entity-change->ledger (d/db conn) [:invoice %])))
repairs (vec (concat txes-missing-ledger-entries invoices-missing-ledger-entries))]
(when (seq repairs)
(log/info (take 3 repairs))
(log/warn "repairing " (count txes-missing-ledger-entries) " missing transactions, " (count invoices-missing-ledger-entries) " missing invoices that were missing ledger entries")
@(d/transact conn repairs))
(log/info "Finished reconciling ledger"))
(catch Exception e
(log/error e)))))
(mount/defstate reconciliation-frequency :start (* 1000 60 60)) (mount/defstate reconciliation-frequency :start (* 1000 60 60))
(mount/defstate ledger-reconciliation-worker (mount/defstate ledger-reconciliation-worker
:start (scheduler/every reconciliation-frequency reconcile-ledger) :start (scheduler/every reconciliation-frequency (heartbeat reconcile-ledger "reconcile-ledger"))
:stop (scheduler/stop ledger-reconciliation-worker)) :stop (scheduler/stop ledger-reconciliation-worker))
@@ -371,56 +365,52 @@
invoice-accounts))) invoice-accounts)))
(defn touch-broken-ledger [] (defn touch-broken-ledger []
(lc/with-context {:source "touch-broken-ledger"} (statsd/event {:title "Reconciling Ledger"
(statsd/event {:title "Reconciling Ledger" :text "This process looks for unbalance ledger entries, or missing ledger entries"
:text "This process looks for unbalance ledger entries, or missing ledger entries" :priority :low}
:priority :low} nil)
nil) (log/info "Attempting to fix transactions that are in the ledger but are wrong")
(try (let [mismatched-ts (mismatched-transactions)]
(log/info "Attempting to fix transactions that are in the ledger but are wrong") (if (seq mismatched-ts)
(let [mismatched-ts (mismatched-transactions)] (do
(if (seq mismatched-ts) (log/warn (count mismatched-ts) " transactions exist but don't match ledger " (pr-str (take 10 mismatched-ts) ))
(do (doseq [[m] mismatched-ts]
(log/warn (count mismatched-ts) " transactions exist but don't match ledger " (pr-str (take 10 mismatched-ts) )) (touch-transaction m))
(doseq [[m] mismatched-ts] (statsd/gauge "data.mismatched_transactions" (count (mismatched-transactions))))
(touch-transaction m)) (statsd/gauge "data.mismatched_transactions" 0.0)))
(statsd/gauge "data.mismatched_transactions" (count (mismatched-transactions)))) (log/info "Attempting to fix transactions that are in the ledger but debits/credits don't add up")
(statsd/gauge "data.mismatched_transactions" 0.0))) (let [unbalanced-ts (unbalanced-transactions)]
(log/info "Attempting to fix transactions that are in the ledger but debits/credits don't add up") (if (seq unbalanced-ts)
(let [unbalanced-ts (unbalanced-transactions)] (do
(if (seq unbalanced-ts) (log/warn (count unbalanced-ts) " transactions exist but don't have matching debits/credits (" (pr-str (take 10 unbalanced-ts) ) ")")
(do (doseq [m unbalanced-ts]
(log/warn (count unbalanced-ts) " transactions exist but don't have matching debits/credits (" (pr-str (take 10 unbalanced-ts) ) ")") (touch-transaction m))
(doseq [m unbalanced-ts] (statsd/gauge "data.unbalanced_transactions" (count (unbalanced-transactions))))
(touch-transaction m)) (statsd/gauge "data.unbalanced_transactions" 0.0)))
(statsd/gauge "data.unbalanced_transactions" (count (unbalanced-transactions)))) (log/info "Finished fixing transactions that are in the ledger but are wrong")
(statsd/gauge "data.unbalanced_transactions" 0.0))) (let [mismatched-is (mismatched-invoices)]
(log/info "Finished fixing transactions that are in the ledger but are wrong") (if (seq mismatched-is)
(let [mismatched-is (mismatched-invoices)] (do
(if (seq mismatched-is) (log/warn (count mismatched-is) " invoice exist but don't match ledger ")
(do (doseq [[m] mismatched-is]
(log/warn (count mismatched-is) " invoice exist but don't match ledger ") (touch-invoice m))
(doseq [[m] mismatched-is] (statsd/gauge "data.mismatched_invoices" (count (mismatched-invoices))))
(touch-invoice m)) (statsd/gauge "data.mismatched_invoices" 0.0)))
(statsd/gauge "data.mismatched_invoices" (count (mismatched-invoices)))) (log/info "Attempting to fix transactions that are in the ledger but debits/credits don't add up")
(statsd/gauge "data.mismatched_invoices" 0.0))) (let [unbalanced-invoices (unbalanced-invoices)]
(log/info "Attempting to fix transactions that are in the ledger but debits/credits don't add up") (if (seq unbalanced-invoices)
(let [unbalanced-invoices (unbalanced-invoices)] (do
(if (seq unbalanced-invoices) (log/warn (count unbalanced-invoices) " invoices exist but don't have matching debits/credits ")
(do (doseq [m unbalanced-invoices]
(log/warn (count unbalanced-invoices) " invoices exist but don't have matching debits/credits ") (touch-invoice m))
(doseq [m unbalanced-invoices] (statsd/gauge "data.unbalanced_invoices" (count (unbalanced-invoices))))
(touch-invoice m)) (statsd/gauge "data.unbalanced_invoices" 0.0)))
(statsd/gauge "data.unbalanced_invoices" (count (unbalanced-invoices))))
(statsd/gauge "data.unbalanced_invoices" 0.0)))
(log/info "Finish fixing invoices that are in the ledger but are wrong") (log/info "Finish fixing invoices that are in the ledger but are wrong")
(statsd/event {:title "Finished Reconciling Ledger" (statsd/event {:title "Finished Reconciling Ledger"
:text "This process looks for unbalance ledger entries, or missing ledger entries" :text "This process looks for unbalance ledger entries, or missing ledger entries"
:priority :low} :priority :low}
nil) nil))
(catch Exception e
(log/error e)))))
(mount/defstate touch-broken-ledger-worker (mount/defstate touch-broken-ledger-worker
:start (scheduler/every reconciliation-frequency (heartbeat touch-broken-ledger "touch-broken-ledger")) :start (scheduler/every reconciliation-frequency (heartbeat touch-broken-ledger "touch-broken-ledger"))

View File

@@ -1,59 +0,0 @@
(ns auto-ap.plaid.import
(:require
[auto-ap.datomic :refer [conn]]
[auto-ap.plaid.core :as p]
[auto-ap.utils :refer [allow-once]]
[auto-ap.yodlee.import :as y]
[clj-time.core :as time]
[clojure.tools.logging :as log]
[datomic.api :as d]
[mount.core :as mount]
[unilog.context :as lc]
[yang.scheduler :as scheduler]
[clj-time.coerce :as coerce]))
(defn get-plaid-accounts [db]
(-> (d/q '[:find ?ba ?c ?external-id ?t
:in $
:where
[?c :client/bank-accounts ?ba]
[?ba :bank-account/plaid-account ?pa]
[?pa :plaid-account/external-id ?external-id]
[?pi :plaid-item/accounts ?pa]
[?pi :plaid-item/access-token ?t]]
db )))
(defn plaid->transaction [t]
#:transaction {:description-original (:name t)
:raw-id (:transaction_id t)
:id (digest/sha-256 (:transaction_id t))
:amount (double (:amount t))
:date (coerce/to-date (auto-ap.time/parse (:date t) auto-ap.time/iso-date))
:status "POSTED"})
(defn import-plaid []
(lc/with-context {:source "Import plaid transactions"}
(let [import-batch (y/start-import-batch :import-source/plaid "Automated plaid user")
end (auto-ap.time/local-now)
start (time/plus end (time/days -30))]
(try
(doseq [[bank-account-id client-id external-id access-token] (get-plaid-accounts (d/db conn))
transaction (:transactions (p/get-transactions access-token external-id start end))]
(when (not (:pending transaction))
(y/import-transaction! import-batch (assoc (plaid->transaction transaction)
:transaction/bank-account bank-account-id
:transaction/client client-id))))
(y/finish! import-batch)
(catch Exception e
(y/fail! import-batch e))))))
(def import-plaid (allow-once import-plaid))
(mount/defstate import-worker
:start (scheduler/every (* 1000 60 60 3) import-plaid)
:stop (scheduler/stop import-worker))

View File

@@ -491,8 +491,7 @@
(defn upsert-all [] (defn upsert-all []
(doseq [client (get-square-clients) (doseq [client (get-square-clients)
:when (seq (filter :square-location/client-location (:client/square-locations client)))] :when (seq (filter :square-location/client-location (:client/square-locations client)))]
(lc/with-context {:source "Square loading" (lc/with-context {:client (:client/code client)}
:client (:client/code client)}
(upsert-locations client) (upsert-locations client)
(log/info "Loading Orders") (log/info "Loading Orders")
(upsert client) (upsert client)

View File

@@ -1,7 +1,9 @@
(ns auto-ap.utils (ns auto-ap.utils
#?@ #?@
(:clj (:clj
[(:require [com.unbounce.dogstatsd.core :as statsd])])) [(:require [com.unbounce.dogstatsd.core :as statsd]
[clojure.tools.logging :as log]
[unilog.context :as lc])]))
(defn by (defn by
([f xs] ([f xs]
@@ -87,9 +89,19 @@
(defn heartbeat [f id] (defn heartbeat [f id]
(fn [] (fn []
#?(:clj (do #?(:clj (do
(f) (lc/with-context {:source id}
(statsd/service-check {:name (str id) (try
:status :ok} (log/info "Starting background process " id)
nil)) (f)
(log/info "Completed background process " id)
(statsd/service-check {:name (str id)
:status :ok}
nil)
(catch Exception e
(log/error e)
(statsd/service-check {:name (str id)
:status :critical}
nil)))))
:cljs (do (println "Heartbeat for " id) :cljs (do (println "Heartbeat for " id)
(f))))) (f)))))