Load metrics via AJAX. Fixes #819
This commit is contained in:
@@ -12,29 +12,11 @@
|
||||
namespace CachetHQ\Cachet\Composers;
|
||||
|
||||
use CachetHQ\Cachet\Models\Metric;
|
||||
use CachetHQ\Cachet\Repositories\Metric\MetricRepository;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
|
||||
class MetricsComposer
|
||||
{
|
||||
/**
|
||||
* @var \CachetHQ\Cachet\Repositories\Metric\MetricRepository
|
||||
*/
|
||||
protected $metricRepository;
|
||||
|
||||
/**
|
||||
* Construct a new home controller instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Repositories\Metric\MetricRepository $metricRepository
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(MetricRepository $metricRepository)
|
||||
{
|
||||
$this->metricRepository = $metricRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Metrics view composer.
|
||||
*
|
||||
@@ -45,22 +27,11 @@ class MetricsComposer
|
||||
public function compose(View $view)
|
||||
{
|
||||
$metrics = null;
|
||||
$metricData = [];
|
||||
if ($displayMetrics = Config::get('setting.display_graphs')) {
|
||||
$metrics = Metric::where('display_chart', 1)->orderBy('id')->get();
|
||||
|
||||
$metrics->map(function ($metric) use (&$metricData) {
|
||||
$metricData[$metric->id] = [
|
||||
'last_hour' => $this->metricRepository->listPointsLastHour($metric),
|
||||
'today' => $this->metricRepository->listPointsToday($metric),
|
||||
'week' => $this->metricRepository->listPointsForWeek($metric),
|
||||
'month' => $this->metricRepository->listPointsForMonth($metric),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
$view->withDisplayMetrics($displayMetrics)
|
||||
->withMetrics($metrics)
|
||||
->withMetricData($metricData);
|
||||
->withMetrics($metrics);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,10 @@
|
||||
namespace CachetHQ\Cachet\Http\Controllers;
|
||||
|
||||
use CachetHQ\Cachet\Dates\DateFactory;
|
||||
use CachetHQ\Cachet\Http\Controllers\Api\AbstractApiController;
|
||||
use CachetHQ\Cachet\Models\Incident;
|
||||
use CachetHQ\Cachet\Models\Metric;
|
||||
use CachetHQ\Cachet\Repositories\Metric\MetricRepository;
|
||||
use Exception;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
use Illuminate\Routing\Controller;
|
||||
@@ -21,8 +24,25 @@ use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\View;
|
||||
use Jenssegers\Date\Date;
|
||||
|
||||
class StatusPageController extends Controller
|
||||
class StatusPageController extends AbstractApiController
|
||||
{
|
||||
/**
|
||||
* @var \CachetHQ\Cachet\Repositories\Metric\MetricRepository
|
||||
*/
|
||||
protected $metricRepository;
|
||||
|
||||
/**
|
||||
* Construct a new status page controller instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Repositories\Metric\MetricRepository $metricRepository
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(MetricRepository $metricRepository)
|
||||
{
|
||||
$this->metricRepository = $metricRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the status page.
|
||||
*
|
||||
@@ -100,4 +120,37 @@ class StatusPageController extends Controller
|
||||
return View::make('incident')
|
||||
->withIncident($incident);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns metrics in a readily formatted way.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function getMetrics(Metric $metric)
|
||||
{
|
||||
$metricData = [];
|
||||
$type = Binput::get('filter', 'last_hour');
|
||||
|
||||
switch ($type) {
|
||||
case 'last_hour':
|
||||
$metricData = $this->metricRepository->listPointsLastHour($metric);
|
||||
break;
|
||||
case 'today':
|
||||
$metricData = $this->metricRepository->listPointsToday($metric);
|
||||
break;
|
||||
case 'week':
|
||||
$metricData = $this->metricRepository->listPointsForWeek($metric);
|
||||
break;
|
||||
case 'month':
|
||||
$metricData = $this->metricRepository->listPointsForMonth($metric);
|
||||
break;
|
||||
}
|
||||
|
||||
return $this->item([
|
||||
'metric' => $metric->toArray(),
|
||||
'items' => $metricData,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,11 @@ class StatusPageRoutes
|
||||
'as' => 'incident',
|
||||
'uses' => 'StatusPageController@showIncident',
|
||||
]);
|
||||
|
||||
$router->get('metrics/{metric}', [
|
||||
'as' => 'metrics',
|
||||
'uses' => 'StatusPageController@getMetrics',
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user