Skip to content

Commit e15f54b

Browse files
author
spaceAngel
committed
[daemon] [gui] downloads calcs (estimated interval, speed) now on daemon side #optimalization
1 parent ac020f5 commit e15f54b

4 files changed

Lines changed: 32 additions & 12 deletions

File tree

gui/snippets/mainpanel/downloads.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
<% download.bytesReceived / 1024| bytesToHuman %> / <% download.bytesTotal / 1024 | bytesToHuman %>
99
</div>
1010
<div class="w33">
11-
<% (Date.parse(download.estimatedEndTime) - (new Date)) | milisecondsToHuman %>
11+
<% download.estimatedInterval * 1000| milisecondsToHuman %>
1212
</div>
1313
<div class="w33">
14-
<% (download.bytesTotal - download.bytesReceived) / (Date.parse(download.estimatedEndTime) - (new Date)) | bytesToHuman %> / s
14+
<% download.speed | bytesToHuman %> / s
1515
</div>
1616
</div>
1717
</div>

src/CyberPanel/Commands/Commands/DownloadsCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public function run() : array {
1313
'filename' => $download->getFilename(),
1414
'bytesReceived' => $download->getDownloaded(),
1515
'bytesTotal' => $download->getTotal(),
16-
'estimatedEndTime' => $download->getEstimatedEndTime(),
16+
'estimatedInterval' => $download->getCalculatedInterval(),
17+
'speed' => $download->getCalculatedSpeed(),
1718
];
1819
}
1920
return [

src/CyberPanel/Commands/Commands/StoreDownloadsCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function run() : array {
1717
$download->setTotal($parameter->bytesTotal);
1818
$download->setDownloaded($parameter->bytesReceived);
1919
$download->setEstimatedEndTime(
20-
empty($parameter->estimatedEndTime) ? NULL : $parameter->estimatedEndTime
20+
new \DateTime($parameter->estimatedEndTime ?? NULL )
2121
);
2222
self::$storedDownloads[] = $download;
2323
}

src/CyberPanel/DataStructs/Download.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,62 @@
22

33
namespace CyberPanel\DataStructs;
44

5+
use \DateTime;
6+
57
class Download {
68

79
private string $filename;
810
private int $downloaded;
911
private int $total;
10-
private $estimatedEndTime;
12+
private ?DateTime $estimatedEndTime;
1113

12-
public function getFilename() {
14+
public function getFilename() : string {
1315
return $this->filename;
1416
}
1517

16-
public function getDownloaded() {
18+
public function getDownloaded() : int {
1719
return $this->downloaded;
1820
}
1921

20-
public function getTotal() {
22+
public function getTotal() : int {
2123
return $this->total;
2224
}
2325

24-
public function getEstimatedEndTime() {
26+
public function getEstimatedEndTime() : ?DateTime {
2527
return $this->estimatedEndTime;
2628
}
2729

28-
public function setFilename($filename) {
30+
public function setFilename(string $filename) : void {
2931
$this->filename = $filename;
3032
}
3133

32-
public function setDownloaded($downloaded) {
34+
public function setDownloaded(int $downloaded) : void {
3335
$this->downloaded = $downloaded;
3436
}
3537

3638
public function setTotal($total) {
3739
$this->total = $total;
3840
}
3941

40-
public function setEstimatedEndTime($estimatedEndTime) {
42+
public function setEstimatedEndTime(\DateTime $estimatedEndTime = NULL) : void {
4143
$this->estimatedEndTime = $estimatedEndTime;
4244
}
4345

46+
public function getRemain() : int {
47+
return $this->getTotal() - $this->getDownloaded();
48+
}
49+
50+
public function getCalculatedSpeed() : int {
51+
if (!empty($this->getEstimatedEndTime()) && $this->getCalculatedInterval() > 0) {
52+
return (int) $this->getRemain() / $this->getCalculatedInterval();
53+
}
54+
return 0;
55+
}
56+
57+
public function getCalculatedInterval() : int {
58+
if (!empty($this->getEstimatedEndTime())) {
59+
return (int)($this->getEstimatedEndTime()->getTimestamp() - time());
60+
}
61+
return 1;
62+
}
4463
}

0 commit comments

Comments
 (0)