mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-23 19:37:23 +08:00
Announce Log
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
use function PHPUnit\Framework\matches;
|
||||
|
||||
enum AnnounceEventEnum: string
|
||||
{
|
||||
case STARTED = "started";
|
||||
case STOPPED = "stopped";
|
||||
case PAUSED = "paused";
|
||||
case COMPLETED = "completed";
|
||||
case NONE = "none";
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::STARTED => nexus_trans("announce_log.events.started"),
|
||||
self::STOPPED => nexus_trans("announce_log.events.stopped"),
|
||||
self::PAUSED => nexus_trans("announce_log.events.paused"),
|
||||
self::COMPLETED => nexus_trans("announce_log.events.completed"),
|
||||
self::NONE => nexus_trans("announce_log.events.none"),
|
||||
default => '',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace App\Filament\Pages;
|
||||
|
||||
use App\Filament\Widgets\AnnounceMonitor\MaxUploadedUser;
|
||||
use Illuminate\Contracts\Support\Htmlable;
|
||||
|
||||
class AnnounceMonitor extends \Filament\Pages\Dashboard
|
||||
{
|
||||
protected ?string $maxContentWidth = 'full';
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-archive-box';
|
||||
|
||||
protected static string $routePath = 'announce-monitor';
|
||||
|
||||
protected static ?string $navigationGroup = 'Torrent';
|
||||
|
||||
protected static ?int $navigationSort = 15;
|
||||
|
||||
public function getTitle(): string|Htmlable
|
||||
{
|
||||
return self::getNavigationLabel();
|
||||
}
|
||||
|
||||
public static function getNavigationLabel(): string
|
||||
{
|
||||
return __('admin.sidebar.announce_monitor');
|
||||
}
|
||||
|
||||
public function getWidgets(): array
|
||||
{
|
||||
return [
|
||||
MaxUploadedUser::class,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Torrent;
|
||||
|
||||
use App\Filament\Resources\Torrent\AnnounceLogResource\Pages;
|
||||
use App\Filament\Resources\Torrent\AnnounceLogResource\RelationManagers;
|
||||
use App\Models\AnnounceLog;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
use Filament\Infolists;
|
||||
use Filament\Infolists\Infolist;
|
||||
|
||||
class AnnounceLogResource extends Resource
|
||||
{
|
||||
protected static ?string $model = AnnounceLog::class;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
|
||||
|
||||
protected static ?string $navigationGroup = 'Torrent';
|
||||
|
||||
protected static ?int $navigationSort = 5;
|
||||
|
||||
public static function getNavigationLabel(): string
|
||||
{
|
||||
return __('admin.sidebar.announce_logs');
|
||||
}
|
||||
|
||||
public static function getBreadcrumb(): string
|
||||
{
|
||||
return self::getNavigationLabel();
|
||||
}
|
||||
|
||||
public static function infolist(Infolist $infolist): Infolist
|
||||
{
|
||||
return $infolist
|
||||
->schema([
|
||||
Infolists\Components\TextEntry::make('timestamp')->label(__('announce-log.timestamp')),
|
||||
Infolists\Components\TextEntry::make('request_id')->label(__('announce-log.request_id'))->copyable(),
|
||||
Infolists\Components\TextEntry::make('user_id')->label(__('announce-log.user_id'))->copyable(),
|
||||
|
||||
|
||||
Infolists\Components\TextEntry::make('torrent_id')->label(__('announce-log.torrent_id'))->copyable(),
|
||||
Infolists\Components\TextEntry::make('torrent_size')->label(__('announce-log.torrent_size'))->formatStateUsing(fn($state) => mksize($state)),
|
||||
Infolists\Components\TextEntry::make('peer_id')->label(__('announce-log.peer_id'))->copyable(),
|
||||
|
||||
Infolists\Components\TextEntry::make('announce_time')->label(__('announce-log.announce_time'))->copyable(),
|
||||
Infolists\Components\TextEntry::make('seeder_count')->label(__('announce-log.seeder_count')),
|
||||
Infolists\Components\TextEntry::make('leecher_count')->label(__('announce-log.leecher_count')),
|
||||
|
||||
Infolists\Components\TextEntry::make('uploaded_offset')->label(__('announce-log.uploaded_offset'))->formatStateUsing(fn($state) => mksize($state)),
|
||||
Infolists\Components\TextEntry::make('uploaded_total')->label(__('announce-log.uploaded_total'))->formatStateUsing(fn($state) => mksize($state)),
|
||||
Infolists\Components\TextEntry::make('uploaded_increment')->label(__('announce-log.uploaded_increment'))->formatStateUsing(fn($state) => mksize($state)),
|
||||
|
||||
Infolists\Components\TextEntry::make('downloaded_offset')->label(__('announce-log.downloaded_offset'))->formatStateUsing(fn($state) => mksize($state)),
|
||||
Infolists\Components\TextEntry::make('downloaded_total')->label(__('announce-log.downloaded_total'))->formatStateUsing(fn($state) => mksize($state)),
|
||||
Infolists\Components\TextEntry::make('downloaded_increment')->label(__('announce-log.downloaded_increment'))->formatStateUsing(fn($state) => mksize($state)),
|
||||
|
||||
Infolists\Components\TextEntry::make('left')->label(__('announce-log.left'))->formatStateUsing(fn($state) => mksize($state)),
|
||||
Infolists\Components\TextEntry::make('port')->label(__('announce-log.port')),
|
||||
Infolists\Components\TextEntry::make('agent')->label(__('announce-log.agent')),
|
||||
|
||||
Infolists\Components\TextEntry::make('started')->label(__('announce-log.started')),
|
||||
Infolists\Components\TextEntry::make('last_action')->label(__('announce-log.last_action')),
|
||||
Infolists\Components\TextEntry::make('prev_action')->label(__('announce-log.prev_action')),
|
||||
|
||||
|
||||
Infolists\Components\TextEntry::make('scheme')->label(__('announce-log.scheme')),
|
||||
Infolists\Components\TextEntry::make('host')->label(__('announce-log.host')),
|
||||
Infolists\Components\TextEntry::make('path')->label(__('announce-log.path')),
|
||||
Infolists\Components\TextEntry::make('ip')->label(__('announce-log.ip'))->copyable(),
|
||||
Infolists\Components\TextEntry::make('ipv4')->label(__('announce-log.ipv4'))->copyable(),
|
||||
Infolists\Components\TextEntry::make('ipv6')->label(__('announce-log.ipv6'))->copyable(),
|
||||
Infolists\Components\TextEntry::make('continent')->label(__('announce-log.continent')),
|
||||
Infolists\Components\TextEntry::make('country')->label(__('announce-log.country')),
|
||||
Infolists\Components\TextEntry::make('city')->label(__('announce-log.city')),
|
||||
|
||||
Infolists\Components\TextEntry::make('event')->label(__('announce-log.event')),
|
||||
Infolists\Components\TextEntry::make('passkey')->label(__('announce-log.passkey'))->copyable(),
|
||||
Infolists\Components\TextEntry::make('client_select')->label(__('announce-log.client_select')),
|
||||
])->columns(3);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('timestamp')->label(__('announce-log.timestamp'))->sortable(),
|
||||
Tables\Columns\TextColumn::make('user_id')->label(__('announce-log.user_id')),
|
||||
Tables\Columns\TextColumn::make('torrent_id')->label(__('announce-log.torrent_id')),
|
||||
Tables\Columns\TextColumn::make('peer_id')->label(__('announce-log.peer_id')),
|
||||
Tables\Columns\TextColumn::make('torrent_size')
|
||||
->label(__('announce-log.torrent_size'))
|
||||
->formatStateUsing(fn ($state): string => mksize($state))
|
||||
,
|
||||
Tables\Columns\TextColumn::make('uploaded_total')
|
||||
->label(__('announce-log.uploaded_total'))
|
||||
->formatStateUsing(fn ($state): string => mksize($state))
|
||||
->sortable()
|
||||
,
|
||||
Tables\Columns\TextColumn::make('uploaded_increment')
|
||||
->label(__('announce-log.uploaded_increment'))
|
||||
->formatStateUsing(fn ($state): string => mksize($state))
|
||||
->sortable()
|
||||
,
|
||||
Tables\Columns\TextColumn::make('downloaded_total')
|
||||
->label(__('announce-log.downloaded_total'))
|
||||
->formatStateUsing(fn ($state): string => mksize($state))
|
||||
->sortable()
|
||||
,
|
||||
Tables\Columns\TextColumn::make('downloaded_increment')
|
||||
->label(__('announce-log.downloaded_increment'))
|
||||
->formatStateUsing(fn ($state): string => mksize($state))
|
||||
->sortable()
|
||||
,
|
||||
Tables\Columns\TextColumn::make('left')
|
||||
->label(__('announce-log.left'))
|
||||
->formatStateUsing(fn ($state): string => mksize($state))
|
||||
->sortable()
|
||||
,
|
||||
Tables\Columns\TextColumn::make('announce_time')
|
||||
->label(__('announce-log.announce_time'))
|
||||
->sortable()
|
||||
,
|
||||
Tables\Columns\TextColumn::make('event')->label(__('announce-log.event')),
|
||||
Tables\Columns\TextColumn::make('ip')->label('IP'),
|
||||
// Tables\Columns\TextColumn::make('agent')->label(__('announce-log.agent')),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\Filter::make('user_id')
|
||||
->form([
|
||||
Forms\Components\TextInput::make('user_id')
|
||||
->label(__('announce-log.user_id'))
|
||||
->numeric()
|
||||
,
|
||||
])
|
||||
,
|
||||
Tables\Filters\Filter::make('torrent_id')
|
||||
->form([
|
||||
Forms\Components\TextInput::make('torrent_id')
|
||||
->label(__('announce-log.torrent_id'))
|
||||
->numeric()
|
||||
,
|
||||
])
|
||||
,
|
||||
Tables\Filters\Filter::make('peer_id')
|
||||
->form([
|
||||
Forms\Components\TextInput::make('peer_id')
|
||||
->label(__('announce-log.peer_id'))
|
||||
,
|
||||
])
|
||||
,
|
||||
Tables\Filters\Filter::make('ip')
|
||||
->form([
|
||||
Forms\Components\TextInput::make('ip')
|
||||
->label('IP')
|
||||
,
|
||||
])
|
||||
,
|
||||
Tables\Filters\Filter::make('event')
|
||||
->form([
|
||||
Forms\Components\Select::make('event')
|
||||
->label(__('announce-log.event'))
|
||||
->options(AnnounceLog::listEvents())
|
||||
,
|
||||
])
|
||||
,
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\ViewAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
// Tables\Actions\BulkActionGroup::make([
|
||||
// Tables\Actions\DeleteBulkAction::make(),
|
||||
// ]),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListAnnounceLogs::route('/'),
|
||||
// 'create' => Pages\CreateAnnounceLog::route('/create'),
|
||||
// 'edit' => Pages\EditAnnounceLog::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Torrent\AnnounceLogResource\Pages;
|
||||
|
||||
use App\Filament\Resources\Torrent\AnnounceLogResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateAnnounceLog extends CreateRecord
|
||||
{
|
||||
protected static string $resource = AnnounceLogResource::class;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Torrent\AnnounceLogResource\Pages;
|
||||
|
||||
use App\Filament\Resources\Torrent\AnnounceLogResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditAnnounceLog extends EditRecord
|
||||
{
|
||||
protected static string $resource = AnnounceLogResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Torrent\AnnounceLogResource\Pages;
|
||||
|
||||
use App\Filament\PageListSingle;
|
||||
use App\Filament\Resources\Torrent\AnnounceLogResource;
|
||||
use App\Models\AnnounceLog;
|
||||
use App\Repositories\AnnounceLogRepository;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Illuminate\Contracts\Pagination\CursorPaginator;
|
||||
use Illuminate\Contracts\Pagination\Paginator;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
class ListAnnounceLogs extends PageListSingle
|
||||
{
|
||||
protected static string $resource = AnnounceLogResource::class;
|
||||
|
||||
public function getTableRecords(): Collection|Paginator|CursorPaginator
|
||||
{
|
||||
$filterableColumns = ['user_id', 'torrent_id', 'peer_id', 'ip', 'event'];
|
||||
$sortableColumns = ['timestamp', 'uploaded_total', 'uploaded_increment', 'downloaded_total', 'downloaded_increment', 'left', 'announce_time'];
|
||||
$sortableDirections = ['asc', 'desc'];
|
||||
$request = request();
|
||||
// dd($request->all());
|
||||
$filters = [];
|
||||
foreach ($request->get('tableFilters', []) as $field => $values) {
|
||||
if (!in_array($field, $filterableColumns)) {
|
||||
continue;
|
||||
}
|
||||
foreach ($values as $k => $v) {
|
||||
if (in_array($k, $filterableColumns)) {
|
||||
$filters[$field] = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
$page = $request->get('page', 1);
|
||||
$perPage = $request->get('per_page', 10);
|
||||
$sortColumn = null;
|
||||
$sortDirection = null;
|
||||
$sortColumnFromQuery = $request->get("tableSortColumn");
|
||||
$sortDirectionFromQuery = $request->get("tableSortDirection");
|
||||
if (in_array($sortColumnFromQuery, $sortableColumns)) {
|
||||
$sortColumn = $sortColumnFromQuery;
|
||||
}
|
||||
if (in_array($sortDirectionFromQuery, $sortableDirections)) {
|
||||
$sortDirection = $sortDirectionFromQuery;
|
||||
}
|
||||
|
||||
$sorts = [];
|
||||
foreach ($request->input('components', []) as $component) {
|
||||
$snapshot = json_decode($component['snapshot'], true);
|
||||
// do_log("snapshot: " . $component['snapshot']);
|
||||
if (isset($snapshot['data']['tableRecordsPerPage'])) {
|
||||
$perPage = $snapshot['data']['tableRecordsPerPage'];
|
||||
}
|
||||
if (isset($snapshot['data']['tableSortColumn']) && in_array($snapshot['data']['tableSortColumn'], $sortableColumns)) {
|
||||
$sortColumn = $snapshot['data']['tableSortColumn'];
|
||||
}
|
||||
if (isset($snapshot['data']['tableSortDirection']) && in_array($snapshot['data']['tableSortDirection'], $sortableDirections)) {
|
||||
$sortDirection = $snapshot['data']['tableSortDirection'];
|
||||
}
|
||||
if ($sortColumn && $sortDirection) {
|
||||
$sorts[$sortColumn] = $sortDirection;
|
||||
}
|
||||
if (isset($snapshot['data']['paginators'])) {
|
||||
foreach ($snapshot['data']['paginators'] as $paginator) {
|
||||
if (isset($paginator['page'])) {
|
||||
$page = $paginator['page'];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isset($snapshot['data']['tableFilters'])) {
|
||||
// dd($snapshot['data']['tableFilters']);
|
||||
foreach ($snapshot['data']['tableFilters'] as $filterItems) {
|
||||
foreach ($filterItems as $field => $items) {
|
||||
if (!in_array($field, $filterableColumns) || !is_array($items)) {
|
||||
continue;
|
||||
}
|
||||
foreach ($items as $values) {
|
||||
if (!is_array($values)) {
|
||||
continue;
|
||||
}
|
||||
foreach ($values as $subField => $value) {
|
||||
if ($field == $subField && $value !== null) {
|
||||
$filters[$field] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// do_log("updates: " . json_encode($component['updates'] ?? []));
|
||||
if (isset($component['updates']['tableRecordsPerPage'])) {
|
||||
$perPage = $component['updates']['tableRecordsPerPage'];
|
||||
}
|
||||
// do_log("calls: " . json_encode($component['calls'] ?? []));
|
||||
if (isset($component['calls'])) {
|
||||
foreach ($component['calls'] as $call) {
|
||||
if ($call['method'] == "gotoPage") {
|
||||
$page = $call['params'][0];
|
||||
}
|
||||
if ($call['method'] == "sortTable") {
|
||||
if (!in_array($call['params'][0], $sortableColumns)) {
|
||||
continue;
|
||||
}
|
||||
$sortColumn = $call['params'][0];
|
||||
if (!isset($sorts[$sortColumn])) {
|
||||
$sortDirection = "asc";
|
||||
} elseif ($sorts[$sortColumn] == "asc") {
|
||||
$sortDirection = "desc";
|
||||
} elseif ($sorts[$sortColumn] == "desc") {
|
||||
$sortDirection = null;
|
||||
}
|
||||
}
|
||||
if ($call['method'] == "resetTableFiltersForm") {
|
||||
$filters = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($filterableColumns as $field) {
|
||||
if (isset($component['updates']["tableFilters.$field.$field"])) {
|
||||
$filters[$field] = $component['updates']["tableFilters.$field.$field"];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
$rep = new AnnounceLogRepository();
|
||||
$result = $rep->listAll($filters, $page, $perPage, $sortColumn, $sortDirection);
|
||||
|
||||
// 转换数据格式以适配 Filament 表格
|
||||
$items = [];
|
||||
foreach ($result['data'] as $announceLog) {
|
||||
$model = new AnnounceLog($announceLog);
|
||||
$items[] = $model;
|
||||
}
|
||||
return new LengthAwarePaginator($items, $result['total'], $perPage, $page);
|
||||
}
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
// Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function resolveTableRecord(?string $key): ?Model
|
||||
{
|
||||
$rep = new AnnounceLogRepository();
|
||||
return $rep->getById($key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets\AnnounceMonitor;
|
||||
|
||||
use App\Models\AnnounceLog;
|
||||
use App\Repositories\AnnounceLogRepository;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Widgets\TableWidget as BaseWidget;
|
||||
use Illuminate\Contracts\Pagination\CursorPaginator;
|
||||
use Illuminate\Contracts\Pagination\Paginator;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\HtmlString;
|
||||
|
||||
class MaxUploadedUser extends BaseWidget
|
||||
{
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->recordTitle("ssss")
|
||||
->heading(fn () => __('announce-monitor.max_uploaded_user', ['interval' => ' 1 ' . __('nexus.time_units.hour')]))
|
||||
->query(AnnounceLog::query())
|
||||
->defaultPaginationPageOption(null)
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('user_id')
|
||||
->label(__('announce-log.user_id'))
|
||||
->formatStateUsing(fn ($state) => username_for_admin($state))
|
||||
,
|
||||
Tables\Columns\TextColumn::make('uploaded_total')
|
||||
->label(__('announce-log.uploaded_total'))
|
||||
->formatStateUsing(fn ($state) => mksize($state))
|
||||
,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getTableRecords(): Collection|Paginator|CursorPaginator
|
||||
{
|
||||
$rep = new AnnounceLogRepository();
|
||||
$list = $rep->listMaxUploadedUser(1);
|
||||
$items = [];
|
||||
foreach ($list as $index => $item) {
|
||||
$record = new AnnounceLog($item);
|
||||
$record->request_id = $index;
|
||||
$items[] = $record;
|
||||
}
|
||||
return new Collection($items);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\AnnounceEventEnum;
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
|
||||
class AnnounceLog extends NexusModel
|
||||
{
|
||||
protected $fillable = [
|
||||
'timestamp', 'user_id', 'passkey', 'torrent_id', 'info_hash', 'torrent_size', 'event', 'peer_id',
|
||||
'uploaded_total', 'uploaded_increment', 'uploaded_offset',
|
||||
'downloaded_total', 'downloaded_increment', 'downloaded_offset',
|
||||
'announce_time', 'ip', 'ipv4', 'ipv6', 'port', 'agent', 'left', 'started', 'prev_action', 'last_action',
|
||||
'client_select', 'seeder_count', 'leecher_count', 'scheme', 'host', 'path',
|
||||
'continent', 'country', 'city', 'request_id'
|
||||
];
|
||||
|
||||
protected $table = null;
|
||||
protected $primaryKey = "request_id";
|
||||
|
||||
protected $keyType = 'string';
|
||||
|
||||
protected function timestamp(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
set: function (?string $value) {
|
||||
return $this->toLocaleTime($value, "Y-m-d H:i:s.u");
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
protected function started(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
set: function (?string $value) {
|
||||
return $this->toLocaleTime($value);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
protected function prevAction(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
set: function (?string $value) {
|
||||
return $this->toLocaleTime($value);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
protected function lastAction(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
set: function (string $value) {
|
||||
return $this->toLocaleTime($value);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
private function toLocaleTime(?string $time, string $format = "Y-m-d H:i:s"): ?string
|
||||
{
|
||||
static $fromTimezone;
|
||||
static $toTimezone;
|
||||
if ($fromTimezone == null) {
|
||||
$fromTimezone = new DateTimeZone('UTC');
|
||||
}
|
||||
if ($toTimezone == null) {
|
||||
$toTimezone = new DateTimeZone(config('app.timezone'));
|
||||
}
|
||||
if (empty($time)) {
|
||||
return $time;
|
||||
}
|
||||
// 创建 DateTime 对象
|
||||
$date = DateTime::createFromFormat($format, $time, $fromTimezone);
|
||||
// 转换时区
|
||||
$date->setTimezone($toTimezone);
|
||||
// 输出转换后的时间
|
||||
return $date->format($format);
|
||||
}
|
||||
|
||||
public static function listEvents(): array
|
||||
{
|
||||
$result = [];
|
||||
foreach (AnnounceEventEnum::cases() as $event) {
|
||||
$result[$event->value] = $event->value;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -258,5 +258,10 @@ class Setting extends NexusModel
|
||||
return (int)self::get("backup.retention_count");
|
||||
}
|
||||
|
||||
public static function getIsRecordAnnounceLog(): bool
|
||||
{
|
||||
return self::get('security.record_announce_logs') == 'yes';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\AnnounceLog;
|
||||
use ClickHouseDB\Client;
|
||||
use Nexus\Database\NexusDB;
|
||||
|
||||
class AnnounceLogRepository extends BaseRepository
|
||||
{
|
||||
const TABLE = 'announce_logs';
|
||||
|
||||
public function listAll(array $filters, int $page, int $perPage, ?string $sortColumn, ?string $sortDirection)
|
||||
{
|
||||
$beginTimestamp = microtime(true);
|
||||
$totalAlias = "total";
|
||||
$offset = ($page - 1) * $perPage;
|
||||
$client = $this->getClient();
|
||||
$bindFields = $bindValues = [];
|
||||
foreach ($filters as $key => $value) {
|
||||
$bindFields[] = "$key = :$key";
|
||||
if ($key == "event" && $value == "none") {
|
||||
$value = "";
|
||||
}
|
||||
$bindValues[$key] = $value;
|
||||
}
|
||||
$selectPrefix = sprintf("select * from %s", self::TABLE);
|
||||
$countPrefix = sprintf("select count(*) as %s from %s", $totalAlias, self::TABLE);
|
||||
$whereStr = "";
|
||||
if (count($bindFields) > 0) {
|
||||
$whereStr = " where " . implode(" and ", $bindFields);
|
||||
}
|
||||
$selectSql = sprintf(
|
||||
"%s %s order by %s %s limit %d offset %d",
|
||||
$selectPrefix, $whereStr, $sortColumn ?: "timestamp", $sortDirection ?: "desc", $perPage, $offset
|
||||
);
|
||||
$countSql = sprintf("%s %s", $countPrefix, $whereStr);
|
||||
$data = $client->select($selectSql, $bindValues);
|
||||
$total = $client->select($countSql, $bindValues)->rows()[0][$totalAlias] ?? 0;
|
||||
do_log(sprintf(
|
||||
"[REQUEST_CLICKHOUSE], filters: %s, page: %s, perPage: %s, sortColumn: %s, sortDirection: %s, selectSql: %s, binds: %s, costTime: %.3f sec.",
|
||||
json_encode($filters), $page, $perPage, $sortColumn, $sortDirection, $selectSql, json_encode($bindValues), microtime(true) - $beginTimestamp
|
||||
));
|
||||
return [
|
||||
'data' => $data->rows(),
|
||||
'total' => (int)$total,
|
||||
'page' => $page,
|
||||
'perPage' => $perPage,
|
||||
];
|
||||
}
|
||||
|
||||
private function getClient(): Client
|
||||
{
|
||||
return app(Client::class);
|
||||
}
|
||||
|
||||
public function getById(?string $id): ?AnnounceLog
|
||||
{
|
||||
if (empty($id)) {
|
||||
return null;
|
||||
}
|
||||
$sql = sprintf("select * from %s where request_id = :id limit 1", self::TABLE);
|
||||
$statement = $this->getClient()->select($sql, ['id' => $id]);
|
||||
$arr = $statement->fetchOne();
|
||||
return $arr ? new AnnounceLog($arr) : null;
|
||||
}
|
||||
|
||||
public function listMaxUploadedUser(int $hours)
|
||||
{
|
||||
$sql = sprintf(
|
||||
"select user_id, sum(uploaded_increment) as uploaded_total from %s where timestamp >= now() - INTERVAL %d HOUR group by user_id order by uploaded_total desc limit 5",
|
||||
self::TABLE, $hours
|
||||
);
|
||||
$data = $this->getClient()->select($sql);
|
||||
return $data->rows();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -128,7 +128,7 @@ class SeedBoxRepository extends BaseRepository
|
||||
return SeedBoxRecord::query()->whereIn('id', Arr::wrap($id))->where('uid', $uid)->delete();
|
||||
}
|
||||
|
||||
public function updateStatus(SeedBoxRecord $seedBoxRecord, $status, $reason = ''): bool
|
||||
public function updateStatus(SeedBoxRecord $seedBoxRecord, $status, $reason = '')
|
||||
{
|
||||
if (Auth::user()->class < User::CLASS_ADMINISTRATOR) {
|
||||
throw new InsufficientPermissionException();
|
||||
|
||||
Reference in New Issue
Block a user