Cachet/app/Models/Metric.php

161 lines
3.4 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;
use CachetHQ\Cachet\Models\Traits\SortableTrait;
2015-06-16 09:46:29 +01:00
use CachetHQ\Cachet\Presenters\MetricPresenter;
use Illuminate\Database\Eloquent\Builder;
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;
2014-11-25 21:35:52 +00:00
2015-05-16 13:57:32 -05:00
class Metric extends Model implements HasPresenter
2014-12-20 21:20:17 +00:00
{
use SortableTrait, ValidatingTrait;
2014-11-25 21:35:52 +00:00
2015-12-25 19:13:28 +08:00
/**
* The calculation type of sum.
*
* @var int
*/
const CALC_SUM = 0;
2015-12-25 19:13:28 +08:00
/**
* The calculation type of average.
*
* @var int
*/
const CALC_AVG = 1;
/**
* The model's attributes.
*
2015-02-23 08:27:43 +00:00
* @var string[]
*/
protected $attributes = [
2015-02-23 08:27:43 +00:00
'name' => '',
'display_chart' => 1,
'default_value' => 0,
'calc_type' => 0,
'places' => 2,
'default_view' => 1,
2016-03-02 12:09:57 +00:00
'threshold' => 5,
'order' => 0,
];
2015-01-01 18:57:33 +00:00
/**
* The attributes that should be casted to native types.
2015-01-01 18:57:33 +00:00
*
* @var string[]
*/
protected $casts = [
'name' => 'string',
'display_chart' => 'bool',
'default_value' => 'int',
'calc_type' => 'int',
'places' => 'int',
'default_view' => 'int',
2016-03-02 12:09:57 +00:00
'threshold' => 'int',
'order' => 'int',
];
2014-11-25 21:35:52 +00:00
2015-01-01 18:57:33 +00:00
/**
* The fillable properties.
*
* @var string[]
*/
protected $fillable = [
'name',
'suffix',
'description',
'display_chart',
'default_value',
'calc_type',
'places',
'default_view',
2016-03-02 12:09:57 +00:00
'threshold',
'order',
];
2014-11-25 21:35:52 +00:00
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'name' => 'required',
'suffix' => 'required',
2015-10-20 20:34:59 +01:00
'display_chart' => 'bool',
'default_value' => 'numeric',
'places' => 'numeric|between:0,4',
'default_view' => 'numeric|between:0,3',
2016-03-02 12:09:57 +00:00
'threshold' => 'numeric|between:0,10',
'threshold' => 'int',
];
/**
* The sortable fields.
*
* @var string[]
*/
protected $sortable = [
'id',
'name',
'display_chart',
'default_value',
'calc_type',
'order',
];
2014-12-01 08:52:34 +00:00
/**
2016-08-10 10:46:45 +01:00
* Get the points relation.
2014-12-30 18:19:22 +00:00
*
2015-01-01 18:57:33 +00:00
* @return \Illuminate\Database\Eloquent\Relations\HasMany
2014-12-01 08:52:34 +00:00
*/
2014-12-20 21:20:17 +00:00
public function points()
{
2015-06-16 09:46:29 +01:00
return $this->hasMany(MetricPoint::class, 'metric_id', 'id');
2014-12-01 08:52:34 +00:00
}
/**
* Scope metrics to those of which are displayable.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeDisplayable(Builder $query)
{
return $query->where('display_chart', 1);
}
/**
* Determines whether a chart should be shown.
2014-12-30 18:19:22 +00:00
*
* @return bool
*/
2014-12-20 21:20:17 +00:00
public function getShouldDisplayAttribute()
{
return $this->display_chart === 1;
}
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 MetricPresenter::class;
2015-05-16 13:57:32 -05:00
}
}