<commit_before><?php

/**
 * @author    <NAME> <<EMAIL>>
 * @copyright 2013-2015 Verein zur Förderung der Netzkultur im Rhein-Main-Gebiet e.V. | http://netzkultur-rheinmain.de/
 */

namespace BCRM\WebBundle\Controller;

use BCRM\BackendBundle\Entity\Event\Event;
use BCRM\BackendBundle\Entity\Event\EventRepository;
use BCRM\BackendBundle\Entity\Event\Registration;
use BCRM\BackendBundle\Entity\Event\Ticket;
use BCRM\BackendBundle\Entity\Event\TicketRepository;
use BCRM\BackendBundle\Entity\Event\Unregistration;
use BCRM\BackendBundle\Entity\Event\UnregistrationRepository;
use BCRM\WebBundle\Content\ContentReader;
use BCRM\WebBundle\Exception\AccesDeniedHttpException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;

/**
 * Stats API endpoints.
 */
class StatsController
{
    public function __construct(
        ContentReader $reader,
        EventRepository $eventRepo,
        TicketRepository $ticketRepo,
        UnregistrationRepository $unregistrationRepo,
        EngineInterface $renderer
    )
    {
        $this->eventRepo          = $eventRepo;
        $this->ticketRepo         = $ticketRepo;
        $this->unregistrationRepo = $unregistrationRepo;
        $this->reader             = $reader;
        $this->renderer           = $renderer;
    }

    /**
     * Generates the event's statistics as json.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function statsAction(Request $request)
    {
        $format = $request->get('_format');
        if ($format === 'json') {
            return $this->statsJson($request);
        }

        $response = new Response();
        $response->setTtl(60 * 5);
        $response->setPublic();
        if ($response->isNotModified($request)) {
            return $response;
        }

        $data = array(
            'sponsors' => $this->reader->getPage('Sponsoren/Index.md')
        );

        return $this->renderer->renderResponse('BCRMWebBundle:Stats:stats.html.twig', $data, $response);
    }

    /**
     * Creates the JSON used to render the stats.
     *
     * @param Request $request
     *
     * @return Response
     */
    protected function statsJson(Request $request)
    {
        $response = new Response();
        $response->setTtl(60 * 5);
        $response->setPublic();
        if ($response->isNotModified($request)) {
            return $response;
        }
        $event           = $this->eventRepo->getNextEvent()->getOrThrow(new AccesDeniedHttpException('No event.'));
        $tickets         = $this->ticketRepo->getTicketsForEvent($event);
        $unregistrations = $this->unregistrationRepo->getUnregistrationsForEvent($event);

        $stats = array(
            'checkins'        => array(
                'sa'      => $this->getCheckinsPerDay($tickets, Ticket::DAY_SATURDAY),
                'sa_hour' => $this->getCheckinsPerHour($tickets, Ticket::DAY_SATURDAY),
                'su'      => $this->getCheckinsPerDay($tickets, Ticket::DAY_SUNDAY),
                'su_hour' => $this->getCheckinsPerHour($tickets, Ticket::DAY_SUNDAY),
                'unique'  => array(
                    'sa'   => $this->getUniqueDayCheckins($tickets, Ticket::DAY_SATURDAY),
                    'su'   => $this->getUniqueDayCheckins($tickets, Ticket::DAY_SUNDAY),
                    'both' => $this->getUniqueDayCheckins($tickets, Ticket::DAY_SUNDAY, true),
                ),
                'noshows' => array(
                    'sa' => $this->getNoShows($tickets, Ticket::DAY_SATURDAY),
                    'su' => $this->getNoShows($tickets, Ticket::DAY_SUNDAY),
                )
            ),
            'unregistrations' => $this->getUnregistrationsPerDay($unregistrations)
        );

        $data = array('stats' => $stats);
        $response->setContent(json_encode($data));<commit_msg>Decrease stats TTL.<commit_after><?php

/**
 * @author    <NAME> <<EMAIL>>
 * @copyright 2013-2015 Verein zur Förderung der Netzkultur im Rhein-Main-Gebiet e.V. | http://netzkultur-rheinmain.de/
 */

namespace BCRM\WebBundle\Controller;

use BCRM\BackendBundle\Entity\Event\Event;
use BCRM\BackendBundle\Entity\Event\EventRepository;
use BCRM\BackendBundle\Entity\Event\Registration;
use BCRM\BackendBundle\Entity\Event\Ticket;
use BCRM\BackendBundle\Entity\Event\TicketRepository;
use BCRM\BackendBundle\Entity\Event\Unregistration;
use BCRM\BackendBundle\Entity\Event\UnregistrationRepository;
use BCRM\WebBundle\Content\ContentReader;
use BCRM\WebBundle\Exception\AccesDeniedHttpException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;

/**
 * Stats API endpoints.
 */
class StatsController
{
    public function __construct(
        ContentReader $reader,
        EventRepository $eventRepo,
        TicketRepository $ticketRepo,
        UnregistrationRepository $unregistrationRepo,
        EngineInterface $renderer
    )
    {
        $this->eventRepo          = $eventRepo;
        $this->ticketRepo         = $ticketRepo;
        $this->unregistrationRepo = $unregistrationRepo;
        $this->reader             = $reader;
        $this->renderer           = $renderer;
    }

    /**
     * Generates the event's statistics as json.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function statsAction(Request $request)
    {
        $format = $request->get('_format');
        if ($format === 'json') {
            return $this->statsJson($request);
        }

        $response = new Response();
        $response->setTtl(60 * 5);
        $response->setPublic();
        if ($response->isNotModified($request)) {
            return $response;
        }

        $data = array(
            'sponsors' => $this->reader->getPage('Sponsoren/Index.md')
        );

        return $this->renderer->renderResponse('BCRMWebBundle:Stats:stats.html.twig', $data, $response);
    }

    /**
     * Creates the JSON used to render the stats.
     *
     * @param Request $request
     *
     * @return Response
     */
    protected function statsJson(Request $request)
    {
        $response = new Response();
        $response->setTtl(10);
        $response->setPublic();
        if ($response->isNotModified($request)) {
            return $response;
        }
        $event           = $this->eventRepo->getNextEvent()->getOrThrow(new AccesDeniedHttpException('No event.'));
        $tickets         = $this->ticketRepo->getTicketsForEvent($event);
        $unregistrations = $this->unregistrationRepo->getUnregistrationsForEvent($event);

        $stats = array(
            'checkins'        => array(
                'sa'      => $this->getCheckinsPerDay($tickets, Ticket::DAY_SATURDAY),
                'sa_hour' => $this->getCheckinsPerHour($tickets, Ticket::DAY_SATURDAY),
                'su'      => $this->getCheckinsPerDay($tickets, Ticket::DAY_SUNDAY),
                'su_hour' => $this->getCheckinsPerHour($tickets, Ticket::DAY_SUNDAY),
                'unique'  => array(
                    'sa'   => $this->getUniqueDayCheckins($tickets, Ticket::DAY_SATURDAY),
                    'su'   => $this->getUniqueDayCheckins($tickets, Ticket::DAY_SUNDAY),
                    'both' => $this->getUniqueDayCheckins($tickets, Ticket::DAY_SUNDAY, true),
                ),
                'noshows' => array(
                    'sa' => $this->getNoShows($tickets, Ticket::DAY_SATURDAY),
                    'su' => $this->getNoShows($tickets, Ticket::DAY_SUNDAY),
                )
            ),
            'unregistrations' => $this->getUnregistrationsPerDay($unregistrations)
        );

        $data = array('stats' => $stats);
        $response->setContent(json_encode($data));
{'fraction_non_alphanumeric': 0.1334853090172239, 'fraction_numerical': 0.003419452887537994, 'mean_word_length': 2.4545056867891515, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7583119', 'n_tokens_mistral': 2391, 'n_tokens_neox': 2359, 'n_words': 372}