Cachet/app/Models/MetricPoint.php

101 lines
2.0 KiB
PHP
Raw Normal View History

2014-11-25 11:19:20 +00:00
<?php
/*
* This file is part of Cachet.
*
2015-07-06 17:37:01 +01:00
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Models;
use AltThree\Validator\ValidatingTrait;
2015-06-16 09:46:29 +01:00
use CachetHQ\Cachet\Presenters\MetricPointPresenter;
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
2015-05-16 13:57:32 -05:00
class MetricPoint extends Model implements HasPresenter
2014-12-20 21:20:17 +00:00
{
use ValidatingTrait;
2016-03-02 12:09:57 +00:00
/**
* The model's attributes.
*
* @var string[]
*/
protected $attributes = [
'value' => 0,
'counter' => 1,
];
/**
* 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',
];
/**
* The attributes that are mass assignable.
*
2015-02-23 08:27:43 +00:00
* @var string[]
*/
2016-03-02 12:09:57 +00:00
protected $fillable = [
'metric_id',
'value',
'counter',
'created_at',
];
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
2016-10-19 12:29:16 +01:00
'value' => 'required|numeric',
];
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);
}
/**
* Override the value attribute.
*
* @param mixed $value
*
* @return float
*/
public function getActiveValueAttribute($value)
{
if ($this->metric->calc_type === Metric::CALC_SUM) {
return round((float) $value * $this->counter, $this->metric->places);
}
return round((float) $value, $this->metric->places);
2014-12-01 08:53:32 +00: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
}
}