FIX: correctly take category/group filters into csv export (#9300)

This commit is contained in:
Joffrey JAFFEUX 2020-03-30 19:08:47 +02:00 committed by GitHub
parent f8cb46c0e1
commit aeaea3c154
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 10 deletions

View File

@ -7,6 +7,10 @@ module Jobs
class ExportCsvFile < ::Jobs::Base
sidekiq_options retry: false
attr_accessor :extra
attr_accessor :current_user
attr_accessor :entity
HEADER_ATTRS_FOR ||= HashWithIndifferentAccess.new(
user_archive: ['topic_title', 'categories', 'is_pm', 'post', 'like_count', 'reply_count', 'url', 'created_at'],
user_list: ['id', 'name', 'username', 'email', 'title', 'created_at', 'last_seen_at', 'last_posted_at', 'last_emailed_at', 'trust_level', 'approved', 'suspended_at', 'suspended_till', 'silenced_till', 'active', 'admin', 'moderator', 'ip_address', 'staged', 'secondary_emails'],
@ -180,8 +184,13 @@ module Jobs
@extra[:start_date] = @extra[:start_date].to_date.beginning_of_day if @extra[:start_date].is_a?(String)
@extra[:end_date] = @extra[:end_date].to_date.end_of_day if @extra[:end_date].is_a?(String)
@extra[:category_id] = @extra[:category_id].present? ? @extra[:category_id].to_i : nil
@extra[:group_id] = @extra[:group_id].present? ? @extra[:group_id].to_i : nil
@extra[:filters] = {}
if @extra[:category_id].present?
@extra[:filters][:category] = @extra[:category_id].to_i
end
if @extra[:group_id].present?
@extra[:filters][:group] = @extra[:group_id].to_i
end
report = Report.find(@extra[:name], @extra)

View File

@ -55,7 +55,7 @@ describe Jobs::ExportCsvFile do
post = Fabricate(:post, topic: topic, user: user)
exporter = Jobs::ExportCsvFile.new
exporter.instance_variable_set(:@current_user, User.find_by(id: user.id))
exporter.current_user = User.find_by(id: user.id)
rows = []
exporter.user_archive_export { |row| rows << row }
@ -79,9 +79,9 @@ describe Jobs::ExportCsvFile do
let(:exporter) do
exporter = Jobs::ExportCsvFile.new
exporter.instance_variable_set(:@entity, 'report')
exporter.instance_variable_set(:@extra, HashWithIndifferentAccess.new(start_date: '2010-01-01', end_date: '2011-01-01'))
exporter.instance_variable_set(:@current_user, User.find_by(id: user.id))
exporter.entity = 'report'
exporter.extra = HashWithIndifferentAccess.new(start_date: '2010-01-01', end_date: '2011-01-01')
exporter.current_user = User.find_by(id: user.id)
exporter
end
@ -89,7 +89,7 @@ describe Jobs::ExportCsvFile do
user.user_visits.create!(visited_at: '2010-01-01', posts_read: 42)
Fabricate(:user).user_visits.create!(visited_at: '2010-01-03', posts_read: 420)
exporter.instance_variable_get(:@extra)['name'] = 'dau_by_mau'
exporter.extra['name'] = 'dau_by_mau'
report = exporter.report_export.to_a
expect(report.first).to contain_exactly("Day", "Percent")
@ -97,11 +97,28 @@ describe Jobs::ExportCsvFile do
expect(report.third).to contain_exactly("2010-01-03", "50.0")
end
it 'works with filters' do
user.user_visits.create!(visited_at: '2010-01-01', posts_read: 42)
group = Fabricate(:group)
user1 = Fabricate(:user)
group_user = Fabricate(:group_user, group: group, user: user1)
user1.user_visits.create!(visited_at: '2010-01-03', posts_read: 420)
exporter.extra['name'] = 'visits'
exporter.extra['group_id'] = group.id
report = exporter.report_export.to_a
expect(report.length).to eq(2)
expect(report.first).to contain_exactly("Day", "Count")
expect(report.second).to contain_exactly("2010-01-03", "1")
end
it 'works with single-column reports with default label' do
user.user_visits.create!(visited_at: '2010-01-01')
Fabricate(:user).user_visits.create!(visited_at: '2010-01-03')
exporter.instance_variable_get(:@extra)['name'] = 'visits'
exporter.extra['name'] = 'visits'
report = exporter.report_export.to_a
expect(report.first).to contain_exactly("Day", "Count")
@ -113,7 +130,7 @@ describe Jobs::ExportCsvFile do
DiscourseIpInfo.stubs(:get).with("1.1.1.1").returns(location: "Earth")
user.user_auth_token_logs.create!(action: "login", client_ip: "1.1.1.1", created_at: '2010-01-01')
exporter.instance_variable_get(:@extra)['name'] = 'staff_logins'
exporter.extra['name'] = 'staff_logins'
report = exporter.report_export.to_a
expect(report.first).to contain_exactly("User", "Location", "Login at")
@ -133,7 +150,7 @@ describe Jobs::ExportCsvFile do
ApplicationRequest.create!(date: '2010-01-02', req_type: 'page_view_crawler', count: 8)
ApplicationRequest.create!(date: '2010-01-03', req_type: 'page_view_crawler', count: 9)
exporter.instance_variable_get(:@extra)['name'] = 'consolidated_page_views'
exporter.extra['name'] = 'consolidated_page_views'
report = exporter.report_export.to_a
expect(report[0]).to contain_exactly("Day", "Logged in users", "Anonymous users", "Crawlers")