Cachet/app/Models/Metric.php

252 lines
5.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;
2017-03-18 11:15:43 +00:00
use AltThree\Validator\ValidationException;
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;
2017-03-18 11:15:43 +00:00
use Illuminate\Support\MessageBag;
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;
/**
* Viewable only authenticated users.
*
* @var int
*/
const VISIBLE_AUTHENTICATED = 0;
/**
* Viewable by public.
*
* @var int
*/
const VISIBLE_GUEST = 1;
/**
* Viewable by nobody.
*
* @var int
*/
const VISIBLE_HIDDEN = 2;
2017-03-18 11:15:43 +00:00
/**
* Array of acceptable threshold minutes.
*
* @var int[]
*/
const ACCEPTABLE_THRESHOLDS = [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60];
/**
* 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,
'visible' => 1,
];
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',
'visible' => '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',
'visible',
];
2014-11-25 21:35:52 +00:00
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'name' => 'required',
'suffix' => 'required',
2016-10-19 12:29:16 +01:00
'display_chart' => 'required|bool',
'default_value' => 'required|numeric',
'places' => 'required|numeric|between:0,4',
'default_view' => 'required|numeric|between:0,3',
'threshold' => 'required|numeric|between:0,10',
'visible' => 'required|numeric|between:0,2',
];
/**
* The sortable fields.
*
* @var string[]
*/
protected $sortable = [
'id',
'name',
'display_chart',
'default_value',
'calc_type',
'order',
'visible',
];
/**
* Overrides the models boot method.
*
* @return void
*/
public static function boot()
{
parent::boot();
// When deleting a metric, delete the points too.
self::deleting(function ($model) {
$model->points()->delete();
});
}
2017-06-13 19:25:43 +01:00
/**
2018-06-16 17:00:27 +01:00
* Get the meta relation.
2017-06-13 19:25:43 +01:00
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function meta()
{
return $this->morphMany(Meta::class, 'meta');
}
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)
{
2018-06-16 17:27:28 +01:00
return $query->where('display_chart', '=', true)->where('visible', '<>', self::VISIBLE_HIDDEN);
}
/**
* Finds all metrics which are visible to public.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeVisible(Builder $query)
{
return $query->where('visible', '=', self::VISIBLE_GUEST);
}
/**
* 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()
{
2016-10-19 12:29:16 +01:00
return $this->display_chart;
}
2015-05-16 13:57:32 -05:00
2017-03-18 11:15:43 +00:00
/**
* Validate the model before save.
*
* @throws \AltThree\Validator\ValidationException
*
* @return void
*/
public function validate()
{
$messages = [];
2017-04-02 14:35:34 +01:00
if (60 % $this->threshold !== 0) {
2017-04-02 14:46:13 +01:00
$messages[] = 'Threshold must divide by 60.';
2017-03-18 11:15:43 +00:00
}
if ($messages) {
throw new ValidationException(new MessageBag($messages));
}
}
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
}
}