-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new internal endpoint for updating public csv data into S3 #645
base: main
Are you sure you want to change the base?
Changes from all commits
3477882
d92a8fd
0349692
f866526
6cbacd7
56c5602
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
(ns solita.etp.api.public-csv | ||
(:require [ring.util.response :as r] | ||
[solita.etp.security :as security] | ||
[solita.etp.service.concurrent :as concurrent] | ||
[solita.etp.service.csv-to-s3 :as csv-to-s3] | ||
[solita.etp.service.kayttaja :as kayttaja-service])) | ||
|
||
|
||
(def internal-routes | ||
[["/public-csv" | ||
["/update" | ||
{:post {:summary "Päivitä julkiset CSV-tiedostot S3:ssa." | ||
:middleware [[security/wrap-db-application-name]] | ||
:responses {200 {:body nil}} | ||
:handler (fn [{:keys [db whoami aws-s3-client]}] | ||
(r/response | ||
(concurrent/run-background | ||
#(csv-to-s3/update-public-csv-in-s3! | ||
db | ||
whoami | ||
aws-s3-client | ||
{:where nil}) | ||
"Aineistot update failed")))}}]]]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
(ns solita.etp.service.csv-to-s3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nimeäminen on vaikeata There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Voisi olla myös joku There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. En pidä tuosta ehdotuksestani enää yhtään. Joku |
||
(:require [clojure.tools.logging :as log] | ||
[solita.etp.service.energiatodistus-csv :as energiatodistus-csv] | ||
[solita.etp.service.file :as file] | ||
[solita.etp.service.aineisto :as aineisto-service]) | ||
(:import [java.nio ByteBuffer] | ||
[java.nio.charset StandardCharsets])) | ||
|
||
(def ^:private buffer-size (* 8 1024 1024)) ; 8MB | ||
(def ^:private upload-threshold (* 5 1024 1024)) ; 5MB | ||
|
||
(defn- create-buffer [] | ||
(ByteBuffer/allocate buffer-size)) | ||
|
||
(def public-csv-key | ||
"/api/csv/public/energiatodistukset.csv") | ||
|
||
(defn aineisto-key [aineisto-id] | ||
(str "/api/signed/aineistot/" aineisto-id "/energiatodistukset.csv")) | ||
|
||
(defn- create-upload-parts-fn [csv-reducible-query] | ||
(let [current-part (create-buffer)] | ||
(fn [upload-part-fn] | ||
(csv-reducible-query | ||
(fn [^String row] | ||
(let [row-bytes (.getBytes row StandardCharsets/UTF_8)] | ||
(.put current-part row-bytes) | ||
(when (> (.position current-part) upload-threshold) | ||
(upload-part-fn (aineisto-service/extract-byte-array-and-reset! current-part)))))) | ||
;; Upload any remaining data | ||
(when (not= 0 (.position current-part)) | ||
(upload-part-fn (aineisto-service/extract-byte-array-and-reset! current-part)))))) | ||
|
||
(defn- process-csv-to-s3! [aws-s3-client key csv-reducible-query log-start log-end] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ehkä tuo logitusten parametrisointi tässä kontekstissa oli vähän overkill mutta tulipahan tehtyä There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Varmaan ihan hyvä, että tulee ainakin muistettua logittaa, kun käyttää tätä funktiota. |
||
(log/info log-start) | ||
(let [upload-parts-fn (create-upload-parts-fn csv-reducible-query)] | ||
(file/upsert-file-in-parts aws-s3-client key upload-parts-fn) | ||
(log/info log-end))) | ||
|
||
(defn update-aineisto-in-s3! [db whoami aws-s3-client aineisto-id] | ||
(let [csv-query (aineisto-service/aineisto-reducible-query db whoami aineisto-id) | ||
key (aineisto-key aineisto-id) | ||
start-msg (str "Starting updating of aineisto (id: " aineisto-id ").") | ||
end-msg (str "Updating of aineisto (id: " aineisto-id ") finished.")] | ||
(process-csv-to-s3! aws-s3-client key csv-query start-msg end-msg))) | ||
|
||
(defn update-public-csv-in-s3! [db whoami aws-s3-client query] | ||
(let [csv-query (energiatodistus-csv/energiatodistukset-public-csv db whoami query)] | ||
(process-csv-to-s3! | ||
aws-s3-client | ||
public-csv-key | ||
csv-query | ||
"Starting updating of public energiatodistus." | ||
"Updating of public energiatodistus finished."))) | ||
|
||
(defn update-aineistot-in-s3! [db whoami aws-s3-client] | ||
(doseq [id [1 2 3]] | ||
(update-aineisto-in-s3! db whoami aws-s3-client id))) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
(ns solita.etp.service.csv-to-s3-test | ||
(:require [solita.etp.service.csv-to-s3 :as csv-to-s3] | ||
[solita.etp.service.file :as file] | ||
[clojure.test :as t] | ||
[solita.etp.test-system :as ts])) | ||
|
||
(t/use-fixtures :each ts/fixture) | ||
|
||
(t/deftest test-public-csv-to-s3 | ||
(t/testing "Public csv doesn't exist before generating" | ||
(t/is (false? (file/file-exists? ts/*aws-s3-client* csv-to-s3/public-csv-key)))) | ||
|
||
(t/testing "Public csv exists after generating" | ||
(csv-to-s3/update-public-csv-in-s3! ts/*db* {:id -5 :rooli 2} ts/*aws-s3-client* {:where nil}) | ||
(t/is (true? (file/file-exists? ts/*aws-s3-client* csv-to-s3/public-csv-key))))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tämäkin olisi ollut hyvä silloin aikoinaan varmaan tehdä omaksi funktiokseen ja käyttää myös testeissä sitä kautta. Refaktoroi toki, jos huvittaa.