StatisticsNissaya.php 2.8 KB

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