/** * 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; }
/** * Update an existing metric. * * @param \CachetHQ\Cachet\Models\Metric $metric * * @return \Illuminate\Http\JsonResponse */ public function putMetric(Metric $metric) { try { $metric->update(Binput::all()); } catch (Exception $e) { throw new BadRequestHttpException(); } return $this->item($metric); }
/** * Returns metrics for the week. * * @param \CachetHQ\Cachet\Models\Metric $metric * * @return int */ public function getPointsForDayInWeek(Metric $metric, $day) { $dateTime = (new Date())->sub(new DateInterval('P' . $day . 'D')); $points = $metric->points()->whereRaw('created_at BETWEEN DATE_SUB(created_at, INTERVAL 1 WEEK) AND NOW()')->whereRaw('DATE_FORMAT(created_at, "%Y%m%d") = ' . $dateTime->format('Ymd'))->groupBy(DB::raw('DATE_FORMAT(created_at, "%Y%m%d")')); if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) { $value = $points->sum('value'); } elseif ($metric->calc_type == Metric::CALC_AVG) { $value = $points->avg('value'); } if ($value === 0 && $metric->default_value != $value) { return $metric->default_value; } return round($value, $metric->places); }
/** * Returns metrics for the week. * * @param \CachetHQ\Cachet\Models\Metric $metric * * @return int */ public function getPointsForDayInWeek(Metric $metric, $day) { $dateTime = (new Date())->sub(new DateInterval('P' . $day . 'D')); $points = $metric->points()->whereRaw('created_at > date("now", "-7 day")')->whereRaw('strftime("%Y%m%d", created_at) = "' . $dateTime->format('Ymd') . '"')->groupBy(DB::raw('strftime("%Y%m%d", created_at)')); if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) { $value = $points->sum('value'); } elseif ($metric->calc_type == Metric::CALC_AVG) { $value = $points->avg('value'); } if ($value === 0 && $metric->default_value != $value) { return $metric->default_value; } return round($value, $metric->places); }
/** * Returns metrics for the week. * * @param \CachetHQ\Cachet\Models\Metric $metric * * @return int */ public function getPointsForDayInWeek(Metric $metric, $day) { $dateTime = (new Date())->setTimezone($this->dateTimeZone); $dateTime->sub(new DateInterval('P' . $day . 'D')); $points = $metric->points()->whereRaw('created_at BETWEEN (created_at - interval \'1 week\') AND now()')->whereRaw('to_char(created_at, \'YYYYMMDD\') = \'' . $dateTime->format('Ymd') . '\'')->groupBy(DB::raw('to_char(created_at, \'YYYYMMDD\')')); if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) { $value = $points->sum('value'); } elseif ($metric->calc_type == Metric::CALC_AVG) { $value = $points->avg('value'); } if ($value === 0 && $metric->default_value != $value) { return $metric->default_value; } return round($value, $metric->places); }
/** * Run the database seeding. */ public function run() { $defaultMetrics = [['name' => 'Cups of coffee', 'suffix' => 'Cups', 'description' => 'How many cups of coffee we\'ve drank.', 'default_value' => 0, 'calc_type' => 1, 'display_chart' => 1]]; Metric::truncate(); foreach ($defaultMetrics as $metric) { Metric::create($metric); } }
/** * Run the database seeding. */ public function run() { $defaultMetrics = [['name' => 'Demo Metric', 'suffix' => 'rnd', 'description' => 'Random data points.', 'default_value' => 0, 'calc_type' => 1, 'display_chart' => 1]]; Metric::truncate(); foreach ($defaultMetrics as $metric) { Metric::create($metric); } }
/** * Metrics view composer. * * @param \Illuminate\Contracts\View\View $view * * @return void */ public function compose(View $view) { $metrics = null; if ($displayMetrics = Config::get('setting.display_graphs')) { $metrics = Metric::where('display_chart', 1)->orderBy('id')->get(); } $view->withDisplayMetrics($displayMetrics)->withMetrics($metrics); }
/** * Metrics view composer. * * @param \Illuminate\Contracts\View\View $view * * @return void */ public function compose(View $view) { $metrics = null; if ($displayMetrics = $this->config->get('setting.display_graphs')) { $metrics = Metric::displayable()->orderBy('order')->orderBy('id')->get(); } $view->withDisplayMetrics($displayMetrics)->withMetrics($metrics); }
/** * Metrics view composer. * * @param \Illuminate\Contracts\View\View $view * * @return void */ public function compose(View $view) { $metrics = null; $metricData = []; if ($displayMetrics = Setting::get('display_graphs')) { $metrics = Metric::where('display_chart', 1)->get(); $metrics->map(function ($metric) use(&$metricData) { $metricData[$metric->id] = ['today' => $this->metricRepository->listPointsToday($metric), 'week' => $this->metricRepository->listPointsForWeek($metric), 'month' => $this->metricRepository->listPointsForMonth($metric)]; }); } $view->withDisplayMetrics($displayMetrics)->withMetrics($metrics)->withMetricData($metricData); }
/** * Returns the rendered Blade templates. * * @return \Illuminate\View\View */ public function showIndex() { $today = Date::now(); $startDate = Date::now(); segment_page('Status Page'); // Check if we have another starting date if (Binput::has('start_date')) { try { // If date provided is valid $oldDate = Date::createFromFormat('Y-m-d', Binput::get('start_date')); segment_track('Status Page', ['start_date' => $oldDate->format('Y-m-d')]); // If trying to get a future date fallback to today if ($today->gt($oldDate)) { $startDate = $oldDate; } } catch (Exception $e) { // Fallback to today } } $metrics = null; if ($displayMetrics = Setting::get('display_graphs')) { $metrics = Metric::where('display_chart', 1)->get(); } $daysToShow = Setting::get('app_incident_days') ?: 7; $incidentDays = range(0, $daysToShow - 1); $dateTimeZone = Setting::get('app_timezone'); $incidentVisiblity = Auth::check() ? 0 : 1; $allIncidents = Incident::notScheduled()->where('visible', '>=', $incidentVisiblity)->whereBetween('created_at', [$startDate->copy()->subDays($daysToShow)->format('Y-m-d') . ' 00:00:00', $startDate->format('Y-m-d') . ' 23:59:59'])->orderBy('created_at', 'desc')->get()->groupBy(function (Incident $incident) use($dateTimeZone) { return (new Date($incident->created_at))->setTimezone($dateTimeZone)->toDateString(); }); // Add in days that have no incidents foreach ($incidentDays as $i) { $date = (new Date($startDate))->setTimezone($dateTimeZone)->subDays($i); if (!isset($allIncidents[$date->toDateString()])) { $allIncidents[$date->toDateString()] = []; } } // Sort the array so it takes into account the added days $allIncidents = $allIncidents->sortBy(function ($value, $key) { return strtotime($key); }, SORT_REGULAR, true)->all(); // Scheduled maintenance code. $scheduledMaintenance = Incident::scheduled()->orderBy('scheduled_at')->get(); // Component & Component Group lists. $usedComponentGroups = Component::where('group_id', '>', 0)->groupBy('group_id')->lists('group_id'); $componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get(); $ungroupedComponents = Component::where('group_id', 0)->orderBy('order')->orderBy('created_at')->get(); $canPageBackward = Incident::notScheduled()->where('created_at', '<', $startDate->format('Y-m-d'))->count() != 0; return View::make('index', ['componentGroups' => $componentGroups, 'ungroupedComponents' => $ungroupedComponents, 'displayMetrics' => $displayMetrics, 'metrics' => $metrics, 'allIncidents' => $allIncidents, 'scheduledMaintenance' => $scheduledMaintenance, 'aboutApp' => Markdown::convertToHtml(Setting::get('app_about')), 'canPageForward' => (bool) $today->gt($startDate), 'canPageBackward' => $canPageBackward, 'previousDate' => $startDate->copy()->subDays($daysToShow)->toDateString(), 'nextDate' => $startDate->copy()->addDays($daysToShow)->toDateString(), 'pageTitle' => Setting::get('app_name') . ' Status']); }
/** * Shows the metrics view. * * @return \Illuminate\View\View */ public function showMetrics() { $metrics = Metric::orderBy('created_at', 'desc')->get(); return View::make('dashboard.metrics.index')->withPageTitle(trans('dashboard.metrics.metrics') . ' - ' . trans('dashboard.dashboard'))->withMetrics($metrics); }
/** * Delete an existing metric. * * @param \CachetHQ\Cachet\Models\Metric $metric * * @return \Illuminate\Http\JsonResponse */ public function deleteMetric(Metric $metric) { $metric->delete(); return $this->noContent(); }
/** * Edit an metric. * * @param \CachetHQ\Cachet\Models\Metric $metric * * @return \Illuminate\Http\RedirectResponse */ public function editMetricAction(Metric $metric) { $metricData = Binput::get('metric', null, false); $metric->update($metricData); if (!$metric->isValid()) { return Redirect::back()->withInput(Binput::all())->with('title', sprintf('<strong>%s</strong>', trans('dashboard.notifications.awesome')))->with('errors', $metric->getErrors()); } $successMsg = sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.metrics.edit.success')); return Redirect::to('dashboard/metrics')->with('success', $successMsg); }
/** * Get all metrics. * * @return \Illuminate\Database\Eloquent\Collection */ public function getMetrics() { $metrics = Metric::paginate(Binput::get('per_page', 20)); return $this->paginator($metrics, Request::instance()); }
/** * Handle the add metric command. * * @param \CachetHQ\Cachet\Bus\Commands\Metric\AddMetricCommand $command * * @return \CachetHQ\Cachet\Models\Metric */ 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, 'default_view' => $command->default_view]); event(new MetricWasAddedEvent($metric)); return $metric; }
/** * Edit an metric. * * @param \CachetHQ\Cachet\Models\Metric $metric * * @return \Illuminate\Http\RedirectResponse */ public function editMetricAction(Metric $metric) { try { $metric->update(Binput::get('metric', null, false)); } catch (ValidationException $e) { return Redirect::route('dashboard.metrics.edit', ['id' => $metric->id])->withInput(Binput::all())->withTitle(sprintf('<strong>%s</strong>', trans('dashboard.notifications.whoops')))->withErrors($e->getMessageBag()); } return Redirect::route('dashboard.metrics.edit', ['id' => $metric->id])->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.metrics.edit.success'))); }
/** * Get all metric points. * * @param \CachetHQ\Cachet\Models\Metric $metric * * @return \Illuminate\Database\Eloquent\Collection */ public function getMetricPoints(Metric $metric) { $points = $metric->points()->paginate(Binput::get('per_page', 20)); return $this->paginator($points, Request::instance()); }
/** * Returns metrics for the week. * * @param \CachetHQ\Cachet\Models\Metric $metric * * @return int */ protected function getPointsForDayInWeek(Metric $metric, $day) { $dateTime = (new Date())->setTimezone($this->dateTimeZone); $dateTime->sub(new DateInterval('P' . $day . 'D')); if (Config::get('database.default') === 'mysql') { $points = $metric->points()->whereRaw('created_at BETWEEN DATE_SUB(created_at, INTERVAL 1 WEEK) AND NOW()')->whereRaw('DATE_FORMAT(created_at, "%Y%m%d") = ' . $dateTime->format('Ymd'))->groupBy(DB::raw('DATE_FORMAT(created_at, "%Y%m%d")')); if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) { $value = $points->sum('value'); } elseif ($metric->calc_type == Metric::CALC_AVG) { $value = $points->avg('value'); } } else { // Default metrics calculations. if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) { $queryType = 'sum(metric_points.value)'; } elseif ($metric->calc_type == Metric::CALC_AVG) { $queryType = 'avg(metric_points.value)'; } else { $queryType = 'sum(metric_points.value)'; } $query = DB::select("select {$queryType} as aggregate FROM metrics JOIN metric_points ON metric_points.metric_id = metrics.id WHERE metric_points.metric_id = {$metric->id} AND to_char(metric_points.created_at, 'YYYYMMDD') = :timestamp GROUP BY to_char(metric_points.created_at, 'YYYYMMDD')", ['timestamp' => $hourInterval]); if (isset($query[0])) { $value = $query[0]->aggregate; } else { $value = 0; } } if ($value === 0 && $metric->default_value != $value) { return $metric->default_value; } return round($value, $metric->places); }
/** * Get all metrics. * * @param \Symfony\Component\HttpFoundation\Request $request * * @return \Illuminate\Database\Eloquent\Collection */ public function getMetrics(Request $request) { $metrics = Metric::paginate(Binput::get('per_page', 20)); return $this->paginator($metrics, $request); }