Passed
Push — master ( 4f5b3b...1c5d75 )
by Darko
09:48
created

SystemMetric   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 1
b 0
f 0
dl 0
loc 53
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeRam() 0 3 1
A scopeBetweenDates() 0 3 1
A scopeForPeriod() 0 3 1
A cleanupOldMetrics() 0 3 1
A scopeCpu() 0 3 1
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Factories\HasFactory;
6
use Illuminate\Database\Eloquent\Model;
7
8
class SystemMetric extends Model
9
{
10
    use HasFactory;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Database\Eloquent\Factories\HasFactory requires the property $factoryClass which is not provided by App\Models\SystemMetric.
Loading history...
11
12
    protected $fillable = [
13
        'metric_type',
14
        'value',
15
        'recorded_at',
16
    ];
17
18
    protected $casts = [
19
        'value' => 'float',
20
        'recorded_at' => 'datetime',
21
    ];
22
23
    /**
24
     * Scope to get CPU metrics
25
     */
26
    public function scopeCpu($query)
27
    {
28
        return $query->where('metric_type', 'cpu');
29
    }
30
31
    /**
32
     * Scope to get RAM metrics
33
     */
34
    public function scopeRam($query)
35
    {
36
        return $query->where('metric_type', 'ram');
37
    }
38
39
    /**
40
     * Scope to get metrics for a specific time period
41
     */
42
    public function scopeForPeriod($query, int $hours)
43
    {
44
        return $query->where('recorded_at', '>=', now()->subHours($hours));
45
    }
46
47
    /**
48
     * Scope to get metrics for a specific date range
49
     */
50
    public function scopeBetweenDates($query, $startDate, $endDate)
51
    {
52
        return $query->whereBetween('recorded_at', [$startDate, $endDate]);
53
    }
54
55
    /**
56
     * Clean up old metrics (older than specified days)
57
     */
58
    public static function cleanupOldMetrics(int $days = 60): int
59
    {
60
        return static::where('recorded_at', '<', now()->subDays($days))->delete();
61
    }
62
}
63