Skip to content
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

threadmap #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/jobs/equities.clj
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,11 @@
:date
util/joda-date->date-str)}
query-params)
data (->> (concat alpha-vantage tiingo morningstar quandl)
(map #(api/get-data % query-params*))
data (->> (concat #_alpha-vantage tiingo morningstar quandl)
util/tmap ;api/get-data query-params*))
#_(map #(future (api/get-data % query-params*)))
flatten)]
(execute! cxn data)))
(println data)
#_(execute! cxn data)))

(util/notify-healthchecks-io (-> :healthchecks-io-api-key env)))
#_(util/notify-healthchecks-io (-> :healthchecks-io-api-key env)))
6 changes: 5 additions & 1 deletion src/markets_etl/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
(query-alpha-vantage-api! ticker {}))
([url ticker paramz]
{:pre [(every? true? (allowed? paramz))]}
(Thread/sleep 5500)
(log/info "query-alpha-vantage-api! called")
(Thread/sleep 3500)
(let [params (dissoc paramz :limit)
response (try
(http/get url)
Expand Down Expand Up @@ -107,6 +108,7 @@
(query-tiingo! ticker {}))
([ticker paramz]
{:pre [(every? true? (allowed? paramz))]}
(log/info "query-tiingo! called")
(let [params (dissoc paramz :limit)
url (str (:protocol tiingo-api)
(:url tiingo-api)
Expand Down Expand Up @@ -170,6 +172,7 @@
(query-morningstar! ticker {}))
([ticker paramz]
{:pre [(every? true? (allowed? paramz))]}
(log/info "query-morningstar! called")
(let [params (dissoc paramz :limit)
url (str (:protocol morningstar-api)
(:url morningstar-api)
Expand Down Expand Up @@ -204,6 +207,7 @@
(query-quandl! dataset ticker {}))
([dataset ticker paramz]
{:pre [(every? true? (allowed? paramz))]}
(log/info "query-quandl! called")
(let [url (str (:protocol quandl-api)
(:url quandl-api)
(str dataset "/")
Expand Down
20 changes: 19 additions & 1 deletion src/markets_etl/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
[clj-time.core :as time]
[clj-time.format :as formatter]
[clojure.pprint :as pprint]
[clojure.string :as string]))
[clojure.string :as string]
[markets-etl.api :as api])
(:import (java.util.concurrent Executors)))

; -- dev -----------------------------------------------
(defn print-it [coll]
Expand Down Expand Up @@ -83,6 +85,22 @@
(map string/join)
(string/join "\n")))

; -- parallelization -----------------------------------
(defn tiled-pmap [grain-size f xs]
(->> xs
(partition-all grain-size)
(pmap (fn [pgroup] (doall (map f pgroup)))) (apply concat)))

(defn tmap [data] ; threadmap
(let [pool (Executors/newFixedThreadPool 3)
query-params {:limit 500
:start_date util/last-week
:end_date util/now}
tasks (map #(api/get-data % query-params) data)]
(doseq [future (.invokeAll pool tasks)]
(println (.get future)))
(.shutdown pool)))

; -- alerts --------------------------------------------
(defn notify-healthchecks-io [api-key]
(http/get (str "https://hchk.io/"
Expand Down