UpgradeProgressPara.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Models\Sentence;
  6. use App\Models\PaliSentence;
  7. use App\Models\Progress;
  8. use Illuminate\Support\Facades\Log;
  9. class UpgradeProgressPara extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. * php artisan upgrade:progress --book=152 --channel=19f53a65-81db-4b7d-8144-ac33f1217d34
  14. * @var string
  15. */
  16. protected $signature = 'upgrade:progress.para {--book=} {--para=} {--channel=} {--resume}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Command description';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle()
  38. {
  39. if (\App\Tools\Tools::isStop()) {
  40. return 0;
  41. }
  42. $this->info('upgrade:progress start');
  43. $startTime = time();
  44. $book = $this->option('book');
  45. $para = $this->option('para');
  46. $channelId = $this->option('channel');
  47. if ($channelId) {
  48. $this->line('channel=' . $channelId);
  49. }
  50. $table = Sentence::where('strlen', '>', 0);
  51. if ($book || $para || $channelId) {
  52. if ($book) {
  53. $table = $table->where('book_id', $book);
  54. }
  55. if ($para) {
  56. $table = $table->where('paragraph', $para);
  57. }
  58. if ($channelId) {
  59. $table = $table->where('channel_uid', $channelId);
  60. }
  61. $sentences = $table->groupby('book_id', 'paragraph', 'channel_uid')
  62. ->select('book_id', 'paragraph', 'channel_uid');
  63. } else {
  64. if ($this->option('resume')) {
  65. $sentences = Sentence::where('strlen', '>', 0)
  66. ->whereBetween('book_id', [$book, 1000])
  67. ->where('paragraph', '>=', $para)
  68. ->whereNotNull('channel_uid')
  69. ->groupby('book_id', 'paragraph', 'channel_uid')
  70. ->select('book_id', 'paragraph', 'channel_uid');
  71. } else {
  72. $sentences = Sentence::where('strlen', '>', 0)
  73. ->where('book_id', '<', 1000)
  74. ->whereNotNull('channel_uid')
  75. ->groupby('book_id', 'paragraph', 'channel_uid')
  76. ->select('book_id', 'paragraph', 'channel_uid');
  77. }
  78. }
  79. $total = DB::query()
  80. ->fromSub($sentences, 't')
  81. ->count();
  82. $sentences = $sentences->cursor();
  83. $this->info('sentences:' . $total);
  84. $curr = 0;
  85. #第二步 更新段落表
  86. foreach ($sentences as $sentence) {
  87. # 第二步 生成para progress 1,2,15,zh-tw
  88. # 计算此段落完成时间
  89. $finalAt = Sentence::where('strlen', '>', 0)
  90. ->where('book_id', $sentence->book_id)
  91. ->where('paragraph', $sentence->paragraph)
  92. ->where('channel_uid', $sentence->channel_uid)
  93. ->max('created_at');
  94. $updateAt = Sentence::where('strlen', '>', 0)
  95. ->where('book_id', $sentence->book_id)
  96. ->where('paragraph', $sentence->paragraph)
  97. ->where('channel_uid', $sentence->channel_uid)
  98. ->max('updated_at');
  99. # 查询每个段落的等效巴利语字符数
  100. $result_sent = Sentence::where('strlen', '>', 0)
  101. ->where('book_id', $sentence->book_id)
  102. ->where('paragraph', $sentence->paragraph)
  103. ->where('channel_uid', $sentence->channel_uid)
  104. ->select('word_start')
  105. ->get();
  106. $paraInfo = [
  107. 'book' => $sentence->book_id,
  108. 'para' => $sentence->paragraph,
  109. 'channel_id' => $sentence->channel_uid
  110. ];
  111. if (count($result_sent) > 0) {
  112. #查询这些句子的总共等效巴利语字符数
  113. $para_strlen = 0;
  114. foreach ($result_sent as $sent) {
  115. # code...
  116. $para_strlen += PaliSentence::where('book', $sentence->book_id)
  117. ->where('paragraph', $sentence->paragraph)
  118. ->where('word_begin', $sent->word_start)
  119. ->value('length');
  120. }
  121. $paraData = [
  122. 'lang' => 'en',
  123. 'all_strlen' => $para_strlen,
  124. 'public_strlen' => $para_strlen,
  125. 'created_at' => $finalAt,
  126. 'updated_at' => $updateAt,
  127. ];
  128. Progress::updateOrInsert($paraInfo, $paraData);
  129. }
  130. $curr++;
  131. if ($curr % 500 === 0) {
  132. $present = (int)($curr * 100 / $total);
  133. $this->info("[{$present}%] Progress " . json_encode($paraInfo));
  134. sleep(1);
  135. }
  136. }
  137. $time = time() - $startTime;
  138. $this->info("upgrade progress finished in {$time}s");
  139. return 0;
  140. }
  141. }