From 2da7d0fe27cba94f8f9c1cec490dc6089f84cf5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20L=C3=B6tzsch?= Date: Tue, 15 Mar 2022 03:51:45 +0100 Subject: [PATCH] backend: geocode --- backend/project.clj | 2 + backend/src/beherbergung/config/state.clj | 2 + .../beherbergung/db/import/offer/lifeline.clj | 41 +++++++++++-------- backend/src/beherbergung/db/seed/test.edn | 2 +- backend/src/config.edn | 1 + 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/backend/project.clj b/backend/project.clj index f3496f9..735f8f0 100644 --- a/backend/project.clj +++ b/backend/project.clj @@ -26,6 +26,8 @@ [crypto-random "1.2.1"] [buddy/buddy-sign "3.4.333"] #_[com.draines/postal "2.0.4"] + ;; ajax (geocoding) + [clj-http "3.12.3"] ;; logging [org.clojure/tools.logging "1.2.4"] [org.slf4j/slf4j-api "2.0.0-alpha6"] diff --git a/backend/src/beherbergung/config/state.clj b/backend/src/beherbergung/config/state.clj index d2b93bb..84c0e9f 100644 --- a/backend/src/beherbergung/config/state.clj +++ b/backend/src/beherbergung/config/state.clj @@ -24,6 +24,7 @@ (s/def ::admin-gpg-id string?) (s/def ::import-file (s/nilable string?)) +(s/def ::import-ngo (s/nilable string?)) (s/def ::mail-host string?) (s/def ::mail-user string?) @@ -43,6 +44,7 @@ ::admin-passphrase ::admin-gpg-id ::import-file + ::import-ngo ;::mail-host ::mail-user ::mail-pass ::mail-port ::mail-from ])) diff --git a/backend/src/beherbergung/db/import/offer/lifeline.clj b/backend/src/beherbergung/db/import/offer/lifeline.clj index 3633b64..d537c14 100644 --- a/backend/src/beherbergung/db/import/offer/lifeline.clj +++ b/backend/src/beherbergung/db/import/offer/lifeline.clj @@ -6,17 +6,23 @@ [beherbergung.model.offer-mapping.lifeline] [beherbergung.model.offer :as offer] [beherbergung.model.ngo :as ngo] + [clj-http.client :as client] [clojure.edn])) -(defn geocode - [record] - (assoc record - :place_lon 12.34 - :place_lat 51.34)) +(defn geocode [record] + (let [params {:city (:place_city record)} + result (client/get "https://nominatim.openstreetmap.org/search?format=json&limit=1" + {:accept :json :as :json :query-params params}) + result_ok (when (= 200 (:status result)) + (first (:body result)))] + (println (or (:display_name result_ok) + result)) + (assoc record + :place_lon (:lon result_ok) + :place_lat (:lat result_ok)))) -(defn update-offers [table] - (let [ngo:id "lifeline_beherbergung" ;; TODO - {:keys [tx-fn-put tx-fn-call]} db_ctx] +(defn update-offers [ngo:id table] + (let [{:keys [tx-fn-put tx-fn-call]} db_ctx] (tx-fn-put :update-offer '(fn [ctx eid doc ngo:id] (let [db (xtdb.api/db ctx) @@ -28,19 +34,22 @@ (doseq [record table :let [existingId (when (:id_tmp record) (str "offer_" (:id_tmp record)))]] + ;; TODO: skip geocoding when it was already successfully done at an earlier import (tx-fn-call :update-offer (or existingId (uuid)) (geocode record) ngo:id)))) (defn import! [] - (let [table (if (:import-file env) - (unify (clojure.edn/read-string (slurp (:import-file env))) - beherbergung.model.offer-mapping.lifeline/mapping) - (clojure.edn/read-string (slurp "./data/import/example.edn")) ;; till conflict between `specialist-server.type` and `with-gen` is fixed - #_(gen/sample (s/gen ::offer)))] - (println "Records to be imported:" (count table)) - (update-offers table)) - (println "imported :)")) + (if-not (:import-ngo env) + (println "No IMPORT_NGO defined") + (let [table (if (:import-file env) + (unify (clojure.edn/read-string (slurp (:import-file env))) + beherbergung.model.offer-mapping.lifeline/mapping) + (clojure.edn/read-string (slurp "./data/import/example.edn")) ;; till conflict between `specialist-server.type` and `with-gen` is fixed + #_(gen/sample (s/gen ::offer)))] + (println "Records to be imported:" (count table)) + (update-offers (:import-ngo env) table) + (println "import finished :)")))) (comment (import!) diff --git a/backend/src/beherbergung/db/seed/test.edn b/backend/src/beherbergung/db/seed/test.edn index cd0b8d7..240dd34 100644 --- a/backend/src/beherbergung/db/seed/test.edn +++ b/backend/src/beherbergung/db/seed/test.edn @@ -13,7 +13,7 @@ {:xt/id "login_max_mueller" :xt/spec :beherbergung.model.login/record - :mail "praxis@max.mueller.de" + :mail "max.mueller@warhelp.eu" :password-hash "100$12$argon2id$v13$hq47jacLIYoiNMD9kdyy+w$ISDi+bSSTmsgqu648LQLv7ySU+lG2VGKRfa06HNfjzk$$$" #_"i!A;z\"'^G3Q)w])%83)" } {:xt/id "login_crewing" diff --git a/backend/src/config.edn b/backend/src/config.edn index 4c55c3c..6044ac3 100644 --- a/backend/src/config.edn +++ b/backend/src/config.edn @@ -17,6 +17,7 @@ :db-validate true :import-file nil #_"./data/sample-data/example.edn" + :import-ngo "warhelp" ;:mail-host "" ;:mail-user ""