Skip to content

Commit 511223b

Browse files
authored
Improve utils (#341)
1 parent f812776 commit 511223b

File tree

5 files changed

+25
-16
lines changed

5 files changed

+25
-16
lines changed

utils/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
print-document-plan:
22
clojure -M:dp print-plan $(dp)
33

4+
export-generated-text:
5+
clojure -M:generate $(dp) $(data_file) $(output_path) $(language)
6+
47
export-all-document-plans:
58
clojure -M:dp export-plans $(dir)

utils/src/utils/config.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns utils.config
22
(:require [mount.core :refer [defstate]]))
33

4-
(defn load-config [] {:graphql-url (or (System/getenv "GRAPHQL_URL") "http://localhost:3001/_graphql")})
4+
(defn load-config [] {:api-url (or (System/getenv "API_URL") "http://0.0.0.0:3001")})
55

66
(defstate config :start (load-config))

utils/src/utils/document_plan.clj

+4-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
[clojure.tools.logging :as log]
77
[jsonista.core :as json]
88
[mount.core :as mount]
9-
[org.httpkit.client :as http]
109
[utils.config :refer [config]]
1110
[utils.queries :as queries]))
1211

@@ -32,15 +31,9 @@
3231
(spit (format "%s/%s.json" dir id)
3332
(json/write-value-as-string document-plan write-mapper))))
3433

35-
(defn run-query [url q]
36-
@(http/post url {:headers {"Content-Type" "application/json"}
37-
:body (->> q
38-
:graphql
39-
(json/write-value-as-string))}))
40-
4134
(defn pprint-document-plan [name]
42-
(-> (run-query (:graphql-url config)
43-
(queries/export-document-plan-query {:name name}))
35+
(-> (queries/run-query (format "%s/_graphql" (:api-url config))
36+
(queries/export-document-plan-query {:name name}))
4437
:body
4538
(json/read-value read-mapper)
4639
:data :documentPlan
@@ -49,8 +42,8 @@
4942
(println)))
5043

5144
(defn export-all-document-plans [output-dir]
52-
(let [{:keys [body error]} (run-query (:graphql-url config)
53-
(queries/export-document-plans-query {}))]
45+
(let [{:keys [body error]} (queries/run-query (format "%s/_graphql" (:api-url config))
46+
(queries/export-document-plans-query {}))]
5447
(if error
5548
(log/errorf "Failed with the error: %s" error)
5649
(doseq [dp (-> (json/read-value body read-mapper)

utils/src/utils/generate.clj

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
[clojure.string :as str]
55
[clojure.tools.logging :as log]
66
[jsonista.core :as json]
7-
[org.httpkit.client :as http])
7+
[org.httpkit.client :as http]
8+
[utils.config :refer [config]]
9+
[mount.core :as mount])
810
(:import (java.io File)
911
(java.util UUID)))
1012

@@ -15,7 +17,7 @@
1517
(doall (csv/read-csv reader))))
1618

1719
(defn- post-generate [body]
18-
@(http/request {:url "http://localhost:3001/nlg/_bulk/"
20+
@(http/request {:url (format "%s/nlg/_bulk/" (:api-url config))
1921
:method :post
2022
:headers {"Content-Type" "application/json"}
2123
:body (json/write-value-as-string body)}
@@ -25,7 +27,7 @@
2527
(log/error (.getMessage ^Throwable error))))))
2628

2729
(defn- fetch-results [id]
28-
@(http/request {:url (format "http://localhost:3001/nlg/%s?format=raw" id)}
30+
@(http/request {:url (format "%s/nlg/%s?format=raw" (:api-url config) id)}
2931
(fn [{:keys [status body error]}]
3032
(cond
3133
(not= status 200) (log/errorf "Failed to fetch result `%s` with status %d" id status)
@@ -48,12 +50,14 @@
4850
[document-plan language data]
4951
(log/infof "Generating text for %s data items" (count data))
5052
(let [ids (take (count data) (repeatedly #(str (UUID/randomUUID))))
53+
id->index (zipmap ids (range))
5154
id->data (zipmap ids data)]
5255
(->> {:documentPlanName document-plan
5356
:dataRows id->data
5457
:readerFlagValues {language true}}
5558
(post-generate)
5659
(map get-results)
60+
(sort-by (fn [[id _]] (get id->index id)))
5761
(map-indexed (fn [i [id variants]]
5862
(log/infof "%.2f%% ready" (float (* 100 (/ (inc i) (count ids)))))
5963
[(get id->data id) variants])))))
@@ -82,6 +86,7 @@
8286
(save-data-with-variants output-file results header)))
8387

8488
(defn -main [& [document-plan data-path output-path language]]
89+
(mount/start)
8590
(if (and document-plan data-path output-path)
8691
(data->text document-plan (io/file data-path) (io/file output-path) (if-not (str/blank? language) language "Eng"))
8792
(println "Usage: pass in four parameters: name of the document plan, path to a data file, path to an output file, and language code")))

utils/src/utils/queries.clj

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
(ns utils.queries
22
(:require [graphql-builder.core :as core]
3-
[graphql-builder.parser :refer [defgraphql]]))
3+
[graphql-builder.parser :refer [defgraphql]]
4+
[jsonista.core :as json]
5+
[org.httpkit.client :as http]))
46

57
(defgraphql doc-plan "export-document-plan.graphql")
68
(defgraphql doc-plans "export-document-plans.graphql")
79

10+
(defn run-query [url q]
11+
@(http/post url {:headers {"Content-Type" "application/json"}
12+
:body (->> q
13+
:graphql
14+
(json/write-value-as-string))}))
15+
816
(def export-document-plans-query
917
(-> doc-plans core/query-map :query :export-document-plans))
1018

0 commit comments

Comments
 (0)