Skip to content

Commit c42a78a

Browse files
committed
update db appender
1 parent 993ad25 commit c42a78a

File tree

4 files changed

+48
-22
lines changed

4 files changed

+48
-22
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TYPE event_type AS ENUM ('domain', 'oms', 'system');
2+
3+
ALTER TABLE event ALTER COLUMN channel_id DROP NOT NULL;
4+
ALTER TABLE event ALTER COLUMN channel_name DROP NOT NULL;
5+
ALTER TABLE event ADD COLUMN type event_type NOT NULL DEFAULT 'domain';
6+
ALTER TABLE event ADD COLUMN payload JSONB NOT NULL DEFAULT '{}';

clojure/wheel/src/wheel/infra/database.clj

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[mount.core :as mount]
44
[hikari-cp.core :as hikari]
55
[toucan.db :as db]
6+
[cheshire.core :as json]
67
[toucan.models :as models])
78
(:import [org.flywaydb.core Flyway]
89
[org.postgresql.util PGobject]))
@@ -25,7 +26,12 @@
2526
(fn [value]
2627
(doto (PGobject.)
2728
(.setType pg-enum-type)
28-
(.setValue (name value)))))
29+
(.setValue (if value (name value) value)))))
30+
31+
(defn- to-pg-jsonb [value]
32+
(doto (PGobject.)
33+
(.setType "jsonb")
34+
(.setValue (json/generate-string value))))
2935

3036
(defn- configure-toucan []
3137
(db/set-default-db-connection! {:datasource datasource})
@@ -36,7 +42,13 @@
3642
:out keyword)
3743
(models/add-type! :channel-name
3844
:in (pg-object-fn "channel_name")
39-
:out keyword))
45+
:out keyword)
46+
(models/add-type! :event-type
47+
:in (pg-object-fn "event_type")
48+
:out keyword)
49+
(models/add-type! :jsonb
50+
:in to-pg-jsonb
51+
:out #(json/parse-string (.getValue %) true)))
4052

4153
(mount/defstate toucan
4254
:start (configure-toucan))

clojure/wheel/src/wheel/infra/log_appender/database.clj

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
(defn- append-to-db [{:keys [msg_]}]
55
(let [evnt (read-string (force msg_))]
6-
(when (= :domain (:type evnt))
7-
(event/create! evnt))))
6+
(event/create! evnt)))
87

9-
(def appender {:enabled? true
8+
(def appender {:enabled? true
109
:output-fn :inherit
11-
:async? true
12-
:fn append-to-db})
10+
:async? true
11+
:fn append-to-db})

clojure/wheel/src/wheel/model/event.clj

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,35 @@
99
(models/defmodel Event :event
1010
models/IModel
1111
(types [_]
12-
{:name :keyword
12+
{:name :keyword
1313
:channel-name :channel-name
14-
:level :event-level}))
14+
:level :event-level
15+
:type :event-type
16+
:payload :jsonb}))
1517

1618
(defn- timestamp->offset-date-time [timestamp]
1719
(OffsetDateTime/parse timestamp DateTimeFormatter/ISO_OFFSET_DATE_TIME))
1820

1921
(defn create! [new-event]
20-
{:pre [(s/assert ::event/event new-event)
21-
(s/assert event/domain? new-event)]}
22-
(as-> new-event evt
23-
(update evt :timestamp timestamp->offset-date-time)
24-
(dissoc evt :type)
25-
(db/insert! Event evt)))
22+
{:pre [(s/assert ::event/event new-event)]}
23+
(try
24+
(db/insert! Event
25+
(update new-event :timestamp timestamp->offset-date-time))
26+
(catch Throwable ex
27+
(prn "<<>>>" ex))))
2628

2729
(comment
28-
(create! {:name :ranging/succeeded
29-
:type :domain
30-
:channel-id "UA"
31-
:level :info
32-
:timestamp "2019-10-01T12:30+05:30"
33-
:id (java.util.UUID/randomUUID)
34-
:channel-name :tata-cliq}))
30+
(db/insert! Event {:id #uuid "a3c18c62-2795-4511-859d-58f56b006f91"
31+
:timestamp "2019-10-18T00:48:46.354+05:30"
32+
:name :oms/items-ranged
33+
:level :info
34+
:type :oms
35+
:payload {:message "<EXTNChannelList> <EXTNChannelItemList> <EXTNChannelItem ChannelID=\"UA\" EAN=\"EAN_1\" ItemID=\"SKU1\" RangeFlag=\"Y\"/> </EXTNChannelItemList> </EXTNChannelList>"
36+
:type :oms/items-ranged}})
37+
(create! {:name :ranging/succeeded
38+
:type :domain
39+
:channel-id "UA"
40+
:level :info
41+
:timestamp "2019-10-01T12:30+05:30"
42+
:id (java.util.UUID/randomUUID)
43+
:channel-name :tata-cliq}))

0 commit comments

Comments
 (0)