2014-11-25 11:19:20 +00:00
< ? php
2015-04-19 08:52:39 +01:00
/*
* This file is part of Cachet .
*
2015-05-25 17:59:08 +01:00
* ( c ) Cachet HQ < support @ cachethq . io >
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-05-20 20:24:54 +01:00
use CachetHQ\Cachet\Facades\Setting as SettingFacade ;
2015-02-24 08:36:41 +00:00
use DateInterval ;
2015-01-01 12:23:17 +00:00
use Illuminate\Database\Eloquent\Model ;
2015-02-24 08:42:47 +00:00
use Illuminate\Support\Facades\Config ;
2015-01-21 02:32:18 -06:00
use Illuminate\Support\Facades\DB ;
2015-05-16 13:57:32 -05:00
use Jenssegers\Date\Date ;
use McCool\LaravelAutoPresenter\HasPresenter ;
2014-11-27 16:05:00 +00:00
use Watson\Validating\ValidatingTrait ;
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
{
2014-11-27 16:05:00 +00:00
use ValidatingTrait ;
2014-11-25 21:35:52 +00:00
2015-03-17 16:41:23 +00:00
const CALC_SUM = 0 ;
const CALC_AVG = 1 ;
2015-01-21 02:32:18 -06:00
/**
* The model ' s attributes .
*
2015-02-23 08:27:43 +00:00
* @ var string []
2015-01-21 02:32:18 -06:00
*/
protected $attributes = [
2015-02-23 08:27:43 +00:00
'name' => '' ,
'display_chart' => 1 ,
2015-03-17 16:41:23 +00:00
'default_value' => 0 ,
'calc_type' => 0 ,
2015-01-21 02:32:18 -06:00
];
2015-01-01 18:57:33 +00:00
/**
* The validation rules .
*
* @ var string []
*/
2014-11-27 16:05:00 +00:00
protected $rules = [
'name' => 'required' ,
'suffix' => 'required' ,
'display_chart' => 'boolean' ,
2015-03-17 16:41:23 +00:00
'default_value' => 'numeric' ,
2014-11-27 16:05:00 +00:00
];
2014-11-25 21:35:52 +00:00
2015-01-01 18:57:33 +00:00
/**
* The fillable properties .
*
* @ var string []
*/
2015-03-17 16:41:23 +00:00
protected $fillable = [ 'name' , 'suffix' , 'description' , 'display_chart' , 'default_value' , 'calc_type' ];
2014-11-25 21:35:52 +00:00
2014-12-01 08:52:34 +00:00
/**
* Metrics contain many metric points .
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-01-02 00:18:19 +00:00
return $this -> hasMany ( 'CachetHQ\Cachet\Models\MetricPoint' , 'metric_id' , 'id' );
2014-12-01 08:52:34 +00:00
}
2015-01-21 02:32:18 -06:00
/**
2015-06-15 13:37:21 +01:00
* Returns the sum of all values a metric has by the hour .
2015-01-21 02:32:18 -06:00
*
* @ param int $hour
*
* @ return int
*/
2015-06-15 13:37:21 +01:00
public function getValuesByHour ( $hour )
2015-01-21 02:32:18 -06:00
{
2015-05-20 20:24:54 +01:00
$dateTimeZone = SettingFacade :: get ( 'app_timezone' );
2015-05-16 13:57:32 -05:00
$dateTime = ( new Date ()) -> setTimezone ( $dateTimeZone ) -> sub ( new DateInterval ( 'PT' . $hour . 'H' ));
2015-02-24 08:36:41 +00:00
2015-05-22 15:32:55 +01:00
$hourInterval = $dateTime -> format ( 'YmdH' );
2015-05-19 08:12:50 +01:00
2015-02-24 08:42:47 +00:00
if ( Config :: get ( 'database.default' ) === 'mysql' ) {
2015-04-19 08:52:39 +01:00
if ( ! isset ( $this -> calc_type ) || $this -> calc_type == self :: CALC_SUM ) {
2015-05-19 08:12:50 +01:00
$value = ( int ) $this -> points ()
-> whereRaw ( 'DATE_FORMAT(created_at, "%Y%m%d%H") = ' . $hourInterval )
-> groupBy ( DB :: raw ( 'HOUR(created_at)' )) -> sum ( 'value' );
2015-04-18 14:06:14 +01:00
} elseif ( $this -> calc_type == self :: CALC_AVG ) {
2015-05-19 08:12:50 +01:00
$value = ( int ) $this -> points ()
-> whereRaw ( 'DATE_FORMAT(created_at, "%Y%m%d%H") = ' . $hourInterval )
-> groupBy ( DB :: raw ( 'HOUR(created_at)' )) -> avg ( 'value' );
2015-03-17 16:41:23 +00:00
}
2015-02-24 08:42:47 +00:00
} else {
2015-03-17 16:41:23 +00:00
// Default metrics calculations.
2015-04-19 08:52:39 +01:00
if ( ! isset ( $this -> calc_type ) || $this -> calc_type == self :: CALC_SUM ) {
2015-04-18 14:06:14 +01:00
$queryType = 'sum(metric_points.value)' ;
} elseif ( $this -> calc_type == self :: CALC_AVG ) {
$queryType = 'avg(metric_points.value)' ;
2015-03-26 15:07:16 -06:00
} else {
2015-04-18 14:06:14 +01:00
$queryType = 'sum(metric_points.value)' ;
2015-03-17 16:41:23 +00:00
}
2015-04-18 14:06:14 +01:00
$query = DB :: select ( " select { $queryType } as aggregate FROM metrics JOIN metric_points ON metric_points.metric_id = metrics.id WHERE metric_points.metric_id = { $this -> id } AND to_char(metric_points.created_at, 'YYYYMMDDHH24') = :timestamp GROUP BY to_char(metric_points.created_at, 'H') " , [
2015-05-19 08:12:50 +01:00
'timestamp' => $hourInterval ,
2015-02-25 15:00:08 +00:00
]);
if ( isset ( $query [ 0 ])) {
$value = $query [ 0 ] -> aggregate ;
2015-02-25 14:23:25 +00:00
} else {
2015-02-25 15:00:08 +00:00
$value = 0 ;
2015-02-25 14:23:25 +00:00
}
2015-02-24 08:42:47 +00:00
}
2015-01-21 02:32:18 -06:00
if ( $value === 0 && $this -> default_value != $value ) {
return $this -> default_value ;
}
return $value ;
}
2014-11-27 16:05:00 +00:00
/**
* Determines whether a chart should be shown .
2014-12-30 18:19:22 +00:00
*
2014-11-27 16:05:00 +00:00
* @ return bool
*/
2014-12-20 21:20:17 +00:00
public function getShouldDisplayAttribute ()
{
2014-11-27 16:05:00 +00:00
return $this -> display_chart === 1 ;
}
2015-05-16 13:57:32 -05:00
/**
* Get the presenter class .
*
* @ return string
*/
public function getPresenterClass ()
{
return 'CachetHQ\Cachet\Presenters\MetricPresenter' ;
}
2014-11-27 22:08:28 +00:00
}