Merge pull request #722 from cachethq/metric-timestamps

Closes #519 - Allow supplying of timestamps to metric points
This commit is contained in:
James Brooks 2015-06-15 13:16:24 +01:00
commit 56802476a6
3 changed files with 33 additions and 2 deletions

View File

@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Models\MetricPoint;
use Carbon\Carbon;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
@ -43,6 +44,11 @@ class MetricPointController extends AbstractApiController
$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);
} catch (Exception $e) {
@ -62,7 +68,15 @@ class MetricPointController extends AbstractApiController
*/
public function putMetricPoint(Metric $metric, MetricPoint $metricPoint)
{
$metricPoint->update(Binput::all());
$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');
}
$metricPoint->update($metricPointData);
return $this->item($metricPoint);
}

View File

@ -31,7 +31,7 @@ class MetricPoint extends Model implements HasPresenter
*
* @var string[]
*/
protected $fillable = ['metric_id', 'value'];
protected $fillable = ['metric_id', 'value', 'created_at'];
/**
* The validation rules.

View File

@ -56,6 +56,23 @@ class MetricPointTest extends AbstractTestCase
$this->seeJson(['value' => (string) $metricPoint->value]);
}
public function testPostMetricPointTimestamp()
{
$this->beUser();
$metric = factory('CachetHQ\Cachet\Models\Metric')->create();
$timestamp = 1434369116;
$datetime = '2015-06-15 11:51:56';
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->make([
'metric_id' => $metric->id,
]);
$postData = $metricPoint->toArray();
$postData['timestamp'] = $timestamp;
$this->post("/api/v1/metrics/{$metric->id}/points", $postData);
$this->seeJson(['value' => (string) $metricPoint->value, 'created_at' => $datetime]);
}
public function testPutMetricPoint()
{
$this->beUser();