Skip to content

Commit

Permalink
Use UpdateWikidataStatsTimeslice class.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabina committed Dec 12, 2024
1 parent 18152b6 commit 8efc49f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
6 changes: 6 additions & 0 deletions app/models/course_wiki_timeslice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def update_cache_from_revisions(revisions)
update_upload_count
update_uploads_in_use_count
update_upload_usages_count
update_stats
self.needs_update = false
save
end
Expand Down Expand Up @@ -154,4 +155,9 @@ def update_upload_usages_count
# TODO: count only uploads updated at during the timeslice range
self.upload_usages_count = course.uploads_in_use.sum(:usage_count)
end

def update_stats
return unless wiki.project == 'wikidata'
self.stats = UpdateWikidataStatsTimeslice.new(course).build_stats_from_revisions(@revisions)
end
end
7 changes: 5 additions & 2 deletions app/services/update_course_stats_timeslice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def initialize(course)
update_article_status if should_update_article_status?
update_average_pageviews
update_caches
# update_wikidata_stats if wikidata
update_wikidata_stats if wikidata
# This needs to happen after `update_caches` because it relies on ArticlesCourses#new_article
# to calculate new article stats for each namespace.
update_wiki_namespace_stats
Expand Down Expand Up @@ -85,7 +85,10 @@ def update_caches
end

def update_wikidata_stats
UpdateWikidataStatsWorker.new.perform(@course)
wikidata = Wiki.get_or_create(language: nil, project: 'wikidata')
timeslices = CourseWikiTimeslice.for_course_and_wiki(@course, wikidata)
stats = timeslices.pluck(:stats)
UpdateWikidataStatsTimeslice.new(@course).update_wikidata_statistics(stats)
@debugger.log_update_progress :wikidata_stats_updated
end

Expand Down
12 changes: 12 additions & 0 deletions app/services/update_course_wiki_timeslices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def initialize(course)
@course = course
@timeslice_manager = TimesliceManager.new(@course)
@debugger = UpdateDebugger.new(@course)
@wikidata_stats_updater = UpdateWikidataStatsTimeslice.new(@course) if wikidata
end

def run(all_time:)
Expand Down Expand Up @@ -88,6 +89,13 @@ def fetch_data(wiki, timeslice_start, timeslice_end)
timeslice_start.strftime('%Y%m%d%H%M%S'),
timeslice_end.strftime('%Y%m%d%H%M%S'),
update_service: self)

# Only for wikidata project, fetch wikidata stats
if wiki.project == 'wikidata' && @revisions.present?
wikidata_revisions = @revisions[wiki][:revisions]
@revisions[wiki][:revisions] =
@wikidata_stats_updater.update_revisions_with_stats(wikidata_revisions)
end
# TODO: replace the logic on ArticlesCourses.update_from_course to remove all
# the ArticlesCourses that do not correspond to course revisions.
# That may happen if the course dates changed, so some revisions are no
Expand Down Expand Up @@ -144,6 +152,10 @@ def update_course_user_wiki_timeslices_for_wiki(wiki, revisions)
end
end

def wikidata
@course.wikis.find { |wiki| wiki.project == 'wikidata' }
end

def log_error(error)
Sentry.capture_message "#{@course.title} update timeslices error: #{error}",
level: 'error'
Expand Down
2 changes: 2 additions & 0 deletions spec/services/update_course_wiki_timeslices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
expect(timeslice.uploads_in_use_count).to eq(0)
expect(timeslice.upload_usages_count).to eq(0)
expect(timeslice.last_mw_rev_datetime).to eq('20181129180841'.to_datetime)
expect(timeslice.stats).to be_empty

# For wikidata
timeslice = course.course_wiki_timeslices.where(wiki: wikidata,
Expand All @@ -122,6 +123,7 @@
expect(timeslice.uploads_in_use_count).to eq(0)
expect(timeslice.upload_usages_count).to eq(0)
expect(timeslice.last_mw_rev_datetime).to eq('20181124045740'.to_datetime)
expect(timeslice.stats['references removed']).to eq(2)
end

it 'rolls back the updates if something goes wrong' do
Expand Down

0 comments on commit 8efc49f

Please sign in to comment.