From 8efc49fbc0fbf10984b0efa7c9c9be88898af1f0 Mon Sep 17 00:00:00 2001 From: gabina Date: Thu, 12 Dec 2024 16:10:55 -0300 Subject: [PATCH] Use UpdateWikidataStatsTimeslice class. --- app/models/course_wiki_timeslice.rb | 6 ++++++ app/services/update_course_stats_timeslice.rb | 7 +++++-- app/services/update_course_wiki_timeslices.rb | 12 ++++++++++++ spec/services/update_course_wiki_timeslices_spec.rb | 2 ++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/models/course_wiki_timeslice.rb b/app/models/course_wiki_timeslice.rb index 0d660ce1b5..7cd9568ccf 100644 --- a/app/models/course_wiki_timeslice.rb +++ b/app/models/course_wiki_timeslice.rb @@ -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 @@ -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 diff --git a/app/services/update_course_stats_timeslice.rb b/app/services/update_course_stats_timeslice.rb index 3c72a94717..7e39115ab0 100644 --- a/app/services/update_course_stats_timeslice.rb +++ b/app/services/update_course_stats_timeslice.rb @@ -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 @@ -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 diff --git a/app/services/update_course_wiki_timeslices.rb b/app/services/update_course_wiki_timeslices.rb index 07a8b69bf0..ac8bf3b631 100644 --- a/app/services/update_course_wiki_timeslices.rb +++ b/app/services/update_course_wiki_timeslices.rb @@ -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:) @@ -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 @@ -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' diff --git a/spec/services/update_course_wiki_timeslices_spec.rb b/spec/services/update_course_wiki_timeslices_spec.rb index 2513bbfff9..dffbcb6992 100644 --- a/spec/services/update_course_wiki_timeslices_spec.rb +++ b/spec/services/update_course_wiki_timeslices_spec.rb @@ -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, @@ -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