<commit_before><?php
namespace Tisseo\EndivBundle\Services\Ogive;

use Doctrine\ORM\Tools\Pagination\Paginator;
use Tisseo\EndivBundle\Entity\Ogive\Event;
use Tisseo\EndivBundle\Entity\Ogive\EventStepStatus;
use Tisseo\EndivBundle\Types\Ogive\MomentType;

class EventManager extends OgiveManager
{
    /**
     * Find parent event if exists
     *
     * @param $disruptionId
     * @return Event|null
     */
    public function findParentEvent($disruptionId)
    {
        $subQueryBuilder = $this->objectManager->createQueryBuilder()
            ->select('identity(e.eventParent)')
            ->from('Tisseo\EndivBundle\Entity\Ogive\Event', 'e')
            ->where('e.eventParent is not null')
            ->distinct();

        $queryBuilder = $this->objectManager->createQueryBuilder();
        $queryBuilder
            ->select('event')
            ->from('Tisseo\EndivBundle\Entity\Ogive\Event', 'event')
            ->where('event.chaosDisruptionId = :disruptionId')
            ->andWhere('event.status = :status')
            ->andWhere($queryBuilder->expr()->notIn('event.id', $subQueryBuilder->getDQL()))
            ->setParameters(array(
                'disruptionId' => $disruptionId,
                'status' => Event::STATUS_OPEN
            ))
            ->setMaxResults(1);

        return $queryBuilder->getQuery()->getOneOrNullResult();
    }

    public function findEventList($archive = false, $limit = null, $offset = 0) {
        $status = ($archive === true) ? array(Event::STATUS_CLOSED, Event::STATUS_REJECTED) : array(Event::STATUS_OPEN);

        $queryBuilder = $this->objectManager->createQueryBuilder()
            ->select('event')
            ->from('Tisseo\EndivBundle\Entity\Ogive\Event', 'event')
            ->leftJoin('event.periods', 'p')
            ->leftJoin('event.eventSteps', 'es')
            ->leftJoin('es.statuses', 's')
            ->where('event.status in (:status)')
            ->setParameter('status', $status)
            ->addSelect('p, es, s');

        if ($archive === true) {
            $queryBuilder
                ->leftJoin('event.objects', 'eo')->addSelect('eo');
        }

        $queryBuilder->orderBy('event.id', 'ASC');

        if ($limit !== null) {
            $queryBuilder->setFirstResult($offset);
            $queryBuilder->setMaxResults($limit);
            $paginator = new Paginator($queryBuilder->getQuery(), true);

            return $paginator;
        }

        return $queryBuilder->getQuery()->getResult();
    }

    /**
     * Check alerts and nbr of finished event steps.
     *
     * @param  array $events
     * @return array
     */
    public function checkEventSteps(array $events)
    {
        $eventsInfos = array();
        $now = new \Datetime();

        foreach ($events as $event) {
            $extrema = $event->getExtremaPeriodDates();
            $nbrFinished = 0;
            $alert = false;

            foreach ($event->getEventSteps() as $eventStep) {
                if ($eventStep->getLastStatus()->getStatus() != EventStepStatus::STATUS_TODO) {
                    $nbrFinished++;
                } elseif (!$alert) {
                    $moment = $eventStep->getMoment();<commit_msg>fix: add order by and like to event list function<commit_after><?php
namespace Tisseo\EndivBundle\Services\Ogive;

use Doctrine\ORM\Tools\Pagination\Paginator;
use Tisseo\EndivBundle\Entity\Ogive\Event;
use Tisseo\EndivBundle\Entity\Ogive\EventStepStatus;
use Tisseo\EndivBundle\Types\Ogive\MomentType;

class EventManager extends OgiveManager
{
    /**
     * Find parent event if exists
     *
     * @param $disruptionId
     * @return Event|null
     */
    public function findParentEvent($disruptionId)
    {
        $subQueryBuilder = $this->objectManager->createQueryBuilder()
            ->select('identity(e.eventParent)')
            ->from('Tisseo\EndivBundle\Entity\Ogive\Event', 'e')
            ->where('e.eventParent is not null')
            ->distinct();

        $queryBuilder = $this->objectManager->createQueryBuilder();
        $queryBuilder
            ->select('event')
            ->from('Tisseo\EndivBundle\Entity\Ogive\Event', 'event')
            ->where('event.chaosDisruptionId = :disruptionId')
            ->andWhere('event.status = :status')
            ->andWhere($queryBuilder->expr()->notIn('event.id', $subQueryBuilder->getDQL()))
            ->setParameters(array(
                'disruptionId' => $disruptionId,
                'status' => Event::STATUS_OPEN
            ))
            ->setMaxResults(1);

        return $queryBuilder->getQuery()->getOneOrNullResult();
    }

    public function findEventList(
        $archive = false,
        $limit = null,
        $offset = 0,
        $search = null,
        $orderBy = array()
    ) {
        $status = ($archive === true) ? array(Event::STATUS_CLOSED, Event::STATUS_REJECTED) : array(Event::STATUS_OPEN);

        $queryBuilder = $this->objectManager->createQueryBuilder()
            ->select('event')
            ->from('Tisseo\EndivBundle\Entity\Ogive\Event', 'event')
            ->leftJoin('event.periods', 'p')
            ->leftJoin('event.eventSteps', 'es')
            ->leftJoin('es.statuses', 's')
            ->where('event.status in (:status)')
            ->setParameter('status', $status)
            ->addSelect('p, es, s');

        if ($archive === true) {
            $queryBuilder
                ->leftJoin('event.objects', 'eo')->addSelect('eo');
        }

        $queryBuilder->orderBy('event.id', 'ASC');

        if ($limit !== null) {
            if ($search !== null) {
                $queryBuilder->andWhere(
                    'unaccent(lower(event.reference)) LIKE unaccent(lower(:search)) OR
                     unaccent(lower(event.chaosInternalCause)) LIKE unaccent(lower(:search))');
                $queryBuilder->setParameter('search', '%'.$search.'%');
            }

            foreach ($orderBy as $field => $direction) {
                $queryBuilder->orderBy($field, $direction);
            }

            $queryBuilder->setFirstResult($offset);
            $queryBuilder->setMaxResults($limit);
            $paginator = new Paginator($queryBuilder->getQuery(), true);

            return $paginator;
        }

        return $queryBuilder->getQuery()->getResult();
    }

    /**
     * Check alerts and nbr of finished event steps.
     *
     * @param  array $events
     * @return array
     */
    public function checkEventSteps(array $events)
    {
        $eventsInfos = array();
        $now = new \Datetime();

        foreach ($events as $event) {
            $extrema = $event->getExtremaPeriodDates();
            $nbrFinished = 0;
            $alert = false;

            foreach ($event->getEventSteps() as $eventStep) {
                if ($eventStep->getLastStatus()->getStatus() != EventStepStatus::STATUS_TODO) {
                    $nbrFinished++;
                } elseif (!$alert) {
                    $moment = $eventStep->getMoment();
{'fraction_non_alphanumeric': 0.15010691375623664, 'fraction_numerical': 0.0008553100498930863, 'mean_word_length': 2.1804170444242974, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 97, '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': '4645236', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1971, 'n_words': 343}