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

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

From e860c8b84493f8cf833bae4f6656c63b7844e550 Mon Sep 17 00:00:00 2001
From: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Date: Mon, 19 Nov 2018 12:20:05 +0100
Subject: [PATCH] FIX: adds support for missing reports from old dashboard
 (#6624)


diff --git a/app/controllers/admin/reports_controller.rb b/app/controllers/admin/reports_controller.rb
index 2702ac3..6907bb8 100644
--- a/app/controllers/admin/reports_controller.rb
+++ b/app/controllers/admin/reports_controller.rb
@@ -2,7 +2,11 @@ require_dependency 'report'
 
 class Admin::ReportsController < Admin::AdminController
   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|
       type = name.to_s.gsub('report_', '')
diff --git a/app/models/incoming_links_report.rb b/app/models/incoming_links_report.rb
index e809261..541185e 100644
--- a/app/models/incoming_links_report.rb
+++ b/app/models/incoming_links_report.rb
@@ -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_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 = []
     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
     report.data = report.data.sort_by { |x| x[:num_clicks] }.reverse[0, 10]
   end
diff --git a/app/models/report.rb b/app/models/report.rb
index cf02c70..09ad166 100644
--- a/app/models/report.rb
+++ b/app/models/report.rb
@@ -673,11 +673,46 @@ class Report
       limit: report.limit || 8,
       category_id: report.category_id
     }
-    result = nil
+
     result = IncomingLinksReport.find(:top_traffic_sources, options)
     report.data = result.data
   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)
     report.labels = [
       {
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index eb66648..ba45644 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1036,6 +1036,10 @@ en:
       xaxis: "User"
       num_clicks: "Clicks"
       num_topics: "Topics"
+      labels:
+        user: "User"
+        num_clicks: "Clicks"
+        num_topics: "Topics"
     top_traffic_sources:
       title: "Top Traffic Sources"
       xaxis: "Domain"
diff --git a/spec/models/incoming_links_report_spec.rb b/spec/models/incoming_links_report_spec.rb
index 7b7f9a8..699e697 100644
--- a/spec/models/incoming_links_report_spec.rb
+++ b/spec/models/incoming_links_report_spec.rb
@@ -45,8 +45,19 @@ describe IncomingLinksReport do
 
       r = IncomingLinksReport.find('top_referrers').as_json
       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
@@ -87,7 +98,13 @@ describe IncomingLinksReport do
 
       r = IncomingLinksReport.find('top_referrers').as_json
       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
@@ -142,8 +159,21 @@ describe IncomingLinksReport do
         Fabricate(:incoming_link, user: bob, post: post1).save
       end
 
-      expect(top_referrers[:data][0]).to eq(username: 'amy', user_id: amy.id, num_clicks: 3, num_topics: 2)
-      expect(top_referrers[:data][1]).to eq(username: 'bob', user_id: bob.id, num_clicks: 2, num_topics: 1)
+      expect(top_referrers[:data][0]).to eq(
+        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

GitHub

1 Like