| Code Coverage | ||||||||||
| Classes and Traits | Functions and Methods | Lines | ||||||||
| Total |  | 0.00% | 0 / 1 |  | 0.00% | 0 / 5 | CRAP |  | 0.00% | 0 / 28 | 
| DockerService |  | 0.00% | 0 / 1 |  | 0.00% | 0 / 5 | 42 |  | 0.00% | 0 / 28 | 
| configs |  | 0.00% | 0 / 1 | 2 |  | 0.00% | 0 / 1 | |||
| getTasksAttribute |  | 0.00% | 0 / 1 | 2 |  | 0.00% | 0 / 3 | |||
| getTasksResumeAttribute |  | 0.00% | 0 / 1 | 2 |  | 0.00% | 0 / 6 | |||
| getLogsResumeAttribute |  | 0.00% | 0 / 1 | 6 |  | 0.00% | 0 / 17 | |||
| 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\DB; | 
| 7 | use Illuminate\Support\Facades\Log; | 
| 8 | use Qmp\Laravel\DockerScale\Models\DockerScalerConfig; | 
| 9 | |
| 10 | class DockerService extends Model | 
| 11 | { | 
| 12 | protected $connection = 'dockerScale'; | 
| 13 | protected $table = 'docker_services'; | 
| 14 | |
| 15 | protected $fillable = ['service_id', 'name', 'mode', 'current_replicas', 'total_replicas', 'image', 'ports', ]; | 
| 16 | |
| 17 | public function configs() | 
| 18 | { | 
| 19 | return $this->hasMany(DockerScalerConfig::class, 'service_id'); | 
| 20 | } | 
| 21 | |
| 22 | public function getTasksAttribute() | 
| 23 | { | 
| 24 | $tasks = DockerTask::where('service', $this->name)->get(); | 
| 25 | $tasks->eachAppend('stats_avg'); | 
| 26 | |
| 27 | return $tasks; | 
| 28 | } | 
| 29 | |
| 30 | public function getTasksResumeAttribute() | 
| 31 | { | 
| 32 | $tasks = $this->tasks; | 
| 33 | return collect([ | 
| 34 | 'count' => $tasks->count(), | 
| 35 | 'cpu' => $tasks->average('stats_avg.cpu'), | 
| 36 | 'memory' => $tasks->average('stats_avg.memory'), | 
| 37 | 'pids' => $tasks->average('stats_avg.pids') | 
| 38 | ]); | 
| 39 | } | 
| 40 | |
| 41 | public function getLogsResumeAttribute() | 
| 42 | { | 
| 43 | $ts = (new \DateTime('-5 minutes'))->getTimestamp(); | 
| 44 | $serviceName = $this->service_name; | 
| 45 | $count = null; | 
| 46 | $time = null; | 
| 47 | if (!empty($serviceName)) { | 
| 48 | $count = HttpLog::select(DB::raw('count(0) as num')) | 
| 49 | ->where('service', $serviceName) | 
| 50 | ->where('ts', '>=', $ts) | 
| 51 | ->groupBy('ts') | 
| 52 | ->get() | 
| 53 | ->average('num'); | 
| 54 | $count = number_format($count, 1); | 
| 55 | |
| 56 | $time = HttpLog::where('service', $serviceName) | 
| 57 | ->where('ts', '>=', $ts) | 
| 58 | ->avg('time'); | 
| 59 | $time = number_format(($time / 1000000), 3); | 
| 60 | } | 
| 61 | |
| 62 | return collect(compact('count', 'time')); | 
| 63 | } | 
| 64 | |
| 65 | /** | 
| 66 | * Prepare a date for array / JSON serialization. | 
| 67 | * | 
| 68 | * @param \DateTimeInterface $date | 
| 69 | * @return string | 
| 70 | */ | 
| 71 | protected function serializeDate(\DateTimeInterface $date) | 
| 72 | { | 
| 73 | return $date->format('Y-m-d H:i:s'); | 
| 74 | } | 
| 75 | |
| 76 | |
| 77 | |
| 78 | } |