fixes sales queries, minor logging tweaks

This commit is contained in:
Bryce Covert
2023-01-06 17:37:01 -08:00
parent 7c0004d7bd
commit 0c08e6a929
4 changed files with 74 additions and 58 deletions

View File

@@ -36,9 +36,13 @@
(when (seq container-data) (when (seq container-data)
(lc/push-context "container" (:DockerId container-data)) (lc/push-context "container" (:DockerId container-data))
(lc/push-context "ip" (-> container-data :Networks first :IPv4Addresses first)) (lc/push-context "ip" (-> container-data :Networks first :IPv4Addresses first))
(mu/start-publisher! {:type :console-json})
(mu/set-global-context! (mu/set-global-context!
{:container (:DockerId container-data) {:container (:DockerId container-data)
:ip (-> container-data :Networks first :IPv4Addresses first)}))) :ip (-> container-data :Networks first :IPv4Addresses first)
:env (:dd-env env)
:service (:dd-service env)})))
(defn stop-logging-context [] (defn stop-logging-context []
(when (seq container-data) (when (seq container-data)

View File

@@ -1,6 +1,8 @@
(ns auto-ap.logging (ns auto-ap.logging
(:require [clojure.tools.logging :as log] (:require [clojure.tools.logging :as log]
[unilog.context :as lc])) [unilog.context :as lc]
[com.brunobonacci.mulog :as mu]))
(defn info-event [message context] (defn info-event [message context]
(lc/with-context context (lc/with-context context
(log/info message))) (log/info message)))
@@ -13,3 +15,21 @@
(defn error-event [message context] (defn error-event [message context]
(lc/with-context context (lc/with-context context
(log/warn message))) (log/warn message)))
(defmacro with-context-as [ctx s & body]
`(mu/with-context ~ctx
(let [~s (mu/local-context)]
~@body)))
(defmacro capture-context->lc [& body]
`(let [~'lc (mu/local-context)]
~@body))
(defmacro info [x & kvs]
`(mu/log ~x :status "INFO" ~@kvs ))
(defmacro warn [x & kvs]
`(mu/log ~x :status "WARN" ~@kvs ))
(defmacro error [x & kvs]
`(mu/log ~x :status "ERROR" ~@kvs ))

View File

@@ -11,7 +11,6 @@
[clojure.data.json :as json] [clojure.data.json :as json]
[clojure.set :as set] [clojure.set :as set]
[clojure.string :as str] [clojure.string :as str]
[clojure.tools.logging :as log]
[cemerick.url :as url] [cemerick.url :as url]
[datomic.api :as d] [datomic.api :as d]
[slingshot.slingshot :refer [try+]] [slingshot.slingshot :refer [try+]]
@@ -19,7 +18,8 @@
[manifold.deferred :as de] [manifold.deferred :as de]
[manifold.time :as mt] [manifold.time :as mt]
[manifold.stream :as s] [manifold.stream :as s]
[com.brunobonacci.mulog :as mu])) [com.brunobonacci.mulog :as mu]
[auto-ap.logging :refer [with-context-as capture-context->lc] :as log]))
(defn client-base-headers [client] (defn client-base-headers [client]
{"Square-Version" "2021-08-18" {"Square-Version" "2021-08-18"
@@ -28,20 +28,11 @@
(defn retry-4 [ex try-count _] (defn retry-4 [ex try-count _]
(mu/log ::aborting-request (log/error ::aborting-request
:attempt try-count :attempt try-count
:exception ex) :exception ex)
(if (> try-count 4) false true)) (if (> try-count 4) false true))
(defmacro with-context-as [ctx s & body]
`(mu/with-context ~ctx
(let [~s (mu/local-context)]
~@body)))
(defmacro capture-context->lc [& body]
`(let [~'lc (mu/local-context)]
~@body))
(def manifold-api-stream (def manifold-api-stream
(let [stream (s/stream 100)] (let [stream (s/stream 100)]
(->> stream (->> stream
@@ -51,7 +42,7 @@
(de/chain (de/chain
(de/loop [attempt 0] (de/loop [attempt 0]
(-> (de/chain (de/future-with (ex/execute-pool) (-> (de/chain (de/future-with (ex/execute-pool)
(mu/log ::request-started (log/info ::request-started
:url (:url request) :url (:url request)
:attempt attempt :attempt attempt
:source "Square 3" :source "Square 3"
@@ -75,9 +66,9 @@
(s/buffer 50) (s/buffer 50)
(s/realize-each) (s/realize-each)
(s/consume (fn [result] (s/consume (fn [result]
(mu/log ::request-completed (log/info ::request-completed
:source "Square 3" :source "Square 3"
:background-job "Square 3")))) :background-job "Square 3"))))
stream)) stream))
(defn manifold-api-call (defn manifold-api-call
@@ -110,8 +101,8 @@
:locations) :locations)
(fn [error] (fn [error]
(mu/with-context lc (mu/with-context lc
(mu/log ::no-locations-found (log/error ::no-locations-found
:exception error)) :exception error))
[])))) []))))
@@ -158,9 +149,8 @@
:else :else
(do (do
(mu/log ::no-look-up-item (log/warn ::no-look-up-item
:item item :item item)
)
"Uncategorized")))) "Uncategorized"))))
@@ -190,9 +180,9 @@
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} #_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn get-order (defn get-order
([client location order-id] ([client location order-id]
(mu/log ::searching-for-order (log/info ::searching-for-order
:location location :location location
:order-id order-id) :order-id order-id)
(let [result (->> (client/get (str "https://connect.squareup.com/v2/orders/" order-id) (let [result (->> (client/get (str "https://connect.squareup.com/v2/orders/" order-id)
{:headers (client-base-headers client) {:headers (client-base-headers client)
:as :json}) :as :json})
@@ -201,8 +191,8 @@
result))) result)))
(defn continue-search [client location start end cursor] (defn continue-search [client location start end cursor]
(mu/log ::continue-order-search (log/info ::continue-order-search
:cursor cursor) :cursor cursor)
(capture-context->lc (capture-context->lc
(de/chain (manifold-api-call (de/chain (manifold-api-call
@@ -218,8 +208,8 @@
(fn [result] (fn [result]
(mu/with-context (mu/with-context
lc lc
(mu/log ::orders-found (log/info ::orders-found
:count (count (:orders result))) :count (count (:orders result)))
(if (not-empty (:cursor result)) (if (not-empty (:cursor result))
(de/chain (continue-search client location start end (:cursor result)) (de/chain (continue-search client location start end (:cursor result))
(fn [continued-results] (fn [continued-results]
@@ -232,7 +222,7 @@
(defn search (defn search
([client location start end] ([client location start end]
(capture-context->lc (capture-context->lc
(mu/log ::searching (log/info ::searching
:location (:square-location/client-location location)) :location (:square-location/client-location location))
(de/chain (manifold-api-call {:url "https://connect.squareup.com/v2/orders/search" (de/chain (manifold-api-call {:url "https://connect.squareup.com/v2/orders/search"
:method :post :method :post
@@ -243,8 +233,8 @@
:body :body
(fn [result] (fn [result]
(mu/with-context lc (mu/with-context lc
(mu/log ::orders-found (log/info ::orders-found
:count (count (:orders result))) :count (count (:orders result)))
(if (not-empty (:cursor result)) (if (not-empty (:cursor result))
(de/chain (continue-search client location start end (:cursor result)) (de/chain (continue-search client location start end (:cursor result))
(fn [continued-results] (fn [continued-results]
@@ -411,9 +401,9 @@
(s/->source) (s/->source)
(s/map (fn [[start-date end-date]] (s/map (fn [[start-date end-date]]
(mu/with-context lc (mu/with-context lc
(mu/log ::searching-settlements (log/info ::searching-settlements
:start-date start-date :start-date start-date
:end-date end-date) :end-date end-date)
(de/chain (manifold-api-call (de/chain (manifold-api-call
{:url (str "https://connect.squareup.com/v1/" (:square-location/square-id location) "/settlements") {:url (str "https://connect.squareup.com/v1/" (:square-location/square-id location) "/settlements")
:method :get :method :get
@@ -433,8 +423,8 @@
(s/->source) (s/->source)
(s/map (fn [settlement-id] (s/map (fn [settlement-id]
(mu/with-context lc (mu/with-context lc
(mu/log ::looking-up-settlement (log/info ::looking-up-settlement
:settlement-id settlement-id) :settlement-id settlement-id)
(de/chain (de/chain
(manifold-api-call (manifold-api-call
{:url (str "https://connect.squareup.com/v1/" (:square-location/square-id location) "/settlements/" settlement-id) {:url (str "https://connect.squareup.com/v1/" (:square-location/square-id location) "/settlements/" settlement-id)
@@ -536,8 +526,8 @@
(fn [results ] (fn [results ]
(mu/with-context lc (mu/with-context lc
(doseq [x (partition-all 100 results)] (doseq [x (partition-all 100 results)]
(mu/log ::loading-orders (log/info ::loading-orders
:count (count x)) :count (count x))
@(d/transact conn x)))))))) @(d/transact conn x))))))))
@@ -554,11 +544,11 @@
(de/chain (daily-settlements client location) (de/chain (daily-settlements client location)
(fn [settlements] (fn [settlements]
(mu/with-context lc (mu/with-context lc
(doseq [x (partition-all 20 settlements)] (doseq [x (partition-all 20 settlements)]
(mu/log ::loading-deposits (log/info ::loading-deposits
:count (count x)) :count (count x))
@(d/transact conn x)) @(d/transact conn x))
(mu/log ::done-loading-deposits))))))) (log/info ::done-loading-deposits)))))))
(defn upsert-refunds (defn upsert-refunds
([client] ([client]
@@ -573,10 +563,10 @@
:client (:client/code client) :client (:client/code client)
:location (:square-location/client-location client)} :location (:square-location/client-location client)}
(doseq [x (partition-all 100 refunds)] (doseq [x (partition-all 100 refunds)]
(mu/log ::loading-refunds (log/info ::loading-refunds
:count (count x)) :count (count x))
@(d/transact conn x)) @(d/transact conn x))
(mu/log ::done-loading-refunds)))))) (log/info ::done-loading-refunds))))))
(def square-read [:db/id (def square-read [:db/id
:client/code :client/code
@@ -653,34 +643,34 @@
(seq (filter :square-location/client-location (:client/square-locations client))))) (seq (filter :square-location/client-location (:client/square-locations client)))))
(s/map (fn [client] (s/map (fn [client]
(with-context-as (merge lc {:client (:client/code client)}) lc (with-context-as (merge lc {:client (:client/code client)}) lc
(mu/log ::import-started) (log/info ::import-started)
(mark-integration-status client {:integration-status/last-attempt (coerce/to-date (time/now))}) (mark-integration-status client {:integration-status/last-attempt (coerce/to-date (time/now))})
(-> (->
(de/chain (upsert-locations client) (de/chain (upsert-locations client)
(fn [_] (fn [_]
(mu/with-context lc (mu/with-context lc
(mu/log ::upsert-orders-started) (log/info ::upsert-orders-started)
(upsert client))) (upsert client)))
(fn [_] (fn [_]
(mu/with-context lc (mu/with-context lc
(mu/log ::upsert-settlements-started) (log/info ::upsert-settlements-started)
(upsert-settlements client))) (upsert-settlements client)))
(fn [_] (fn [_]
(mu/with-context lc (mu/with-context lc
(mu/log ::upsert-refunds-started) (log/info ::upsert-refunds-started)
(upsert-refunds client))) (upsert-refunds client)))
(fn [_] (fn [_]
(mu/with-context lc (mu/with-context lc
(mu/log ::upsert-done)) (log/info ::upsert-done))
(mark-integration-status client {:integration-status/state :integration-state/success (mark-integration-status client {:integration-status/state :integration-state/success
:integration-status/last-updated (coerce/to-date (time/now))}))) :integration-status/last-updated (coerce/to-date (time/now))})))
(de/catch (fn [e] (de/catch (fn [e]
(mu/with-context lc (mu/with-context lc
(let [data (ex-data e)] (let [data (ex-data e)]
(mu/log ::upsert-all-failed (log/info ::upsert-all-failed
:severity :warn :severity :warn
:exception e) :exception e)
(cond (= (:status data) 401) (cond (= (:status data) 401)
(mark-integration-status client {:integration-status/state :integration-state/unauthorized (mark-integration-status client {:integration-status/state :integration-state/unauthorized
:integration-status/message (-> data :body str)}) :integration-status/message (-> data :body str)})

View File

@@ -20,7 +20,8 @@
[datomic.api :as d] [datomic.api :as d]
[mount.core :as mount] [mount.core :as mount]
[nrepl.middleware.print] [nrepl.middleware.print]
[unilog.context :as lc]) [unilog.context :as lc]
[com.brunobonacci.mulog :as mu])
(:import (:import
(org.apache.commons.io.input BOMInputStream))) (org.apache.commons.io.input BOMInputStream)))
@@ -396,6 +397,7 @@
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} #_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn start-db [] (defn start-db []
(mu/start-publisher! {:type :console-json})
(mount.core/start (mount.core/only #{#'auto-ap.datomic/conn}))) (mount.core/start (mount.core/only #{#'auto-ap.datomic/conn})))