Class: IncomingLinksReport
- Inherits:
-
Object
- Object
- IncomingLinksReport
- Defined in:
- app/models/incoming_links_report.rb
Instance Attribute Summary collapse
-
#category_id ⇒ Object
Returns the value of attribute category_id.
-
#data ⇒ Object
Returns the value of attribute data.
-
#end_date ⇒ Object
Returns the value of attribute end_date.
-
#include_subcategories ⇒ Object
Returns the value of attribute include_subcategories.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#start_date ⇒ Object
Returns the value of attribute start_date.
-
#type ⇒ Object
Returns the value of attribute type.
-
#y_titles ⇒ Object
Returns the value of attribute y_titles.
Class Method Summary collapse
- .find(type, _opts = {}) ⇒ Object
- .link_count_per_domain(limit: 10, start_date:, end_date:, category_id:, include_subcategories:) ⇒ Object
- .link_count_per_topic(start_date:, end_date:, category_id:, include_subcategories:) ⇒ Object
- .link_count_per_user(start_date:, end_date:, category_id:, include_subcategories:) ⇒ Object
- .per_domain(domains, options = {}) ⇒ Object
- .per_user(start_date:, end_date:, category_id:, include_subcategories:) ⇒ Object
- .public_incoming_links(category_id: nil, include_subcategories: nil) ⇒ Object
- .report_top_referred_topics(report) ⇒ Object
-
.report_top_referrers(report) ⇒ Object
Return top 10 users who brought traffic to the site within the last 30 days.
-
.report_top_traffic_sources(report) ⇒ Object
Return top 10 domains that brought traffic to the site within the last 30 days.
- .topic_count_per_domain(domains, options = {}) ⇒ Object
- .topic_count_per_user(start_date:, end_date:, category_id:, include_subcategories:) ⇒ Object
Instance Method Summary collapse
- #as_json(_options = nil) ⇒ Object
-
#initialize(type) ⇒ IncomingLinksReport
constructor
A new instance of IncomingLinksReport.
Constructor Details
#initialize(type) ⇒ IncomingLinksReport
Returns a new instance of IncomingLinksReport.
13 14 15 16 17 18 19 |
# File 'app/models/incoming_links_report.rb', line 13 def initialize(type) @type = type @y_titles = {} @data = nil @category_id = nil @include_subcategories = false end |
Instance Attribute Details
#category_id ⇒ Object
Returns the value of attribute category_id.
4 5 6 |
# File 'app/models/incoming_links_report.rb', line 4 def category_id @category_id end |
#data ⇒ Object
Returns the value of attribute data.
4 5 6 |
# File 'app/models/incoming_links_report.rb', line 4 def data @data end |
#end_date ⇒ Object
Returns the value of attribute end_date.
4 5 6 |
# File 'app/models/incoming_links_report.rb', line 4 def end_date @end_date end |
#include_subcategories ⇒ Object
Returns the value of attribute include_subcategories.
4 5 6 |
# File 'app/models/incoming_links_report.rb', line 4 def include_subcategories @include_subcategories end |
#limit ⇒ Object
Returns the value of attribute limit.
4 5 6 |
# File 'app/models/incoming_links_report.rb', line 4 def limit @limit end |
#start_date ⇒ Object
Returns the value of attribute start_date.
4 5 6 |
# File 'app/models/incoming_links_report.rb', line 4 def start_date @start_date end |
#type ⇒ Object
Returns the value of attribute type.
4 5 6 |
# File 'app/models/incoming_links_report.rb', line 4 def type @type end |
#y_titles ⇒ Object
Returns the value of attribute y_titles.
4 5 6 |
# File 'app/models/incoming_links_report.rb', line 4 def y_titles @y_titles end |
Class Method Details
.find(type, _opts = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/models/incoming_links_report.rb', line 33 def self.find(type, _opts = {}) report_method = :"report_#{type}" return nil unless respond_to?(report_method) # Load the report report = IncomingLinksReport.new(type) report.start_date = _opts[:start_date] || 30.days.ago report.end_date = _opts[:end_date] || Time.now.end_of_day report.limit = _opts[:limit].to_i if _opts[:limit] report.category_id = _opts[:category_id] if _opts[:category_id] report.include_subcategories = _opts[:include_subcategories] if _opts[:include_subcategories] public_send(report_method, report) report end |
.link_count_per_domain(limit: 10, start_date:, end_date:, category_id:, include_subcategories:) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'app/models/incoming_links_report.rb', line 155 def self.link_count_per_domain( limit: 10, start_date:, end_date:, category_id:, include_subcategories: ) public_incoming_links(category_id: category_id, include_subcategories: include_subcategories) .where( "incoming_links.created_at > ? AND incoming_links.created_at < ?", start_date, end_date, ) .joins(incoming_referer: :incoming_domain) .group("incoming_domains.name") .order("count_all DESC") .limit(limit) .count end |
.link_count_per_topic(start_date:, end_date:, category_id:, include_subcategories:) ⇒ Object
234 235 236 237 238 239 240 241 242 243 |
# File 'app/models/incoming_links_report.rb', line 234 def self.link_count_per_topic(start_date:, end_date:, category_id:, include_subcategories:) public_incoming_links(category_id: category_id, include_subcategories: include_subcategories) .where( "incoming_links.created_at > ? AND incoming_links.created_at < ? AND topic_id IS NOT NULL", start_date, end_date, ) .group("topic_id") .count end |
.link_count_per_user(start_date:, end_date:, category_id:, include_subcategories:) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'app/models/incoming_links_report.rb', line 105 def self.link_count_per_user(start_date:, end_date:, category_id:, include_subcategories:) per_user( start_date: start_date, end_date: end_date, category_id: category_id, include_subcategories: include_subcategories, ).count end |
.per_domain(domains, options = {}) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'app/models/incoming_links_report.rb', line 175 def self.per_domain(domains, = {}) public_incoming_links( category_id: [:category_id], include_subcategories: [:include_subcategories], ) .joins(incoming_referer: :incoming_domain) .where( "incoming_links.created_at > ? AND incoming_links.created_at < ?", [:start_date], [:end_date], ) .where("incoming_domains.name IN (?)", domains) .group("incoming_domains.name") end |
.per_user(start_date:, end_date:, category_id:, include_subcategories:) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'app/models/incoming_links_report.rb', line 94 def self.per_user(start_date:, end_date:, category_id:, include_subcategories:) public_incoming_links(category_id: category_id, include_subcategories: include_subcategories) .where( "incoming_links.created_at > ? AND incoming_links.created_at < ? AND incoming_links.user_id IS NOT NULL", start_date, end_date, ) .joins(:user) .group("users.username") end |
.public_incoming_links(category_id: nil, include_subcategories: nil) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'app/models/incoming_links_report.rb', line 245 def self.public_incoming_links(category_id: nil, include_subcategories: nil) links = IncomingLink.joins(post: :topic).where("topics.archetype = ?", Archetype.default) if category_id if include_subcategories links = links.where("topics.category_id IN (?)", Category.subcategory_ids(category_id)) else links = links.where("topics.category_id = ?", category_id) end end links end |
.report_top_referred_topics(report) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'app/models/incoming_links_report.rb', line 195 def self.report_top_referred_topics(report) report.y_titles[:num_clicks] = I18n.t("reports.#{report.type}.labels.num_clicks") num_clicks = link_count_per_topic( start_date: report.start_date, end_date: report.end_date, category_id: report.category_id, include_subcategories: report.include_subcategories, ) num_clicks = num_clicks.to_a.sort_by { |x| x[1] }.last(report.limit || 10).reverse report.data = [] topics = Topic.select("id, slug, title").where("id in (?)", num_clicks.map { |z| z[0] }) if report.category_id topics = topics.where( category_id: ( if report.include_subcategories Category.subcategory_ids(report.category_id) else report.category_id end ), ) end num_clicks.each do |topic_id, num_clicks_element| topic = topics.find { |t| t.id == topic_id } if topic report.data << { topic_id: topic_id, topic_title: topic.title, topic_url: topic.relative_url, num_clicks: num_clicks_element, } end end report.data end |
.report_top_referrers(report) ⇒ Object
Return top 10 users who brought traffic to the site within the last 30 days
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/models/incoming_links_report.rb', line 51 def self.report_top_referrers(report) report.y_titles[:num_clicks] = I18n.t("reports.#{report.type}.num_clicks") report.y_titles[:num_topics] = I18n.t("reports.#{report.type}.num_topics") num_clicks = link_count_per_user( start_date: report.start_date, end_date: report.end_date, category_id: report.category_id, include_subcategories: report.include_subcategories, ) num_topics = topic_count_per_user( start_date: report.start_date, end_date: report.end_date, category_id: report.category_id, include_subcategories: report.include_subcategories, ) user_id_lookup = User .where(username: num_clicks.keys) .select(:id, :username, :uploaded_avatar_id) .inject({}) do |sum, v| sum[v.username] = { id: v.id, user_avatar_template: User.avatar_template(v.username, v.uploaded_avatar_id), } sum end report.data = [] num_clicks.each_key do |username| report.data << { username: username, user_id: user_id_lookup[username][:id], user_avatar_template: user_id_lookup[username][:user_avatar_template], num_clicks: num_clicks[username], num_topics: num_topics[username], } end report.data = report.data.sort_by { |x| x[:num_clicks] }.reverse[0, 10] end |
.report_top_traffic_sources(report) ⇒ Object
Return top 10 domains that brought traffic to the site within the last 30 days
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/models/incoming_links_report.rb', line 124 def self.report_top_traffic_sources(report) report.y_titles[:num_clicks] = I18n.t("reports.#{report.type}.num_clicks") report.y_titles[:num_topics] = I18n.t("reports.#{report.type}.num_topics") report.y_titles[:num_users] = I18n.t("reports.#{report.type}.num_users") num_clicks = link_count_per_domain( start_date: report.start_date, end_date: report.end_date, category_id: report.category_id, include_subcategories: report.include_subcategories, ) num_topics = topic_count_per_domain( num_clicks.keys, category_id: report.category_id, include_subcategories: report.include_subcategories, start_date: report.start_date, end_date: report.end_date, ) report.data = [] num_clicks.each_key do |domain| report.data << { domain: domain, num_clicks: num_clicks[domain], num_topics: num_topics[domain], } end report.data = report.data.sort_by { |x| x[:num_clicks] }.reverse[0, 10] end |
.topic_count_per_domain(domains, options = {}) ⇒ Object
190 191 192 193 |
# File 'app/models/incoming_links_report.rb', line 190 def self.topic_count_per_domain(domains, = {}) # COUNT(DISTINCT) is slow per_domain(domains, ).count("DISTINCT posts.topic_id") end |
.topic_count_per_user(start_date:, end_date:, category_id:, include_subcategories:) ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'app/models/incoming_links_report.rb', line 114 def self.topic_count_per_user(start_date:, end_date:, category_id:, include_subcategories:) per_user( start_date: start_date, end_date: end_date, category_id: category_id, include_subcategories: include_subcategories, ).joins(:post).count("DISTINCT posts.topic_id") end |
Instance Method Details
#as_json(_options = nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/models/incoming_links_report.rb', line 21 def as_json( = nil) { type: self.type, title: I18n.t("reports.#{self.type}.title"), xaxis: I18n.t("reports.#{self.type}.xaxis"), ytitles: self.y_titles, data: self.data, start_date: start_date, end_date: end_date, } end |