2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-15 05:39:07 +08:00
|
|
|
module Jobs
|
2019-10-02 12:01:53 +08:00
|
|
|
class RetroRecentTimeRead < ::Jobs::Onceoff
|
2017-11-15 05:39:07 +08:00
|
|
|
def execute_onceoff(args)
|
|
|
|
# update past records by evenly distributing total time reading among each post read
|
|
|
|
sql = <<~SQL
|
|
|
|
UPDATE user_visits uv1
|
|
|
|
SET time_read = (
|
|
|
|
SELECT (
|
|
|
|
uv1.posts_read
|
|
|
|
/ (SELECT CAST(sum(uv2.posts_read) AS FLOAT) FROM user_visits uv2 where uv2.user_id = uv1.user_id)
|
|
|
|
* COALESCE((SELECT us.time_read FROM user_stats us WHERE us.user_id = uv1.user_id), 0)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
WHERE EXISTS (SELECT 1 FROM user_stats stats WHERE stats.user_id = uv1.user_id AND stats.posts_read_count > 0 LIMIT 1)
|
|
|
|
AND EXISTS (SELECT 1 FROM user_visits visits WHERE visits.user_id = uv1.user_id AND visits.posts_read > 0 LIMIT 1)
|
|
|
|
SQL
|
|
|
|
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec(sql)
|
2017-11-15 05:39:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|