api snatches

This commit is contained in:
xiaomlove
2021-05-17 00:10:15 +08:00
parent 3d7ab7a7dc
commit 6789e7e5ea
3 changed files with 53 additions and 20 deletions
+4 -1
View File
@@ -16,7 +16,10 @@ class SnatchController extends Controller
*/ */
public function index(Request $request) public function index(Request $request)
{ {
$torrentId = $request->torrent_id; $request->validate([
'torrent_id' => 'required',
]);
$snatches = Snatch::query()->where('torrentid', $torrentId)->with(['user'])->paginate(); $snatches = Snatch::query()->where('torrentid', $torrentId)->with(['user'])->paginate();
$resource = SnatchResource::collection($snatches); $resource = SnatchResource::collection($snatches);
$resource->additional(['card_titles' => Snatch::$cardTitles]); $resource->additional(['card_titles' => Snatch::$cardTitles]);
+7 -15
View File
@@ -15,23 +15,15 @@ class SnatchResource extends PeerResource
*/ */
public function toArray($request) 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 [ return [
'id' => $this->id, 'id' => $this->id,
'upload_text' => $uploaded . "@" . $uprate . "/s", 'upload_text' => $this->upload_text,
'download_text' => $downloaded . "@" . $downrate . "/s", 'download_text' => $this->download_text,
'share_ratio' => $this->getShareRatio($this->resource), 'share_ratio' => $this->share_ratio,
'seed_time' => $seedtime, 'seed_time' => $this->seed_time,
'leech_time' => $leechtime, 'leech_time' => $this->leech_time,
'completed_at_human' => mkprettytime($nowTimestamp - $this->completedat->timestamp), 'completed_at_human' => $this->completed_at_human,
'last_action_human' => mkprettytime($nowTimestamp - $this->last_action->timestamp), 'last_action_human' => $this->last_action_human,
'user' => new UserResource($this->whenLoaded('user')), 'user' => new UserResource($this->whenLoaded('user')),
]; ];
} }
+42 -4
View File
@@ -8,6 +8,7 @@ use App\Models\Codec;
use App\Models\Media; use App\Models\Media;
use App\Models\Peer; use App\Models\Peer;
use App\Models\Processing; use App\Models\Processing;
use App\Models\Snatch;
use App\Models\Source; use App\Models\Source;
use App\Models\Standard; use App\Models\Standard;
use App\Models\Team; 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; $diff = $peer->uploaded - $peer->uploadoffset;
$seconds = max(1, $peer->started->diffInSeconds($peer->last_action)); $seconds = max(1, $peer->started->diffInSeconds($peer->last_action));
return mksize($diff / $seconds) . '/s'; return mksize($diff / $seconds) . '/s';
} }
public function getDownloadSpeed($peer): string public function getPeerDownloadSpeed($peer): string
{ {
$diff = $peer->downloaded - $peer->downloadoffset; $diff = $peer->downloaded - $peer->downloadoffset;
if ($peer->isSeeder()) { if ($peer->isSeeder()) {
@@ -202,8 +203,8 @@ class TorrentRepository extends BaseRepository
{ {
$nowTimestamp = time(); $nowTimestamp = time();
foreach ($peers as &$item) { foreach ($peers as &$item) {
$item->upload_text = sprintf('%s@%s', mksize($item->uploaded), $this->getUploadSpeed($item)); $item->upload_text = sprintf('%s@%s', mksize($item->uploaded), $this->getPeerUploadSpeed($item));
$item->download_text = sprintf('%s@%s', mksize($item->downloaded), $this->getDownloadSpeed($item)); $item->download_text = sprintf('%s@%s', mksize($item->downloaded), $this->getPeerDownloadSpeed($item));
$item->download_progress = $this->getDownloadProgress($item); $item->download_progress = $this->getDownloadProgress($item);
$item->share_ratio = $this->getShareRatio($item); $item->share_ratio = $this->getShareRatio($item);
$item->connect_time_total = mkprettytime($nowTimestamp - $item->started->timestamp); $item->connect_time_total = mkprettytime($nowTimestamp - $item->started->timestamp);
@@ -213,4 +214,41 @@ class TorrentRepository extends BaseRepository
return $peers; 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);
}
} }