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;
class AddNewMetricCommand
class AddMetricCommand
{
/**
* The metric name.
@ -39,40 +39,63 @@ class AddNewMetricCommand
*
* @var float
*/
public $default;
public $default_value;
/**
* The metric calc type.
* The metric calculation type.
*
* @var int
*/
public $type;
public $calc_type;
/**
* The metric display chart.
*
* @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 $suffix
* @param string $description
* @param float $default
* @param int $type
* @param int $chart
* @param float $default_value
* @param int $calc_type
* @param int $display_chart
* @param int $places
*
* @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->suffix = $suffix;
$this->description = $description;
$this->default = $default;
$this->chart = $chart;
$this->default_value = $default_value;
$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;
use CachetHQ\Cachet\Commands\Metric\AddMetricCommand;
use CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand;
use CachetHQ\Cachet\Models\Metric;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class MetricController extends AbstractApiController
{
use DispatchesJobs;
/**
* Get all metrics.
*
@ -65,7 +70,15 @@ class MetricController extends AbstractApiController
public function postMetrics()
{
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) {
throw new BadRequestHttpException();
}
@ -100,7 +113,7 @@ class MetricController extends AbstractApiController
*/
public function deleteMetric(Metric $metric)
{
$metric->delete();
$this->dispatch(new RemoveMetricCommand($metric));
return $this->noContent();
}

View File

@ -11,14 +11,20 @@
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\MetricPoint;
use Carbon\Carbon;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class MetricPointController extends AbstractApiController
{
use DispatchesJobs;
/**
* Get a single metric point.
*
@ -41,16 +47,8 @@ class MetricPointController extends AbstractApiController
*/
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 {
$metricPoint = MetricPoint::create($metricPointData);
$metricPoint = $this->dispatch(new AddMetricPointCommand($metric, Binput::get('value'), Binput::get('timestamp')));
} catch (Exception $e) {
throw new BadRequestHttpException();
}
@ -91,7 +89,7 @@ class MetricPointController extends AbstractApiController
*/
public function deleteMetricPoint(Metric $metric, MetricPoint $metricPoint)
{
$metricPoint->delete();
$this->dispatch(new RemoveMetricPointCommand($metricPoint));
return $this->noContent();
}

View File

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

View File

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