2014-11-25 11:19:20 +00:00
|
|
|
<?php
|
|
|
|
|
2015-04-19 08:52:39 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
2015-07-06 17:37:01 +01:00
|
|
|
* (c) Alt Three Services Limited
|
2015-04-19 08:52:39 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2015-01-02 00:18:19 +00:00
|
|
|
namespace CachetHQ\Cachet\Models;
|
|
|
|
|
2015-08-03 22:32:36 +01:00
|
|
|
use AltThree\Validator\ValidatingTrait;
|
2015-06-16 09:46:29 +01:00
|
|
|
use CachetHQ\Cachet\Presenters\MetricPointPresenter;
|
2018-06-27 14:28:38 +01:00
|
|
|
use Carbon\Carbon;
|
2018-06-28 12:01:22 +01:00
|
|
|
use DateTime;
|
2015-01-01 12:23:17 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2015-05-16 13:57:32 -05:00
|
|
|
use McCool\LaravelAutoPresenter\HasPresenter;
|
2015-01-01 12:23:17 +00:00
|
|
|
|
2018-06-28 12:01:22 +01:00
|
|
|
/**
|
|
|
|
* This is the metric point model class.
|
|
|
|
*
|
|
|
|
* @author James Brooks <james@alt-three.com>
|
|
|
|
* @author Joseph Cohen <joe@alt-three.com>
|
|
|
|
* @author Graham Campbell <graham@alt-three.com>
|
|
|
|
*/
|
2015-05-16 13:57:32 -05:00
|
|
|
class MetricPoint extends Model implements HasPresenter
|
2014-12-20 21:20:17 +00:00
|
|
|
{
|
2015-01-21 02:32:18 -06:00
|
|
|
use ValidatingTrait;
|
|
|
|
|
2018-07-02 18:05:12 +01:00
|
|
|
/**
|
|
|
|
* The accessors to append to the model's array form.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $appends = [
|
|
|
|
'calculated_value',
|
|
|
|
];
|
|
|
|
|
2016-03-02 12:09:57 +00:00
|
|
|
/**
|
|
|
|
* The model's attributes.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $attributes = [
|
|
|
|
'value' => 0,
|
|
|
|
'counter' => 1,
|
|
|
|
];
|
|
|
|
|
2015-08-22 15:57:53 +01:00
|
|
|
/**
|
|
|
|
* The attributes that should be casted to native types.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'metric_id' => 'int',
|
2016-03-02 12:09:57 +00:00
|
|
|
'value' => 'float',
|
|
|
|
'counter' => 'int',
|
2015-08-22 15:57:53 +01:00
|
|
|
];
|
|
|
|
|
2015-01-21 02:32:18 -06:00
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
2015-02-23 08:27:43 +00:00
|
|
|
* @var string[]
|
2015-01-21 02:32:18 -06:00
|
|
|
*/
|
2016-03-02 12:09:57 +00:00
|
|
|
protected $fillable = [
|
|
|
|
'metric_id',
|
|
|
|
'value',
|
|
|
|
'counter',
|
|
|
|
'created_at',
|
|
|
|
];
|
2015-01-21 02:32:18 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The validation rules.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2015-08-03 22:32:36 +01:00
|
|
|
public $rules = [
|
2016-10-19 12:29:16 +01:00
|
|
|
'value' => 'required|numeric',
|
2015-01-21 02:32:18 -06:00
|
|
|
];
|
|
|
|
|
2014-12-01 08:53:32 +00:00
|
|
|
/**
|
2016-08-10 10:46:45 +01:00
|
|
|
* Get the metric relation.
|
2014-12-30 18:19:22 +00:00
|
|
|
*
|
2014-12-01 08:53:32 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2014-12-20 21:20:17 +00:00
|
|
|
public function metric()
|
|
|
|
{
|
2016-03-02 12:09:57 +00:00
|
|
|
return $this->belongsTo(Metric::class);
|
|
|
|
}
|
|
|
|
|
2018-07-02 18:05:12 +01:00
|
|
|
/**
|
|
|
|
* Show the actual calculated value; as per (value * counter).
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getCalculatedValueAttribute()
|
|
|
|
{
|
|
|
|
return $this->value * $this->counter;
|
|
|
|
}
|
|
|
|
|
2018-06-27 14:28:38 +01:00
|
|
|
/**
|
|
|
|
* Round the created at value into intervals of 30 seconds.
|
|
|
|
*
|
2018-06-27 14:39:49 +01:00
|
|
|
* @param string $createdAt
|
2018-06-27 14:28:38 +01:00
|
|
|
*
|
2018-06-27 14:39:49 +01:00
|
|
|
* @return string|void
|
2018-06-27 14:28:38 +01:00
|
|
|
*/
|
2018-06-27 14:39:49 +01:00
|
|
|
public function setCreatedAtAttribute($createdAt)
|
2018-06-27 14:28:38 +01:00
|
|
|
{
|
2018-06-27 14:40:19 +01:00
|
|
|
if (!$createdAt) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-27 14:39:49 +01:00
|
|
|
|
2018-06-28 12:01:22 +01:00
|
|
|
if (!$createdAt instanceof DateTime) {
|
|
|
|
$createdAt = Carbon::parse($createdAt);
|
|
|
|
}
|
2018-06-27 14:39:49 +01:00
|
|
|
|
|
|
|
$timestamp = $createdAt->format('U');
|
2018-06-27 14:28:38 +01:00
|
|
|
$timestamp = 30 * round($timestamp / 30);
|
|
|
|
|
2018-07-02 19:22:01 -05:00
|
|
|
$date = Carbon::createFromFormat('U', $timestamp)->toDateTimeString();
|
|
|
|
|
|
|
|
$this->attributes['created_at'] = $date;
|
|
|
|
|
|
|
|
return $date;
|
2018-06-27 14:28:38 +01:00
|
|
|
}
|
|
|
|
|
2015-05-16 13:57:32 -05:00
|
|
|
/**
|
|
|
|
* Get the presenter class.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPresenterClass()
|
|
|
|
{
|
2015-06-16 09:46:29 +01:00
|
|
|
return MetricPointPresenter::class;
|
2015-05-16 13:57:32 -05:00
|
|
|
}
|
2014-11-27 22:08:28 +00:00
|
|
|
}
|