|
2 | 2 | (:require [datomic.api :as d] |
3 | 3 | [clojurians-log.time-util :as time-util])) |
4 | 4 |
|
| 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 | + |
5 | 32 | (defn channel-list |
6 | 33 | ([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)) |
12 | 38 | (sort-by :channel/name))) |
13 | 39 | ([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))))) |
22 | 46 |
|
23 | 47 | (defn- assoc-inst [message] |
24 | 48 | (assoc message :message/inst (time-util/ts->inst (:message/ts message)))) |
|
75 | 99 | (compare y x)) |
76 | 100 |
|
77 | 101 | (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)))) |
87 | 107 |
|
88 | 108 | (defn channel [db name] |
89 | 109 | (d/q '[:find (pull ?chan [*]) . |
|
94 | 114 | name)) |
95 | 115 |
|
96 | 116 | (defn user-names |
97 | | - [db names] |
| 117 | + [db ids] |
98 | 118 | (d/q '[:find ?id ?username |
99 | 119 | :in $ [?id ...] |
100 | 120 | :where |
101 | 121 | [?user :user/slack-id ?id] |
102 | 122 | [?user :user/name ?username]] |
103 | 123 | 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)) |
113 | 125 |
|
114 | 126 | (defn thread-messages |
115 | 127 | "Retrieve all child messages for the given parent threads" |
|
0 commit comments