Best effort glimpse

This commit is contained in:
2024-05-31 16:37:40 -07:00
parent bd3432f540
commit 495751df48
3 changed files with 97 additions and 32 deletions

View File

@@ -1,14 +1,18 @@
(ns auto-ap.parse (ns auto-ap.parse
(:require (:require [amazonica.aws.lambda :as lambda]
[auto-ap.parse.csv :as csv] [amazonica.aws.s3 :as s3]
[auto-ap.parse.excel :as excel] [auto-ap.logging :as alog]
[auto-ap.parse.templates :as t] [auto-ap.parse.csv :as csv]
[auto-ap.parse.util :as u] [auto-ap.parse.excel :as excel]
[clj-fuzzy.metrics :as m] [auto-ap.parse.templates :as t]
[clojure.java.shell :as sh] [auto-ap.parse.util :as u]
[clojure.set :as set] [auto-ap.ssr.vendor :as vendors]
[clojure.string :as str] [clj-fuzzy.metrics :as m]
[auto-ap.logging :as alog])) [clojure.data.json :as json]
[clojure.java.io :as io]
[clojure.java.shell :as sh]
[clojure.set :as set]
[clojure.string :as str]))
(def last-text (atom nil)) (def last-text (atom nil))
@@ -57,12 +61,44 @@
(defmulti parse-file (fn [_ filename] (.toLowerCase (last (str/split filename #"\." ))))) (defmulti parse-file (fn [_ filename] (.toLowerCase (last (str/split filename #"\." )))))
(defn invoke-glimpse2 [f]
(doto
(-> (lambda/invoke {:function-name "glimpse2" :payload
(json/write-str
(alog/peek ::x {"url" (str "https://" "data.prod.app.integreatconsult.com" "/" f ) }))})
:payload
slurp
json/read-str)
println))
(defn glimpse2 [file]
(try
(let [tmp-key (str "glimpse2/import/" (java.util.UUID/randomUUID) ".pdf")
_ (with-open [f (io/input-stream file)]
(s3/put-object {:bucket-name "data.prod.app.integreatconsult.com"
:key tmp-key
:input-stream f}))
g (invoke-glimpse2 tmp-key) ]
[ {:date (u/parse-value :clj-time "yyyy-MM-dd" (str/trim (get g "date")))
:customer-identifier (get g "customer_identifier")
:account-number (not-empty (get g "account_number"))
:vendor-code (-> (vendors/best-match (get g "vendor_identifier") )
(get "label"))
:total (get g "total")
:invoice-number (get g "invoice_number")}]
)
(catch Exception e
(alog/warn ::glimpse2-not-work :error e)
nil)))
(defmethod parse-file (defmethod parse-file
"pdf" "pdf"
[file _] [file _]
(-> (sh/sh "pdftotext" "-layout" file "-") (or
:out (-> (sh/sh "pdftotext" "-layout" file "-")
parse)) :out
parse)
(alog/peek ::glimpse2-result (glimpse2 file))))
(defmethod parse-file (defmethod parse-file
"csv" "csv"

View File

@@ -337,11 +337,16 @@
(com/content-card {} (com/content-card {}
[:div.px-4.py-3.space-y-4 [:div.px-4.py-3.space-y-4
{:x-data (hx/json {"a" false})
"@started" "a=true"
"@completed" "a=false"}
#_[:div.bg-red-50.p-8 {:x-show "a"} "HELLO THERE"]
[:div.flex.justify-between.items-center [:h1.text-2xl.mb-3.font-bold "Import new invoices"] [:div.flex.justify-between.items-center [:h1.text-2xl.mb-3.font-bold "Import new invoices"]
[:div.flex.gap-2.items-baseline "Trouble with the new upload experience?" [:div.flex.gap-2.items-baseline "Trouble with the new upload experience?"
[:a {:href (bidi/path-for client-routes/routes :import-invoices )} [:a {:href (bidi/path-for client-routes/routes :import-invoices)}
(com/pill {:color :secondary} (com/pill {:color :secondary}
"Go back to previous version")]]] "Go back to previous version")]]]
[:div#page-notification.notification.block {:style {:display "none"}}] [:div#page-notification.notification.block {:style {:display "none"}}]
@@ -385,18 +390,29 @@
:url (bidi/path-for ssr-routes/only-routes :vendor-search) :url (bidi/path-for ssr-routes/only-routes :vendor-search)
:value (fc/field-value) :value (fc/field-value)
:content-fn (fn [c] (pull-attr (dc/db conn) :vendor/name c))})]))]) :content-fn (fn [c] (pull-attr (dc/db conn) :vendor/name c))})]))])
[:div.bg-blue-100.border-2.border-dashed.rounded-lg.border-blue-300.p-4.w-full.text-center.cursor-pointer.h-64.flex.items-center.justify-center.text-blue-700.text-lg "Drop files to upload here"]] [:div {":class" "a ? 'htmx-request' : ''"}
[:script [:div.bg-blue-100.border-2.border-dashed.rounded-lg.border-blue-300.p-4.w-full.text-center.cursor-pointer.h-64.flex.items-center.justify-center.text-blue-700.text-lg
(hiccup/raw [:div.htmx-indicator.flex.items-center
"ezcater_dropzone = new Dropzone (\"#upload\", { (svg/spinner {:class "inline w-4 h-4 text-blue-700"})
success: function (file, response) { [:div.ml-3 "Loading..."]]
document.getElementById(\"page-notification\").innerHTML = response; [:div.htmx-indicator-hidden "Drop files to upload here"]]
document.getElementById(\"page-notification\").style[\"display\"] = \"block\"; [:script
document.body.dispatchEvent(new Event('invalidated')); (hiccup/raw
}, "ezcater_dropzone = new Dropzone (\"#upload\", {
acceptedFiles: '.xls,.xlsx,.pdf,.csv', init: function() {
disablePreviews: true this.on('addedfile', () => { document.getElementById('upload').dispatchEvent(new Event('started', {bubbles:true})) });
});")]])) },
success: function (file, response) {
document.getElementById(\"page-notification\").innerHTML = response;
document.getElementById(\"page-notification\").style[\"display\"] = \"block\";
document.getElementById('upload').dispatchEvent(new Event('completed', {bubbles:true}))
document.body.dispatchEvent(new Event('invalidated'));
},
acceptedFiles: '.xls,.xlsx,.pdf,.csv',
disablePreviews: true
})
")]]]]))
:fetch-page fetch-page :fetch-page fetch-page
:oob-render :oob-render

View File

@@ -7,6 +7,19 @@
[datomic.api :as dc] [datomic.api :as dc]
[ring.middleware.json :refer [wrap-json-response]])) [ring.middleware.json :refer [wrap-json-response]]))
(defn best-match [q]
(let [name-like-ids (when (not-empty q)
(map (comp #(Long/parseLong %) :id)
(solr/query solr/impl "vendors"
{"query" (cond-> (format "name:(%s*)" (str/upper-case (solr/escape q)))
true (str " hidden:false"))
"fields" "id"
"limit" 300})))
valid-clients (for [n name-like-ids]
{"value" n "label" (pull-attr (dc/db conn) :vendor/name n)})]
(first valid-clients)))
(defn search [{:keys [clients query-params identity]}] (defn search [{:keys [clients query-params identity]}]
(let [name-like-ids (when (not-empty (get query-params "q")) (let [name-like-ids (when (not-empty (get query-params "q"))