Improve metric threshold

This commit is contained in:
James Brooks
2017-03-18 11:15:43 +00:00
parent f7435748ec
commit 4607d90401
6 changed files with 45 additions and 6 deletions

View File

@@ -111,7 +111,7 @@ final class CreateMetricCommand
'display_chart' => 'nullable|int',
'places' => 'nullable|int|between:0,4',
'default_view' => 'required|int|between:0,3',
'threshold' => 'nullable|numeric|between:0,10',
'threshold' => 'required|int',
'order' => 'nullable|int',
'visible' => 'required|int|between:0,2',
];

View File

@@ -114,7 +114,7 @@ final class UpdateMetricCommand
'display_chart' => 'nullable|int',
'places' => 'nullable|numeric|between:0,4',
'default_view' => 'nullable|numeric|between:0,4',
'threshold' => 'nullable|numeric|between:0,10',
'threshold' => 'nullable|int',
'order' => 'nullable|int',
'visible' => 'required|int|between:0,2',
];

View File

@@ -45,7 +45,8 @@ class MetricController extends Controller
public function showAddMetric()
{
return View::make('dashboard.metrics.add')
->withPageTitle(trans('dashboard.metrics.add.title').' - '.trans('dashboard.dashboard'));
->withPageTitle(trans('dashboard.metrics.add.title').' - '.trans('dashboard.dashboard'))
->withAcceptableThresholds(Metric::ACCEPTABLE_THRESHOLDS);
}
/**
@@ -131,7 +132,8 @@ class MetricController extends Controller
{
return View::make('dashboard.metrics.edit')
->withPageTitle(trans('dashboard.metrics.edit.title').' - '.trans('dashboard.dashboard'))
->withMetric($metric);
->withMetric($metric)
->withAcceptableThresholds(Metric::ACCEPTABLE_THRESHOLDS);
}
/**

View File

@@ -12,10 +12,12 @@
namespace CachetHQ\Cachet\Models;
use AltThree\Validator\ValidatingTrait;
use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Models\Traits\SortableTrait;
use CachetHQ\Cachet\Presenters\MetricPresenter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\MessageBag;
use McCool\LaravelAutoPresenter\HasPresenter;
class Metric extends Model implements HasPresenter
@@ -57,6 +59,13 @@ class Metric extends Model implements HasPresenter
*/
const VISIBLE_HIDDEN = 2;
/**
* 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.
*
@@ -200,6 +209,26 @@ class Metric extends Model implements HasPresenter
return $this->display_chart;
}
/**
* Validate the model before save.
*
* @throws \AltThree\Validator\ValidationException
*
* @return void
*/
public function validate()
{
$messages = [];
if (60 % $this->threshold === 0) {
$messages[] = 'Threshold must be divisible by 60.';
}
if ($messages) {
throw new ValidationException(new MessageBag($messages));
}
}
/**
* Get the presenter class.
*

View File

@@ -57,7 +57,11 @@
</div>
<div class="form-group">
<label for="metric-places">{{ trans('forms.metrics.threshold') }}</label>
<input type="number" min="0" max="100" class="form-control" name="metric[threshold]" id="metric-threshold" required value="{{ Binput::old('metric.threshold') }}" placeholder="{{ trans('forms.metrics.threshold') }}">
<select name="metric[threshold]" class="form-control" required>
@foreach ($acceptable_thresholds as $threshold)
<option {{ (int) Binput::old('metric.threshold') === $threshold ? 'selected' : null }}>{{ $threshold }}</option>
@endforeach
</select>
</div>
<div class="checkbox">
<label>

View File

@@ -57,7 +57,11 @@
</div>
<div class="form-group">
<label for="metric-places">{{ trans('forms.metrics.threshold') }}</label>
<input type="number" min="0" max="100" class="form-control" name="threshold" id="metric-threshold" required value="{{ $metric->threshold }}" placeholder="{{ trans('forms.metrics.threshold') }}">
<select name="threshold" class="form-control" required>
@foreach ($acceptable_thresholds as $threshold)
<option {{ (int) Binput::old('metric.threshold') === $threshold || $metric->threshold === $threshold ? 'selected' : null }}>{{ $threshold }}</option>
@endforeach
</select>
</div>
<div class="checkbox">
<label>