Completed
Pull Request — master (#94)
by Brandon
08:20
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 0
Metric Value
c 0
b 0
f 0
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')->orderBy('id', 'DESC');
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("CONCAT(DATE_FORMAT(created_at, '%Y-%m-%d %H:%i'), ':00') AS date, AVG(value) AS value")
82
            ->whereRaw("created_at > DATE_SUB(NOW(), INTERVAL 1 HOUR)")
83
            ->groupBy(\DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i')"))
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
        // SELECT CONCAT(DATE_FORMAT(created_at, '%Y-%m-%d %H'), ':00:00') AS date, AVG(value) As value FROM sensor_data WHERE sensor_id = 20 AND created_at > DATE_SUB(NOW(), INTERVAL 1 DAY) GROUP BY DATE_FORMAT(created_at, '%Y-%m-%d %H')
93
        return $this->hasMany('App\SensorData')
94
            ->selectRaw("CONCAT(DATE_FORMAT(created_at, '%Y-%m-%d %H'), ':00:00') AS date, AVG(value) AS value")
95
            ->whereRaw("created_at > DATE_SUB(NOW(), INTERVAL 1 DAY)")
96
            ->groupBy(\DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d %H')"))
97
            ->get();
98
    }
99
    
100
    /**
101
     * Get the last weeks's sensor data averaged daily for the sensor.
102
     */
103
    public function getLastWeekDailyAvgDataAttribute()
104
    {
105
        return $this->hasMany('App\SensorData')
106
            ->selectRaw("CONCAT(DATE_FORMAT(created_at, '%Y-%m-%d '), '00:00:00') AS date, AVG(value) AS value")
107
            ->whereRaw("created_at > DATE_SUB(NOW(), INTERVAL 7 DAY)")
108
            ->groupBy(\DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d ')"))
109
            ->get();
110
    }
111
    
112
    /**
113
     * Get the last months's sensor data averaged daily for the sensor.
114
     */
115
    public function getLastMonthDailyAvgDataAttribute()
116
    {
117
        return $this->hasMany('App\SensorData')
118
            ->selectRaw("CONCAT(DATE_FORMAT(created_at, '%Y-%m-%d'), ' 00:00:00') AS date, AVG(value) AS value")
119
            ->whereRaw("created_at > DATE_SUB(NOW(), INTERVAL 30 DAY)")
120
            ->groupBy(\DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d ')"))
121
            ->get();
122
    }
123
    
124
    /**
125
     * Get the last year's sensor data averaged monthly for the sensor.
126
     */
127
    public function getLastYearMonthlyAvgDataAttribute()
128
    {
129
        return $this->hasMany('App\SensorData')
130
            ->selectRaw("CONCAT(DATE_FORMAT(created_at, '%Y-%m'), '-00 00:00:00') AS date, AVG(value) AS value")
131
            ->whereRaw("created_at > DATE_SUB(NOW(), INTERVAL 1 YEAR)")
132
            ->groupBy(\DB::raw("DATE_FORMAT(created_at, '%Y-%m')"))
133
            ->get();
134
    }
135
}