From 11e531b09908c12d3f8ed934a15bbd8a03d50de3 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Tue, 28 Nov 2023 12:28:36 -0300 Subject: [PATCH] FEATURE: Backfill task for sentiment module (#316) * FEATURE: Backfill task for sentiment module * fix join clause --- lib/tasks/modules/sentiment/backfill.rake | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lib/tasks/modules/sentiment/backfill.rake diff --git a/lib/tasks/modules/sentiment/backfill.rake b/lib/tasks/modules/sentiment/backfill.rake new file mode 100644 index 00000000..0e14bf52 --- /dev/null +++ b/lib/tasks/modules/sentiment/backfill.rake @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +desc "Backfill sentiment for all posts" +task "ai:sentiment:backfill", [:start_post] => [:environment] do |_, args| + public_categories = Category.where(read_restricted: false).pluck(:id) + + Post + .joins("INNER JOIN topics ON topics.id = posts.topic_id") + .joins( + "LEFT JOIN classification_results ON classification_results.target_id = posts.id AND classification_results.target_type = 'Post'", + ) + .where("classification_results.target_id IS NULL") + .where("posts.id >= ?", args[:start_post].to_i || 0) + .where("category_id IN (?)", public_categories) + .where(posts: { deleted_at: nil }) + .order("posts.id ASC") + .find_each do |post| + print "." + DiscourseAi::PostClassificator.new( + DiscourseAi::Sentiment::SentimentClassification.new, + ).classify!(post) + end +end