Skip to content
Merged

Dev #295

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/source/jobs/handlers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[source.db.honey :as hon]
[taoensso.telemere :as t]
[source.workers.bundles :as bundles]
[pg.core :as pg]))
[pg.core :as pg]
[source.services.analytics.interface :as analytics]))

(defmulti handler
(fn [opts]
Expand Down Expand Up @@ -180,6 +181,8 @@
(hon/delete! ds (db.util/tname :outgoing-posts bundle-id))
(hon/insert! ds (-> (db.util/tname :outgoing-posts bundle-id)
(assoc :data outgoing-posts))))
(analytics/insert-selected-events! ds outgoing-posts bundle-id)

(when (< (count outgoing-posts) 10)
(throw
(t/error!
Expand Down
23 changes: 23 additions & 0 deletions src/source/migrations/027_events_check.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns source.migrations.027-events-check
(:require [source.db.master]
[pg.core :as pg]))

(defn run-up! [context]
(let [ds-master (:db-master context)]
(pg/execute
ds-master
"ALTER TABLE events
DROP CONSTRAINT IF EXISTS events_event_check")))

(defn run-down! [context]
(let [ds-master (:db-master context)]
(pg/with-transaction [ds-master ds-master]
(pg/execute
ds-master
"ALTER TABLE events
DROP CONSTRAINT IF EXISTS events_event_check")
(pg/execute
ds-master
"ALTER TABLE events
ADD CONSTRAINT events_event_check
CHECK (event IN ('impression', 'click', 'view', 'bot_post'))"))))
3 changes: 2 additions & 1 deletion src/source/routes/slack.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
:bundle-id state
:channel-id channel-id
:access-token access-token
:post-interval (* 1000 60 60 24)})
:post-interval (* 1000 60 60 24)
:posts 4})
(res/response {:message "successfully added channel"})))
38 changes: 37 additions & 1 deletion src/source/services/analytics/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,36 @@
:ret :*})]
(insert-post-event-categories! ds event' post))))

(defn insert-selected-event! [ds {:keys [id feed-id content-type-id creator-id] :as post} bundle-id]
(pg/with-transaction [ds ds]
(let [bundle (bundles/bundle ds {:id bundle-id})
event {:timestamp (util/get-utc-timestamp-string)
:event "selected"
:feed-id feed-id
:post-id id
:content-type-id content-type-id
:creator-id creator-id
:bundle-id bundle-id
:distributor-id (:user-id bundle)}]
(insert-event! ds {:data event}))))

(defn insert-selected-events!
"Given a list of posts and a bundle id, inserts selected event reconds
for each given post. Inserts event categories for each post."
[ds posts bundle-id]
(pg/with-transaction [ds ds]
(let [bundle (bundles/bundle ds {:id bundle-id})
events (mapv (fn [{:keys [id feed-id content-type-id creator-id]}]
{:timestamp (util/get-utc-timestamp-string)
:event "selected"
:feed-id feed-id
:post-id id
:content-type-id content-type-id
:creator-id creator-id
:bundle-id bundle-id
:distributor-id (:user-id bundle)}) posts)]
(when (seq posts) (insert-event! ds {:data events})))))

(comment
(require '[source.db.util :as db.util])

Expand Down Expand Up @@ -572,5 +602,11 @@
:ret :*}
{:content-type-id 3}))

())
(hon/execute!
ds
(-> (hsql/select [[:count :*]])
(hsql/from :events)
(hsql/where [:= :event "selected"]))
{:ret :*})

())
9 changes: 9 additions & 0 deletions src/source/services/analytics/interface.clj
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,12 @@
"Returns a paginated list of the best performing posts on the system in terms of impressions, clicks and views"
[ds {:keys [_start _limit] :as opts}]
(core/top-post-statistics ds opts))

(defn insert-selected-event! [ds {:keys [_id _feed-id _content-type-id _creator-id] :as post} bundle-id]
(core/insert-selected-event! ds post bundle-id))

(defn insert-selected-events!
"Given a list of posts and a bundle id, inserts selected event reconds
for each given post. Inserts event categories for each post."
[ds posts bundle-id]
(core/insert-selected-events! ds posts bundle-id))
Loading