mirror of
https://github.com/CachetHQ/Cachet.git
synced 2025-01-17 21:49:01 +01:00
Merge pull request #722 from cachethq/metric-timestamps
Closes #519 - Allow supplying of timestamps to metric points
This commit is contained in:
commit
56802476a6
@ -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);
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user