2019-05-12 21:55:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-02 00:01:53 -04:00
|
|
|
class Jobs::ReviewablePriorities < ::Jobs::Scheduled
|
2019-05-07 13:25:11 -04:00
|
|
|
every 1.day
|
|
|
|
|
2019-09-19 14:07:56 -04:00
|
|
|
# We need this many reviewables before we'll calculate priorities
|
2019-09-19 13:42:50 -04:00
|
|
|
def self.min_reviewables
|
|
|
|
15
|
|
|
|
end
|
|
|
|
|
2019-09-19 14:07:56 -04:00
|
|
|
# We want to look at scores for items with this many reviewables (flags) attached
|
|
|
|
def self.target_count
|
|
|
|
2
|
|
|
|
end
|
|
|
|
|
2019-05-07 13:25:11 -04:00
|
|
|
def execute(args)
|
2019-09-19 13:42:50 -04:00
|
|
|
return unless Reviewable.where('score > 0').count >= self.class.min_reviewables
|
2019-05-07 13:25:11 -04:00
|
|
|
|
2019-09-19 14:07:56 -04:00
|
|
|
res = DB.query_single(<<~SQL, target_count: self.class.target_count)
|
2019-05-16 13:45:20 -04:00
|
|
|
SELECT COALESCE(PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY score), 0.0) AS medium,
|
|
|
|
COALESCE(PERCENTILE_DISC(0.85) WITHIN GROUP (ORDER BY score), 0.0) AS high
|
2019-09-19 14:07:56 -04:00
|
|
|
FROM (
|
|
|
|
SELECT r.score
|
|
|
|
FROM reviewables AS r
|
|
|
|
INNER JOIN reviewable_scores AS rs ON rs.reviewable_id = r.id
|
|
|
|
GROUP BY r.id
|
|
|
|
HAVING COUNT(*) >= :target_count
|
|
|
|
) AS x
|
2019-05-07 13:25:11 -04:00
|
|
|
SQL
|
|
|
|
|
2019-09-19 14:07:56 -04:00
|
|
|
return unless res && res.size == 2
|
|
|
|
|
2019-05-22 17:23:45 -04:00
|
|
|
medium, high = res
|
|
|
|
|
|
|
|
Reviewable.set_priorities(medium: medium, high: high)
|
2019-05-07 13:25:11 -04:00
|
|
|
end
|
|
|
|
end
|