Set an order on metrics via the API. Closes #1874

This commit is contained in:
James Brooks
2016-06-02 09:06:40 +01:00
parent 439ac9fe44
commit cab030237b
12 changed files with 106 additions and 17 deletions
+11 -1
View File
@@ -76,6 +76,13 @@ final class AddMetricCommand
*/
public $threshold;
/**
* The order of which to place the metric in.
*
* @var int
*/
public $order;
/**
* The validation rules.
*
@@ -92,6 +99,7 @@ final class AddMetricCommand
'places' => 'int|between:0,4',
'default_view' => 'int|between:0,3',
'threshold' => 'numeric|between:0,10',
'order' => 'int',
];
/**
@@ -106,10 +114,11 @@ final class AddMetricCommand
* @param int $places
* @param int $default_view
* @param int $threshold
* @param int $order
*
* @return void
*/
public function __construct($name, $suffix, $description, $default_value, $calc_type, $display_chart, $places, $default_view, $threshold)
public function __construct($name, $suffix, $description, $default_value, $calc_type, $display_chart, $places, $default_view, $threshold, $order = 0)
{
$this->name = $name;
$this->suffix = $suffix;
@@ -120,5 +129,6 @@ final class AddMetricCommand
$this->places = $places;
$this->default_view = $default_view;
$this->threshold = $threshold;
$this->order = $order;
}
}
@@ -85,6 +85,13 @@ final class UpdateMetricCommand
*/
public $threshold;
/**
* The order of which to place the metric in.
*
* @var int
*/
public $order;
/**
* The validation rules.
*
@@ -101,6 +108,7 @@ final class UpdateMetricCommand
'places' => 'numeric|between:0,4',
'default_view' => 'numeric|between:0,4',
'threshold' => 'numeric|between:0,10',
'order' => 'int',
];
/**
@@ -116,10 +124,11 @@ final class UpdateMetricCommand
* @param int $places
* @param int $default_view
* @param int $threshold
* @param int $order
*
* @return void
*/
public function __construct(Metric $metric, $name, $suffix, $description, $default_value, $calc_type, $display_chart, $places, $default_view, $threshold)
public function __construct(Metric $metric, $name, $suffix, $description, $default_value, $calc_type, $display_chart, $places, $default_view, $threshold, $order)
{
$this->metric = $metric;
$this->name = $name;
@@ -131,5 +140,6 @@ final class UpdateMetricCommand
$this->places = $places;
$this->default_view = $default_view;
$this->threshold = $threshold;
$this->order = $order;
}
}
@@ -36,6 +36,7 @@ class AddMetricCommandHandler
'places' => $command->places,
'default_view' => $command->default_view,
'threshold' => $command->threshold,
'order' => $command->order,
]);
event(new MetricWasAddedEvent($metric));
@@ -54,6 +54,7 @@ class UpdateMetricCommandHandler
'places' => $command->places,
'default_view' => $command->default_view,
'threshold' => $command->threshold,
'order' => $command->order,
];
return array_filter($params, function ($val) {
+1 -1
View File
@@ -28,7 +28,7 @@ class MetricsComposer
{
$metrics = null;
if ($displayMetrics = Config::get('setting.display_graphs')) {
$metrics = Metric::where('display_chart', 1)->orderBy('id')->get();
$metrics = Metric::displayable()->orderBy('order')->get();
}
$view->withDisplayMetrics($displayMetrics)
@@ -85,7 +85,8 @@ class MetricController extends AbstractApiController
Binput::get('display_chart', true),
Binput::get('places', 2),
Binput::get('view', 1),
Binput::get('threshold', 5)
Binput::get('threshold', 5),
Binput::get('order', 0)
));
} catch (QueryException $e) {
throw new BadRequestHttpException();
@@ -114,7 +115,8 @@ class MetricController extends AbstractApiController
Binput::get('display_chart'),
Binput::get('places'),
Binput::get('view'),
Binput::get('threshold')
Binput::get('threshold'),
Binput::get('order')
));
} catch (QueryException $e) {
throw new BadRequestHttpException();
@@ -31,7 +31,7 @@ class MetricController extends Controller
*/
public function showMetrics()
{
$metrics = Metric::orderBy('created_at', 'desc')->get();
$metrics = Metric::orderBy('order')->get();
return View::make('dashboard.metrics.index')
->withPageTitle(trans('dashboard.metrics.metrics').' - '.trans('dashboard.dashboard'))
+18
View File
@@ -14,6 +14,7 @@ namespace CachetHQ\Cachet\Models;
use AltThree\Validator\ValidatingTrait;
use CachetHQ\Cachet\Models\Traits\SortableTrait;
use CachetHQ\Cachet\Presenters\MetricPresenter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use McCool\LaravelAutoPresenter\HasPresenter;
@@ -48,6 +49,7 @@ class Metric extends Model implements HasPresenter
'places' => 2,
'default_view' => 1,
'threshold' => 5,
'order' => 0,
];
/**
@@ -63,6 +65,7 @@ class Metric extends Model implements HasPresenter
'places' => 'int',
'default_view' => 'int',
'threshold' => 'int',
'order' => 'int',
];
/**
@@ -80,6 +83,7 @@ class Metric extends Model implements HasPresenter
'places',
'default_view',
'threshold',
'order',
];
/**
@@ -95,6 +99,7 @@ class Metric extends Model implements HasPresenter
'places' => 'numeric|between:0,4',
'default_view' => 'numeric|between:0,3',
'threshold' => 'numeric|between:0,10',
'threshold' => 'int',
];
/**
@@ -108,6 +113,7 @@ class Metric extends Model implements HasPresenter
'display_chart',
'default_value',
'calc_type',
'order',
];
/**
@@ -120,6 +126,18 @@ class Metric extends Model implements HasPresenter
return $this->hasMany(MetricPoint::class, 'metric_id', 'id');
}
/**
* 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.
*