Pregenerates recommendatios

This commit is contained in:
Bryce
2023-08-19 13:52:44 -07:00
parent 750a147f1c
commit 486c428917
11 changed files with 820 additions and 1042 deletions

View File

@@ -38,6 +38,7 @@
(def page page/page-)
(def pill tags/pill-)
(def badge tags/badge-)
(def data-grid data-grid/data-grid-)
(def data-grid-header data-grid/header-)

View File

@@ -18,3 +18,6 @@
(= :red (:color params))
(update :class str " bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-300"))]
children))
(defn badge- [params & children]
[:div {:class "absolute inline-flex items-center justify-center w-6 h-6 text-xs font-bold text-white bg-red-500 border-2 border-white rounded-full -top-2 -right-2 dark:border-gray-900"} children])

View File

@@ -49,7 +49,7 @@
:transaction-insights (wrap-client-redirect-unauthenticated (wrap-admin insights/page))
:transaction-insight-table (wrap-client-redirect-unauthenticated (wrap-admin insights/insight-table))
:transaction-insight-rows (wrap-client-redirect-unauthenticated (wrap-admin insights/transaction-rows))
:transaction-insight-approve (wrap-client-redirect-unauthenticated (wrap-admin insights/approve))
:transaction-insight-code (wrap-client-redirect-unauthenticated (wrap-admin insights/code))
:transaction-insight-disapprove (wrap-client-redirect-unauthenticated (wrap-admin insights/disapprove))
:transaction-insight-explain (wrap-client-redirect-unauthenticated (wrap-admin insights/explain))
:admin-ezcater-xls (wrap-client-redirect-unauthenticated (wrap-admin ezcater-xls/page))

View File

@@ -14,18 +14,18 @@
[clj-http.client :as http]
[clj-time.coerce :as coerce]
[datomic.api :as dc]
[iol-ion.tx :refer [random-tempid]]))
[iol-ion.tx :refer [random-tempid]]
[hiccup2.core :as hiccup]))
(def pull-expr [:transaction/description-original
:db/id
:transaction/outcome-recommendation
:transaction/amount
{:transaction/client [:client/code]
:transaction/bank-account [:bank-account/code]
:transaction/recommended-vendor [:vendor/name :db/id]
:transaction/recommended-account [:account/name :account/numeric-code :db/id]}
:transaction/bank-account [:bank-account/code]}
:transaction/account-confidence
:transaction/date
])
:transaction/date])
(defn transaction-recommendations [identity selected-client & {:keys [after]}]
(let [visible-clients (visible-clients identity)]
@@ -33,13 +33,13 @@
(dc/qseq {:query '[:find (pull ?t pull-expr)
:in $ ?starting [?c ...] pull-expr
:where
[?t :transaction/outcome-recommendation]
[?t :transaction/client ?c]
[?t :transaction/approval-status :transaction-approval-status/unapproved]
;; [?t :transaction/vendor] ;; should be not
[?t :transaction/date ?d]
[(>= ?d ?starting)]
]
[(>= ?d ?starting)]]
:args [(dc/db conn)
(iol-ion.query/recent-date 120)
(if selected-client
@@ -54,6 +54,20 @@
(#(if after
(drop 1 %)
%))
(map (fn [tx]
(update tx :transaction/outcome-recommendation
(fn [ors]
(map
(fn [[v a c s]]
{:vendor (dc/pull (dc/db conn)
[:vendor/name :db/id]
v)
:account (dc/pull (dc/db conn)
[:account/name :db/id]
a)
:count c
:seen-by-client? s})
ors)))))
(take 50)
(into []))))
@@ -108,97 +122,94 @@
:score score}))
(defn transaction-row [r & {:keys [hide-actions? class last?]}]
(let [simality-list (try (pinecone-similarity-list (:db/id r)) (catch Exception e
[]))]
(when (seq simality-list)
(com/data-grid-row
(cond-> {:class class}
last? (assoc :hx-get (bidi/path-for ssr-routes/only-routes
:transaction-insight-rows
:after (:db/id r))
:hx-trigger "intersect once"
:hx-indicator "#insight-table"
:hx-swap "afterend"))
(com/data-grid-cell {} (:client/code (:transaction/client r)))
(com/data-grid-cell {} (:bank-account/code (:transaction/bank-account r)))
(com/data-grid-cell {} (some-> (:transaction/date r) coerce/to-date-time (atime/unparse-local atime/normal-date)))
(defn transaction-row [r & {:keys [hide-actions? class last?] hs "_"}]
(com/data-grid-row
(cond-> {:class class}
hs (assoc "_" hs)
(com/data-grid-cell {} (str (:transaction/description-original r)))
(com/data-grid-cell {}
(if (> (:transaction/amount r) 0.0)
[:div.tag.is-success.is-light (str "$" (Math/round (:transaction/amount r)))]
[:div.tag.is-danger.is-light (str "$" (Math/round (:transaction/amount r)))]))
(com/data-grid-cell {:style {:width "12em"}}
[:div.flex.gap-2.flex-wrap {:style {:width "12em"}}
(when-let [vendor-name (:vendor-name (first simality-list))]
(com/pill {:color :primary} vendor-name))
(when-let [numeric-code (:numeric-code (first simality-list))]
(com/pill {:color :secondary} numeric-code))
#_(com/pill {:class (cond
(> (:transaction/account-confidence r) 0.90)
"is-success is-light"
(> (:transaction/account-confidence r) 0.80)
"is-info is-light"
last? (assoc :hx-get (bidi/path-for ssr-routes/only-routes
:transaction-insight-rows
:after (:db/id r))
:hx-trigger "intersect once"
:hx-indicator "#insight-table"
:hx-swap "afterend"))
(com/data-grid-cell {} (:client/code (:transaction/client r)))
(com/data-grid-cell {} (:bank-account/code (:transaction/bank-account r)))
(com/data-grid-cell {} (some-> (:transaction/date r) coerce/to-date-time (atime/unparse-local atime/normal-date)))
:else
"is-warning is-light")} (str "%" (Math/round (* 100.0 (:transaction/account-confidence r)))))])
(com/data-grid-right-stack-cell {}
(when-not hide-actions?
[:form.flex.gap-2
[:input {:type :hidden :name "id" :value (:db/id r)}]
(com/icon-button {:hx-post (bidi/path-for ssr-routes/only-routes
:transaction-insight-approve
:transaction-id (:db/id r))
:hx-target "closest tr"
:color :primary-light}
svg/thumbs-up)
(com/icon-button {:hx-delete (bidi/path-for ssr-routes/only-routes
:transaction-insight-disapprove
:transaction-id (:db/id r))
:hx-target "closest tr"
:color :danger-light}
svg/thumbs-down)
(com/icon-button {:hx-get (bidi/path-for ssr-routes/only-routes
:transaction-insight-explain
:transaction-id (:db/id r))
:hx-target "#modal-holder"
:hx-swap "outerHTML"}
svg/question)]))))))
(com/data-grid-cell {} (str (:transaction/description-original r)))
(com/data-grid-cell {}
(if (> (:transaction/amount r) 0.0)
[:div.tag.is-success.is-light (str "$" (Math/round (:transaction/amount r)))]
[:div.tag.is-danger.is-light (str "$" (Math/round (:transaction/amount r)))]))
(defn approve [{:keys [identity session] {:keys [transaction-id]} :route-params}]
(com/data-grid-right-stack-cell {}
(when-not hide-actions?
[:div.flex.gap-2.flex-col {:style {:width "25em"}}
(for [or (sort-by (comp - :count)
(:transaction/outcome-recommendation r))]
[:form {:hx-post (bidi/path-for ssr-routes/only-routes
:transaction-insight-code
:transaction-id (:db/id r))
:hx-target "closest tr"}
(when-let [vendor-id (:db/id (:vendor or))]
[:input {:type :hidden :value vendor-id :name "vendor"}])
(when-let [account-id (:db/id (:account or))]
[:input {:type :hidden :value account-id :name "account"}])
(com/button {:color (if (:seen-by-client? or)
:primary
:secondary)
:style {:position "relative"}}
(:vendor/name (:vendor or))
(when (:vendor/name (:vendor or))
" | ")
(:account/name (:account or))
(com/badge {:color :secondary}
(:count or)))])
(com/icon-button {:hx-get (bidi/path-for ssr-routes/only-routes
:transaction-insight-explain
:transaction-id (:db/id r))
:hx-target "#modal-holder"
:hx-swap "outerHTML"}
svg/question)]))))
(defn code [{:keys [identity session] {:keys [transaction-id]} :route-params {:strs [vendor account]} :form-params}]
(let [approval-details (dc/pull (dc/db conn) [{:transaction/recommended-account [:account/location :db/id]}
:transaction/recommended-vendor
:transaction/amount
:db/id
{:transaction/client [:client/locations]} ]
{:transaction/client [:client/locations]}]
(cond-> transaction-id
string? (Long/parseLong)))
account (dc/pull (dc/db conn) [:account/location :db/id]
(cond-> account
string? (Long/parseLong)))
cents-to-distribute (int (Math/round (Math/abs (* (:transaction/amount approval-details) 100))))
valid-locations (or
(some-> approval-details :transaction/recommended-account :account/location vector)
(->> approval-details
:transaction/client
:client/locations))
(some-> approval-details :transaction/recommended-account :account/location vector)
(->> approval-details
:transaction/client
:client/locations))
updated-transaction [:upsert-transaction {:db/id (:db/id approval-details)
:transaction/approval-status :transaction-approval-status/approved
:transaction/vendor (:transaction/recommended-vendor approval-details)
:transaction/vendor (some-> vendor not-empty (Long/parseLong))
:transaction/accounts (->> valid-locations
(map
(fn [cents location]
{:db/id (random-tempid)
:transaction-account/account (-> approval-details :transaction/recommended-account :db/id)
:transaction-account/amount (* 0.01 cents)
:transaction-account/location location})
(spread-cents cents-to-distribute (count valid-locations))))}]]
(map
(fn [cents location]
{:db/id (random-tempid)
:transaction-account/account (-> account :db/id)
:transaction-account/amount (* 0.01 cents)
:transaction-account/location location})
(spread-cents cents-to-distribute (count valid-locations))))}]]
@(dc/transact conn [updated-transaction])
(html-response (transaction-row
(dc/pull (dc/db conn)
pull-expr
(Long/parseLong transaction-id))
:auto-remove? true
:hide-actions? true
:class "live-added"))))
(dc/pull (dc/db conn)
pull-expr
(Long/parseLong transaction-id))
:auto-remove? true
:hide-actions? true
:class "live-added"))))
(defn disapprove [{:keys [identity session] {:keys [transaction-id]} :route-params}]
(let [transaction-id (cond-> transaction-id string? (Long/parseLong))]
@@ -207,7 +218,8 @@
(dc/pull (dc/db conn) pull-expr transaction-id)
:auto-remove? true
:hide-actions? true
:class "live-removed"))))
:class "live-removed"
"_" (hiccup/raw "init transition opacity to 0 then remove me")))))
(defn explain [{:keys [identity session] {:keys [transaction-id]} :route-params}]
(let [r (dc/pull (dc/db conn)
pull-expr
@@ -272,13 +284,11 @@
:rows (for [r recommendations
:let [last? (= r (last recommendations))]]
(transaction-row r :last? last?))
:headers [(com/data-grid-header {} "Client")
(com/data-grid-header {} "Account")
(com/data-grid-header {} "Date")
:headers [(com/data-grid-header {:style {:width "10em"}} "Client")
(com/data-grid-header {:style {:width "15em"}} "Account")
(com/data-grid-header {:style {:width "8em"}} "Date")
(com/data-grid-header {} "Description")
(com/data-grid-header {} "Amount")
(com/data-grid-header {:style {:width "4em"}} "Vendor / Account")
(com/data-grid-header {})]})))
(com/data-grid-header {:style {:width "8em"}} "Amount")]})))
(defn insight-table [{:keys [session identity]}]
(html-response (insight-table* {:selected-client