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

View File

@@ -76,6 +76,13 @@ final class AddMetricCommand
*/ */
public $threshold; public $threshold;
/**
* The order of which to place the metric in.
*
* @var int
*/
public $order;
/** /**
* The validation rules. * The validation rules.
* *
@@ -92,6 +99,7 @@ final class AddMetricCommand
'places' => 'int|between:0,4', 'places' => 'int|between:0,4',
'default_view' => 'int|between:0,3', 'default_view' => 'int|between:0,3',
'threshold' => 'numeric|between:0,10', 'threshold' => 'numeric|between:0,10',
'order' => 'int',
]; ];
/** /**
@@ -106,10 +114,11 @@ final class AddMetricCommand
* @param int $places * @param int $places
* @param int $default_view * @param int $default_view
* @param int $threshold * @param int $threshold
* @param int $order
* *
* @return void * @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->name = $name;
$this->suffix = $suffix; $this->suffix = $suffix;
@@ -120,5 +129,6 @@ final class AddMetricCommand
$this->places = $places; $this->places = $places;
$this->default_view = $default_view; $this->default_view = $default_view;
$this->threshold = $threshold; $this->threshold = $threshold;
$this->order = $order;
} }
} }

View File

@@ -85,6 +85,13 @@ final class UpdateMetricCommand
*/ */
public $threshold; public $threshold;
/**
* The order of which to place the metric in.
*
* @var int
*/
public $order;
/** /**
* The validation rules. * The validation rules.
* *
@@ -101,6 +108,7 @@ final class UpdateMetricCommand
'places' => 'numeric|between:0,4', 'places' => 'numeric|between:0,4',
'default_view' => 'numeric|between:0,4', 'default_view' => 'numeric|between:0,4',
'threshold' => 'numeric|between:0,10', 'threshold' => 'numeric|between:0,10',
'order' => 'int',
]; ];
/** /**
@@ -116,10 +124,11 @@ final class UpdateMetricCommand
* @param int $places * @param int $places
* @param int $default_view * @param int $default_view
* @param int $threshold * @param int $threshold
* @param int $order
* *
* @return void * @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->metric = $metric;
$this->name = $name; $this->name = $name;
@@ -131,5 +140,6 @@ final class UpdateMetricCommand
$this->places = $places; $this->places = $places;
$this->default_view = $default_view; $this->default_view = $default_view;
$this->threshold = $threshold; $this->threshold = $threshold;
$this->order = $order;
} }
} }

View File

@@ -36,6 +36,7 @@ class AddMetricCommandHandler
'places' => $command->places, 'places' => $command->places,
'default_view' => $command->default_view, 'default_view' => $command->default_view,
'threshold' => $command->threshold, 'threshold' => $command->threshold,
'order' => $command->order,
]); ]);
event(new MetricWasAddedEvent($metric)); event(new MetricWasAddedEvent($metric));

View File

@@ -54,6 +54,7 @@ class UpdateMetricCommandHandler
'places' => $command->places, 'places' => $command->places,
'default_view' => $command->default_view, 'default_view' => $command->default_view,
'threshold' => $command->threshold, 'threshold' => $command->threshold,
'order' => $command->order,
]; ];
return array_filter($params, function ($val) { return array_filter($params, function ($val) {

View File

@@ -28,7 +28,7 @@ class MetricsComposer
{ {
$metrics = null; $metrics = null;
if ($displayMetrics = Config::get('setting.display_graphs')) { 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) $view->withDisplayMetrics($displayMetrics)

View File

@@ -85,7 +85,8 @@ class MetricController extends AbstractApiController
Binput::get('display_chart', true), Binput::get('display_chart', true),
Binput::get('places', 2), Binput::get('places', 2),
Binput::get('view', 1), Binput::get('view', 1),
Binput::get('threshold', 5) Binput::get('threshold', 5),
Binput::get('order', 0)
)); ));
} catch (QueryException $e) { } catch (QueryException $e) {
throw new BadRequestHttpException(); throw new BadRequestHttpException();
@@ -114,7 +115,8 @@ class MetricController extends AbstractApiController
Binput::get('display_chart'), Binput::get('display_chart'),
Binput::get('places'), Binput::get('places'),
Binput::get('view'), Binput::get('view'),
Binput::get('threshold') Binput::get('threshold'),
Binput::get('order')
)); ));
} catch (QueryException $e) { } catch (QueryException $e) {
throw new BadRequestHttpException(); throw new BadRequestHttpException();

View File

@@ -31,7 +31,7 @@ class MetricController extends Controller
*/ */
public function showMetrics() public function showMetrics()
{ {
$metrics = Metric::orderBy('created_at', 'desc')->get(); $metrics = Metric::orderBy('order')->get();
return View::make('dashboard.metrics.index') return View::make('dashboard.metrics.index')
->withPageTitle(trans('dashboard.metrics.metrics').' - '.trans('dashboard.dashboard')) ->withPageTitle(trans('dashboard.metrics.metrics').' - '.trans('dashboard.dashboard'))

View File

@@ -14,6 +14,7 @@ namespace CachetHQ\Cachet\Models;
use AltThree\Validator\ValidatingTrait; use AltThree\Validator\ValidatingTrait;
use CachetHQ\Cachet\Models\Traits\SortableTrait; use CachetHQ\Cachet\Models\Traits\SortableTrait;
use CachetHQ\Cachet\Presenters\MetricPresenter; use CachetHQ\Cachet\Presenters\MetricPresenter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use McCool\LaravelAutoPresenter\HasPresenter; use McCool\LaravelAutoPresenter\HasPresenter;
@@ -48,6 +49,7 @@ class Metric extends Model implements HasPresenter
'places' => 2, 'places' => 2,
'default_view' => 1, 'default_view' => 1,
'threshold' => 5, 'threshold' => 5,
'order' => 0,
]; ];
/** /**
@@ -63,6 +65,7 @@ class Metric extends Model implements HasPresenter
'places' => 'int', 'places' => 'int',
'default_view' => 'int', 'default_view' => 'int',
'threshold' => 'int', 'threshold' => 'int',
'order' => 'int',
]; ];
/** /**
@@ -80,6 +83,7 @@ class Metric extends Model implements HasPresenter
'places', 'places',
'default_view', 'default_view',
'threshold', 'threshold',
'order',
]; ];
/** /**
@@ -95,6 +99,7 @@ class Metric extends Model implements HasPresenter
'places' => 'numeric|between:0,4', 'places' => 'numeric|between:0,4',
'default_view' => 'numeric|between:0,3', 'default_view' => 'numeric|between:0,3',
'threshold' => 'numeric|between:0,10', 'threshold' => 'numeric|between:0,10',
'threshold' => 'int',
]; ];
/** /**
@@ -108,6 +113,7 @@ class Metric extends Model implements HasPresenter
'display_chart', 'display_chart',
'default_value', 'default_value',
'calc_type', 'calc_type',
'order',
]; ];
/** /**
@@ -120,6 +126,18 @@ class Metric extends Model implements HasPresenter
return $this->hasMany(MetricPoint::class, 'metric_id', 'id'); 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. * Determines whether a chart should be shown.
* *

View File

@@ -0,0 +1,41 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AlterTableMetricsAddOrderColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('metrics', function (Blueprint $table) {
$table->tinyInteger('order')->after('threshold')->default(0);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('metrics', function (Blueprint $table) {
$table->dropColumn('order');
});
}
}

View File

@@ -62,6 +62,8 @@ class MetricTest extends AbstractApiTestCase
'display_chart' => 1, 'display_chart' => 1,
'places' => 0, 'places' => 0,
'view' => 0, 'view' => 0,
'threshold' => 5,
'order' => 1,
]); ]);
$this->seeJson(['name' => 'Foo']); $this->seeJson(['name' => 'Foo']);
$this->assertResponseOk(); $this->assertResponseOk();

View File

@@ -29,15 +29,16 @@ class AddMetricCommandTest extends AbstractTestCase
protected function getObjectAndParams() protected function getObjectAndParams()
{ {
$params = [ $params = [
'name' => 'Coffee', 'name' => 'Coffee',
'suffix' => 'cups', 'suffix' => 'cups',
'description' => 'Cups of coffee consumed', 'description' => 'Cups of coffee consumed',
'default_value' => 0, 'default_value' => 0,
'calc_type' => 0, 'calc_type' => 0,
'display_chart' => 1, 'display_chart' => 1,
'places' => 0, 'places' => 0,
'default_view' => 0, 'default_view' => 0,
'threshold' => 0, 'threshold' => 0,
'order' => 0,
]; ];
$object = new AddMetricCommand( $object = new AddMetricCommand(
@@ -49,7 +50,8 @@ class AddMetricCommandTest extends AbstractTestCase
$params['display_chart'], $params['display_chart'],
$params['places'], $params['places'],
$params['default_view'], $params['default_view'],
$params['threshold'] $params['threshold'],
$params['order']
); );
return compact('params', 'object'); return compact('params', 'object');

View File

@@ -40,6 +40,7 @@ class UpdateMetricCommandTest extends AbstractTestCase
'places' => 0, 'places' => 0,
'default_view' => 0, 'default_view' => 0,
'threshold' => 0, 'threshold' => 0,
'order' => 0,
]; ];
$object = new UpdateMetricCommand( $object = new UpdateMetricCommand(
@@ -52,7 +53,8 @@ class UpdateMetricCommandTest extends AbstractTestCase
$params['display_chart'], $params['display_chart'],
$params['places'], $params['places'],
$params['default_view'], $params['default_view'],
$params['threshold'] $params['threshold'],
$params['order']
); );
return compact('params', 'object'); return compact('params', 'object');