diff --git a/app/Http/Controllers/SnatchController.php b/app/Http/Controllers/SnatchController.php index 9bedc036..3a92f19e 100644 --- a/app/Http/Controllers/SnatchController.php +++ b/app/Http/Controllers/SnatchController.php @@ -16,7 +16,10 @@ class SnatchController extends Controller */ public function index(Request $request) { - $torrentId = $request->torrent_id; + $request->validate([ + 'torrent_id' => 'required', + ]); + $snatches = Snatch::query()->where('torrentid', $torrentId)->with(['user'])->paginate(); $resource = SnatchResource::collection($snatches); $resource->additional(['card_titles' => Snatch::$cardTitles]); diff --git a/app/Http/Resources/SnatchResource.php b/app/Http/Resources/SnatchResource.php index 0c2e7ca1..b33c5c86 100644 --- a/app/Http/Resources/SnatchResource.php +++ b/app/Http/Resources/SnatchResource.php @@ -15,23 +15,15 @@ class SnatchResource extends PeerResource */ public function toArray($request) { - $uploaded = mksize($this->uploaded); - $downloaded = mksize($this->downloaded); - $seedtime = mkprettytime($this->seedtime); - $leechtime = mkprettytime($this->leechtime); - $uprate = $this->seedtime > 0 ? mksize($this->uploaded / ($this->seedtime + $this->leechtime)) : mksize(0); - $downrate = $this->leechtime > 0 ? mksize($this->downloaded / $this->leechtime) : mksize(0); - $nowTimestamp = time(); - return [ 'id' => $this->id, - 'upload_text' => $uploaded . "@" . $uprate . "/s", - 'download_text' => $downloaded . "@" . $downrate . "/s", - 'share_ratio' => $this->getShareRatio($this->resource), - 'seed_time' => $seedtime, - 'leech_time' => $leechtime, - 'completed_at_human' => mkprettytime($nowTimestamp - $this->completedat->timestamp), - 'last_action_human' => mkprettytime($nowTimestamp - $this->last_action->timestamp), + 'upload_text' => $this->upload_text, + 'download_text' => $this->download_text, + 'share_ratio' => $this->share_ratio, + 'seed_time' => $this->seed_time, + 'leech_time' => $this->leech_time, + 'completed_at_human' => $this->completed_at_human, + 'last_action_human' => $this->last_action_human, 'user' => new UserResource($this->whenLoaded('user')), ]; } diff --git a/app/Repositories/TorrentRepository.php b/app/Repositories/TorrentRepository.php index 53c26bf7..cc7047e5 100644 --- a/app/Repositories/TorrentRepository.php +++ b/app/Repositories/TorrentRepository.php @@ -8,6 +8,7 @@ use App\Models\Codec; use App\Models\Media; use App\Models\Peer; use App\Models\Processing; +use App\Models\Snatch; use App\Models\Source; use App\Models\Standard; use App\Models\Team; @@ -162,14 +163,14 @@ class TorrentRepository extends BaseRepository } - public function getUploadSpeed($peer): string + public function getPeerUploadSpeed($peer): string { $diff = $peer->uploaded - $peer->uploadoffset; $seconds = max(1, $peer->started->diffInSeconds($peer->last_action)); return mksize($diff / $seconds) . '/s'; } - public function getDownloadSpeed($peer): string + public function getPeerDownloadSpeed($peer): string { $diff = $peer->downloaded - $peer->downloadoffset; if ($peer->isSeeder()) { @@ -202,8 +203,8 @@ class TorrentRepository extends BaseRepository { $nowTimestamp = time(); foreach ($peers as &$item) { - $item->upload_text = sprintf('%s@%s', mksize($item->uploaded), $this->getUploadSpeed($item)); - $item->download_text = sprintf('%s@%s', mksize($item->downloaded), $this->getDownloadSpeed($item)); + $item->upload_text = sprintf('%s@%s', mksize($item->uploaded), $this->getPeerUploadSpeed($item)); + $item->download_text = sprintf('%s@%s', mksize($item->downloaded), $this->getPeerDownloadSpeed($item)); $item->download_progress = $this->getDownloadProgress($item); $item->share_ratio = $this->getShareRatio($item); $item->connect_time_total = mkprettytime($nowTimestamp - $item->started->timestamp); @@ -213,4 +214,41 @@ class TorrentRepository extends BaseRepository return $peers; } + + public function listSnatches($torrentId) + { + $snatches = Snatch::query() + ->where('torrentid', $torrentId) + ->with(['user']) + ->orderBy('completedat', 'desc') + ->get(); + $nowTimestamp = time(); + foreach ($snatches as &$snatch) { + $snatch->upload_text = sprintf('%s@%s', mksize($snatch->uploaded), $this->getSnatchUploadSpeed($snatch)); + $snatch->download_text = sprintf('%s@%s', mksize($snatch->uploaded), $this->getSnatchDownloadSpeed($snatch)); + $snatch->share_ratio = $this->getShareRatio($snatch); + $snatch->seed_time = mkprettytime($snatch->seedtime); + $snatch->leech_time = mkprettytime($snatch->leechtime); + $snatch->completed_at_human = mkprettytime($nowTimestamp - $snatch->completedat->timestamp); + $snatch->last_action_human = mkprettytime($nowTimestamp - $snatch->last_action->timestamp); + } + return $snatches; + } + + public function getSnatchUploadSpeed($snatch) + { + if ($snatch->seedtime <= 0) { + return mksize(0); + } + return mksize($snatch->uploaded / ($snatch->seedtime + $snatch->leechtime)); + } + + public function getSnatchDownloadSpeed($snatch) + { + if ($snatch->leechtime <= 0) { + return mksize(0); + } + return mksize($snatch->downloaded / $snatch->leechtime); + } + }