diff --git a/app/Console/Commands/CalculateReportCommand.php b/app/Console/Commands/CalculateReportCommand.php index 0ddb99e..028af08 100644 --- a/app/Console/Commands/CalculateReportCommand.php +++ b/app/Console/Commands/CalculateReportCommand.php @@ -55,14 +55,14 @@ class CalculateReportCommand extends Command $type = $this->choice('Chọn loại báo cáo ', ['daily', 'weekly', 'monthly']); } - // Ghi log xem có gọi không - Crontjob::create(['message' => 'CalculateReportCommand is called (' . $type . ')']); - // Xử lý di chuyển echo "\n Bắt đầu xử lý tính toán BXH: " . $type; switch ($type) { case 'daily': + // Ghi log xem có gọi không + Crontjob::create(['message' => 'CalculateReportCommand is called (' . $type . ')']); + // Lấy ngày tạo BXH ngày từ cấu hình, nếu xử lý BXH ngày hôm sau thì ngày của BXH là ngày hôm trước. if (Constants::IS_CREATE_DAILY_RANKING_SAME_DAY) { $date_daily_ranking = date('Y-m-d'); @@ -152,83 +152,90 @@ class CalculateReportCommand extends Command break; case 'weekly': - // Check xem đã insert vào report_views_weekly chưa - $check = WeeklyReport::where('week', date('W')) - ->where('month', date('m')) - ->where('year', date('Y')) - ->count(); - - if($check >= 100){ - echo "\n Báo cáo đã được tổng hợp trước đó."; - return false; - } - - if (strtoupper(date('D')) == 'SUN') { - $weekly_data = WeeklyReportView::select('*')->take(100)->get()->toArray(); - if (count($weekly_data) > 0) { - $weekly_report = []; - foreach ($weekly_data as $key => $weekly) { - $weekly_report[$key]['week'] = date('W'); - $weekly_report[$key]['month'] = date('m'); - $weekly_report[$key]['year'] = date('Y'); - $weekly_report[$key]['track_id'] = $weekly['track_id']; - - $weekly_report[$key]['views_zing'] = $weekly['week_zing']; - $weekly_report[$key]['views_nct'] =$weekly['week_nct']; - $weekly_report[$key]['views_keeng'] = $weekly['week_keeng']; - - $weekly_report[$key]['point_zing'] = $weekly['point_zing']; - $weekly_report[$key]['point_nct'] = $weekly['point_nct']; - $weekly_report[$key]['point_keeng'] = $weekly['point_keeng']; - - $weekly_report[$key]['total_point'] = $weekly['total_point']; - $weekly_report[$key]['order'] = $key + 1; - - $weekly_report[$key]['created_at'] = Carbon::now(); - $weekly_report[$key]['updated_at'] = Carbon::now(); - } - - if (count($weekly_report) > 0) { - DB::table(Constants::TABLE_REPORT_VIEWS_WEEKLY)->insert($weekly_report); - } - - // Check xem đã insert chưa - $check_again = WeeklyReport::where('week', date('W')) - ->where('month', date('m')) - ->where('year', date('Y')) - ->count(); - - if ($check_again >= 100) { - echo "\n Đã lưu dữ liệu báo cáo và bắt đầu reset lượt nghe tuần."; - // Reset số lượt nghe tháng về 0 trong bảng crawler_listens - DB::table(Constants::TABLE_CRAWLER_LISTENS)->update([ - 'week_zing' => 0, - 'week_nct' => 0, - 'week_nhacvn' => 0, - 'week_keeng' => 0 - ]); - echo "\n Đã reset lượt nghe tuần."; + if (strtoupper(date('D')) == 'SUN' && date('H') >= 4) { + // Ghi log xem có gọi không + Crontjob::create(['message' => 'CalculateReportCommand is called (' . $type . ')']); + + // Check xem đã insert vào report_views_weekly_new chưa + $check = WeeklyReport::where('week', date('W')) + ->where('month', date('m')) + ->where('year', date('Y')) + ->count(); + + if($check >= 100){ + echo "\n - BXH Tuần đã được tổng hợp trước đó."; + } else { + $weekly_data = WeeklyReportView::select('*')->take(100)->get()->toArray(); + if (count($weekly_data) > 0 && $weekly_data[0]['total_point'] > 0) { + echo "\n - Bắt đầu xử lý BXH Tuần..."; + $weekly_report = []; + foreach ($weekly_data as $key => $weekly) { + $weekly_report[$key]['week'] = date('W'); + $weekly_report[$key]['month'] = date('m'); + $weekly_report[$key]['year'] = date('Y'); + $weekly_report[$key]['track_id'] = $weekly['track_id']; + + $weekly_report[$key]['views_zing'] = $weekly['week_zing']; + $weekly_report[$key]['views_nct'] =$weekly['week_nct']; + $weekly_report[$key]['views_keeng'] = $weekly['week_keeng']; + + $weekly_report[$key]['point_zing'] = $weekly['point_zing']; + $weekly_report[$key]['point_nct'] = $weekly['point_nct']; + $weekly_report[$key]['point_keeng'] = $weekly['point_keeng']; + + $weekly_report[$key]['total_point'] = $weekly['total_point']; + $weekly_report[$key]['order'] = $key + 1; + + $weekly_report[$key]['created_at'] = Carbon::now(); + $weekly_report[$key]['updated_at'] = Carbon::now(); + } + + if (count($weekly_report) > 0) { + DB::table(Constants::TABLE_REPORT_VIEWS_WEEKLY_NEW)->insert($weekly_report); + } + + // Check xem đã insert chưa + $check_again = WeeklyReport::where('week', date('W')) + ->where('month', date('m')) + ->where('year', date('Y')) + ->count(); + + if ($check_again >= 100) { + echo "\n - Đã lưu dữ liệu BXH Tuần và bắt đầu Reset lượt nghe tuần."; + // Reset số lượt nghe tháng về 0 trong bảng crawler_listens + DB::table(Constants::TABLE_CRAWLER_LISTENS)->update([ + 'week_zing' => 0, + 'week_nct' => 0, + 'week_nhacvn' => 0, + 'week_keeng' => 0 + ]); + echo "\n - Đã reset lượt nghe tuần."; + } + } else { + echo "\n - Dữ liệu lượt nghe Tuần đã bị Reset nên không tổng hợp được BXH."; } } } else { - echo "\n Ngày thực hiện báo cáo tuần phải là Chủ nhật."; - return false; + echo "\n - Ngày thực hiện báo cáo tuần phải là Chủ nhật."; } break; case 'monthly': - // Check xem đã insert vào reports_views_monthly chưa - $check = MonthlyReport::where('month', date('m')) - ->where('year', date('Y')) - ->count(); - - if($check >= 100){ - echo "\n Báo cáo đã được tổng hợp trước đó."; - return false; - } - // Chỉ thực hiện nếu là ngày 21 hàng tháng, phải thực hiện trước 21h if (date('d') == 21) { + // Ghi log xem có gọi không + Crontjob::create(['message' => 'CalculateReportCommand is called (' . $type . ')']); + + // Check xem đã insert vào reports_views_monthly chưa + $check = MonthlyReport::where('month', date('m')) + ->where('year', date('Y')) + ->count(); + + if($check >= 100){ + echo "\n - Báo cáo đã được tổng hợp trước đó."; + return false; + } + $monthly_data = MonthlyReportView::select('*')->take(100)->get()->toArray(); if (count($monthly_data) > 0) { $monthly_report = []; @@ -259,7 +266,7 @@ class CalculateReportCommand extends Command ->where('year', date('Y')) ->count(); if ($check_again >= 100) { - echo "\n Đã lưu dữ liệu báo cáo và bắt đầu reset lượt nghe tháng."; + echo "\n - Đã lưu dữ liệu báo cáo và bắt đầu reset lượt nghe tháng."; // Reset số lượt nghe tháng về 0 trong bảng crawler_listens DB::table(Constants::TABLE_CRAWLER_LISTENS)->update([ 'month_zing' => 0, @@ -267,11 +274,11 @@ class CalculateReportCommand extends Command 'month_nhacvn' => 0, 'month_keeng' => 0 ]); - echo "\n Đã reset lượt nghe tháng."; + echo "\n - Đã reset lượt nghe tháng."; } } } else { - echo "\n Ngày thực hiện báo cáo tháng phải là ngày 21."; + echo "\n - Ngày thực hiện báo cáo tháng phải là ngày 21."; return false; } break; diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 5c91346..34b24ca 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -84,12 +84,12 @@ class Kernel extends ConsoleKernel $schedule->command('crawler:get-listen zing')->dailyAt('22:45'); $schedule->command('crawler:get-listen zing --limit=-1')->dailyAt('22:50'); - $schedule->command('calculate:daily-listen')->dailyAt('5:00'); - $schedule->command('calculate:daily-listen')->dailyAt('5:10'); + $schedule->command('calculate:daily-listen')->dailyAt('2:00'); + $schedule->command('calculate:daily-listen')->dailyAt('2:10'); - $schedule->command('calculate:report daily')->dailyAt('5:30'); - $schedule->command('calculate:report weekly')->sundays(); - $schedule->command('calculate:report monthly')->monthlyOn(21, '6:10'); + $schedule->command('calculate:report daily')->dailyAt('3:05'); + $schedule->command('calculate:report weekly')->dailyAt('4:05'); + $schedule->command('calculate:report monthly')->monthlyOn(21, '5:05'); $schedule->command('queue:work --once --timeout=120')->everyMinute()->withoutOverlapping(); } diff --git a/app/Helpers/Constants.php b/app/Helpers/Constants.php index d436e95..ef9e838 100644 --- a/app/Helpers/Constants.php +++ b/app/Helpers/Constants.php @@ -24,7 +24,9 @@ class Constants const TABLE_PERMISSIONS = 'permissions'; const TABLE_REPORT_VIEWS_DAILY = 'report_views_daily'; + const TABLE_REPORT_VIEWS_MONTHLY_NEW = 'report_views_monthly_new'; const TABLE_REPORT_VIEWS_WEEKLY = 'report_views_weekly'; + const TABLE_REPORT_VIEWS_WEEKLY_NEW = 'report_views_weekly_new'; const TABLE_REPORTS_VIEWS_MONTHLY = 'reports_views_monthly'; const TABLE_ROLE_USER = 'role_user'; diff --git a/app/Models/Report/MonthlyReport.php b/app/Models/Report/MonthlyReport.php index 3239959..45ad1ec 100644 --- a/app/Models/Report/MonthlyReport.php +++ b/app/Models/Report/MonthlyReport.php @@ -7,7 +7,7 @@ use Illuminate\Database\Eloquent\Model; class MonthlyReport extends Model { - protected $table = Constants::TABLE_REPORTS_VIEWS_MONTHLY; + protected $table = Constants::TABLE_REPORT_VIEWS_MONTHLY_NEW; public $timestamps = true; diff --git a/app/Models/Report/WeeklyReport.php b/app/Models/Report/WeeklyReport.php index baf6f0f..fb399c8 100644 --- a/app/Models/Report/WeeklyReport.php +++ b/app/Models/Report/WeeklyReport.php @@ -7,7 +7,7 @@ use Illuminate\Database\Eloquent\Model; class WeeklyReport extends Model { - protected $table = Constants::TABLE_REPORT_VIEWS_WEEKLY; + protected $table = Constants::TABLE_REPORT_VIEWS_WEEKLY_NEW; public $timestamps = true;