Commit fc1c2275 by Phạm Văn Đoan

cập nhật task report

parent 8cd0fa51
<?php
namespace App\Console\Commands;
use App\Helpers\Constants;
use App\Models\Crontjob;
use App\Models\Report\DailyReport;
use App\Models\Report\MonthlyReport;
use App\Models\Report\WeeklyReport;
use App\Models\View\DailyReportView;
use App\Models\View\MonthlyReportView;
use App\Models\View\WeeklyReportView;
use App\Repositories\TrackRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class CalculateReportCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'calculate:report {type?} {--option=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Tính toán số liệu báo cáo ngày/tuần/tháng';
protected $trackRepository;
/**
* Create a new command instance.
*
* CalculateReportCommand constructor.
* @param TrackRepository $trackRepository
*/
public function __construct(TrackRepository $trackRepository)
{
parent::__construct();
ini_set('max_execution_time', -1);
$this->trackRepository = $trackRepository;
}
public function handle()
{
$type = $this->argument('type');
$option = $this->option('option');
if (empty($type) || !in_array($type, ['daily', 'weekly', 'monthly'])) {
$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 báo cáo: " . $type;
switch ($type) {
case 'daily':
// Check xem đã insert vào report_views_daily chưa
$check = DailyReport::where('date', date('Y-m-d'))->count();
if($check >= 100){
echo "\n Báo cáo đã được tổng hợp trước đó.";
return false;
}
$daily_data = DailyReportView::select('*')->take(100)->get()->toArray();
if (count($daily_data) > 0) {
$daily_report = [];
foreach ($daily_data as $key => $daily) {
$daily_report[$key]['date'] = date('Y-m-d');
$daily_report[$key]['track_id'] = $daily['track_id'];
$daily_report[$key]['views_zing'] = $daily['date_zing'];
$daily_report[$key]['views_nct'] =$daily['date_nct'];
$daily_report[$key]['views_keeng'] = $daily['date_keeng'];
$daily_report[$key]['point_zing'] = $daily['point_zing'];
$daily_report[$key]['point_nct'] = $daily['point_nct'];
$daily_report[$key]['point_keeng'] = $daily['point_keeng'];
$daily_report[$key]['total_point'] = $daily['total_point'];
$daily_report[$key]['ranking'] = $key + 1;
$daily_report[$key]['created_at'] = Carbon::now();
$daily_report[$key]['updated_at'] = Carbon::now();
}
if (count($daily_report) > 0) {
DB::table(Constants::TABLE_REPORT_VIEWS_DAILY)->insert($daily_report);
}
}
// Lưu log lượt nghe theo ngày
$daily_data_all = DailyReportView::select('*')->get()->toArray();
if (count($daily_data_all) > 0) {
$daily_report = [];
foreach ($daily_data_all as $key => $daily) {
$daily_report[$key]['track_id'] = $daily['track_id'];
$daily_report[$key]['zing_count'] = $daily['date_zing'];
$daily_report[$key]['nct_count'] =$daily['date_nct'];
$daily_report[$key]['nhacvn_count'] = 0;
$daily_report[$key]['keeng_count'] = $daily['date_keeng'];
$daily_report[$key]['total_listen'] = $daily['total_listen'];
$daily_report[$key]['created_at'] = Carbon::now();
$daily_report[$key]['updated_at'] = Carbon::now();
}
if (count($daily_report) > 0) {
DB::table(Constants::TABLE_CRAWLER_LISTEN_HISTORIES)->insert($daily_report);
}
}
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', strtotime('2020-03-29'))) == '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.";
}
}
} else {
echo "\n Ngày thực hiện báo cáo tuần phải là Chủ nhật.";
return false;
}
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) {
$monthly_data = MonthlyReportView::select('*')->take(100)->get()->toArray();
if (count($monthly_data) > 0) {
$monthly_report = [];
foreach ($monthly_data as $key => $monthly) {
$monthly_report[$key]['month'] = date('m');
$monthly_report[$key]['year'] = date('Y');
$monthly_report[$key]['track_id'] = $monthly['track_id'];
$monthly_report[$key]['views_zing'] = $monthly['month_zing'];
$monthly_report[$key]['views_nct'] = $monthly['month_nct'];
$monthly_report[$key]['views_keeng'] = $monthly['month_keeng'];
$monthly_report[$key]['point_zing'] = $monthly['point_zing'];
$monthly_report[$key]['point_nct'] = $monthly['point_nct'];
$monthly_report[$key]['point_keeng'] = $monthly['point_keeng'];
$monthly_report[$key]['total_point'] = $monthly['total_point'];
$monthly_report[$key]['order'] = $key + 1;
$monthly_report[$key]['created_at'] = Carbon::now();
$monthly_report[$key]['updated_at'] = Carbon::now();
}
if (count($monthly_report) > 0) {
DB::table(Constants::TABLE_REPORTS_VIEWS_MONTHLY)->insert($monthly_report);
}
// Check xem đã insert chưa
$check_again = MonthlyReport::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 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,
'month_nct' => 0,
'month_nhacvn' => 0,
'month_keeng' => 0
]);
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.";
return false;
}
break;
}
echo "\n Kết thúc xử lý tính toán báo cáo: " . $type;
}
}
<?php
namespace App\Console\Commands;
use App\Models\Crontjob;
use App\Repositories\TrackRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;
class CalculateWeeklyReportCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'calculate:weekly-report';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Tính toán số liệu báo cáo tuần';
protected $trackRepository;
/**
* Create a new command instance.
*
* CalculateWeeklyReportCommand constructor.
* @param TrackRepository $trackRepository
*/
public function __construct(TrackRepository $trackRepository)
{
parent::__construct();
ini_set('max_execution_time', -1);
$this->trackRepository = $trackRepository;
}
public function handle()
{
// Ghi log xem có gọi không
Crontjob::create(['message' => 'CalculateWeeklyReportCommand is called']);
// Xử lý di chuyển
echo "\n Bắt đầu xử lý tính toán báo cáo tuần";
echo "\n Kết thúc xử lý tính toán báo cáo tuần";
}
}
......@@ -3,7 +3,7 @@
namespace App\Console;
use App\Console\Commands\CalculateDailyListenCommand;
use App\Console\Commands\CalculateWeeklyReportCommand;
use App\Console\Commands\CalculateReportCommand;
use App\Console\Commands\CrawlerTrackListenCommand;
use App\Console\Commands\CrontjobCommand;
use App\Console\Commands\Keeng\KeengCrawlerByCateCommand;
......@@ -40,7 +40,7 @@ class Kernel extends ConsoleKernel
CalculateDailyListenCommand::class,
CalculateWeeklyReportCommand::class,
CalculateReportCommand::class,
];
......@@ -87,6 +87,10 @@ class Kernel extends ConsoleKernel
$schedule->command('calculate:daily-listen')->dailyAt('1:00');
$schedule->command('calculate:daily-listen')->dailyAt('1:10');
$schedule->command('php artisan calculate:report daily')->dailyAt('8:00');
$schedule->command('php artisan calculate:report weekly')->sundays();
$schedule->command('php artisan calculate:report monthly')->monthlyOn(21, '6:10');
$schedule->command('queue:work --once --timeout=120')->everyMinute()->withoutOverlapping();
}
......
......@@ -37,6 +37,10 @@ class Constants
const TABLE_USER_LOGS = 'user_logs';
const TABLE_USERS = 'users';
const VIEW_TABLE_DAILY_REPORT_VIEW = 'daily_report_view';
const VIEW_TABLE_WEEKLY_REPORT_VIEW = 'weekly_report_view';
const VIEW_TABLE_MONTHLY_REPORT_VIEW = 'monthly_report_view';
const USER_STATUS_INACTIVE = 0;
const USER_STATUS_ACTIVE = 1;
const USER_STATUS_LOCKED = 2;
......
......@@ -293,3 +293,38 @@ if (!function_exists('convertViews')) {
return $point;
}
}
if (!function_exists('saveLogFile')) {
/**
* Hàm lưu file log txt trong storage
*
* @param $file_name
* @param $data
* @param string $type
*
* @return bool
*/
function saveLogFile($file_name, $data, $type='daily') {
switch ($type) {
case 'daily': $new_path = 'storage/app/public/daily_report/'; break;
case 'weekly': $new_path = 'storage/app/public/weekly_report/'; break;
case 'monthly': $new_path = 'storage/app/public/monthly_report/'; break;
default: return false;
}
// Kiểm tra tồn tại thư mục, ko tồn tại thì tạo mới
if (!file_exists($new_path)) {
@mkdir($new_path, 0777, true);
}
// Ghi dữ liệu ra file
$new_file_path = $new_path . $file_name;
@copy('storage/app/public/report_template.txt', $new_file_path);
$my_file = fopen($new_file_path, "w");
@fwrite($my_file, $data);
@fclose($my_file);
//
return true;
}
}
......@@ -16,6 +16,7 @@ class CrawlerListenHistory extends Model
'zing_count',
'nct_count',
'nhacvn_count',
'keeng_count'
'keeng_count',
'total_listen'
];
}
<?php
namespace App\Models;
namespace App\Models\Report;
use App\Helpers\Constants;
use Illuminate\Database\Eloquent\Model;
......@@ -12,11 +12,11 @@ class DailyReport extends Model
public $timestamps = true;
protected $fillable = [
'date', 'day', 'month', 'year',
'date',
'track_id',
'views_zing', 'views_nct', 'views_keeng',
'point_zing', 'point_keeng', 'total_point',
'order'
'point_zing', 'point_nct', 'point_keeng', 'total_point',
'ranking'
];
}
<?php
namespace App\Models;
namespace App\Models\Report;
use App\Helpers\Constants;
use Illuminate\Database\Eloquent\Model;
......@@ -12,10 +12,10 @@ class MonthlyReport extends Model
public $timestamps = true;
protected $fillable = [
'date', 'day', 'month', 'year',
'month', 'year',
'track_id',
'views_zing', 'views_nct', 'views_keeng',
'point_zing', 'point_keeng', 'total_point',
'point_zing', 'point_nct', 'point_keeng', 'total_point',
'order'
];
......
<?php
namespace App\Models;
namespace App\Models\Report;
use App\Helpers\Constants;
use Illuminate\Database\Eloquent\Model;
......@@ -12,10 +12,10 @@ class WeeklyReport extends Model
public $timestamps = true;
protected $fillable = [
'date', 'day', 'month', 'year',
'week', 'month', 'year',
'track_id',
'views_zing', 'views_nct', 'views_keeng',
'point_zing', 'point_keeng', 'total_point',
'point_zing', 'point_nct', 'point_keeng', 'total_point',
'order'
];
......
<?php
namespace App\Models\View;
use App\Helpers\Constants;
use Illuminate\Database\Eloquent\Model;
class DailyReportView extends Model
{
protected $table = Constants::VIEW_TABLE_DAILY_REPORT_VIEW;
public $timestamps = false;
protected $fillable = [
'track_id',
'date_zing', 'date_nct', 'date_keeng', 'total_listen',
'point_zing', 'point_nct', 'point_keeng', 'total_point'
];
}
<?php
namespace App\Models\View;
use App\Helpers\Constants;
use Illuminate\Database\Eloquent\Model;
class MonthlyReportView extends Model
{
protected $table = Constants::VIEW_TABLE_MONTHLY_REPORT_VIEW;
public $timestamps = false;
protected $fillable = [
'track_id',
'month_zing', 'month_nct', 'month_keeng', 'total_listen',
'point_zing', 'point_nct', 'point_keeng', 'total_point'
];
}
<?php
namespace App\Models\View;
use App\Helpers\Constants;
use Illuminate\Database\Eloquent\Model;
class WeeklyReportView extends Model
{
protected $table = Constants::VIEW_TABLE_WEEKLY_REPORT_VIEW;
public $timestamps = false;
protected $fillable = [
'track_id',
'week_zing', 'week_nct', 'week_keeng', 'total_listen',
'point_zing', 'point_nct', 'point_keeng', 'total_point'
];
}
......@@ -52,6 +52,9 @@ return [
'prefix' => '',
'strict' => false,
'engine' => null,
'options' => [
\PDO::ATTR_EMULATE_PREPARES => true
]
],
'pgsql' => [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment