UpgradeChapterDynamicWeekly.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. 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(\App\Tools\Tools::isStop()){
  42. return 0;
  43. }
  44. $this->info('upgrade:chapter.dynamic.weekly start.');
  45. $startAt = time();
  46. $weeks = 60; //统计多少周
  47. //更新总动态
  48. $this->info("更新总动态");
  49. $table = ProgressChapter::select('book','para')
  50. ->groupBy('book','para')
  51. ->orderBy('book');
  52. if($this->option('book')){
  53. $table = $table->where('book',$this->option('book'));
  54. }
  55. $chapters = $table->get();
  56. $bar = $this->output->createProgressBar(count($chapters));
  57. foreach ($chapters as $key => $chapter) {
  58. #章节长度
  59. $paraEnd = PaliText::where('book',$chapter->book)
  60. ->where('paragraph',$chapter->para)
  61. ->value('chapter_len')+$chapter->para-1;
  62. $progress = [];
  63. for ($i=$weeks; $i > 0 ; $i--) {
  64. #这一周有多少次更新
  65. $currDay = $i*7+$this->option('offset',0);
  66. $count = SentHistory::whereBetween('sent_histories.created_at',
  67. [Carbon::today()->subDays($currDay)->startOfWeek(),
  68. Carbon::today()->subDays($currDay)->endOfWeek()])
  69. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  70. ->where('book_id',$chapter->book)
  71. ->whereBetween('paragraph',[$chapter->para,$paraEnd])
  72. ->count();
  73. $progress[] = $count;
  74. }
  75. $key="/chapter_dynamic/{$chapter->book}/{$chapter->para}/global";
  76. Cache::put($key,$progress,3600*24*7);
  77. $bar->advance();
  78. sleep(2);
  79. if($this->option('test')){
  80. $this->info("key:{$key}");
  81. if(Cache::has($key)){
  82. $this->info('has key '.$key);
  83. }
  84. break; //调试代码
  85. }
  86. }
  87. $bar->finish();
  88. $time = time()- $startAt;
  89. $this->info("用时 {$time}");
  90. $startAt = time();
  91. $startAt = time();
  92. //更新chennel动态
  93. $this->info('更新chennel动态');
  94. $table = ProgressChapter::select('book','para','channel_id');
  95. if($this->option('book')){
  96. $table = $table->where('book',$this->option('book'));
  97. }
  98. $bar = $this->output->createProgressBar($table->count());
  99. foreach ($table->cursor() as $chapter) {
  100. # code...
  101. $max=0;
  102. #章节长度
  103. $paraEnd = PaliText::where('book',$chapter->book)
  104. ->where('paragraph',$chapter->para)
  105. ->value('chapter_len')+$chapter->para-1;
  106. $progress = [];
  107. for ($i=$weeks; $i > 0 ; $i--) {
  108. #这一周有多少次更新
  109. $currDay = $i*7+$this->option('offset',0);
  110. $count = SentHistory::whereBetween('sent_histories.created_at',
  111. [Carbon::today()->subDays($currDay)->startOfWeek(),
  112. Carbon::today()->subDays($currDay)->endOfWeek()])
  113. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  114. ->where('book_id',$chapter->book)
  115. ->whereBetween('paragraph',[$chapter->para,$paraEnd])
  116. ->where('sentences.channel_uid',$chapter->channel_id)
  117. ->count();
  118. $progress[] = $count;
  119. }
  120. $key="/chapter_dynamic/{$chapter->book}/{$chapter->para}/ch_{$chapter->channel_id}";
  121. Cache::put($key,$progress,3600*24*7);
  122. $bar->advance();
  123. sleep(2);
  124. if($this->option('test')){
  125. $this->info("key:{$key}");
  126. if(Cache::has($key)){
  127. $this->info('has key '.$key);
  128. }
  129. break; //调试代码
  130. }
  131. }
  132. $bar->finish();
  133. $time = time()- $startAt;
  134. $this->info("用时 {$time}");
  135. $this->info("upgrade:chapter.dynamic done");
  136. return 0;
  137. }
  138. }