diff --git a/app/Bus/Commands/Metric/AddMetricCommand.php b/app/Bus/Commands/Metric/AddMetricCommand.php index 683caf1a..d1dd2546 100644 --- a/app/Bus/Commands/Metric/AddMetricCommand.php +++ b/app/Bus/Commands/Metric/AddMetricCommand.php @@ -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; } } diff --git a/app/Bus/Commands/Metric/UpdateMetricCommand.php b/app/Bus/Commands/Metric/UpdateMetricCommand.php index c12b0ec0..295f553b 100644 --- a/app/Bus/Commands/Metric/UpdateMetricCommand.php +++ b/app/Bus/Commands/Metric/UpdateMetricCommand.php @@ -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; } } diff --git a/app/Bus/Handlers/Commands/Metric/AddMetricCommandHandler.php b/app/Bus/Handlers/Commands/Metric/AddMetricCommandHandler.php index 487db4a7..a5733b28 100644 --- a/app/Bus/Handlers/Commands/Metric/AddMetricCommandHandler.php +++ b/app/Bus/Handlers/Commands/Metric/AddMetricCommandHandler.php @@ -36,6 +36,7 @@ class AddMetricCommandHandler 'places' => $command->places, 'default_view' => $command->default_view, 'threshold' => $command->threshold, + 'order' => $command->order, ]); event(new MetricWasAddedEvent($metric)); diff --git a/app/Bus/Handlers/Commands/Metric/UpdateMetricCommandHandler.php b/app/Bus/Handlers/Commands/Metric/UpdateMetricCommandHandler.php index d354bf69..e8abda24 100644 --- a/app/Bus/Handlers/Commands/Metric/UpdateMetricCommandHandler.php +++ b/app/Bus/Handlers/Commands/Metric/UpdateMetricCommandHandler.php @@ -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) { diff --git a/app/Composers/MetricsComposer.php b/app/Composers/MetricsComposer.php index 86a5e7ee..efc4a413 100644 --- a/app/Composers/MetricsComposer.php +++ b/app/Composers/MetricsComposer.php @@ -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) diff --git a/app/Http/Controllers/Api/MetricController.php b/app/Http/Controllers/Api/MetricController.php index a54b9d22..07e4b3af 100644 --- a/app/Http/Controllers/Api/MetricController.php +++ b/app/Http/Controllers/Api/MetricController.php @@ -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(); diff --git a/app/Http/Controllers/Dashboard/MetricController.php b/app/Http/Controllers/Dashboard/MetricController.php index 0e1d2e5b..ad50d14f 100644 --- a/app/Http/Controllers/Dashboard/MetricController.php +++ b/app/Http/Controllers/Dashboard/MetricController.php @@ -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')) diff --git a/app/Models/Metric.php b/app/Models/Metric.php index 83580691..3ef826f3 100644 --- a/app/Models/Metric.php +++ b/app/Models/Metric.php @@ -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. * diff --git a/database/migrations/2016_06_02_075012_AlterTableMetricsAddOrderColumn.php b/database/migrations/2016_06_02_075012_AlterTableMetricsAddOrderColumn.php new file mode 100644 index 00000000..967235a8 --- /dev/null +++ b/database/migrations/2016_06_02_075012_AlterTableMetricsAddOrderColumn.php @@ -0,0 +1,41 @@ +tinyInteger('order')->after('threshold')->default(0); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('metrics', function (Blueprint $table) { + $table->dropColumn('order'); + }); + } +} diff --git a/tests/Api/MetricTest.php b/tests/Api/MetricTest.php index 0e1e6461..e65adc81 100644 --- a/tests/Api/MetricTest.php +++ b/tests/Api/MetricTest.php @@ -62,6 +62,8 @@ class MetricTest extends AbstractApiTestCase 'display_chart' => 1, 'places' => 0, 'view' => 0, + 'threshold' => 5, + 'order' => 1, ]); $this->seeJson(['name' => 'Foo']); $this->assertResponseOk(); diff --git a/tests/Bus/Commands/Metric/AddMetricCommandTest.php b/tests/Bus/Commands/Metric/AddMetricCommandTest.php index 5242aedc..0107d6e8 100644 --- a/tests/Bus/Commands/Metric/AddMetricCommandTest.php +++ b/tests/Bus/Commands/Metric/AddMetricCommandTest.php @@ -29,15 +29,16 @@ class AddMetricCommandTest extends AbstractTestCase protected function getObjectAndParams() { $params = [ - 'name' => 'Coffee', - 'suffix' => 'cups', - 'description' => 'Cups of coffee consumed', - 'default_value' => 0, - 'calc_type' => 0, - 'display_chart' => 1, - 'places' => 0, - 'default_view' => 0, - 'threshold' => 0, + 'name' => 'Coffee', + 'suffix' => 'cups', + 'description' => 'Cups of coffee consumed', + 'default_value' => 0, + 'calc_type' => 0, + 'display_chart' => 1, + 'places' => 0, + 'default_view' => 0, + 'threshold' => 0, + 'order' => 0, ]; $object = new AddMetricCommand( @@ -49,7 +50,8 @@ class AddMetricCommandTest extends AbstractTestCase $params['display_chart'], $params['places'], $params['default_view'], - $params['threshold'] + $params['threshold'], + $params['order'] ); return compact('params', 'object'); diff --git a/tests/Bus/Commands/Metric/UpdateMetricCommandTest.php b/tests/Bus/Commands/Metric/UpdateMetricCommandTest.php index eac55a60..048a4ec2 100644 --- a/tests/Bus/Commands/Metric/UpdateMetricCommandTest.php +++ b/tests/Bus/Commands/Metric/UpdateMetricCommandTest.php @@ -40,6 +40,7 @@ class UpdateMetricCommandTest extends AbstractTestCase 'places' => 0, 'default_view' => 0, 'threshold' => 0, + 'order' => 0, ]; $object = new UpdateMetricCommand( @@ -52,7 +53,8 @@ class UpdateMetricCommandTest extends AbstractTestCase $params['display_chart'], $params['places'], $params['default_view'], - $params['threshold'] + $params['threshold'], + $params['order'] ); return compact('params', 'object');