<commit_before><?php
/**
 * Litus is a project by a group of students from the KU Leuven. The goal is to create
 * various applications to support the IT needs of student unions.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace CommonBundle\Controller\Admin;

use CommonBundle\Component\Piwik\Analytics,
    DateInterval,
    DateTime,
    Zend\View\Model\ViewModel;

/**
 * IndexController
 *
 * @author <NAME> <<EMAIL>>
 */
class IndexController extends \CommonBundle\Component\Controller\ActionController\AdminController
{
    public function indexAction()
    {
        $piwikEnabled = $this->getEntityManager()
            ->getRepository('CommonBundle\Entity\General\Config')
            ->getConfigValue('common.piwik_enabled');

        $piwik = null;
        if ('development' != getenv('APPLICATION_ENV') && $piwikEnabled) {
            $analytics = new Analytics(
                $this->getEntityManager()
                    ->getRepository('CommonBundle\Entity\General\Config')
                    ->getConfigValue('common.piwik_api_url'),
                $this->getEntityManager()
                    ->getRepository('CommonBundle\Entity\General\Config')
                    ->getConfigValue('common.piwik_token_auth'),
                $this->getEntityManager()
                    ->getRepository('CommonBundle\Entity\General\Config')
                    ->getConfigValue('common.piwik_id_site')
            );

            $piwik = array(
                'uniqueVisitors' => $analytics->getUniqueVisitors(),
                'liveCounters' => $analytics->getLiveCounters(),
                'visitsGraph' => $this->_getVisitsGraph($analytics)
            );
        }

        $registrationEnabled = $this->getEntityManager()
            ->getRepository('CommonBundle\Entity\General\Config')
            ->getConfigValue('secretary.registration_enabled');

        $registrationsGraph = null;
        if ($registrationEnabled)
            $registrationsGraph = $this->_getRegistrationsGraph();

        $profActions = $this->getEntityManager()
            ->getRepository('CudiBundle\Entity\Prof\Action')
            ->findAllUncompleted(10);

        $subjectComments = $this->getEntityManager()
            ->getRepository('SyllabusBundle\Entity\Subject\Comment')
            ->findLast(10);

        $subjectReplies = $this->getEntityManager()
            ->getRepository('SyllabusBundle\Entity\Subject\Reply')
            ->findLast(10);

        $activeSessions = array();
        if ($this->getAuthentication()->isAuthenticated()) {
            $activeSessions = $this->getEntityManager()
                ->getRepository('CommonBundle\Entity\User\Session')
                ->findAllActiveByPerson($this->getAuthentication()->getPersonObject());
        }

        $currentSession = $this->getAuthentication()->getSessionObject();

        return new ViewModel(
            array(
                'profActions' => $profActions,
                'subjectComments' => $subjectComments,
                'subjectReplies' => $subjectReplies,
                'activeSessions' => $activeSessions,
                'currentSession' => $currentSession,
                'piwik' => $piwik,
                'registrationsGraph' => $registrationsGraph,
                'versions' => array(
                    'php' => phpversion(),
                    'zf' => \Zend\Version\Version::VERSION,
                    'doctrine' => \Doctrine\Common\Version::VERSION
                ),
            )
        );
    }

    private function _getVisitsGraph(Analytics $analytics)
    {
        if (null !== $this->getCache()) {
            if($this->getCache()->hasItem('CommonBundle_Controller_IndexController_VisitsGraph')) {
                $now = new DateTime();
                if ($this->getCache()->getItem('CommonBundle_Controller_IndexController_VisitsGraph')['expirationTime'] > $now)
                    return $this->getCache()->getItem('CommonBundle_Controller_IndexController_VisitsGraph');
            }

            $this->getCache()->setItem(
                'CommonBundle_Controller_IndexController_VisitsGraph',
                $this->_getVisitsGraphData($analytics)
            );

            return $this->getCache()->getItem('CommonBundle_Controller_IndexController_VisitsGraph');
        }

        return $this->_getVisitsGraphData($analytics);
    }

    private function _getVisitsGraphData(Analytics $analytics)
    {
        $now = new DateTime();

        $visitsGraphData = array(
            'expirationTime' => $now->add(new DateInterval('P1D')),

            'labels' => array(),
            'dataset' => array()
        );

        foreach ((array) $analytics->getUniqueVisitors('previous7') as $dateString => $count) {
            $date = new DateTime($dateString);

            $visitsGraphData['labels'][] = $date->format('d/m/Y');
            $visitsGraphData['dataset'][] = $count;
        }

        return $visitsGraphData;
    }

    private function _getRegistrationsGraph()
    {
        if (null !== $this->getCache()) {
            if($this->getCache()->hasItem('CommonBundle_Controller_IndexController_RegistrationsGraph')) {
                $now = new DateTime();
                if ($this->getCache()->getItem('CommonBundle_Controller_IndexController_RegistrationsGraph')['expirationTime'] > $now)
                    return $this->getCache()->getItem('CommonBundle_Controller_IndexController_RegistrationsGraph');
            }

            $this->getCache()->setItem(
                'CommonBundle_Controller_IndexController_RegistrationsGraph',
                $this->_getRegistrationsGraphData()
            );

            return $this->getCache()->getItem('CommonBundle_Controller_IndexController_RegistrationsGraph');
        }

        return $this->_getRegistrationsGraphData();
    }

    private function _getRegistrationsGraphData()
    {
        $now = new DateTime();

        $registationGraphData = array(
            'expirationTime' => $now->add(new DateInterval('PT1H')),

            'labels' => array(),
            'dataset' => array()
        );

        $registrations = $this->getEntityManager()
            ->getRepository('SecretaryBundle\Entity\Registration')
            ->findBy(
                array(
                    'academicYear' => $this->getCurrentAcademicYear()
                ),
                array(
                    'timestamp' => 'ASC'
                )
            );

        $data = array();
        foreach (array_reverse($registrations) as $registration) {
            if (count($data) >= 7)
                break;

            if (!isset($data[$registration->getTimestamp()->format('d/m/Y')])) {
                $data[$registration->getTimestamp()->format('d/m/Y')] = 1;
            } else {
                $data[$registration->getTimestamp()->format('d/m/Y')]++;
            }
        }

        foreach ($data as $label => $value) {
            $registationGraphData['labels'][] = $label;
            $registationGraphData['dataset'][] = $value;
        }

        return $registationGraphData;
    }
}<commit_msg>As it turns out, I needed a combination of the two<commit_after><?php
/**
 * Litus is a project by a group of students from the KU Leuven. The goal is to create
 * various applications to support the IT needs of student unions.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace CommonBundle\Controller\Admin;

use CommonBundle\Component\Piwik\Analytics,
    DateInterval,
    DateTime,
    Zend\View\Model\ViewModel;

/**
 * IndexController
 *
 * @author <NAME> <<EMAIL>>
 */
class IndexController extends \CommonBundle\Component\Controller\ActionController\AdminController
{
    public function indexAction()
    {
        $piwikEnabled = $this->getEntityManager()
            ->getRepository('CommonBundle\Entity\General\Config')
            ->getConfigValue('common.piwik_enabled');

        $piwik = null;
        if ('development' != getenv('APPLICATION_ENV') && $piwikEnabled) {
            $analytics = new Analytics(
                $this->getEntityManager()
                    ->getRepository('CommonBundle\Entity\General\Config')
                    ->getConfigValue('common.piwik_api_url'),
                $this->getEntityManager()
                    ->getRepository('CommonBundle\Entity\General\Config')
                    ->getConfigValue('common.piwik_token_auth'),
                $this->getEntityManager()
                    ->getRepository('CommonBundle\Entity\General\Config')
                    ->getConfigValue('common.piwik_id_site')
            );

            $piwik = array(
                'uniqueVisitors' => $analytics->getUniqueVisitors(),
                'liveCounters' => $analytics->getLiveCounters(),
                'visitsGraph' => $this->_getVisitsGraph($analytics)
            );
        }

        $registrationEnabled = $this->getEntityManager()
            ->getRepository('CommonBundle\Entity\General\Config')
            ->getConfigValue('secretary.registration_enabled');

        $registrationsGraph = null;
        if ($registrationEnabled)
            $registrationsGraph = $this->_getRegistrationsGraph();

        $profActions = $this->getEntityManager()
            ->getRepository('CudiBundle\Entity\Prof\Action')
            ->findAllUncompleted(10);

        $subjectComments = $this->getEntityManager()
            ->getRepository('SyllabusBundle\Entity\Subject\Comment')
            ->findLast(10);

        $subjectReplies = $this->getEntityManager()
            ->getRepository('SyllabusBundle\Entity\Subject\Reply')
            ->findLast(10);

        $activeSessions = array();
        if ($this->getAuthentication()->isAuthenticated()) {
            $activeSessions = $this->getEntityManager()
                ->getRepository('CommonBundle\Entity\User\Session')
                ->findAllActiveByPerson($this->getAuthentication()->getPersonObject());
        }

        $currentSession = $this->getAuthentication()->getSessionObject();

        return new ViewModel(
            array(
                'profActions' => $profActions,
                'subjectComments' => $subjectComments,
                'subjectReplies' => $subjectReplies,
                'activeSessions' => $activeSessions,
                'currentSession' => $currentSession,
                'piwik' => $piwik,
                'registrationsGraph' => $registrationsGraph,
                'versions' => array(
                    'php' => phpversion(),
                    'zf' => \Zend\Version\Version::VERSION,
                    'doctrine' => \Doctrine\Common\Version::VERSION
                ),
            )
        );
    }

    private function _getVisitsGraph(Analytics $analytics)
    {
        if (null !== $this->getCache()) {
            if($this->getCache()->hasItem('CommonBundle_Controller_IndexController_VisitsGraph')) {
                $now = new DateTime();
                if ($this->getCache()->getItem('CommonBundle_Controller_IndexController_VisitsGraph')['expirationTime'] > $now)
                    return $this->getCache()->getItem('CommonBundle_Controller_IndexController_VisitsGraph');
            }

            $this->getCache()->setItem(
                'CommonBundle_Controller_IndexController_VisitsGraph',
                $this->_getVisitsGraphData($analytics)
            );

            return $this->getCache()->getItem('CommonBundle_Controller_IndexController_VisitsGraph');
        }

        return $this->_getVisitsGraphData($analytics);
    }

    private function _getVisitsGraphData(Analytics $analytics)
    {
        $now = new DateTime();

        $visitsGraphData = array(
            'expirationTime' => $now->add(new DateInterval('P1D')),

            'labels' => array(),
            'dataset' => array()
        );

        foreach ((array) $analytics->getUniqueVisitors('previous7') as $dateString => $count) {
            $date = new DateTime($dateString);

            $visitsGraphData['labels'][] = $date->format('d/m/Y');
            $visitsGraphData['dataset'][] = $count;
        }

        return $visitsGraphData;
    }

    private function _getRegistrationsGraph()
    {
        if (null !== $this->getCache()) {
            if($this->getCache()->hasItem('CommonBundle_Controller_IndexController_RegistrationsGraph')) {
                $now = new DateTime();
                if ($this->getCache()->getItem('CommonBundle_Controller_IndexController_RegistrationsGraph')['expirationTime'] > $now)
                    return $this->getCache()->getItem('CommonBundle_Controller_IndexController_RegistrationsGraph');
            }

            $this->getCache()->setItem(
                'CommonBundle_Controller_IndexController_RegistrationsGraph',
                $this->_getRegistrationsGraphData()
            );

            return $this->getCache()->getItem('CommonBundle_Controller_IndexController_RegistrationsGraph');
        }

        return $this->_getRegistrationsGraphData();
    }

    private function _getRegistrationsGraphData()
    {
        $now = new DateTime();

        $registationGraphData = array(
            'expirationTime' => $now->add(new DateInterval('PT1H')),

            'labels' => array(),
            'dataset' => array()
        );

        $registrations = $this->getEntityManager()
            ->getRepository('SecretaryBundle\Entity\Registration')
            ->findBy(
                array(
                    'academicYear' => $this->getCurrentAcademicYear()
                ),
                array(
                    'timestamp' => 'ASC'
                )
            );

        $data = array();
        foreach (array_reverse($registrations) as $registration) {
            if (count($data) >= 7)
                break;

            if (!isset($data[$registration->getTimestamp()->format('d/m/Y')])) {
                $data[$registration->getTimestamp()->format('d/m/Y')] = 1;
            } else {
                $data[$registration->getTimestamp()->format('d/m/Y')]++;
            }
        }

        foreach(array_reverse($data) as $label => $value) {
            $registationGraphData['labels'][] = $label;
            $registationGraphData['dataset'][] = $value;
        }

        return $registationGraphData;
    }
}
{'fraction_non_alphanumeric': 0.1358431049852568, 'fraction_numerical': 0.0015086059109922514, 'mean_word_length': 2.1663048198002604, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 247, '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': '1083931', 'n_tokens_mistral': 3917, 'n_tokens_neox': 3745, 'n_words': 569}