forked from v2board/v2board
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d70382
commit 7e5e696
Showing
6 changed files
with
200 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Jobs\StatServerJob; | ||
use Illuminate\Console\Command; | ||
use App\Models\ServerLog; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class StatServer extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'stat:server'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = '统计节点数据'; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
$endAt = strtotime(date('Y-m-d')); | ||
$startAt = strtotime('-1 day', $endAt); | ||
$statistics = ServerLog::select([ | ||
'server_id', | ||
'method as server_type', | ||
DB::raw("sum(u) as u"), | ||
DB::raw("sum(d) as d"), | ||
]) | ||
->where('log_at', '>=', $startAt) | ||
->where('log_at', '<', $endAt) | ||
->groupBy('server_id', 'method') | ||
->get() | ||
->toArray(); | ||
foreach ($statistics as $statistic) { | ||
$statistic['record_type'] = 'm'; | ||
$statistic['record_at'] = $startAt; | ||
StatServerJob::dispatch($statistic); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Models\StatServer; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class StatServerJob implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
protected $statistic; | ||
|
||
public $tries = 3; | ||
public $timeout = 5; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(array $statistic) | ||
{ | ||
$this->onQueue('stat_server'); | ||
$this->statistic = $statistic; | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
* | ||
* @return void | ||
*/ | ||
public function handle() | ||
{ | ||
$statistic = $this->statistic; | ||
$data = StatServer::where('record_at', $statistic['record_at']) | ||
->where('server_id', $statistic['server_id']) | ||
->first(); | ||
if ($data) { | ||
try { | ||
$data->update($statistic); | ||
} catch (\Exception $e) { | ||
abort(500, '节点统计数据更新失败'); | ||
} | ||
} else { | ||
if (!StatServer::create($statistic)) { | ||
abort(500, '节点统计数据创建失败'); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class StatServer extends Model | ||
{ | ||
protected $table = 'v2_stat_server'; | ||
protected $dateFormat = 'U'; | ||
protected $guarded = ['id']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
apps: | ||
- name : 'V2Board' | ||
script : 'php artisan queue:work --queue=send_email,send_telegram' | ||
script : 'php artisan queue:work --queue=send_email,send_telegram,stat_server' | ||
instances: 4 | ||
out_file : './storage/logs/queue/queue.log' |