<commit_before><?php
namespace DrupalReleaseDate\Repository;

use DateTime;
use DateInterval;
use Doctrine\DBAL\Connection as DbConnection;

use DrupalReleaseDate\Random\GeometricWeightedRandom;
use DrupalReleaseDate\Sampling\Sample;
use DrupalReleaseDate\Sampling\SampleSet;
use DrupalReleaseDate\Sampling\TimeGroupedSampleSetCollection;
use DrupalReleaseDate\Sampling\SampleSetRandomSampleSelector;
use DrupalReleaseDate\Sampling\TimeGroupedRandomSampleSelector;
use DrupalReleaseDate\MonteCarlo;
use DrupalReleaseDate\MonteCarloIncreasingRunException;

/**
 * Class to encapsulate methods that make updates to the database.
 */
class Updater
{
    protected $db;

    public function __construct(DbConnection $db)
    {
        $this->db = $db;
    }

    /**
     * Calculate a new estimate based on the available data, and store it to the
     * database.
     *
     * @param  array $config
     */
    public function estimate(array $config = array())
    {
        $config += array(
            'iterations' => 100000,
        );

        $db = $this->db;

        // Group samples by week.
        $samples = new TimeGroupedSampleSetCollection(604800);

        $samplesResultSet = $db->createQueryBuilder()
            ->select('s.when', 'sv_bugs.value + sv_tasks.value AS value')
            ->from('samples', 's')
            ->join('s', 'sample_values', 'sv_bugs', 's.version = sv_bugs.version && s.when = sv_bugs.when && sv_bugs.key="critical_bugs"')
            ->join('s', 'sample_values', 'sv_tasks', 's.version = sv_tasks.version && s.when = sv_tasks.when && sv_tasks.key="critical_tasks"')
            ->where('s.version = 8')
            ->orderBy($db->quoteIdentifier('when'), 'ASC')
            ->execute();
        $lastResult = null;
        while ($result = $samplesResultSet->fetchObject()) {
            $lastResult = $result;
            $samples->insert(new Sample(
                $db->convertToPhpValue($result->when, 'datetime')->getTimestamp(),
                $db->convertToPhpValue($result->value, 'smallint')
            ));
        }

        // Insert empty before run, update if succsesful.
        $db->insert(
            $db->quoteIdentifier('estimates'),
            array(
                $db->quoteIdentifier('when') => $lastResult->when,
                $db->quoteIdentifier('version') => 8,
                $db->quoteIdentifier('estimate') => null,
                $db->quoteIdentifier('note') => 'Timeout during run',
                $db->quoteIdentifier('data') => '',
            )
        );
        // Close connection during processing to prevent "Database has gone away" exception.
        $db->close();

        if (isset($config['timeout'])) {
            set_time_limit($config['timeout']);
        }

        // Give samples twice the weight of those from six months before.
        $geometricRandom = new GeometricWeightedRandom(0, $samples->length() - 1, pow(2, 1/16));
        $sampleSelector = new TimeGroupedRandomSampleSelector($samples, $geometricRandom);

        $monteCarlo = new MonteCarlo($sampleSelector);

        $update = array();

        try {
            $estimateDistribution = $monteCarlo->runDistribution($config['iterations']);
            $estimateInterval = MonteCarlo::getMedianFromDistribution($estimateDistribution);

            $estimateDate = new DateTime('@' . $_SERVER['REQUEST_TIME']);
            $estimateDate->add(DateInterval::createFromDateString($estimateInterval . ' seconds'));

            $update += array(
                $db->quoteIdentifier('estimate') => $db->convertToDatabaseValue($estimateDate, 'date'),
                $db->quoteIdentifier('note') => 'Run completed in ' . (time() - $_SERVER['REQUEST_TIME']) . ' seconds',
                $db->quoteIdentifier('data') => serialize($estimateDistribution),
            );
        } catch (MonteCarloIncreasingRunException $e) {
            $update += array(
                $db->quoteIdentifier('estimate') => '0000-00-00',
                $db->quoteIdentifier('note') => 'Run failed due to increasing issue count',
            );
        }

        $db->connect();
        $db->update(
            $db->quoteIdentifier('estimates'),
            $update,
            array(
                $db->quoteIdentifier('when') => $lastResult->when,<commit_msg>Restore 6 month weighting for estimate generation<commit_after><?php
namespace DrupalReleaseDate\Repository;

use DateTime;
use DateInterval;
use Doctrine\DBAL\Connection as DbConnection;

use DrupalReleaseDate\Random\GeometricWeightedRandom;
use DrupalReleaseDate\Sampling\Sample;
use DrupalReleaseDate\Sampling\SampleSet;
use DrupalReleaseDate\Sampling\TimeGroupedSampleSetCollection;
use DrupalReleaseDate\Sampling\SampleSetRandomSampleSelector;
use DrupalReleaseDate\Sampling\TimeGroupedRandomSampleSelector;
use DrupalReleaseDate\MonteCarlo;
use DrupalReleaseDate\MonteCarloIncreasingRunException;

/**
 * Class to encapsulate methods that make updates to the database.
 */
class Updater
{
    protected $db;

    public function __construct(DbConnection $db)
    {
        $this->db = $db;
    }

    /**
     * Calculate a new estimate based on the available data, and store it to the
     * database.
     *
     * @param  array $config
     */
    public function estimate(array $config = array())
    {
        $config += array(
            'iterations' => 100000,
        );

        $db = $this->db;

        // Group samples by week.
        $samples = new TimeGroupedSampleSetCollection(604800);

        $samplesResultSet = $db->createQueryBuilder()
            ->select('s.when', 'sv_bugs.value + sv_tasks.value AS value')
            ->from('samples', 's')
            ->join('s', 'sample_values', 'sv_bugs', 's.version = sv_bugs.version && s.when = sv_bugs.when && sv_bugs.key="critical_bugs"')
            ->join('s', 'sample_values', 'sv_tasks', 's.version = sv_tasks.version && s.when = sv_tasks.when && sv_tasks.key="critical_tasks"')
            ->where('s.version = 8')
            ->orderBy($db->quoteIdentifier('when'), 'ASC')
            ->execute();
        $lastResult = null;
        while ($result = $samplesResultSet->fetchObject()) {
            $lastResult = $result;
            $samples->insert(new Sample(
                $db->convertToPhpValue($result->when, 'datetime')->getTimestamp(),
                $db->convertToPhpValue($result->value, 'smallint')
            ));
        }

        // Insert empty before run, update if succsesful.
        $db->insert(
            $db->quoteIdentifier('estimates'),
            array(
                $db->quoteIdentifier('when') => $lastResult->when,
                $db->quoteIdentifier('version') => 8,
                $db->quoteIdentifier('estimate') => null,
                $db->quoteIdentifier('note') => 'Timeout during run',
                $db->quoteIdentifier('data') => '',
            )
        );
        // Close connection during processing to prevent "Database has gone away" exception.
        $db->close();

        if (isset($config['timeout'])) {
            set_time_limit($config['timeout']);
        }

        // Give samples twice the weight of those from six months before.
        $geometricRandom = new GeometricWeightedRandom(0, $samples->length() - 1, pow(2, 1/26));
        $sampleSelector = new TimeGroupedRandomSampleSelector($samples, $geometricRandom);

        $monteCarlo = new MonteCarlo($sampleSelector);

        $update = array();

        try {
            $estimateDistribution = $monteCarlo->runDistribution($config['iterations']);
            $estimateInterval = MonteCarlo::getMedianFromDistribution($estimateDistribution);

            $estimateDate = new DateTime('@' . $_SERVER['REQUEST_TIME']);
            $estimateDate->add(DateInterval::createFromDateString($estimateInterval . ' seconds'));

            $update += array(
                $db->quoteIdentifier('estimate') => $db->convertToDatabaseValue($estimateDate, 'date'),
                $db->quoteIdentifier('note') => 'Run completed in ' . (time() - $_SERVER['REQUEST_TIME']) . ' seconds',
                $db->quoteIdentifier('data') => serialize($estimateDistribution),
            );
        } catch (MonteCarloIncreasingRunException $e) {
            $update += array(
                $db->quoteIdentifier('estimate') => '0000-00-00',
                $db->quoteIdentifier('note') => 'Run failed due to increasing issue count',
            );
        }

        $db->connect();
        $db->update(
            $db->quoteIdentifier('estimates'),
            $update,
            array(
                $db->quoteIdentifier('when') => $lastResult->when,
{'fraction_non_alphanumeric': 0.1384011090573013, 'fraction_numerical': 0.006585027726432532, 'mean_word_length': 2.526272912423625, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1779482', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2301, 'n_words': 477}