Skip to content

Commit 54924f2

Browse files
authored
Merge pull request #55 from clojureverse/fix-test-suite
Fix test suite
2 parents de94f1d + d89873b commit 54924f2

File tree

11 files changed

+132
-63
lines changed

11 files changed

+132
-63
lines changed

src/clojurians_log/application.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[system.components.endpoint :refer [new-endpoint]]
55
[clojurians-log.components.server-info :refer [server-info]]
66
[clojurians-log.components.datomic-schema :refer [new-datomic-schema]]
7+
[clojurians-log.components.indexer :refer [new-indexer]]
78
[system.components.handler :refer [new-handler]]
89
[system.components.middleware :refer [new-middleware]]
910
[system.components.http-kit :refer [new-web-server]]
@@ -38,7 +39,9 @@
3839
:server-info (server-info (:port http))
3940
:datomic (new-datomic-db (:uri datomic))
4041
:datomic-schema (-> (new-datomic-schema)
41-
(component/using [:datomic]))))
42+
(component/using [:datomic]))
43+
:indexer (-> (new-indexer)
44+
(component/using [:datomic]))))
4245

4346
(defn -main [& [config-file]]
4447
(let [conf (if (and config-file (.exists (io/file config-file)))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(ns clojurians-log.components.indexer
2+
(:require [com.stuartsierra.component :as component]
3+
[datomic.api :as d]
4+
[clojurians-log.db.queries :as queries]))
5+
6+
(defrecord Indexer [datomic]
7+
component/Lifecycle
8+
(start [this]
9+
(let [thread
10+
(Thread.
11+
(fn []
12+
(queries/build-indexes! (d/db (:conn datomic)))
13+
(Thread/sleep 3600)
14+
(recur)))]
15+
(.start thread)
16+
(assoc this :thread thread)))
17+
18+
(stop [this]
19+
(when-let [thread (:thread this)]
20+
(.interrupt thread))
21+
(dissoc this :thread)))
22+
23+
(defn new-indexer []
24+
(map->Indexer {}))

src/clojurians_log/components/server_info.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
component)
99
(stop [component]
1010
component))
11+
1112
(defn server-info [http-port]
1213
(->ServerInfoPrinter http-port))

src/clojurians_log/db/queries.clj

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,47 @@
22
(:require [datomic.api :as d]
33
[clojurians-log.time-util :as time-util]))
44

5+
(defonce !indexes (atom {}))
6+
7+
(defn channels-dates-msgcounts [db]
8+
(d/q '[:find ?slack-id ?chan-name ?day (count ?msg)
9+
:in $
10+
:where
11+
[?chan :channel/slack-id ?slack-id]
12+
[?chan :channel/name ?chan-name]
13+
[?msg :message/channel ?chan]
14+
[?msg :message/day ?day]]
15+
db))
16+
17+
(defn build-indexes [db]
18+
(let [cdm (channels-dates-msgcounts db)]
19+
(reduce
20+
(fn [acc [slack-id chan-name day msgcount]]
21+
(-> acc
22+
(assoc-in [:chan-day-cnt slack-id day] msgcount)
23+
(assoc-in [:day-chan-cnt day slack-id] msgcount)
24+
(assoc-in [:chan-id->name slack-id] chan-name)
25+
(assoc-in [:chan-name->id chan-name] slack-id)))
26+
{}
27+
cdm)))
28+
29+
(defn build-indexes! [db]
30+
(reset! !indexes (build-indexes db)))
31+
532
(defn channel-list
633
([db]
7-
(->> (d/q '[:find [(pull ?chan [:channel/slack-id :channel/name]) ...]
8-
:in $
9-
:where
10-
[?msg :message/channel ?chan]]
11-
db)
34+
(->> (map (fn [[id name]]
35+
#:channel{:slack-id id
36+
:name name})
37+
(:chan-id->name @!indexes))
1238
(sort-by :channel/name)))
1339
([db day]
14-
(->> (d/q '[:find (pull ?chan [:channel/slack-id :channel/name]) (count ?msg)
15-
:in $ ?day
16-
:where
17-
[?msg :message/day ?day]
18-
[?msg :message/channel ?chan]]
19-
db
20-
day)
21-
(map #(assoc (first %) :channel/message-count (last %))))))
40+
(let [{:keys [day-chan-cnt chan-id->name]} @!indexes]
41+
(->> (for [[ch-id cnt] (get day-chan-cnt day)]
42+
#:channel{:slack-id ch-id
43+
:name (chan-id->name ch-id)
44+
:message-count cnt})
45+
(sort-by :channel/name)))))
2246

2347
(defn- assoc-inst [message]
2448
(assoc message :message/inst (time-util/ts->inst (:message/ts message))))
@@ -75,15 +99,11 @@
7599
(compare y x))
76100

77101
(defn channel-days [db chan-name]
78-
(->> (d/q '[:find ?day (count ?msg)
79-
:in $ ?chan-name
80-
:where
81-
[?chan :channel/name ?chan-name]
82-
[?msg :message/channel ?chan]
83-
[?msg :message/day ?day]]
84-
db
85-
chan-name)
86-
(sort-by first reverse-compare)))
102+
(let [{:keys [chan-day-cnt chan-name->id]} @!indexes]
103+
(some->> chan-name
104+
chan-name->id
105+
chan-day-cnt
106+
(sort-by first reverse-compare))))
87107

88108
(defn channel [db name]
89109
(d/q '[:find (pull ?chan [*]) .
@@ -94,22 +114,14 @@
94114
name))
95115

96116
(defn user-names
97-
[db names]
117+
[db ids]
98118
(d/q '[:find ?id ?username
99119
:in $ [?id ...]
100120
:where
101121
[?user :user/slack-id ?id]
102122
[?user :user/name ?username]]
103123
db
104-
names))
105-
106-
(defn message-by-ts [db ts]
107-
(d/q '[:find (pull ?msg [*]) .
108-
:in $ ?ts
109-
:where
110-
[?msg :message/ts ?ts]]
111-
db
112-
ts))
124+
ids))
113125

114126
(defn thread-messages
115127
"Retrieve all child messages for the given parent threads"

src/clojurians_log/db/schema.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
#:db{:ident :message/thread-ts
3131
:valueType :db.type/string
3232
:cardinality :db.cardinality/one
33-
:doc "Thread parent message timestamp (seconds since epoch up to 6 decimals). Stored as string because it is used by slack as a kind of identifier. Unique per channel."}
33+
:doc "Thread parent message timestamp (seconds since epoch up to 6 decimals). Stored as string because it is used by slack as a kind of identifier. Unique per channel."
34+
:index true}
3435
#:db{:ident :message/thread-inst
3536
:valueType :db.type/instant
3637
:cardinality :db.cardinality/one

src/clojurians_log/repl.clj

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,45 @@
173173
(use 'clojurians-log.repl)
174174
(load-slack-data!)
175175
(def result (load-files! (log-files)))
176-
@(second result)
177176
result
178177

178+
(def result (load-files! (drop 1508 (log-files))))
179+
180+
(while (not (realized? (second result)))
181+
(println (java.util.Date.) "\t" @(first result))
182+
(Thread/sleep 5000))
183+
179184
;; old way (slower)
180185
(run! load-log-file! (log-files))
181186

182187
;; incremental
183-
(load-from "2016-08-04")
188+
(load-from "2019-08-23")
184189

185190

186191

187192
(load-demo-data! "/home/arne/github/clojurians-log-demo-data")
188-
193+
(build-indexes! (d/db (conn)))
189194

190195
(do
191196
(write-edn "users.edn" (slack/users))
192197
(write-edn "channels.edn" (slack/channels)))
193198

199+
(time
200+
(do
201+
(time (clojurians-log.db.queries/channel-day-messages db "clojurescript" "2018-02-04"))
202+
(time (clojurians-log.db.queries/thread-messages db '("1517722327.000023" "1517722363.000043" "1517722613.000012" "1517724278.000043" "1517724340.000044" "1517724770.000024" "1517724836.000023" "1517725105.000054")))
203+
(time (clojurians-log.db.queries/channel db "clojurescript"))
204+
(time (clojurians-log.db.queries/channel-list db "2018-02-04"))
205+
(time (clojurians-log.db.queries/user-names db #{"U2TUBBPNU"}))
206+
(time (clojurians-log.db.queries/channel-days db "clojurescript"))
207+
208+
nil))
209+
210+
"Elapsed time: 18.166254 msecs"
211+
"Elapsed time: 631.458841 msecs"
212+
"Elapsed time: 1.568807 msecs"
213+
"Elapsed time: 16.425878 msecs"
214+
"Elapsed time: 1.126005 msecs"
215+
"Elapsed time: 1535.355001 msecs"
216+
"Elapsed time: 2205.20762 msecs"
194217
)

src/clojurians_log/routes.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
(-> request
9898
context
9999
(assoc :data/title (str "Clojurians Slack Log | " channel)
100-
:data/days (queries/channel-days db channel)
100+
:data/channel-days (queries/channel-days db channel)
101101
:data/channel-name channel)
102102
views/channel-page
103103
response/render)))

src/clojurians_log/views.clj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@
9595
`offset` positions away. Returns nil if the applying the offset goes out of
9696
bounds."
9797
[channel-days today offset]
98-
9998
(as-> channel-days $
10099
(map vector (range) $)
101-
(some (fn [[index [a-date msg-count]]] (when (and (= a-date today)
102-
(not (zero? msg-count)))
103-
index)) $)
100+
(some (fn [[index [a-date]]]
101+
(when (= today a-date)
102+
index))
103+
$)
104104
(+ $ offset)
105105
(nth channel-days $ nil)
106106
(first $)))
@@ -192,14 +192,14 @@
192192
[:div.listings_direct-messages]]
193193
(message-history context)]]])
194194

195-
(defn- channel-page-html [{:data/keys [days channel-name] :as context}]
195+
(defn- channel-page-html [{:data/keys [channel-days channel-name] :as context}]
196196
[:html
197197
(page-head context)
198198
[:body
199199
[:div.main
200200
[:h1 "Channel: #" channel-name]
201201
[:ul
202-
(for [[day cnt] days]
202+
(for [[day cnt] channel-days]
203203
[:li [:a {:href (bidi/path-for routes
204204
:log
205205
:channel channel-name

test/clojurians_log/db/queries_test.clj

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
(deftest channel-list-test
1010
(testing "without arg - return all channels"
1111
(let [[conn db] (test-db)]
12-
(is (= (channel-list db)
13-
[#:channel{:slack-id "C03S1KBA2", :name "clojure"}
14-
#:channel{:slack-id "C03S1L9DN", :name "clojurescript"}]))))
12+
(is (= [#:channel{:slack-id "C03S1KBA2", :name "clojure"}
13+
#:channel{:slack-id "C03S1L9DN", :name "clojurescript"}]
14+
(channel-list db)))))
1515

1616
(testing "returns only channels with messages on the given day, includes message-count"
1717
(let [[conn db] (test-db "quiet-channels")]
18-
(is (= (channel-list db "2018-02-03")
19-
[#:channel{:slack-id "C7YF1SBT3", :name "reitit", :message-count 3}
20-
#:channel{:slack-id "C05006WDW", :name "jobs", :message-count 1}])))))
18+
(is (= [#:channel{:slack-id "C05006WDW", :name "jobs", :message-count 1}
19+
#:channel{:slack-id "C7YF1SBT3", :name "reitit", :message-count 3}]
20+
(channel-list db "2018-02-03"))))))
2121

2222
(deftest threaded-messages-test
2323
(let [[conn db] (test-db "threaded-messages")
@@ -50,15 +50,15 @@
5050
;; The following tests should hold given the contents of threaded-messages.edn
5151
;; To see how the data was generated, see repl/test_data.clj.
5252
(testing "returns messages for the expected threads"
53-
(is (= parents-ts
54-
#{"1517995093.000487" "1518040988.000079"}))
53+
(is (= #{"1517995093.000487" "1518040988.000079"}
54+
parents-ts))
5555

5656
;; Does not contain message for threads not starting on the target date
5757
(is (not (contains? parents-ts "1517924158.000577"))))
5858

5959
(testing "returns the expected messages"
60-
(is (= thread-messages-ts
61-
#{"1518008583.000370" "1518058291.000073" "1518095027.000404" "1518095379.000012"}))
60+
(is (= #{"1518008583.000370" "1518058291.000073" "1518095027.000404" "1518095379.000012"}
61+
thread-messages-ts))
6262

6363
;; Should exclude non-threaded message from the same day
6464
(is (not (contains? thread-messages-ts "1518034517.000637")))

test/clojurians_log/test_helper.clj

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns clojurians-log.test-helper
22
(:require [clojure.java.io :as io]
33
[clojurians-log.db.schema :as schema]
4+
[clojurians-log.db.queries :as queries]
45
[clojurians-log.xml2hiccup :as x2h]
56
[datomic.api :as d]
67
[net.cgrand.enlive-html :as enlive]
@@ -35,7 +36,9 @@
3536
([fixture-name]
3637
(let [conn (test-conn)]
3738
(transact-txs conn (slurp-fixture fixture-name))
38-
[conn (d/db conn)])))
39+
(let [db (d/db conn)]
40+
(queries/build-indexes! db)
41+
[conn (d/db conn)]))))
3942

4043
(defn html->hiccup [html]
4144
(-> html
@@ -64,8 +67,10 @@
6467
(assoc-in [:datomic :uri] (str "datomic:mem:" (gensym "test_db")))
6568
(dissoc :http) ;; don't actually start a http server
6669
(dissoc :server-info) ;; silence http server startup message
67-
component/start-system)) ;; start the whole system
70+
component/start-system))
6871

6972
(defn system-load-fixture! [system fixture-name]
7073
(transact-txs (system-db-conn system)
71-
(slurp-fixture fixture-name)))
74+
(slurp-fixture fixture-name))
75+
(queries/build-indexes! (system-db system))
76+
nil)

0 commit comments

Comments
 (0)