api peer list

This commit is contained in:
xiaomlove
2021-05-16 14:44:02 +08:00
parent d2bb1e7944
commit 6ccf135b6c
4 changed files with 119 additions and 97 deletions

View File

@@ -14,69 +14,19 @@ class PeerResource extends JsonResource
*/
public function toArray($request)
{
$seconds = $this->started->diff($this->last_action)->s;
if ($this->uploaded == 0) {
$uploadSpeed = mksize(0) . '/s';
} else {
$uploadSpeed = mksize(($this->uploaded - $this->uploadoffset) / $seconds) . '/s';
}
$nowTimestamp = time();
return [
'id' => $this->id,
'connectable_text' => $this->connectableText,
'upload_text' => sprintf('%s@%s', mksize($this->uploaded), $uploadSpeed),
'download_text' => sprintf('%s@%s', mksize($this->downloaded), $this->getUploadSpeed($this->resource)),
'share_ratio' => $this->getShareRatio($this->resource),
'download_progress' => $this->getDownloadProgress($this->resource),
'connect_time_total' => mkprettytime($nowTimestamp - $this->started->timestamp),
'last_action_human' => mkprettytime($nowTimestamp - $this->last_action->timestamp),
'agent_human' => htmlspecialchars(get_agent($this->peer_id, $this->agent)),
'upload_text' => $this->upload_text,
'download_text' => $this->download_text,
'share_ratio' => $this->share_ratio,
'download_progress' => $this->download_progress,
'connect_time_total' => $this->connect_time_total,
'last_action_human' => $this->last_action_human,
'agent_human' => $this->agent_human,
'user' => new UserResource($this->whenLoaded('user')),
];
}
/**
* 获得上传速度
*
* @see viewpeerlist.php
*
* @param $peer
* @return string
*/
protected function getUploadSpeed($peer)
{
$diff = $peer->downloaded - $peer->downloadoffset;
if ($peer->isSeeder()) {
$seconds = $peer->finishedat - $peer->started->getTimestamp();
} else {
$seconds = $this->started->diff($this->last_action)->s;
}
return mksize($diff / $seconds) . '/s';
}
protected function getShareRatio($peer)
{
if ($peer->downloaded) {
$ratio = floor(($peer->uploaded / $peer->downloaded) * 1000) / 1000;
} elseif ($peer->uploaded) {
//@todo 读语言文件
$ratio = '无限';
} else {
$ratio = '---';
}
return $ratio;
}
protected function getDownloadProgress($peer)
{
return sprintf("%.2f%%", 100 * (1 - ($peer->to_go / $peer->relative_torrent->size)));
}
}