UpgradeChapterDynamicWeekly.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 Illuminate\Support\Facades\Cache;
  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(
  71. 'sent_histories.created_at',
  72. [
  73. Carbon::today()->subDays($currDay)->startOfWeek(),
  74. Carbon::today()->subDays($currDay)->endOfWeek()
  75. ]
  76. )
  77. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  78. ->where('book_id', $chapter->book)
  79. ->whereBetween('paragraph', [$chapter->para, $paraEnd])
  80. ->count();
  81. $progress[] = $count;
  82. }
  83. $key = "/chapter_dynamic/{$chapter->book}/{$chapter->para}/global";
  84. Cache::put($key, $progress, 3600 * 24 * 7);
  85. $bar->advance();
  86. if ($this->option('test')) {
  87. $this->info("key:{$key}");
  88. if (Cache::has($key)) {
  89. $this->info('has key ' . $key);
  90. }
  91. break; //调试代码
  92. }
  93. }
  94. $bar->finish();
  95. Log::debug("task:更新总动态结束");
  96. $time = time() - $startAt;
  97. $this->info("用时 {$time}");
  98. $startAt = time();
  99. $startAt = time();
  100. //更新chennel动态
  101. $this->info('更新channel动态');
  102. Log::debug("task:更新channel动态开始");
  103. $table = ProgressChapter::select('book', 'para', 'channel_id');
  104. if ($this->option('book')) {
  105. $table = $table->where('book', $this->option('book'));
  106. }
  107. $bar = $this->output->createProgressBar($table->count());
  108. Log::debug("更新channel动态 {count}", ['count' => $table->count()]);
  109. foreach ($table->cursor() as $chapter) {
  110. # code...
  111. $max = 0;
  112. #章节长度
  113. $paraEnd = PaliText::where('book', $chapter->book)
  114. ->where('paragraph', $chapter->para)
  115. ->value('chapter_len') + $chapter->para - 1;
  116. $progress = [];
  117. for ($i = $weeks; $i > 0; $i--) {
  118. #这一周有多少次更新
  119. $currDay = $i * 7 + $this->option('offset', 0);
  120. $count = SentHistory::whereBetween(
  121. 'sent_histories.created_at',
  122. [
  123. Carbon::today()->subDays($currDay)->startOfWeek(),
  124. Carbon::today()->subDays($currDay)->endOfWeek()
  125. ]
  126. )
  127. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  128. ->where('book_id', $chapter->book)
  129. ->whereBetween('paragraph', [$chapter->para, $paraEnd])
  130. ->where('sentences.channel_uid', $chapter->channel_id)
  131. ->count();
  132. $progress[] = $count;
  133. }
  134. $key = "/chapter_dynamic/{$chapter->book}/{$chapter->para}/ch_{$chapter->channel_id}";
  135. Cache::put($key, $progress, 3600 * 24 * 7);
  136. $bar->advance();
  137. if ($this->option('test')) {
  138. $this->info("key:{$key}");
  139. if (Cache::has($key)) {
  140. $this->info('has key ' . $key);
  141. }
  142. break; //调试代码
  143. }
  144. }
  145. $bar->finish();
  146. Log::debug("task:更新channel动态结束");
  147. $time = time() - $startAt;
  148. $this->info("用时 {$time}");
  149. $this->info("upgrade:chapter.dynamic done");
  150. Log::debug("task: upgrade:chapter.dynamic done");
  151. return 0;
  152. }
  153. }