FIX: adds support for missing reports from old dashboard (#6624)

This commit is contained in:
Joffrey JAFFEUX 2018-11-19 12:20:05 +01:00 committed by GitHub
parent 4c8cfe0f29
commit e860c8b844
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 9 deletions

View File

@ -2,7 +2,11 @@ require_dependency 'report'
class Admin::ReportsController < Admin::AdminController class Admin::ReportsController < Admin::AdminController
def index def index
reports_methods = Report.singleton_methods.grep(/^report_(?!about)/) reports_methods = ['page_view_total_reqs'] +
ApplicationRequest.req_types.keys
.select { |r| r =~ /^page_view_/ && r !~ /mobile/ }
.map { |r| r + "_reqs" } +
Report.singleton_methods.grep(/^report_(?!about)/)
reports = reports_methods.map do |name| reports = reports_methods.map do |name|
type = name.to_s.gsub('report_', '') type = name.to_s.gsub('report_', '')

View File

@ -44,10 +44,26 @@ class IncomingLinksReport
num_clicks = link_count_per_user(start_date: report.start_date, end_date: report.end_date, category_id: report.category_id) num_clicks = link_count_per_user(start_date: report.start_date, end_date: report.end_date, category_id: report.category_id)
num_topics = topic_count_per_user(start_date: report.start_date, end_date: report.end_date, category_id: report.category_id) num_topics = topic_count_per_user(start_date: report.start_date, end_date: report.end_date, category_id: report.category_id)
user_id_lookup = User.where(username: num_clicks.keys).select(:id, :username).inject({}) { |sum, v| sum[v.username] = v.id; sum; } user_id_lookup = User
.where(username: num_clicks.keys)
.select(:id, :username, :uploaded_avatar_id)
.inject({}) { |sum, v|
sum[v.username] = {
id: v.id,
user_avatar_template: User.avatar_template(v.username, v.uploaded_avatar_id)
}
sum
}
report.data = [] report.data = []
num_clicks.each_key do |username| num_clicks.each_key do |username|
report.data << { username: username, user_id: user_id_lookup[username], num_clicks: num_clicks[username], num_topics: num_topics[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 end
report.data = report.data.sort_by { |x| x[:num_clicks] }.reverse[0, 10] report.data = report.data.sort_by { |x| x[:num_clicks] }.reverse[0, 10]
end end

View File

@ -673,11 +673,46 @@ class Report
limit: report.limit || 8, limit: report.limit || 8,
category_id: report.category_id category_id: report.category_id
} }
result = nil
result = IncomingLinksReport.find(:top_traffic_sources, options) result = IncomingLinksReport.find(:top_traffic_sources, options)
report.data = result.data report.data = result.data
end end
def self.report_top_referrers(report)
report.modes = [:table]
report.labels = [
{
type: :user,
properties: {
username: :username,
id: :user_id,
avatar: :user_avatar_template,
},
title: I18n.t("reports.top_referrers.labels.user")
},
{
property: :num_clicks,
type: :number,
title: I18n.t("reports.top_referrers.labels.num_clicks")
},
{
property: :num_topics,
type: :number,
title: I18n.t("reports.top_referrers.labels.num_topics")
}
]
options = {
end_date: report.end_date,
start_date: report.start_date,
limit: report.limit || 8
}
result = IncomingLinksReport.find(:top_referrers, options)
report.data = result.data
end
def self.report_trending_search(report) def self.report_trending_search(report)
report.labels = [ report.labels = [
{ {

View File

@ -1036,6 +1036,10 @@ en:
xaxis: "User" xaxis: "User"
num_clicks: "Clicks" num_clicks: "Clicks"
num_topics: "Topics" num_topics: "Topics"
labels:
user: "User"
num_clicks: "Clicks"
num_topics: "Topics"
top_traffic_sources: top_traffic_sources:
title: "Top Traffic Sources" title: "Top Traffic Sources"
xaxis: "Domain" xaxis: "Domain"

View File

@ -45,8 +45,19 @@ describe IncomingLinksReport do
r = IncomingLinksReport.find('top_referrers').as_json r = IncomingLinksReport.find('top_referrers').as_json
expect(r[:data]).to eq [ expect(r[:data]).to eq [
{ username: p1.user.username, user_id: p1.user.id, num_clicks: 7 + 2, num_topics: 2 }, {
{ username: p2.user.username, user_id: p2.user.id, num_clicks: 3, num_topics: 1 } user_avatar_template: User.default_template(p1.user.username),
username: p1.user.username,
user_id: p1.user.id,
num_clicks: 7 + 2, num_topics: 2
},
{
user_avatar_template: User.default_template(p2.user.username),
username: p2.user.username,
user_id: p2.user.id,
num_clicks: 3,
num_topics: 1
}
] ]
r = IncomingLinksReport.find('top_traffic_sources').as_json r = IncomingLinksReport.find('top_traffic_sources').as_json
@ -87,7 +98,13 @@ describe IncomingLinksReport do
r = IncomingLinksReport.find('top_referrers').as_json r = IncomingLinksReport.find('top_referrers').as_json
expect(r[:data]).to eq [ expect(r[:data]).to eq [
{ username: public_post.user.username, user_id: public_post.user.id, num_clicks: 1, num_topics: 1 }, {
user_avatar_template: User.default_template(public_post.user.username),
username: public_post.user.username,
user_id: public_post.user.id,
num_clicks: 1,
num_topics: 1
}
] ]
r = IncomingLinksReport.find('top_traffic_sources').as_json r = IncomingLinksReport.find('top_traffic_sources').as_json
@ -142,8 +159,21 @@ describe IncomingLinksReport do
Fabricate(:incoming_link, user: bob, post: post1).save Fabricate(:incoming_link, user: bob, post: post1).save
end end
expect(top_referrers[:data][0]).to eq(username: 'amy', user_id: amy.id, num_clicks: 3, num_topics: 2) expect(top_referrers[:data][0]).to eq(
expect(top_referrers[:data][1]).to eq(username: 'bob', user_id: bob.id, num_clicks: 2, num_topics: 1) user_avatar_template: User.default_template('amy'),
username: 'amy',
user_id: amy.id,
num_clicks: 3,
num_topics: 2
)
expect(top_referrers[:data][1]).to eq(
user_avatar_template: User.default_template('bob'),
username: 'bob',
user_id: bob.id,
num_clicks: 2,
num_topics: 1
)
end end
end end