Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 14 |
DockerTask | |
0.00% |
0 / 1 |
|
0.00% |
0 / 3 |
12 | |
0.00% |
0 / 14 |
getStatsAttribute | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
getStatsAvgAttribute | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 11 |
|||
serializeDate | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\DockerScale\Models; |
4 | |
5 | use Illuminate\Database\Eloquent\Model; |
6 | use Illuminate\Support\Facades\Log; |
7 | |
8 | class DockerTask extends Model |
9 | { |
10 | protected $connection = 'dockerScale'; |
11 | protected $table = 'docker_tasks'; |
12 | |
13 | protected $fillable = ['task_id', 'service', 'task', 'node', 'desired_state', 'current_state', 'error', ]; |
14 | |
15 | public function getStatsAttribute() |
16 | { |
17 | $stats = DockerStat::where('name', $this->service . '.' . $this->task . '.' . $this->task_id)->where('updated_at', '>=', (new \DateTime('-5 minutes'))->format('Y-m-d H:i:s'))->get(); |
18 | |
19 | return $stats; |
20 | } |
21 | |
22 | public function getStatsAvgAttribute() |
23 | { |
24 | $stats = $this->stats->map(function($item) { |
25 | $item->cpu_perc = (float) $item->cpu_perc; |
26 | $item->memory_perc = (float) $item->memory_perc; |
27 | $item->pids = (float) $item->pids; |
28 | |
29 | return $item; |
30 | }); |
31 | |
32 | $cpu = number_format($stats->average('cpu_perc'), 3); |
33 | $memory = number_format($stats->average('memory_perc'), 3); |
34 | $pids = (int) $stats->average('pids'); |
35 | $stats = collect(compact('cpu', 'memory', 'pids')); |
36 | |
37 | return $stats; |
38 | } |
39 | |
40 | /** |
41 | * Prepare a date for array / JSON serialization. |
42 | * |
43 | * @param \DateTimeInterface $date |
44 | * @return string |
45 | */ |
46 | protected function serializeDate(\DateTimeInterface $date) |
47 | { |
48 | return $date->format('Y-m-d H:i:s'); |
49 | } |
50 | |
51 | } |