Class: Jobs::TruncateUserFlagStats
- Defined in:
- app/jobs/regular/truncate_user_flag_stats.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#execute(args) ⇒ Object
To give users a chance to improve, we limit their flag stats to the last N flags.
Methods inherited from Base
acquire_cluster_concurrency_lock!, clear_cluster_concurrency_lock!, cluster_concurrency, cluster_concurrency_redis_key, delayed_perform, #error_context, get_cluster_concurrency, #last_db_duration, #log, #perform, #perform_immediately
Class Method Details
.truncate_to ⇒ Object
4 5 6 |
# File 'app/jobs/regular/truncate_user_flag_stats.rb', line 4 def self.truncate_to 100 end |
Instance Method Details
#execute(args) ⇒ Object
To give users a chance to improve, we limit their flag stats to the last N flags
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/jobs/regular/truncate_user_flag_stats.rb', line 9 def execute(args) raise Discourse::InvalidParameters.new(:user_ids) if args[:user_ids].blank? args[:user_ids].each do |u| user_stat = UserStat.find_by(user_id: u) next if user_stat.blank? total = user_stat.flags_agreed + user_stat.flags_disagreed + user_stat.flags_ignored next if total < self.class.truncate_to params = ReviewableScore .statuses .slice(:agreed, :disagreed, :ignored) .merge(user_id: u, truncate_to: self.class.truncate_to) result = DB.query(<<~SQL, params) SELECT SUM(CASE WHEN x.status = :agreed THEN 1 ELSE 0 END) AS agreed, SUM(CASE WHEN x.status = :disagreed THEN 1 ELSE 0 END) AS disagreed, SUM(CASE WHEN x.status = :ignored THEN 1 ELSE 0 END) AS ignored FROM ( SELECT rs.status FROM reviewable_scores AS rs INNER JOIN reviewables AS r ON r.id = rs.reviewable_id INNER JOIN posts AS p ON p.id = r.target_id WHERE rs.user_id = :user_id AND r.type = 'ReviewableFlaggedPost' AND rs.status IN (:agreed, :disagreed, :ignored) AND rs.user_id <> p.user_id ORDER BY rs.created_at DESC LIMIT :truncate_to ) AS x SQL user_stat.update_columns( flags_agreed: result[0].agreed || 0, flags_disagreed: result[0].disagreed || 0, flags_ignored: result[0].ignored || 0, ) end end |