Completed
Pull Request — master (#94)
by Brandon
07:52
created

Sensor::getLastMonthDailyAvgDataAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\Activitylog\Traits\LogsActivity;
7
use Illuminate\Support\Facades\Auth;
8
use Carbon\Carbon;
9
10
class Sensor extends Model 
11
{
12
    use LogsActivity;
13
14
    protected $table = 'sensors';
15
16
    /**
17
     * Update the updated_at and created_at timestamps?
18
     *
19
     * @var array
20
     */
21
    public $timestamps = false;
22
23
    /**
24
     * The attributes that are mass assignable.
25
     *
26
     * @var array
27
     */
28
    protected $fillable = array('device_id', 'name', 'type');
29
30
    /**
31
     * The attributes that are visible.
32
     *
33
     * @var array
34
     */
35
    protected $visible = array('id', 'device_id', 'name', 'type');
36
37
    /**
38
     * The attributes to log in the Activity Log
39
     *
40
     * @var array
41
     */
42
    protected static $logAttributes = array('id', 'device_id', 'name', 'type');
43
44
    /**
45
     * Only log those that have actually changed after the update.
46
     *
47
     * @var array
48
     */
49
    protected static $logOnlyDirty = true;
50
51
    /**
52
     * Get the device associated with the sensor.
53
     */
54
    public function device()
55
    {
56
        return $this->belongsTo('App\Device');
57
    }
58
59
    /**
60
     * Get the sensor data associated with the sensor.
61
     */
62
    public function data()
63
    {
64
        return $this->hasMany('App\SensorData');
65
    }
66
    
67
    /**
68
     * Get the latest sensor data entry associated with the sensor.
69
     */
70
    public function getLatestDataAttribute()
71
    {
72
        return $this->hasOne('App\SensorData')->latest()->first() ?? (object)[];
73
    }
74
    
75
    /**
76
     * Get the last hour's sensor data averaged by the minute for the sensor.
77
     */
78
    public function getLastHourMinutelyAvgDataAttribute()
79
    {
80
        return $this->hasMany('App\SensorData')
81
            ->selectRaw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i') AS date, AVG(value) AS value")
82
            ->whereRaw("created_at > DATE_SUB(NOW(), INTERVAL 1 HOUR)")
83
            ->groupBy("date")
84
            ->get();
85
    }
86
    
87
    /**
88
     * Get the last day's sensor data averaged hourly for the sensor.
89
     */
90
    public function getLastDayHourlyAvgDataAttribute()
91
    {
92
        return $this->hasMany('App\SensorData')
93
            ->selectRaw("DATE_FORMAT(created_at, '%Y-%m-%d %H') AS date, AVG(value) AS value")
94
            ->whereRaw("created_at > DATE_SUB(NOW(), INTERVAL 1 DAY)")
95
            ->groupBy("date")
96
            ->get();
97
    }
98
    
99
    /**
100
     * Get the last weeks's sensor data averaged daily for the sensor.
101
     */
102
    public function getLastWeekDailyAvgDataAttribute()
103
    {
104
        return $this->hasMany('App\SensorData')
105
            ->selectRaw("DATE_FORMAT(created_at, '%Y-%m-%d') AS date, AVG(value) AS value")
106
            ->whereRaw("created_at > DATE_SUB(NOW(), INTERVAL 7 DAY)")
107
            ->groupBy("date")
108
            ->get();
109
    }
110
    
111
    /**
112
     * Get the last months's sensor data averaged daily for the sensor.
113
     */
114
    public function getLastMonthDailyAvgDataAttribute()
115
    {
116
        return $this->hasMany('App\SensorData')
117
            ->selectRaw("DATE_FORMAT(created_at, '%Y-%m-%d') AS date, AVG(value) AS value")
118
            ->whereRaw("created_at > DATE_SUB(NOW(), INTERVAL 30 DAY)")
119
            ->groupBy("date")
120
            ->get();
121
    }
122
    
123
    /**
124
     * Get the last year's sensor data averaged monthly for the sensor.
125
     */
126
    public function getLastYearMonthlyAvgDataAttribute()
127
    {
128
        return $this->hasMany('App\SensorData')
129
            ->selectRaw("DATE_FORMAT(created_at, '%Y-%m') AS date, AVG(value) AS value")
130
            ->whereRaw("created_at > DATE_SUB(NOW(), INTERVAL 1 YEAR)")
131
            ->groupBy("date")
132
            ->get();
133
    }
134
}