<commit_before><?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity as EntityDir;

/**
 * @Route("/stats")
 */
class StatsController extends RestController
{
    /**
     * @Route("/users")
     * @Method({"GET"})
     */
    public function users(Request $request)
    {
        $ret = [];
        $this->denyAccessUnlessGranted(EntityDir\Role::ADMIN);

        //$deputy = $this->getRepository('Role')->findBy(['role'=>'ROLE_LAY_DEPUTY']);
        // pre-join data to reduce number of queries
        // $users = $this->getRepository('User')->findBy(['role'=>$deputy], ['id' => 'DESC']);
        $qb = $this->getEntityManager()->createQuery(
            "SELECT u, c, r, role FROM AppBundle\Entity\User u
                LEFT JOIN u.role role
                LEFT JOIN u.clients c
                LEFT JOIN c.reports r
                WHERE role.role = 'ROLE_LAY_DEPUTY'");
        $users = $qb->getResult();

        // alternative without join and lazy-loading
        // $deputy = $this->getRepository('Role')->findBy(['role'=>'ROLE_LAY_DEPUTY']);
        // $users = $this->getRepository('User')->findBy(['role'=>$deputy], ['id' => 'DESC']);

        foreach ($users as $user) {
            /* @var $user EntityDir\User */
            $row = [
                'id' => $user->getId(),
                'email' => $user->getEmail(),
                'name' => $user->getFirstname(),
                'lastname' => $user->getLastname(),
                'registration_date' => $user->getRegistrationDate() ? $user->getRegistrationDate()->format('Y-m-d') : '-',
                'last_logged_in' => $user->getLastLoggedIn() ? $user->getLastLoggedIn()->format('Y-m-d H:i:s') : '-',
                'client_name' => 'n.a.',
                'client_lastname' => 'n.a.',
                'client_casenumber' => 'n.a.',
                'client_court_order_date' => 'n.a.',
                'total_reports' => 0,
                'active_reports' => 0,
            ];

            foreach ($user->getClients() as $client) {
                $row['client_name'] = $client->getFirstname();
                $row['client_lastname'] = $client->getLastname();
                $row['client_casenumber'] = $client->getCaseNumber();
                $row['client_court_order_date'] = $client->getCourtDate() ? $client->getCourtDate()->format('Y-m-d') : '-';<commit_msg>DDPB-438 limit option<commit_after><?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity as EntityDir;

/**
 * @Route("/stats")
 */
class StatsController extends RestController
{
    /**
     * @Route("/users")
     * @Method({"GET"})
     */
    public function users(Request $request)
    {
        $ret = [];
        $this->denyAccessUnlessGranted(EntityDir\Role::ADMIN);

        //$deputy = $this->getRepository('Role')->findBy(['role'=>'ROLE_LAY_DEPUTY']);
        // pre-join data to reduce number of queries
        // $users = $this->getRepository('User')->findBy(['role'=>$deputy], ['id' => 'DESC']);
        $qb = $this->getEntityManager()->createQuery(
            "SELECT u, c, r, role FROM AppBundle\Entity\User u
                LEFT JOIN u.role role
                LEFT JOIN u.clients c
                LEFT JOIN c.reports r
                WHERE role.role = 'ROLE_LAY_DEPUTY' ORDER BY u.id DESC");

        if ($maxResults = $request->query->get('limit')) {
            $qb->setMaxResults($maxResults);
        }
        $users = $qb->getResult();

        // alternative without join and lazy-loading
        // $deputy = $this->getRepository('Role')->findBy(['role'=>'ROLE_LAY_DEPUTY']);
        // $users = $this->getRepository('User')->findBy(['role'=>$deputy], ['id' => 'DESC']);

        foreach ($users as $user) {
            /* @var $user EntityDir\User */
            $row = [
                'id' => $user->getId(),
                'email' => $user->getEmail(),
                'name' => $user->getFirstname(),
                'lastname' => $user->getLastname(),
                'registration_date' => $user->getRegistrationDate() ? $user->getRegistrationDate()->format('Y-m-d') : '-',
                'last_logged_in' => $user->getLastLoggedIn() ? $user->getLastLoggedIn()->format('Y-m-d H:i:s') : '-',
                'client_name' => 'n.a.',
                'client_lastname' => 'n.a.',
                'client_casenumber' => 'n.a.',
                'client_court_order_date' => 'n.a.',
                'total_reports' => 0,
                'active_reports' => 0,
            ];

            foreach ($user->getClients() as $client) {
                $row['client_name'] = $client->getFirstname();
                $row['client_lastname'] = $client->getLastname();
                $row['client_casenumber'] = $client->getCaseNumber();
                $row['client_court_order_date'] = $client->getCourtDate() ? $client->getCourtDate()->format('Y-m-d') : '-';
{'fraction_non_alphanumeric': 0.18287037037037038, 'fraction_numerical': 0.0013503086419753086, 'mean_word_length': 2.24874686716792, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 100, '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': '243392', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1524, 'n_words': 260}