Added metrics filter dropdown. Closes #518

This commit is contained in:
James Brooks
2015-08-02 15:43:20 +01:00
committed by James Brooks
parent 4a22b1b053
commit d99f95b2d6
15 changed files with 348 additions and 139 deletions
+29 -2
View File
@@ -16,6 +16,7 @@ use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Repositories\MetricRepository;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use GrahamCampbell\Markdown\Facades\Markdown;
@@ -27,7 +28,24 @@ use Jenssegers\Date\Date;
class HomeController extends Controller
{
/**
* Returns the rendered Blade templates.
* @var \CachetHQ\Cachet\Repositories\MetricRepository
*/
protected $metricRepository;
/**
* Construct a new home controller instance.
*
* @param \CachetHQ\Cachet\Repositories\MetricRepository $metricRepository
*
* @return void
*/
public function __construct(MetricRepository $metricRepository)
{
$this->metricRepository = $metricRepository;
}
/**
* Displays the status page.
*
* @return \Illuminate\View\View
*/
@@ -52,9 +70,17 @@ class HomeController extends Controller
}
$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),
];
});
}
$daysToShow = Setting::get('app_incident_days') ?: 7;
@@ -98,6 +124,7 @@ class HomeController extends Controller
->withUngroupedComponents($ungroupedComponents)
->withDisplayMetrics($displayMetrics)
->withMetrics($metrics)
->withMetricData($metricData)
->withAllIncidents($allIncidents)
->withScheduledMaintenance($scheduledMaintenance)
->withAboutApp(Markdown::convertToHtml(Setting::get('app_about')))
-57
View File
@@ -12,13 +12,8 @@
namespace CachetHQ\Cachet\Models;
use AltThree\Validator\ValidatingTrait;
use CachetHQ\Cachet\Facades\Setting as SettingFacade;
use CachetHQ\Cachet\Presenters\MetricPresenter;
use DateInterval;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Jenssegers\Date\Date;
use McCool\LaravelAutoPresenter\HasPresenter;
class Metric extends Model implements HasPresenter
@@ -86,58 +81,6 @@ class Metric extends Model implements HasPresenter
return $this->hasMany(MetricPoint::class, 'metric_id', 'id');
}
/**
* Returns the sum of all values a metric has by the hour.
*
* @param int $hour
*
* @return int
*/
public function getValuesByHour($hour)
{
$dateTimeZone = SettingFacade::get('app_timezone');
$dateTime = (new Date())->setTimezone($dateTimeZone)->sub(new DateInterval('PT'.$hour.'H'));
$hourInterval = $dateTime->format('YmdH');
if (Config::get('database.default') === 'mysql') {
if (!isset($this->calc_type) || $this->calc_type == self::CALC_SUM) {
$value = $this->points()
->whereRaw('DATE_FORMAT(created_at, "%Y%m%d%H") = '.$hourInterval)
->groupBy(DB::raw('HOUR(created_at)'))->sum('value');
} elseif ($this->calc_type == self::CALC_AVG) {
$value = $this->points()
->whereRaw('DATE_FORMAT(created_at, "%Y%m%d%H") = '.$hourInterval)
->groupBy(DB::raw('HOUR(created_at)'))->avg('value');
}
} else {
// Default metrics calculations.
if (!isset($this->calc_type) || $this->calc_type == self::CALC_SUM) {
$queryType = 'sum(metric_points.value)';
} elseif ($this->calc_type == self::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 = {$this->id} AND to_char(metric_points.created_at, 'YYYYMMDDHH24') = :timestamp GROUP BY to_char(metric_points.created_at, 'H')", [
'timestamp' => $hourInterval,
]);
if (isset($query[0])) {
$value = $query[0]->aggregate;
} else {
$value = 0;
}
}
if ($value === 0 && $this->default_value != $value) {
return $this->default_value;
}
return round($value, $this->places);
}
/**
* Determines whether a chart should be shown.
*
+207
View File
@@ -0,0 +1,207 @@
<?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\Repositories;
use CachetHQ\Cachet\Facades\Setting as SettingFacade;
use CachetHQ\Cachet\Models\Metric;
use DateInterval;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Jenssegers\Date\Date;
class MetricRepository
{
/**
* The timezone the status page is showing in.
*
* @var string
*/
protected $dateTimeZone;
/**
* Creates a new instance of the metric repository.
*
* @return void
*/
public function __construct()
{
$this->dateTimeZone = SettingFacade::get('app_timezone');
}
/**
* Returns all points as an array, by x hours.
*
* @param \CachetHQ\Cachet\Models\Metric $metric
* @param int $hours
*
* @return array
*/
public function listPointsToday(Metric $metric, $hours = 12)
{
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
$points = [];
$pointKey = $dateTime->format('H:00');
for ($i = 0; $i <= $hours; $i++) {
$points[$pointKey] = $this->getPointsByHour($metric, $i + 1);
$pointKey = $dateTime->sub(new DateInterval('PT1H'))->format('H:00');
}
return array_reverse($points);
}
/**
* Returns all points as an array, in the last week.
*
* @param \CachetHQ\Cachet\Models\Metric $metric
*
* @return array
*/
public function listPointsForWeek(Metric $metric)
{
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
$points = [];
$pointKey = $dateTime->format('jS M');
for ($i = 0; $i <= 7; $i++) {
$points[$pointKey] = $this->getPointsForDayInWeek($metric, $i);
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('D jS M');
}
return array_reverse($points);
}
/**
* Returns all points as an array, in the last month.
*
* @param \CachetHQ\Cachet\Models\Metric $metric
*
* @return array
*/
public function listPointsForMonth(Metric $metric)
{
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
$daysInMonth = $dateTime->format('t');
$points = [];
$pointKey = $dateTime->format('jS M');
for ($i = 0; $i <= $daysInMonth; $i++) {
$points[$pointKey] = $this->getPointsForDayInWeek($metric, $i);
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('jS M');
}
return array_reverse($points);
}
/**
* Returns metrics for a given hour.
*
* @param \CachetHQ\Cachet\Models\Metric $metric
* @param int $hour
*
* @return int
*/
protected function getPointsByHour(Metric $metric, $hour)
{
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
$dateTime->sub(new DateInterval('PT'.$hour.'H'));
$hourInterval = $dateTime->format('YmdH');
if (Config::get('database.default') === 'mysql') {
$points = $metric->points()
->whereRaw('DATE_FORMAT(created_at, "%Y%m%d%H") = '.$hourInterval)
->groupBy(DB::raw('HOUR(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');
}
} 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, 'YYYYMMDDHH24') = :timestamp GROUP BY to_char(metric_points.created_at, 'H')", [
'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);
}
/**
* 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);
}
}