UpgradeChapterDynamicWeekly.php 5.6 KB

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