More fixes

This commit is contained in:
Graham Campbell
2016-02-02 20:46:46 +00:00
parent 4923a59b76
commit 921116a198
5 changed files with 51 additions and 25 deletions

View File

@@ -78,13 +78,13 @@ class DateFactory
/** /**
* Make a Carbon instance from a string. * Make a Carbon instance from a string.
* *
* @param string $time * @param string|null $time
* *
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* *
* @return \Carbon\Carbon * @return \Carbon\Carbon
*/ */
public function make($time) public function make($time = null)
{ {
return (new Date($time))->setTimezone($this->cachetTimezone); return (new Date($time))->setTimezone($this->cachetTimezone);
} }

View File

@@ -11,6 +11,7 @@
namespace CachetHQ\Cachet\Http\Controllers; namespace CachetHQ\Cachet\Http\Controllers;
use CachetHQ\Cachet\Dates\DateFactory;
use CachetHQ\Cachet\Models\Incident; use CachetHQ\Cachet\Models\Incident;
use Exception; use Exception;
use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Binput\Facades\Binput;
@@ -54,27 +55,19 @@ class StatusPageController extends Controller
} else { } else {
$incidentDays = range(0, $daysToShow); $incidentDays = range(0, $daysToShow);
} }
$dateTimeZone = Config::get('cachet.timezone');
$incidentVisibility = Auth::check() ? 0 : 1; $incidentVisibility = Auth::check() ? 0 : 1;
$allIncidents = Incident::notScheduled()->where('visible', '>=', $incidentVisibility)->whereBetween('created_at', [ $allIncidents = Incident::notScheduled()->where('visible', '>=', $incidentVisibility)->whereBetween('created_at', [
$startDate->copy()->subDays($daysToShow)->format('Y-m-d').' 00:00:00', $startDate->copy()->subDays($daysToShow)->format('Y-m-d').' 00:00:00',
$startDate->format('Y-m-d').' 23:59:59', $startDate->format('Y-m-d').' 23:59:59',
])->orderBy('scheduled_at', 'desc')->orderBy('created_at', 'desc')->get()->groupBy(function (Incident $incident) use ($dateTimeZone) { ])->orderBy('scheduled_at', 'desc')->orderBy('created_at', 'desc')->get()->groupBy(function (Incident $incident) {
// If it's scheduled, get the scheduled at date. return app(DateFactory::class)->make($incident->is_scheduled ? $incident->scheduled_at : $incident->created_at)->toDateString();
if ($incident->is_scheduled) {
return (new Date($incident->scheduled_at))
->setTimezone($dateTimeZone)->toDateString();
}
return (new Date($incident->created_at))
->setTimezone($dateTimeZone)->toDateString();
}); });
// Add in days that have no incidents // Add in days that have no incidents
foreach ($incidentDays as $i) { foreach ($incidentDays as $i) {
$date = (new Date($startDate))->setTimezone($dateTimeZone)->subDays($i); $date = app(DateFactory::class)->make($startDate)->subDays($i);
if (!isset($allIncidents[$date->toDateString()])) { if (!isset($allIncidents[$date->toDateString()])) {
$allIncidents[$date->toDateString()] = []; $allIncidents[$date->toDateString()] = [];

View File

@@ -28,6 +28,8 @@ class Admin
* Create a new admin middleware instance. * Create a new admin middleware instance.
* *
* @param \Illuminate\Contracts\Auth\Guard $auth * @param \Illuminate\Contracts\Auth\Guard $auth
*
* @return void
*/ */
public function __construct(Guard $auth) public function __construct(Guard $auth)
{ {

View File

@@ -12,9 +12,29 @@
namespace CachetHQ\Cachet\Http\Middleware; namespace CachetHQ\Cachet\Http\Middleware;
use Closure; use Closure;
use Illuminate\Contracts\Config\Repository;
class Timezone class Timezone
{ {
/**
* Config repository.
*
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;
/**
* Creates a new release instance.
*
* @param \Illuminate\Contracts\Config\Repository $config
*
* @return void
*/
public function __construct(Repository $config)
{
$this->config = $config;
}
/** /**
* Handle an incoming request. * Handle an incoming request.
* *
@@ -26,7 +46,7 @@ class Timezone
public function handle($request, Closure $next) public function handle($request, Closure $next)
{ {
if ($tz = $request->header('Time-Zone')) { if ($tz = $request->header('Time-Zone')) {
app('config')->set('cachet.timezone', $tz); $this->config->set('cachet.timezone', $tz);
} }
return $next($request); return $next($request);

View File

@@ -11,10 +11,9 @@
namespace CachetHQ\Cachet\Repositories\Metric; namespace CachetHQ\Cachet\Repositories\Metric;
use CachetHQ\Cachet\Dates\DateFactory;
use CachetHQ\Cachet\Models\Metric; use CachetHQ\Cachet\Models\Metric;
use DateInterval; use DateInterval;
use Illuminate\Support\Facades\Config;
use Jenssegers\Date\Date;
class MetricRepository class MetricRepository
{ {
@@ -26,21 +25,24 @@ class MetricRepository
protected $repository; protected $repository;
/** /**
* The timezone the status page is showing in. * The date factory instance.
* *
* @var string * @var \CachetHQ\Cachet\Dates\DateFactory
*/ */
protected $dateTimeZone; protected $dates;
/** /**
* Create a new metric repository class. * Create a new metric repository class.
* *
* @param \CachetHQ\Cachet\Repositories\Metric\MetricInterface $repository * @param \CachetHQ\Cachet\Repositories\Metric\MetricInterface $repository
* @param \CachetHQ\Cachet\Dates\DateFactory $dates
*
* @return void
*/ */
public function __construct(MetricInterface $repository) public function __construct(MetricInterface $repository, DateFactory $dates)
{ {
$this->repository = $repository; $this->repository = $repository;
$this->dateTimeZone = Config::get('cachet.timezone'); $this->dates = $dates;
} }
/** /**
@@ -52,10 +54,12 @@ class MetricRepository
*/ */
public function listPointsLastHour(Metric $metric) public function listPointsLastHour(Metric $metric)
{ {
$dateTime = (new Date())->setTimezone($this->dateTimeZone); $dateTime = $this->dates->make();
$points = []; $points = [];
$pointKey = $dateTime->format('H:i'); $pointKey = $dateTime->format('H:i');
for ($i = 0; $i <= 60; $i++) { for ($i = 0; $i <= 60; $i++) {
$points[$pointKey] = $this->repository->getPointsLastHour($metric, 0, $i); $points[$pointKey] = $this->repository->getPointsLastHour($metric, 0, $i);
$pointKey = $dateTime->sub(new DateInterval('PT1M'))->format('H:i'); $pointKey = $dateTime->sub(new DateInterval('PT1M'))->format('H:i');
@@ -74,10 +78,12 @@ class MetricRepository
*/ */
public function listPointsToday(Metric $metric, $hours = 12) public function listPointsToday(Metric $metric, $hours = 12)
{ {
$dateTime = (new Date())->setTimezone($this->dateTimeZone); $dateTime = $this->dates->make();
$points = []; $points = [];
$pointKey = $dateTime->format('H:00'); $pointKey = $dateTime->format('H:00');
for ($i = 0; $i <= $hours; $i++) { for ($i = 0; $i <= $hours; $i++) {
$points[$pointKey] = $this->repository->getPointsByHour($metric, $i); $points[$pointKey] = $this->repository->getPointsByHour($metric, $i);
$pointKey = $dateTime->sub(new DateInterval('PT1H'))->format('H:00'); $pointKey = $dateTime->sub(new DateInterval('PT1H'))->format('H:00');
@@ -95,10 +101,12 @@ class MetricRepository
*/ */
public function listPointsForWeek(Metric $metric) public function listPointsForWeek(Metric $metric)
{ {
$dateTime = (new Date())->setTimezone($this->dateTimeZone); $dateTime = $this->dates->make();
$points = []; $points = [];
$pointKey = $dateTime->format('jS M'); $pointKey = $dateTime->format('jS M');
for ($i = 0; $i <= 7; $i++) { for ($i = 0; $i <= 7; $i++) {
$points[$pointKey] = $this->repository->getPointsForDayInWeek($metric, $i); $points[$pointKey] = $this->repository->getPointsForDayInWeek($metric, $i);
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('D jS M'); $pointKey = $dateTime->sub(new DateInterval('P1D'))->format('D jS M');
@@ -116,11 +124,14 @@ class MetricRepository
*/ */
public function listPointsForMonth(Metric $metric) public function listPointsForMonth(Metric $metric)
{ {
$dateTime = (new Date())->setTimezone($this->dateTimeZone); $dateTime = $this->dates->make();
$daysInMonth = $dateTime->format('t'); $daysInMonth = $dateTime->format('t');
$points = []; $points = [];
$pointKey = $dateTime->format('jS M'); $pointKey = $dateTime->format('jS M');
for ($i = 0; $i <= $daysInMonth; $i++) { for ($i = 0; $i <= $daysInMonth; $i++) {
$points[$pointKey] = $this->repository->getPointsForDayInWeek($metric, $i); $points[$pointKey] = $this->repository->getPointsForDayInWeek($metric, $i);
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('jS M'); $pointKey = $dateTime->sub(new DateInterval('P1D'))->format('jS M');