Added a very basic metric API. Closes #4
This commit is contained in:
committed by
James Brooks
parent
99964998ec
commit
cfd3df4266
@@ -5,6 +5,7 @@ namespace CachetHQ\Cachet\Models;
|
||||
use CachetHQ\Cachet\Transformers\MetricTransformer;
|
||||
use Dingo\Api\Transformer\TransformableInterface;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Watson\Validating\ValidatingTrait;
|
||||
|
||||
/**
|
||||
@@ -20,6 +21,15 @@ class Metric extends Model implements TransformableInterface
|
||||
{
|
||||
use ValidatingTrait;
|
||||
|
||||
/**
|
||||
* The model's attributes.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $attributes = [
|
||||
'default_value' => 0,
|
||||
];
|
||||
|
||||
/**
|
||||
* The validation rules.
|
||||
*
|
||||
@@ -48,6 +58,24 @@ class Metric extends Model implements TransformableInterface
|
||||
return $this->hasMany('CachetHQ\Cachet\Models\MetricPoint', 'metric_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all values a metric has.
|
||||
*
|
||||
* @param int $hour
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getValues($hour)
|
||||
{
|
||||
$value = (int) $this->points()->whereRaw('DATE_FORMAT(created_at, "%Y%c%e%H") = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL '.$hour.' HOUR), "%Y%c%e%H")')->whereRaw('HOUR(created_at) = HOUR(DATE_SUB(NOW(), INTERVAL '.$hour.' HOUR))')->groupBy(DB::raw('HOUR(created_at)'))->sum('value');
|
||||
|
||||
if ($value === 0 && $this->default_value != $value) {
|
||||
return $this->default_value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether a chart should be shown.
|
||||
*
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace CachetHQ\Cachet\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Watson\Validating\ValidatingTrait;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
@@ -13,6 +14,24 @@ use Illuminate\Database\Eloquent\Model;
|
||||
*/
|
||||
class MetricPoint extends Model
|
||||
{
|
||||
use ValidatingTrait;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['metric_id', 'value'];
|
||||
|
||||
/**
|
||||
* The validation rules.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $rules = [
|
||||
'value' => 'integer|required',
|
||||
];
|
||||
|
||||
/**
|
||||
* A metric point belongs to a metric unit.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user