<commit_before><?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\Http\Controllers;

use AltThree\Badger\Facades\Badger;
use CachetHQ\Cachet\Http\Controllers\Api\AbstractApiController;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Models\Schedule;
use CachetHQ\Cachet\Repositories\Metric\MetricRepository;
use CachetHQ\Cachet\Services\Dates\DateFactory;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\View;
use Jenssegers\Date\Date;
use McCool\LaravelAutoPresenter\Facades\AutoPresenter;

/**
 * This is the status page controller class.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class StatusPageController extends AbstractApiController
{
    /**
     * Displays the status page.
     *
     * @return \Illuminate\View\View
     */
    public function showIndex()
    {
        $onlyDisruptedDays = Config::get('setting.only_disrupted_days');
        $appIncidentDays = (int) Config::get('setting.app_incident_days', 1);

        $startDate = Date::createFromFormat('Y-m-d', Binput::get('start_date', Date::now()->toDateString()));
        $endDate = $startDate->copy()->subDays($appIncidentDays);

        $canPageForward = false;
        $canPageBackward = false;
        $previousDate = null;
        $nextDate = null;

        if ($onlyDisruptedDays) {
            // In this case, start_date GET parameter means the page
            $page = (int) Binput::get('start_date', 0);

            $allIncidentDays = Incident::where('visible', '>=', (int) !Auth::check())
                                       ->select('occurred_at')
                                       ->whereBetween('occurred_at', [
                                           $endDate->format('Y-m-d').' 00:00:00',
                                           $startDate->format('Y-m-d').' 23:59:59',
                                       ])
                                       ->distinct()
                                       ->orderBy('occurred_at', 'desc')
                                       ->get()
                                       ->map(function (Incident $incident) {
                                           return app(DateFactory::class)->make($incident->occurred_at)->toDateString();
                                       })->unique()
                                      ->values();

            $numIncidentDays = count($allIncidentDays);
            $numPages = round($numIncidentDays / max($appIncidentDays, 1));

            $selectedDays = $allIncidentDays->slice($page * $appIncidentDays, $appIncidentDays)->all();

            if (count($selectedDays) > 0) {
                $startDate = Date::createFromFormat('Y-m-d', array_values($selectedDays)[0]);
                $endDate = Date::createFromFormat('Y-m-d', array_values(array_slice($selectedDays, -1))[0]);
            }

            $canPageForward = $page > 0;
            $canPageBackward = ($page + 1) < $numPages;
            $previousDate = $page + 1;
            $nextDate = $page - 1;
        } else {
            $date = Date::now();

            $canPageForward = (bool) $startDate->lt($date->sub('1 day'));
            $canPageBackward = Incident::where('occurred_at', '<', $date->format('Y-m-d'))->count() > 0;
            $previousDate = $startDate->copy()->subDays($appIncidentDays)->toDateString();
            $nextDate = $startDate->copy()->addDays($appIncidentDays)->toDateString();
        }

        $allIncidents = Incident::with('component')->with('updates.incident')
            ->where('visible', '>=', (int) !Auth::check())->whereBetween('occurred_at', [
            $endDate->format('Y-m-d').' 00:00:00',
            $startDate->format('Y-m-d').' 23:59:59',
        ])->orderBy('occurred_at', 'desc')->get()->groupBy(function (Incident $incident) {
            return app(DateFactory::class)->make($incident->occurred_at)->toDateString();
        });

        if (!$onlyDisruptedDays) {
            $incidentDays = array_pad([], $appIncidentDays, null);

            // Add in days that have no incidents
            foreach ($incidentDays as $i => $day) {
                $date = app(DateFactory::class)->make($startDate)->subDays($i);

                if (!isset($allIncidents[$date->toDateString()])) {<commit_msg>Don't call with twice<commit_after><?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\Http\Controllers;

use AltThree\Badger\Facades\Badger;
use CachetHQ\Cachet\Http\Controllers\Api\AbstractApiController;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Models\Schedule;
use CachetHQ\Cachet\Repositories\Metric\MetricRepository;
use CachetHQ\Cachet\Services\Dates\DateFactory;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\View;
use Jenssegers\Date\Date;
use McCool\LaravelAutoPresenter\Facades\AutoPresenter;

/**
 * This is the status page controller class.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class StatusPageController extends AbstractApiController
{
    /**
     * Displays the status page.
     *
     * @return \Illuminate\View\View
     */
    public function showIndex()
    {
        $onlyDisruptedDays = Config::get('setting.only_disrupted_days');
        $appIncidentDays = (int) Config::get('setting.app_incident_days', 1);

        $startDate = Date::createFromFormat('Y-m-d', Binput::get('start_date', Date::now()->toDateString()));
        $endDate = $startDate->copy()->subDays($appIncidentDays);

        $canPageForward = false;
        $canPageBackward = false;
        $previousDate = null;
        $nextDate = null;

        if ($onlyDisruptedDays) {
            // In this case, start_date GET parameter means the page
            $page = (int) Binput::get('start_date', 0);

            $allIncidentDays = Incident::where('visible', '>=', (int) !Auth::check())
                                       ->select('occurred_at')
                                       ->whereBetween('occurred_at', [
                                           $endDate->format('Y-m-d').' 00:00:00',
                                           $startDate->format('Y-m-d').' 23:59:59',
                                       ])
                                       ->distinct()
                                       ->orderBy('occurred_at', 'desc')
                                       ->get()
                                       ->map(function (Incident $incident) {
                                           return app(DateFactory::class)->make($incident->occurred_at)->toDateString();
                                       })->unique()
                                      ->values();

            $numIncidentDays = count($allIncidentDays);
            $numPages = round($numIncidentDays / max($appIncidentDays, 1));

            $selectedDays = $allIncidentDays->slice($page * $appIncidentDays, $appIncidentDays)->all();

            if (count($selectedDays) > 0) {
                $startDate = Date::createFromFormat('Y-m-d', array_values($selectedDays)[0]);
                $endDate = Date::createFromFormat('Y-m-d', array_values(array_slice($selectedDays, -1))[0]);
            }

            $canPageForward = $page > 0;
            $canPageBackward = ($page + 1) < $numPages;
            $previousDate = $page + 1;
            $nextDate = $page - 1;
        } else {
            $date = Date::now();

            $canPageForward = (bool) $startDate->lt($date->sub('1 day'));
            $canPageBackward = Incident::where('occurred_at', '<', $date->format('Y-m-d'))->count() > 0;
            $previousDate = $startDate->copy()->subDays($appIncidentDays)->toDateString();
            $nextDate = $startDate->copy()->addDays($appIncidentDays)->toDateString();
        }

        $allIncidents = Incident::with('component', 'updates.incident')
            ->where('visible', '>=', (int) !Auth::check())->whereBetween('occurred_at', [
            $endDate->format('Y-m-d').' 00:00:00',
            $startDate->format('Y-m-d').' 23:59:59',
        ])->orderBy('occurred_at', 'desc')->get()->groupBy(function (Incident $incident) {
            return app(DateFactory::class)->make($incident->occurred_at)->toDateString();
        });

        if (!$onlyDisruptedDays) {
            $incidentDays = array_pad([], $appIncidentDays, null);

            // Add in days that have no incidents
            foreach ($incidentDays as $i => $day) {
                $date = app(DateFactory::class)->make($startDate)->subDays($i);

                if (!isset($allIncidents[$date->toDateString()])) {
{'fraction_non_alphanumeric': 0.15090329436769395, 'fraction_numerical': 0.007863974495217854, 'mean_word_length': 2.209754433833561, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 116, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '924599', 'n_tokens_mistral': 2865, 'n_tokens_neox': 2675, 'n_words': 474}