<commit_before><?php

namespace App\Model\Table;

use App\Model\Entity\Session;
use Cake\Chronos\Chronos;
use Cake\Event\Event;
use Cake\I18n\FrozenTime;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Sessions Model
 *
 * @property \Cake\ORM\Association\BelongsTo $Projects
 * @property \Cake\ORM\Association\HasMany $Tickets
 *
 * @method \App\Model\Entity\Session get($primaryKey, $options = [])
 * @method \App\Model\Entity\Session newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\Session[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\Session|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Session patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\Session[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\Session findOrCreate($search, callable $callback = null, $options = [])
 *
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
 */
class SessionsTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config
     *            The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->addBehavior('Timestamp');

        $this->belongsTo('Projects', [
            'foreignKey' => 'project_id',
            'joinType' => 'INNER'
        ]);
        $this->hasMany('Tickets', [
            'foreignKey' => 'session_id'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator
     *            Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator->uuid('id')->allowEmpty('id', 'create');

        $validator->dateTime('begin')
            ->requirePresence('begin', 'create')
            ->notEmpty('begin');

        $validator->dateTime('end')->allowEmpty('end');

        $validator->time('time')->allowEmpty('time');

        $validator->integer('duration')->allowEmpty('duration');

        $validator->integer('section')->allowEmpty('section');

        $validator->integer('subsection')->allowEmpty('subsection');

        $validator->allowEmpty('task');

        $validator->numeric('expected_hours')->allowEmpty('expected_hours');

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules
     *            The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->existsIn([
            'project_id'
        ], 'Projects'));

        return $rules;
    }

    /**
     * Find ongoing sessions.
     *
     * Sessions that has not a defined end.
     *
     * @param Query $q
     * @return Query
     */
    public function findOngoingSessions(Query $q)
    {
        $q->select([
            $this->aliasField('id'),
            $this->aliasField('project_id'),
            $this->aliasField('begin'),
            $this->aliasField('section'),
            $this->aliasField('subsection'),
            $this->aliasField('task')
        ])
            ->where([
                $this->aliasField('end IS') => null
            ])
            ->order([
                $this->aliasField('created') => 'ASC'
            ]);

        return $q;
    }

    public function getMonitor()
    {
        $q = $this->Projects->find('sums');

        $monitor = $q->toArray();

        foreach ($monitor as &$monitor_item) {
            if ($monitor_item['weekly_goal']) {
                $monitor_item['week_diff'] = (3600 * $monitor_item['weekly_goal']) - $monitor_item['week_sum'];
                $monitor_item['week_percentage'] = $monitor_item['week_sum'] / (3600 * $monitor_item['weekly_goal']) * 100;
            }

            if ($monitor_item['monthly_goal']) {
                $monitor_item['month_diff'] = (3600 * $monitor_item['monthly_goal']) - $monitor_item['month_sum'];
                $monitor_item['month_percentage'] = $monitor_item['month_sum'] / (3600 * $monitor_item['monthly_goal']) * 100;
            }
        }

        // Order by week_diff desc
        usort($monitor, function ($a, $b) {
            return ($a->week_diff < $b->week_diff);
        });

        return $monitor;
    }

    public function findProjectDuration(Query $q, array $options)
    {
        $q->select([
            'total_duration' => $q->func()
                ->sum($this->aliasField('duration'))
        ])
            ->where([
                'Sessions.project_id = Projects.id',
                $this->aliasField('begin >=') => $options['begin'],
                $this->aliasField('begin <') => $options['end']
            ]);
<commit_msg>Order by month_diff desc<commit_after><?php

namespace App\Model\Table;

use App\Model\Entity\Session;
use Cake\Chronos\Chronos;
use Cake\Event\Event;
use Cake\I18n\FrozenTime;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Sessions Model
 *
 * @property \Cake\ORM\Association\BelongsTo $Projects
 * @property \Cake\ORM\Association\HasMany $Tickets
 *
 * @method \App\Model\Entity\Session get($primaryKey, $options = [])
 * @method \App\Model\Entity\Session newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\Session[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\Session|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Session patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\Session[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\Session findOrCreate($search, callable $callback = null, $options = [])
 *
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
 */
class SessionsTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config
     *            The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->addBehavior('Timestamp');

        $this->belongsTo('Projects', [
            'foreignKey' => 'project_id',
            'joinType' => 'INNER'
        ]);
        $this->hasMany('Tickets', [
            'foreignKey' => 'session_id'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator
     *            Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator->uuid('id')->allowEmpty('id', 'create');

        $validator->dateTime('begin')
            ->requirePresence('begin', 'create')
            ->notEmpty('begin');

        $validator->dateTime('end')->allowEmpty('end');

        $validator->time('time')->allowEmpty('time');

        $validator->integer('duration')->allowEmpty('duration');

        $validator->integer('section')->allowEmpty('section');

        $validator->integer('subsection')->allowEmpty('subsection');

        $validator->allowEmpty('task');

        $validator->numeric('expected_hours')->allowEmpty('expected_hours');

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules
     *            The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->existsIn([
            'project_id'
        ], 'Projects'));

        return $rules;
    }

    /**
     * Find ongoing sessions.
     *
     * Sessions that has not a defined end.
     *
     * @param Query $q
     * @return Query
     */
    public function findOngoingSessions(Query $q)
    {
        $q->select([
            $this->aliasField('id'),
            $this->aliasField('project_id'),
            $this->aliasField('begin'),
            $this->aliasField('section'),
            $this->aliasField('subsection'),
            $this->aliasField('task')
        ])
            ->where([
                $this->aliasField('end IS') => null
            ])
            ->order([
                $this->aliasField('created') => 'ASC'
            ]);

        return $q;
    }

    public function getMonitor()
    {
        $q = $this->Projects->find('sums');

        $monitor = $q->toArray();

        foreach ($monitor as &$monitor_item) {
            if ($monitor_item['weekly_goal']) {
                $monitor_item['week_diff'] = (3600 * $monitor_item['weekly_goal']) - $monitor_item['week_sum'];
                $monitor_item['week_percentage'] = $monitor_item['week_sum'] / (3600 * $monitor_item['weekly_goal']) * 100;
            }

            if ($monitor_item['monthly_goal']) {
                $monitor_item['month_diff'] = (3600 * $monitor_item['monthly_goal']) - $monitor_item['month_sum'];
                $monitor_item['month_percentage'] = $monitor_item['month_sum'] / (3600 * $monitor_item['monthly_goal']) * 100;
            }
        }

        // Order by week_diff desc
        usort($monitor, function ($a, $b) {
            return ($a->month_diff < $b->month_diff);
        });

        return $monitor;
    }

    public function findProjectDuration(Query $q, array $options)
    {
        $q->select([
            'total_duration' => $q->func()
                ->sum($this->aliasField('duration'))
        ])
            ->where([
                'Sessions.project_id = Projects.id',
                $this->aliasField('begin >=') => $options['begin'],
                $this->aliasField('begin <') => $options['end']
            ]);

{'fraction_non_alphanumeric': 0.15974755941228674, 'fraction_numerical': 0.0047332610196233115, 'mean_word_length': 2.323066841415465, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 113, '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': '799556', 'n_tokens_mistral': 3022, 'n_tokens_neox': 2919, 'n_words': 527}