-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add new internal endpoint for updating public csv data into S3
- Loading branch information
Showing
8 changed files
with
307 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
etp-core/etp-backend/src/main/clj/solita/etp/api/public_csv.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(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])) | ||
|
||
|
||
(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}) | ||
"Public csv update failed")))}}]]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
etp-core/etp-backend/src/main/clj/solita/etp/service/csv_to_s3.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
(ns solita.etp.service.csv-to-s3 | ||
(: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/public/energiatodistukset/csv/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] | ||
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.