StatisticsNissaya.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Channel;
  4. use App\Models\Sentence;
  5. use App\Tools\Tools;
  6. use Carbon\Carbon;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\Storage;
  9. class StatisticsNissaya extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. * php artisan statistics:nissaya
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'statistics:nissaya';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '统计nissaya 每月录入进度';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. if (Tools::isStop()) {
  41. return 0;
  42. }
  43. $nissaya_channels = Channel::where('type', 'nissaya')
  44. ->where('lang', 'my')
  45. ->select('uid')->get();
  46. $this->info('channel:'.count($nissaya_channels));
  47. $file = 'public/statistics/nissaya-monthly.csv';
  48. Storage::disk('local')->put($file, '');
  49. // 按月获取数据
  50. $firstDay = Sentence::whereIn('channel_uid', $nissaya_channels)
  51. ->orderBy('created_at')
  52. ->select('created_at')
  53. ->first();
  54. $firstDay = strtotime($firstDay->created_at);
  55. $firstMonth = Carbon::create(date('Y-m', $firstDay));
  56. $now = Carbon::now();
  57. $current = $firstMonth;
  58. $sumStrlen = 0;
  59. $sumCount = 0;
  60. while ($current <= $now) {
  61. // code...
  62. $start = Carbon::create($current)->startOfMonth();
  63. $end = Carbon::create($current)->endOfMonth();
  64. $date = $current->format('Y-m');
  65. $table = Sentence::whereIn('channel_uid', $nissaya_channels)
  66. ->whereDate('created_at', '>=', $start)
  67. ->whereDate('created_at', '<=', $end);
  68. $strlen = $table->sum('strlen');
  69. $sumStrlen += $strlen;
  70. $count = $table->count();
  71. $sumCount += $count;
  72. $editor = Sentence::whereIn('channel_uid', $nissaya_channels)
  73. ->whereDate('created_at', '>=', $start)
  74. ->whereDate('created_at', '<=', $end)
  75. ->groupBy('editor_uid')
  76. ->select('editor_uid')->get();
  77. $info = "{$date},{$strlen},{$sumStrlen},{$count},{$sumCount},".count($editor);
  78. $this->info($info);
  79. Storage::disk('local')->append($file, $info);
  80. $current->addMonth(1);
  81. }
  82. $this->info('path='.$file);
  83. return 0;
  84. }
  85. }