22
33namespace Pterodactyl \Transformers \Api \Client ;
44
5- use Pterodactyl \Models \Server ;
6- use GuzzleHttp \Exception \RequestException ;
7- use Pterodactyl \Exceptions \Http \Connection \DaemonConnectionException ;
8- use Pterodactyl \Contracts \Repository \Daemon \ServerRepositoryInterface ;
5+ use Illuminate \Support \Arr ;
96
107class StatsTransformer extends BaseClientTransformer
118{
12- /**
13- * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
14- */
15- private $ repository ;
16-
17- /**
18- * Perform dependency injection.
19- *
20- * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $repository
21- */
22- public function handle (ServerRepositoryInterface $ repository )
23- {
24- $ this ->repository = $ repository ;
25- }
26-
279 /**
2810 * @return string
2911 */
@@ -36,60 +18,21 @@ public function getResourceName(): string
3618 * Transform stats from the daemon into a result set that can be used in
3719 * the client API.
3820 *
39- * @param \Pterodactyl\Models\Server $model
21+ * @param array $data
4022 * @return array
41- *
42- * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
4323 */
44- public function transform (Server $ model )
24+ public function transform (array $ data )
4525 {
46- try {
47- $ stats = $ this ->repository ->setServer ($ model )->details ();
48- } catch (RequestException $ exception ) {
49- throw new DaemonConnectionException ($ exception );
50- }
51-
52- $ object = json_decode ($ stats ->getBody ()->getContents ());
53-
5426 return [
55- 'state ' => $ this ->transformState (object_get ($ object , 'status ' , 0 )),
56- 'memory ' => [
57- 'current ' => round (object_get ($ object , 'proc.memory.total ' , 0 ) / 1024 / 1024 ),
58- 'limit ' => floatval ($ model ->memory ),
59- ],
60- 'cpu ' => [
61- 'current ' => object_get ($ object , 'proc.cpu.total ' , 0 ),
62- 'cores ' => object_get ($ object , 'proc.cpu.cores ' , []),
63- 'limit ' => floatval ($ model ->cpu ),
64- ],
65- 'disk ' => [
66- 'current ' => round (object_get ($ object , 'proc.disk.used ' , 0 )),
67- 'limit ' => floatval ($ model ->disk ),
68- 'io ' => $ model ->io ,
27+ 'current_state ' => Arr::get ($ data , 'state ' , 'stopped ' ),
28+ 'is_suspended ' => Arr::get ($ data , 'suspended ' , false ),
29+ 'resources ' => [
30+ 'memory_bytes ' => Arr::get ($ data , 'resources.memory_bytes ' , 0 ),
31+ 'cpu_absolute ' => Arr::get ($ data , 'resources.cpu_absolute ' , 0 ),
32+ 'disk_bytes ' => Arr::get ($ data , 'resources.disk_bytes ' , 0 ),
33+ 'network_rx_bytes ' => Arr::get ($ data , 'resources.network.rx_bytes ' , 0 ),
34+ 'network_tx_bytes ' => Arr::get ($ data , 'resources.network.tx_bytes ' , 0 ),
6935 ],
70- 'installed ' => $ model ->installed === 1 ,
71- 'suspended ' => (bool ) $ model ->suspended ,
7236 ];
7337 }
74-
75- /**
76- * Transform the state returned by the daemon into a human readable string.
77- *
78- * @param int $state
79- * @return string
80- */
81- private function transformState (int $ state ): string
82- {
83- switch ($ state ) {
84- case 1 :
85- return 'on ' ;
86- case 2 :
87- return 'starting ' ;
88- case 3 :
89- return 'stopping ' ;
90- case 0 :
91- default :
92- return 'off ' ;
93- }
94- }
9538}
0 commit comments