mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 06:29:30 +08:00
d1191b7f5f
This gives us daily fidelity of topic view stats New table stores a row per topic viewed per day tracking anonymous and logged on views We also have a new endpoint `/t/ID/views-stats.json` to get the statistics for the topic.
41 lines
1.2 KiB
Ruby
41 lines
1.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class TopicViewStat < ActiveRecord::Base
|
|
belongs_to :topic
|
|
|
|
def self.add(topic_id:, date:, anonymous_views:, logged_in_views:)
|
|
sql = <<~SQL
|
|
INSERT INTO topic_view_stats (topic_id, viewed_at, anonymous_views, logged_in_views)
|
|
VALUES (:topic_id, :viewed_at, :anon_views, :logged_in_views)
|
|
ON CONFLICT (topic_id, viewed_at)
|
|
DO UPDATE SET
|
|
anonymous_views = topic_view_stats.anonymous_views + :anon_views,
|
|
logged_in_views = topic_view_stats.logged_in_views + :logged_in_views
|
|
SQL
|
|
|
|
DB.exec(
|
|
sql,
|
|
topic_id: topic_id,
|
|
viewed_at: date,
|
|
anon_views: anonymous_views,
|
|
logged_in_views: logged_in_views,
|
|
)
|
|
end
|
|
end
|
|
|
|
# == Schema Information
|
|
#
|
|
# Table name: topic_view_stats
|
|
#
|
|
# id :bigint not null, primary key
|
|
# topic_id :integer not null
|
|
# viewed_at :date not null
|
|
# anonymous_views :integer default(0), not null
|
|
# logged_in_views :integer default(0), not null
|
|
#
|
|
# Indexes
|
|
#
|
|
# index_topic_view_stats_on_topic_id_and_viewed_at (topic_id,viewed_at) UNIQUE
|
|
# index_topic_view_stats_on_viewed_at_and_topic_id (viewed_at,topic_id)
|
|
#
|