Skip to content

Commit

Permalink
Yet another try at fixing #5455
Browse files Browse the repository at this point in the history
  • Loading branch information
ragesoss committed May 10, 2024
1 parent c76246f commit 2f2654b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
7 changes: 5 additions & 2 deletions lib/analytics/campaign_csv_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ def wikidata_to_csv
courses = @campaign.courses
.joins(:course_stat)
courses.find_each do |course|
csv_data << CourseWikidataCsvBuilder.new(course).stat_row
builder = CourseWikidataCsvBuilder.new(course)
next unless builder.wikidata_stats?

csv_data << builder.stat_row
end

csv_data << sum_wiki_columns(csv_data) if courses.any?
Expand All @@ -60,7 +63,7 @@ def wikidata_to_csv

def sum_wiki_columns(csv_data)
# Skip 1st header row + 1st column course name
csv_data[1..].transpose[1..]&.map(&:sum)&.unshift('Total')
csv_data[1..].transpose[1..].map(&:sum).unshift('Total')
end

class AllCourses
Expand Down
13 changes: 4 additions & 9 deletions lib/analytics/course_wikidata_csv_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ def generate_csv
CSV.generate { |csv| csv_data.each { |line| csv << line } }
end

def wikidata_stats?
@course.course_stat && @course.course_stat.stats_hash['www.wikidata.org']
end

def stat_row
hash_stats = @course
.course_stat.stats_hash['www.wikidata.org']
&.merge({ 'course name' => @course.title })

# this method gets called directly from CampaignCsvBuilder, so we need
# to guard for courses that don't have any Wikidata stats.
return [] unless hash_stats
CSV_HEADERS.map { |elmnt| hash_stats.fetch elmnt, 0 }
end

Expand Down Expand Up @@ -55,10 +56,4 @@ def stat_row
'no data',
'total revisions'
].freeze

private

def wikidata_stats?
@course.course_stat && @course.course_stat.stats_hash['www.wikidata.org']
end
end

0 comments on commit 2f2654b

Please sign in to comment.