/** * Update a model by id. * * @param int $id * @param array $data * * @return \Illuminate\Database\Eloquent\Model */ public function update($id, array $data) { $metric = $this->model->findOrFail($id); $metric->fill($data); $this->validate($metric); $metric->update($data); return $metric; }
/** * Updates a metric point. * * @param \CachetHQ\Cachet\Models\Metric $metric * @param \CachetHQ\Cachet\Models\MetircPoint $metricPoint * * @return \Illuminate\Http\JsonResponse */ public function putMetricPoint(Metric $metric, MetricPoint $metricPoint) { $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); }
/** * Creates a new metric point. * * @return \Illuminate\Http\RedirectResponse */ public function createMetricPointAction() { try { MetricPoint::create(Binput::get('point', null, false)); } catch (ValidationException $e) { return Redirect::back()->withInput(Binput::all())->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.metrics.points.add.failure')))->withErrors($e->getMessageBag()); } return Redirect::back()->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.metrics.points.add.success'))); }
/** * Seed the metric points table. * * @return void */ protected function seedMetricPoints() { MetricPoint::truncate(); // Generate 11 hours of metric points for ($i = 0; $i < 11; $i++) { $metricTime = (new DateTime())->sub(new DateInterval('PT' . $i . 'H')); MetricPoint::create(['metric_id' => 1, 'value' => rand(1, 10), 'created_at' => $metricTime, 'updated_at' => $metricTime]); } }
/** * Creates a new metric point. * * @return \Illuminate\Http\RedirectResponse */ public function createMetricPointAction() { $_point = Binput::get('point', null, false); $point = MetricPoint::create($_point); if (!$point->isValid()) { return Redirect::back()->withInput(Binput::all())->with('title', sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.metrics.points.add.failure')))->with('errors', $point->getErrors()); } $successMsg = sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.metrics.points.add.success')); return Redirect::back()->with('success', $successMsg); }
/** * Run the database seeding. */ public function run() { MetricPoint::truncate(); // Generate 11 hours of metric points for ($i = 0; $i < 11; $i++) { $metricTime = (new DateTime())->sub(new DateInterval('PT' . $i . 'H')); $point = MetricPoint::create(['metric_id' => 1, 'value' => rand(1, 100), 'created_at' => $metricTime]); $point->update(['created_at' => $metricTime]); } }
/** * Handle the add metric point command. * * @param \CachetHQ\Cachet\Commands\Metric\AddMetricPointCommand $command * * @return \CachetHQ\Cachet\Models\MetricPoint */ public function handle(AddMetricPointCommand $command) { $metric = $command->metric; $createdAt = $command->created_at; $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; }
protected function findOrCreatePoint(AddMetricPointCommand $command) { $buffer = Carbon::now()->subMinutes($command->metric->threshold); $point = MetricPoint::where('metric_id', $command->metric->id)->where('value', $command->value)->where('created_at', '>=', $buffer)->first(); if ($point) { return $point; } $data = ['metric_id' => $command->metric->id, 'value' => $command->value, 'counter' => 0]; if ($command->created_at) { $data['created_at'] = $this->dates->create('U', $command->created_at)->format('Y-m-d H:i:s'); } return MetricPoint::create($data); }
/** * Shows the metric points. * * @return \Illuminate\View\View */ public function showMetricPoints() { return View::make('dashboard.metrics.points.index')->withPageTitle(trans('dashboard.metrics.points.title') . ' - ' . trans('dashboard.dashboard'))->withMetrics(MetricPoint::all()); }
/** * Destroys a metric point. * * @param \CachetHQ\Cachet\Models\Metric $metric * @param \CachetHQ\Cachet\Models\MetricPoint $metricPoint * * @return \Dingo\Api\Http\Response */ public function deleteMetricPoint(Metric $metric, MetricPoint $metricPoint) { $metricPoint->delete(); return $this->noContent(); }