Show subscribers and incidents over last 30 days

This commit is contained in:
James Brooks 2015-08-24 21:49:10 +01:00
parent 70a5022740
commit 69e9d75037
2 changed files with 98 additions and 13 deletions

View File

@ -13,13 +13,41 @@ namespace CachetHQ\Cachet\Http\Controllers\Admin;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\Subscriber;
use CachetHQ\Cachet\Facades\Setting;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;
use Jenssegers\Date\Date;
use stdClass;
class DashboardController extends Controller
{
/**
* Start date.
*
* @var \Jenssegers\Date\Date
*/
protected $startDate;
/**
* The timezone the status page is running in.
*
* @var string
*/
protected $timeZone;
/**
* Creates a new dashboard controller.
*
* @return void
*/
public function __construct()
{
$this->startDate = new Date();
$this->dateTimeZone = Setting::get('app_timezone');
}
/**
* Shows the dashboard view.
*
@ -29,10 +57,12 @@ class DashboardController extends Controller
{
$components = Component::orderBy('order')->get();
$incidents = $this->getIncidents();
$subscribers = $this->getSubscribers();
return View::make('dashboard.index')
->withComponents($components)
->withIncidents($incidents);
->withIncidents($incidents)
->withSubscribers($subscribers);
}
/**
@ -53,16 +83,60 @@ class DashboardController extends Controller
*/
protected function getIncidents()
{
$incidents = Incident::select(DB::raw('COUNT(id) AS counter'))->groupBy(DB::raw('DATE_FORMAT(created_at, "%d%m%y")'))->get();
$range = (30 - $incidents->count()) - 1;
$allIncidents = Incident::notScheduled()->whereBetween('created_at', [
$this->startDate->copy()->subDays(30)->format('Y-m-d').' 00:00:00',
$this->startDate->format('Y-m-d').' 23:59:59',
])->orderBy('created_at', 'desc')->get()->groupBy(function (Incident $incident) {
return (new Date($incident->created_at))
->setTimezone($this->dateTimeZone)->toDateString();
});
$fake = new stdClass();
$fake->counter = 0;
// Add in days that have no incidents
foreach (range(0, 30) as $i) {
$date = (new Date($this->startDate))->setTimezone($this->dateTimeZone)->subDays($i);
foreach (range(1, $range) as $key) {
$incidents->prepend($fake);
if (!isset($allIncidents[$date->toDateString()])) {
$allIncidents[$date->toDateString()] = [];
}
}
return $incidents;
// Sort the array so it takes into account the added days
$allIncidents = $allIncidents->sortBy(function ($value, $key) {
return strtotime($key);
}, SORT_REGULAR, false);
return $allIncidents;
}
/**
* Fetches all of the subscribers over the last 30 days.
*
* @return \Illuminate\Support\Collection
*/
protected function getSubscribers()
{
$allSubscribers = Subscriber::whereBetween('created_at', [
$this->startDate->copy()->subDays(30)->format('Y-m-d').' 00:00:00',
$this->startDate->format('Y-m-d').' 23:59:59',
])->orderBy('created_at', 'desc')->get()->groupBy(function (Subscriber $incident) {
return (new Date($incident->created_at))
->setTimezone($this->dateTimeZone)->toDateString();
});
// Add in days that have no incidents
foreach (range(0, 30) as $i) {
$date = (new Date($this->startDate))->setTimezone($this->dateTimeZone)->subDays($i);
if (!isset($allSubscribers[$date->toDateString()])) {
$allSubscribers[$date->toDateString()] = [];
}
}
// Sort the array so it takes into account the added days
$allSubscribers = $allSubscribers->sortBy(function ($value, $key) {
return strtotime($key);
}, SORT_REGULAR, false);
return $allSubscribers;
}
}

View File

@ -45,16 +45,27 @@
</div>
<div class="row">
<div class="col-lg-6">
<div class="stats-widget full-stats-block">
<div class="col-sm-12 col-lg-6">
<div class="stats-widget">
<div class="stats-top">
<span class="stats-value">{{ $incidents->map(function($incident) { return $incident->counter; })->sum() }}</span>
<span class="stats-value">{{ $incidents->map(function($incident) { return count($incident); })->sum() }}</span>
<span class="stats-label">{{ trans('dashboard.incidents.incidents') }}</span>
</div>
<div class="stats-chart">
<div class="sparkline" data-type="line" data-resize="true" data-height="80" data-width="100%" data-line-width="2" data-min-spot-color="#e65100" data-max-spot-color="#ffb300" data-line-color="#3498db" data-spot-color="#00838f" data-fill-color="#3498db" data-highlight-line-color="#00acc1" data-highlight-spot-color="#ff8a65" data-spot-radius="false" data-data="[{{ $incidents->implode('counter', ',') }}]"></div>
<div class="sparkline" data-type="line" data-resize="true" data-height="80" data-width="100%" data-line-width="2" data-min-spot-color="#e65100" data-max-spot-color="#ffb300" data-line-color="#3498db" data-spot-color="#00838f" data-fill-color="#3498db" data-highlight-line-color="#00acc1" data-highlight-spot-color="#ff8a65" data-spot-radius="false" data-data="[{{ $incidents->map(function ($incident) { return count($incident); } )->implode(',') }}]"></div>
</div>
</div>
</div>
<div class="col-sm-12 col-lg-6">
<div class="stats-widget">
<div class="stats-top">
<span class="stats-value">{{ $subscribers->map(function($subscribers) { return count($subscribers); })->sum() }}</span>
<span class="stats-label">{{ trans('dashboard.subscribers.subscribers') }}</span>
</div>
<div class="stats-chart">
<div class="sparkline" data-type="line" data-resize="true" data-height="80" data-width="100%" data-line-width="2" data-min-spot-color="#e65100" data-max-spot-color="#ffb300" data-line-color="#3498db" data-spot-color="#00838f" data-fill-color="#3498db" data-highlight-line-color="#00acc1" data-highlight-spot-color="#ff8a65" data-spot-radius="false" data-data="[{{ $subscribers->map(function ($subscriber) { return count($subscriber); } )->implode(',') }}]"></div>
</div>
<div class="stats-bottom bg-blue"></div>
</div>
</div>
</div>