From f0e4dbf4538c47a70b245798754853df631137d2 Mon Sep 17 00:00:00 2001 From: jasder duan Date: Thu, 30 Nov 2023 14:33:46 +0800 Subject: [PATCH] =?UTF-8?q?ADD=20=E6=B7=BB=E5=8A=A0=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AE=9E=E8=B7=B5=E9=A1=B9=E7=9B=AE=E7=9A=84?= =?UTF-8?q?myshixun=5Fcount=20=E5=92=8Caverge=5Fstar=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 1 + lib/tasks/daily_update_star_for_shixun.rake | 24 +++++++++++++++++++ .../update_myshixuns_count_for_shixun.rake | 20 ++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 lib/tasks/daily_update_star_for_shixun.rake create mode 100644 lib/tasks/update_myshixuns_count_for_shixun.rake diff --git a/Gemfile b/Gemfile index d89f9a24..a482415a 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,7 @@ unless RUBY_PLATFORM =~ /w32/ gem 'nokogiri' end +gem 'parallel' gem 'simple_xlsx_reader' gem 'wechat',path: 'lib/wechat' gem 'grack', path:'lib/grack' diff --git a/lib/tasks/daily_update_star_for_shixun.rake b/lib/tasks/daily_update_star_for_shixun.rake new file mode 100644 index 00000000..aebac0d7 --- /dev/null +++ b/lib/tasks/daily_update_star_for_shixun.rake @@ -0,0 +1,24 @@ +#coding=utf-8 +# 通过配置服务器的crontab每天定时更新实践项目的star数量 +namespace :daily_update_star_for_shixun do + desc "update star count every day for shixun " + task :execute => :environment do + shixuns = Shixun.select(:id, :averge_star) + + shixuns.find_in_batches(batch_size: 10000) do |group| + update_sql = "UPDATE shixuns SET averge_star = CASE id " + Parallel.each(group, in_threads: 6) do |shixun| + averge_star = Game.find_by_sql("select ifnull(sum(g.star),0)/ifnull(count(*),1) as averge_star from (games g left join + (myshixuns m join shixuns s on s.id = m.shixun_id) on m.id = g.myshixun_id) + where star != 0 and s.id = #{shixun.id}").first.try(:averge_star) + averge_star = averge_star || 5 + + update_sql += " WHEN #{shixun.id} THEN #{averge_star.round(1)&.to_i} " + end + + update_sql += " ELSE averge_star END WHERE id IN (#{group.map(&:id).join(',')})" + Rails.logger.info "########## #{Time.now} 更新实践项目averge_starSQL语句: #{update_sql}" + ActiveRecord::Base.connection.execute(update_sql) if group.present? + end + end +end diff --git a/lib/tasks/update_myshixuns_count_for_shixun.rake b/lib/tasks/update_myshixuns_count_for_shixun.rake new file mode 100644 index 00000000..ce8f530e --- /dev/null +++ b/lib/tasks/update_myshixuns_count_for_shixun.rake @@ -0,0 +1,20 @@ +#coding=utf-8 +# 通过配置服务器的crontab每天定时更新实践项目的myshixuns数量 +namespace :update_myshixuns_count_for_shixun do + desc "update myshixuns count for shixun every day" + task :execute => :environment do + shixuns = Shixun.joins(:myshixuns).select("shixuns.id, COUNT(myshixuns.id) AS query_myshixuns_count") + .group("shixuns.id") + + shixuns.find_in_batches(batch_size: 10000) do |group| + update_sql = "UPDATE shixuns SET myshixuns_count = CASE id " + Parallel.each(group, in_threads: 6) do |shixun| + update_sql += " WHEN #{shixun.id} THEN #{shixun.query_myshixuns_count&.to_i} " + end + + update_sql += " ELSE myshixuns_count END WHERE id IN (#{group.map(&:id).join(',')})" + Rails.logger.info "########## #{Time.now} 更新实践项目myshixuns_count的SQL语句: #{update_sql}" + ActiveRecord::Base.connection.execute(update_sql) if group.present? + end + end +end