Add and remove metrics and metric points commands

This commit is contained in:
Joseph Cohen 2015-08-15 22:15:41 -05:00 committed by James Brooks
parent 64ff4d73c2
commit 9581c5a394
16 changed files with 489 additions and 26 deletions

View File

@ -11,7 +11,7 @@
namespace CachetHQ\Cachet\Commands\Metric; namespace CachetHQ\Cachet\Commands\Metric;
class AddNewMetricCommand class AddMetricCommand
{ {
/** /**
* The metric name. * The metric name.
@ -39,40 +39,63 @@ class AddNewMetricCommand
* *
* @var float * @var float
*/ */
public $default; public $default_value;
/** /**
* The metric calc type. * The metric calculation type.
* *
* @var int * @var int
*/ */
public $type; public $calc_type;
/** /**
* The metric display chart. * The metric display chart.
* *
* @var int * @var int
*/ */
public $chart; public $display_chart;
/** /**
* Create a new add team member command instance. * The metric decimal places.
*
* @var int
*/
public $places;
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'name' => 'required',
'suffix' => 'required',
'display_chart' => 'boolean',
'default_value' => 'numeric',
'places' => 'numeric|min:0|max:4',
];
/**
* Create a new add metric command instance.
* *
* @param string $name * @param string $name
* @param string $suffix * @param string $suffix
* @param string $description * @param string $description
* @param float $default * @param float $default_value
* @param int $type * @param int $calc_type
* @param int $chart * @param int $display_chart
* @param int $places
* *
* @return void * @return void
*/ */
public function __construct($name, $suffix, $description, $default, $type, $chart) public function __construct($name, $suffix, $description, $default_value, $calc_type, $display_chart, $places)
{ {
$this->name = $name; $this->name = $name;
$this->suffix = $suffix; $this->suffix = $suffix;
$this->description = $description; $this->description = $description;
$this->default = $default; $this->default_value = $default_value;
$this->chart = $chart; $this->calc_type = $calc_type;
$this->display_chart = $display_chart;
$this->places = $places;
} }
} }

View File

@ -0,0 +1,54 @@
<?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.
*/
namespace CachetHQ\Cachet\Commands\Metric;
use CachetHQ\Cachet\Models\Metric;
class AddMetricPointCommand
{
/**
* The metric to add.
*
* @var \CachetHQ\Cachet\Models\Metric
*/
public $metric;
/**
* The metric point value.
*
* @var int
*/
public $value;
/**
* The metric point created at.
*
* @var string
*/
public $createdAt;
/**
* Create a new add metric point command instance.
*
* @param \CachetHQ\Cachet\Models\Metric $metric
* @param int $value
* @param string $createdAt
*
* @return void
*/
public function __construct(Metric $metric, $value, $createdAt)
{
$this->metric = $metric;
$this->value = $value;
$this->createdAt = $createdAt;
}
}

View File

@ -0,0 +1,36 @@
<?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.
*/
namespace CachetHQ\Cachet\Commands\Metric;
use CachetHQ\Cachet\Models\Metric;
class RemoveMetricCommand
{
/**
* The metric to remove.
*
* @var \CachetHQ\Cachet\Models\Metric
*/
public $metric;
/**
* Create a new remove metric command instance.
*
* @param \CachetHQ\Cachet\Models\Metric $metric
*
* @return void
*/
public function __construct(Metric $metric)
{
$this->metric = $metric;
}
}

View File

@ -0,0 +1,36 @@
<?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.
*/
namespace CachetHQ\Cachet\Commands\Metric;
use CachetHQ\Cachet\Models\MetricPoint;
class RemoveMetricPointCommand
{
/**
* The metric point to remove.
*
* @var \CachetHQ\Cachet\Models\MetricPoint
*/
public $metricPoint;
/**
* Create a new remove metric point command instance.
*
* @param \CachetHQ\Cachet\Models\MetricPoint $metricPoint
*
* @return void
*/
public function __construct(MetricPoint $metricPoint)
{
$this->metricPoint = $metricPoint;
}
}

View File

@ -0,0 +1,34 @@
<?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.
*/
namespace CachetHQ\Cachet\Events\Metric;
use CachetHQ\Cachet\Models\MetricPoint;
class MetricPointWasAddedEvent
{
/**
* The metric point that was added.
*
* @var \CachetHQ\Cachet\Models\MetricPoint
*/
public $metric;
/**
* Create a new metric point was added event instance.
*
* @return void
*/
public function __construct(MetricPoint $metric)
{
$this->metric = $metric;
}
}

View File

@ -0,0 +1,34 @@
<?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.
*/
namespace CachetHQ\Cachet\Events\Metric;
use CachetHQ\Cachet\Models\MetricPoint;
class MetricPointWasRemovedEvent
{
/**
* The metric point that was removed.
*
* @var \CachetHQ\Cachet\Models\MetricPoint
*/
public $metricPoint;
/**
* Create a new metric point was removed event instance.
*
* @return void
*/
public function __construct(MetricPoint $metricPoint)
{
$this->metricPoint = $metricPoint;
}
}

View File

@ -0,0 +1,34 @@
<?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.
*/
namespace CachetHQ\Cachet\Events\Metric;
use CachetHQ\Cachet\Models\Metric;
class MetricWasAddedEvent
{
/**
* The metric that was added.
*
* @var \CachetHQ\Cachet\Models\Metric
*/
public $metric;
/**
* Create a new metric was added event instance.
*
* @return void
*/
public function __construct(Metric $metric)
{
$this->metric = $metric;
}
}

View File

@ -0,0 +1,34 @@
<?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.
*/
namespace CachetHQ\Cachet\Events\Metric;
use CachetHQ\Cachet\Models\Metric;
class MetricWasRemovedEvent
{
/**
* The metric that was removed.
*
* @var \CachetHQ\Cachet\Models\Metric
*/
public $metric;
/**
* Create a new metric was removed event instance.
*
* @return void
*/
public function __construct(Metric $metric)
{
$this->metric = $metric;
}
}

View File

@ -0,0 +1,43 @@
<?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.
*/
namespace CachetHQ\Cachet\Handlers\Commands\Metric;
use CachetHQ\Cachet\Commands\Metric\AddMetricCommand;
use CachetHQ\Cachet\Events\Metric\MetricWasAddedEvent;
use CachetHQ\Cachet\Models\Metric;
class AddMetricCommandHandler
{
/**
* Handle the add metric command.
*
* @param \CachetHQ\Cachet\Commands\Metric\AddMetricCommand $command
*
* @return void
*/
public function handle(AddMetricCommand $command)
{
$metric = Metric::create([
'name' => $command->name,
'suffix' => $command->suffix,
'description' => $command->description,
'default_value' => $command->default_value,
'calc_type' => $command->calc_type,
'display_chart' => $command->display_chart,
'places' => $command->places,
]);
event(new MetricWasAddedEvent($metric));
return $metric;
}
}

View File

@ -0,0 +1,48 @@
<?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.
*/
namespace CachetHQ\Cachet\Handlers\Commands\Metric;
use Carbon\Carbon;
use CachetHQ\Cachet\Commands\Metric\AddMetricPointCommand;
use CachetHQ\Cachet\Events\Metric\MetricPointWasAddedEvent;
use CachetHQ\Cachet\Models\MetricPoint;
class AddMetricPointCommandHandler
{
/**
* Handle the add metric point command.
*
* @param \CachetHQ\Cachet\Commands\Metric\AddMetricPointCommand $command
*
* @return void
*/
public function handle(AddMetricPointCommand $command)
{
$metric = $command->metric;
$createdAt = $command->createdAt;
$data = [
'metric_id' => $metric->id,
'value' => $command->value,
];
if ($createdAt) {
$data['created_at'] = Carbon::createFromFormat('U', $createdAt)->format('Y-m-d H:i:s');
}
$metricPoint = MetricPoint::create($data);
event(new MetricPointWasAddedEvent($metricPoint));
return $metricPoint;
}
}

View File

@ -0,0 +1,35 @@
<?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.
*/
namespace CachetHQ\Cachet\Handlers\Commands\Metric;
use CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand;
use CachetHQ\Cachet\Events\Metric\MetricWasRemovedEvent;
use CachetHQ\Cachet\Models\Metric;
class RemoveMetricCommandHandler
{
/**
* Handle the remove metric command.
*
* @param \CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand $command
*
* @return void
*/
public function handle(RemoveMetricCommand $command)
{
$metric = $command->metric;
event(new MetricWasRemovedEvent($metric));
$metric->delete();
}
}

View File

@ -0,0 +1,35 @@
<?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.
*/
namespace CachetHQ\Cachet\Handlers\Commands\Metric;
use CachetHQ\Cachet\Commands\Metric\RemoveMetricPointCommand;
use CachetHQ\Cachet\Events\Metric\MetricPointWasRemovedEvent;
use CachetHQ\Cachet\Models\Metric;
class RemoveMetricPointCommandHandler
{
/**
* Handle the remove metric point command.
*
* @param \CachetHQ\Cachet\Commands\Metric\RemoveMetricPointCommand $command
*
* @return void
*/
public function handle(RemoveMetricPointCommand $command)
{
$metricPoint = $command->metricPoint;
event(new MetricPointWasRemovedEvent($metricPoint));
$metricPoint->delete();
}
}

View File

@ -11,14 +11,19 @@
namespace CachetHQ\Cachet\Http\Controllers\Api; namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Commands\Metric\AddMetricCommand;
use CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand;
use CachetHQ\Cachet\Models\Metric; use CachetHQ\Cachet\Models\Metric;
use Exception; use Exception;
use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class MetricController extends AbstractApiController class MetricController extends AbstractApiController
{ {
use DispatchesJobs;
/** /**
* Get all metrics. * Get all metrics.
* *
@ -65,7 +70,15 @@ class MetricController extends AbstractApiController
public function postMetrics() public function postMetrics()
{ {
try { try {
$metric = Metric::create(Binput::all()); $metric = $this->dispatch(new AddMetricCommand(
Binput::get('name'),
Binput::get('suffix'),
Binput::get('description'),
Binput::get('default_value'),
Binput::get('calc_type', 0),
Binput::get('display_chart'),
Binput::get('places')
));
} catch (Exception $e) { } catch (Exception $e) {
throw new BadRequestHttpException(); throw new BadRequestHttpException();
} }
@ -100,7 +113,7 @@ class MetricController extends AbstractApiController
*/ */
public function deleteMetric(Metric $metric) public function deleteMetric(Metric $metric)
{ {
$metric->delete(); $this->dispatch(new RemoveMetricCommand($metric));
return $this->noContent(); return $this->noContent();
} }

View File

@ -11,14 +11,20 @@
namespace CachetHQ\Cachet\Http\Controllers\Api; namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Commands\Metric\AddMetricPointCommand;
use CachetHQ\Cachet\Commands\Metric\RemoveMetricPointCommand;
use CachetHQ\Cachet\Models\Metric; use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Models\MetricPoint; use CachetHQ\Cachet\Models\MetricPoint;
use Carbon\Carbon; use Carbon\Carbon;
use Exception; use Exception;
use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class MetricPointController extends AbstractApiController class MetricPointController extends AbstractApiController
{ {
use DispatchesJobs;
/** /**
* Get a single metric point. * Get a single metric point.
* *
@ -41,16 +47,8 @@ class MetricPointController extends AbstractApiController
*/ */
public function postMetricPoints(Metric $metric) public function postMetricPoints(Metric $metric)
{ {
$metricPointData = Binput::all();
$metricPointData['metric_id'] = $metric->id;
if ($timestamp = array_pull($metricPointData, 'timestamp')) {
$pointTimestamp = Carbon::createFromFormat('U', $timestamp);
$metricPointData['created_at'] = $pointTimestamp->format('Y-m-d H:i:s');
}
try { try {
$metricPoint = MetricPoint::create($metricPointData); $metricPoint = $this->dispatch(new AddMetricPointCommand($metric, Binput::get('value'), Binput::get('timestamp')));
} catch (Exception $e) { } catch (Exception $e) {
throw new BadRequestHttpException(); throw new BadRequestHttpException();
} }
@ -91,7 +89,7 @@ class MetricPointController extends AbstractApiController
*/ */
public function deleteMetricPoint(Metric $metric, MetricPoint $metricPoint) public function deleteMetricPoint(Metric $metric, MetricPoint $metricPoint)
{ {
$metricPoint->delete(); $this->dispatch(new RemoveMetricPointCommand($metricPoint));
return $this->noContent(); return $this->noContent();
} }

View File

@ -12,15 +12,20 @@
namespace CachetHQ\Cachet\Http\Controllers\Dashboard; namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
use AltThree\Validator\ValidationException; use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Commands\Metric\AddMetricCommand;
use CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand;
use CachetHQ\Cachet\Models\Metric; use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Models\MetricPoint; use CachetHQ\Cachet\Models\MetricPoint;
use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller; use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View; use Illuminate\Support\Facades\View;
class MetricController extends Controller class MetricController extends Controller
{ {
use DispatchesJobs;
/** /**
* Shows the metrics view. * Shows the metrics view.
* *
@ -66,7 +71,7 @@ class MetricController extends Controller
public function createMetricAction() public function createMetricAction()
{ {
try { try {
Metric::create(Binput::get('metric')); $this->dispatchFromArray(AddMetricCommand::class, Binput::get('metric'));
} catch (ValidationException $e) { } catch (ValidationException $e) {
return Redirect::route('dashboard.metrics.add') return Redirect::route('dashboard.metrics.add')
->withInput(Binput::all()) ->withInput(Binput::all())
@ -98,7 +103,7 @@ class MetricController extends Controller
*/ */
public function deleteMetricAction(Metric $metric) public function deleteMetricAction(Metric $metric)
{ {
$metric->delete(); $this->dispatch(new RemoveMetricCommand($metric));
return Redirect::route('dashboard.metrics.index'); return Redirect::route('dashboard.metrics.index');
} }

View File

@ -59,6 +59,7 @@ class MetricTest extends AbstractTestCase
'description' => 'Lorem ipsum dolor', 'description' => 'Lorem ipsum dolor',
'default_value' => 1, 'default_value' => 1,
'display_chart' => 1, 'display_chart' => 1,
'places' => 0,
]); ]);
$this->seeJson(['name' => 'Foo']); $this->seeJson(['name' => 'Foo']);
$this->assertResponseOk(); $this->assertResponseOk();