Skip to content
This repository was archived by the owner on Apr 17, 2018. It is now read-only.

Commit 9cf0dec

Browse files
committed
finish augment
1 parent a15ace6 commit 9cf0dec

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

functions/src/app/augment/index.cljs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
[shared.protocols.actionable :as ac])
99
(:require-macros [cljs.core.async.macros :refer [go]]))
1010

11-
(def adapters {:db {:table-names {:bookmarks (.. js/process -env -bookmarksTable)
12-
:resources (.. js/process -env -resourcesTable)
13-
:courses (.. js/process -env -coursesTable)}}})
11+
(def adapters {:db {:table-names {:bookmarks (.. js/process -env -bookmarksTable)
12+
:resources (.. js/process -env -resourcesTable)
13+
:courses (.. js/process -env -coursesTable)}}
14+
:stream {:stream-names {:courses (.. js/process -env -coursesStream)}}})
1415

1516
(defn augment [& args]
1617
(go
1718
(let [{:keys [event] :as service} (apply service/create specs mappings adapters args)
1819
{:keys [added] :as payload} (cv/to-db-events event)
1920
{:keys [found errors]} (async/<! (qa/fetch service added))
20-
res (async/<! (ac/perform service [:transform found]))]
21+
payloads (async/<! (ac/perform service [:transform found]))
22+
ops-chans (async/merge (map #(ac/perform service [:put %])
23+
(vals payloads)))
24+
res (async/<! (async/into [] ops-chans))]
2125
(service/done service res))))

functions/src/app/augment/mappings.cljs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@
55
[shared.protocols.queryable :as qa]
66
[shared.protocols.loggable :as log]
77
[cljs.core.async :as async]
8-
[clojure.string :as str])
8+
[clojure.string :as str]
9+
[shared.protocols.convertible :as cv])
910
(:require-macros [cljs.core.async.macros :refer [go]]))
1011

1112
(defn mappings []
1213

13-
(defn to-course-query [{:keys [offcourse-id] :as bm}]
14-
(let [[repo curator course-id revision checkpoint-id] (str/split offcourse-id "::")]
15-
{:course-id (str repo "::" curator "::" course-id)
16-
:revision (int revision)}))
17-
1814
(defmethod fetch :resources [{:keys [db]} bookmarks]
1915
(go
20-
(let [courses-query (map to-course-query bookmarks)
16+
(let [courses-query (->> bookmarks
17+
(map :offcourse-id)
18+
(map cv/to-query))
2119
courses-res (async/<! (qa/fetch db courses-query))
2220
courses (:found courses-res)
2321
resources-res (async/<! (qa/fetch db bookmarks))
@@ -31,7 +29,9 @@
3129
(go
3230
(let [bookmarks-res (async/<! (qa/fetch db resources))
3331
bookmarks (:found bookmarks-res)
34-
courses-query (map to-course-query bookmarks)
32+
courses-query (->> bookmarks
33+
(map :offcourse-id)
34+
(map cv/to-query))
3535
courses-res (async/<! (qa/fetch db courses-query))
3636
courses (:found courses-res)
3737
errors (mapcat :errors [courses-res bookmarks-res])]
@@ -44,13 +44,13 @@
4444
tags-data (map #(identity [(:tags %1) (:resource-url %1)]) resources)
4545
;; move impl logic to course model
4646
courses (map #(impl/augment-course %1 tags-data) courses)]
47-
(go courses)))
47+
(go {:courses courses})))
4848

4949
(defmethod perform [:put :nothing] [_ _]
5050
(go {:error :no-payload}))
5151

5252
(defmethod perform [:put :errors] [_ [_ errors]]
5353
(go errors))
5454

55-
(defmethod perform :default [{:keys [bucket]} action]
56-
(ac/perform bucket action)))
55+
(defmethod perform :default [{:keys [stream]} action]
56+
(ac/perform stream action)))

functions/src/app/augment/specs.cljs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@
1010

1111
(defn specs []
1212

13-
(defmethod action-spec :transform [_]
14-
(spec/tuple :offcourse/actions (spec/or :motherload ::motherload)))
15-
1613
(spec/def :offcourse/query (spec/or :bookmarks (spec/coll-of :offcourse/resource)
1714
:resources (spec/coll-of :offcourse/bookmark)
1815
:courses (spec/coll-of :query/course-by-id)
19-
:errors (spec/coll-of :offcourse/error))))
16+
:errors (spec/coll-of :offcourse/error)))
17+
18+
(defmethod action-spec :transform [_]
19+
(spec/tuple :offcourse/actions (spec/or :motherload ::motherload)))
20+
21+
(spec/def :offcourse/payload (spec/or :courses ::courses
22+
:error (spec/coll-of :offcourse/error)
23+
:nothing nil?))
24+
25+
(defmethod action-spec :put [_]
26+
(spec/tuple :offcourse/actions :offcourse/payload)))

serverless.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ functions:
4747
augment:
4848
handler: functions/index.augment
4949
environment:
50+
coursesStream: "courses-${opt:stage}"
5051
bookmarksTable: "bookmarks-${opt:stage}"
5152
resourcesTable: "resources-${opt:stage}"
5253
coursesTable: "courses-${opt:stage}"

0 commit comments

Comments
 (0)