mirror of
https://github.com/v2board/v2board.git
synced 2025-06-15 22:27:50 +08:00
update: log api
This commit is contained in:
@ -36,11 +36,13 @@ class StatController extends Controller
|
||||
->get()
|
||||
->makeHidden(['record_at', 'created_at', 'updated_at', 'id', 'record_type'])
|
||||
->toArray();
|
||||
} else {
|
||||
$statisticalService = new StatisticalService();
|
||||
return [
|
||||
'data' => $statisticalService->generateStatData()
|
||||
];
|
||||
}
|
||||
|
||||
$statisticalService = new StatisticalService();
|
||||
$stats = array_merge($stats ?? [], [$statisticalService->generateStatData()]);
|
||||
|
||||
$stats = array_reduce($stats, function($carry, $item) {
|
||||
foreach($item as $key => $value) {
|
||||
if(isset($carry[$key]) && $carry[$key]) {
|
||||
@ -60,9 +62,9 @@ class StatController extends Controller
|
||||
public function getStatRecord(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'type' => 'required|in:order_total,commission_total,register_count',
|
||||
'type' => 'required|in:paid_total,commission_total,register_count',
|
||||
'start_at' => '',
|
||||
'end_at'
|
||||
'end_at' => ''
|
||||
]);
|
||||
|
||||
$statisticalService = new StatisticalService();
|
||||
@ -73,6 +75,23 @@ class StatController extends Controller
|
||||
];
|
||||
}
|
||||
|
||||
public function getRanking(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'type' => 'required|in:server_traffic_rank,user_consumption_rank,invite_rank',
|
||||
'start_at' => '',
|
||||
'end_at' => '',
|
||||
'limit' => 'nullable|integer'
|
||||
]);
|
||||
|
||||
$statisticalService = new StatisticalService();
|
||||
$statisticalService->setStartAt($request->input('start_at'));
|
||||
$statisticalService->setEndAt($request->input('end_at'));
|
||||
return [
|
||||
'data' => $statisticalService->getRanking($request->input('type'), $request->input('limit') ?? 20)
|
||||
];
|
||||
}
|
||||
|
||||
public function getOverride(Request $request)
|
||||
{
|
||||
return [
|
||||
|
11
app/Logging/MysqlLogger.php
Normal file
11
app/Logging/MysqlLogger.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace App\Logging;
|
||||
|
||||
class MysqlLogger
|
||||
{
|
||||
public function __invoke(array $config){
|
||||
return tap(new \Monolog\Logger('mysql'), function ($logger) {
|
||||
$logger->pushHandler(new MysqlLoggerHandler());
|
||||
});
|
||||
}
|
||||
}
|
44
app/Logging/MysqlLoggerHandler.php
Normal file
44
app/Logging/MysqlLoggerHandler.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace App\Logging;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use Monolog\Logger;
|
||||
use App\Models\Log as LogModel;
|
||||
|
||||
class MysqlLoggerHandler extends AbstractProcessingHandler
|
||||
{
|
||||
public function __construct($level = Logger::DEBUG, bool $bubble = true)
|
||||
{
|
||||
parent::__construct($level, $bubble);
|
||||
}
|
||||
|
||||
protected function write(array $record): void
|
||||
{
|
||||
try{
|
||||
if(isset($record['context']['exception']) && is_object($record['context']['exception'])){
|
||||
$record['context']['exception'] = (array)$record['context']['exception'];
|
||||
}
|
||||
$record['request_data'] = request()->all() ??[];
|
||||
$log = [
|
||||
'title' => $record['message'],
|
||||
'level' => $record['level_name'],
|
||||
'host' => $record['request_host'] ?? request()->getSchemeAndHttpHost(),
|
||||
'uri' => $record['request_uri'] ?? request()->getRequestUri(),
|
||||
'method' => $record['request_method'] ?? request()->getMethod(),
|
||||
'ip' => request()->getClientIp(),
|
||||
'data' => json_encode($record['request_data']) ,
|
||||
'context' => isset($record['context']) ? json_encode($record['context']) : '',
|
||||
'created_at' => strtotime($record['datetime']),
|
||||
'updated_at' => strtotime($record['datetime']),
|
||||
];
|
||||
|
||||
LogModel::insert(
|
||||
$log
|
||||
);
|
||||
}catch (\Exception $e){
|
||||
Log::channel('daily')->error($e->getMessage().$e->getFile().$e->getTraceAsString());
|
||||
}
|
||||
}
|
||||
}
|
16
app/Models/Log.php
Normal file
16
app/Models/Log.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Log extends Model
|
||||
{
|
||||
protected $table = 'v2_log';
|
||||
protected $dateFormat = 'U';
|
||||
protected $guarded = ['id'];
|
||||
protected $casts = [
|
||||
'created_at' => 'timestamp',
|
||||
'updated_at' => 'timestamp'
|
||||
];
|
||||
}
|
@ -209,40 +209,75 @@ class StatisticalService {
|
||||
{
|
||||
switch ($type) {
|
||||
case 'server_traffic_rank': {
|
||||
return StatServer::select([
|
||||
'server_id',
|
||||
'server_type',
|
||||
DB::raw('sum(u) as u'),
|
||||
DB::raw('sum(d) as d'),
|
||||
DB::raw('sum(u) + sum(d) as total')
|
||||
])
|
||||
->where('record_at', '>=', $this->startAt)
|
||||
->where('record_at', '<', $this->endAt)
|
||||
->groupBy('server_id', 'server_type')
|
||||
->orderBy('total', 'DESC')
|
||||
->limit($limit)
|
||||
->get();
|
||||
return $this->buildServerTrafficRank($limit);
|
||||
}
|
||||
case 'user_consumption_rank': {
|
||||
$stats = StatUser::select([
|
||||
'user_id',
|
||||
DB::raw('sum(u) as u'),
|
||||
DB::raw('sum(d) as d'),
|
||||
DB::raw('sum(u) + sum(d) as total')
|
||||
])
|
||||
->where('record_at', '>=', $this->startAt)
|
||||
->where('record_at', '<', $this->endAt)
|
||||
->groupBy('user_id')
|
||||
->orderBy('total', 'DESC')
|
||||
->limit($limit)
|
||||
->get();
|
||||
$users = User::whereIn('id', $stats->pluck('user_id')->toArray())->get()->keyBy('id');
|
||||
foreach ($stats as $k => $v) {
|
||||
if (!isset($users[$v['user_id']])) continue;
|
||||
$stats[$k]['email'] = $users[$v['user_id']]['email'];
|
||||
}
|
||||
return $stats;
|
||||
return $this->buildUserConsumptionRank($limit);
|
||||
}
|
||||
case 'invite_rank': {
|
||||
return $this->buildInviteRank($limit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function buildInviteRank($limit)
|
||||
{
|
||||
$stats = User::select([
|
||||
'invite_user_id',
|
||||
DB::raw('count(*) as count')
|
||||
])
|
||||
->where('created_at', '>=', $this->startAt)
|
||||
->where('created_at', '<', $this->endAt)
|
||||
->whereNotNull('invite_user_id')
|
||||
->groupBy('invite_user_id')
|
||||
->orderBy('count', 'DESC')
|
||||
->limit($limit)
|
||||
->get();
|
||||
|
||||
$users = User::whereIn('id', $stats->pluck('invite_user_id')->toArray())->get()->keyBy('id');
|
||||
foreach ($stats as $k => $v) {
|
||||
if (!isset($users[$v['invite_user_id']])) continue;
|
||||
$stats[$k]['email'] = $users[$v['invite_user_id']]['email'];
|
||||
}
|
||||
return $stats;
|
||||
}
|
||||
|
||||
private function buildUserConsumptionRank($limit)
|
||||
{
|
||||
$stats = StatUser::select([
|
||||
'user_id',
|
||||
DB::raw('sum(u) as u'),
|
||||
DB::raw('sum(d) as d'),
|
||||
DB::raw('sum(u) + sum(d) as total')
|
||||
])
|
||||
->where('record_at', '>=', $this->startAt)
|
||||
->where('record_at', '<', $this->endAt)
|
||||
->groupBy('user_id')
|
||||
->orderBy('total', 'DESC')
|
||||
->limit($limit)
|
||||
->get();
|
||||
$users = User::whereIn('id', $stats->pluck('user_id')->toArray())->get()->keyBy('id');
|
||||
foreach ($stats as $k => $v) {
|
||||
if (!isset($users[$v['user_id']])) continue;
|
||||
$stats[$k]['email'] = $users[$v['user_id']]['email'];
|
||||
}
|
||||
return $stats;
|
||||
}
|
||||
|
||||
private function buildServerTrafficRank($limit)
|
||||
{
|
||||
return StatServer::select([
|
||||
'server_id',
|
||||
'server_type',
|
||||
DB::raw('sum(u) as u'),
|
||||
DB::raw('sum(d) as d'),
|
||||
DB::raw('sum(u) + sum(d) as total')
|
||||
])
|
||||
->where('record_at', '>=', $this->startAt)
|
||||
->where('record_at', '<', $this->endAt)
|
||||
->groupBy('server_id', 'server_type')
|
||||
->orderBy('total', 'DESC')
|
||||
->limit($limit)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user