UpgradeChapterDynamicWeekly.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Support\Facades\Storage;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Carbon;
  6. use App\Models\SentHistory;
  7. use App\Models\Sentence;
  8. use App\Models\ProgressChapter;
  9. use App\Models\PaliText;
  10. use App\Tools\RedisClusters;
  11. use Illuminate\Support\Facades\Log;
  12. class UpgradeChapterDynamicWeekly extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'upgrade:chapter.dynamic.weekly {--test} {--book=} {--offset=}';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = '更新章节活跃程度svg';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return int
  39. */
  40. public function handle()
  41. {
  42. if(\App\Tools\Tools::isStop()){
  43. return 0;
  44. }
  45. Log::debug('upgrade:chapter.dynamic.weekly start.');
  46. $this->info('upgrade:chapter.dynamic.weekly start.');
  47. $startAt = time();
  48. $weeks = 60; //统计多少周
  49. //更新总动态
  50. $this->info("更新总动态");
  51. Log::debug("task:更新总动态开始");
  52. $table = ProgressChapter::select('book','para')
  53. ->groupBy('book','para')
  54. ->orderBy('book');
  55. if($this->option('book')){
  56. $table = $table->where('book',$this->option('book'));
  57. }
  58. $chapters = $table->get();
  59. $bar = $this->output->createProgressBar(count($chapters));
  60. Log::debug('chapter {count} ',['count',count($chapters)]);
  61. foreach ($chapters as $key => $chapter) {
  62. #章节长度
  63. $paraEnd = PaliText::where('book',$chapter->book)
  64. ->where('paragraph',$chapter->para)
  65. ->value('chapter_len')+$chapter->para-1;
  66. $progress = [];
  67. for ($i=$weeks; $i > 0 ; $i--) {
  68. #这一周有多少次更新
  69. $currDay = $i*7+$this->option('offset',0);
  70. $count = SentHistory::whereBetween('sent_histories.created_at',
  71. [Carbon::today()->subDays($currDay)->startOfWeek(),
  72. Carbon::today()->subDays($currDay)->endOfWeek()])
  73. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  74. ->where('book_id',$chapter->book)
  75. ->whereBetween('paragraph',[$chapter->para,$paraEnd])
  76. ->count();
  77. $progress[] = $count;
  78. }
  79. $key="/chapter_dynamic/{$chapter->book}/{$chapter->para}/global";
  80. RedisClusters::put($key,$progress,3600*24*7);
  81. $bar->advance();
  82. if($this->option('test')){
  83. $this->info("key:{$key}");
  84. if(RedisClusters::has($key)){
  85. $this->info('has key '.$key);
  86. }
  87. break; //调试代码
  88. }
  89. }
  90. $bar->finish();
  91. Log::debug("task:更新总动态结束");
  92. $time = time()- $startAt;
  93. $this->info("用时 {$time}");
  94. $startAt = time();
  95. $startAt = time();
  96. //更新chennel动态
  97. $this->info('更新channel动态');
  98. Log::debug("task:更新channel动态开始");
  99. $table = ProgressChapter::select('book','para','channel_id');
  100. if($this->option('book')){
  101. $table = $table->where('book',$this->option('book'));
  102. }
  103. $bar = $this->output->createProgressBar($table->count());
  104. Log::debug("更新channel动态 {count}",['count'=>$table->count()]);
  105. foreach ($table->cursor() as $chapter) {
  106. # code...
  107. $max=0;
  108. #章节长度
  109. $paraEnd = PaliText::where('book',$chapter->book)
  110. ->where('paragraph',$chapter->para)
  111. ->value('chapter_len')+$chapter->para-1;
  112. $progress = [];
  113. for ($i=$weeks; $i > 0 ; $i--) {
  114. #这一周有多少次更新
  115. $currDay = $i*7+$this->option('offset',0);
  116. $count = SentHistory::whereBetween('sent_histories.created_at',
  117. [Carbon::today()->subDays($currDay)->startOfWeek(),
  118. Carbon::today()->subDays($currDay)->endOfWeek()])
  119. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  120. ->where('book_id',$chapter->book)
  121. ->whereBetween('paragraph',[$chapter->para,$paraEnd])
  122. ->where('sentences.channel_uid',$chapter->channel_id)
  123. ->count();
  124. $progress[] = $count;
  125. }
  126. $key="/chapter_dynamic/{$chapter->book}/{$chapter->para}/ch_{$chapter->channel_id}";
  127. RedisClusters::put($key,$progress,3600*24*7);
  128. $bar->advance();
  129. if($this->option('test')){
  130. $this->info("key:{$key}");
  131. if(RedisClusters::has($key)){
  132. $this->info('has key '.$key);
  133. }
  134. break; //调试代码
  135. }
  136. }
  137. $bar->finish();
  138. Log::debug("task:更新channel动态结束");
  139. $time = time()- $startAt;
  140. $this->info("用时 {$time}");
  141. $this->info("upgrade:chapter.dynamic done");
  142. Log::debug("task: upgrade:chapter.dynamic done");
  143. return 0;
  144. }
  145. }