<commit_before><?php

namespace Outlandish\Wpackagist\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;

class RequestRepository extends EntityRepository
{
    /**
     * Get a count of sensitive requests for an IP, incrementing the counter as a side effect.
     *
     * @param string $ip
     * @return int The number of requests within the past 24 hours
     * @throws \Doctrine\DBAL\DBALException
     */
    public function getRequestCountByIp(string $ip): int
    {
        $em = $this->getEntityManager();
        $qb = new QueryBuilder($em);

        $oneHourAgo = (new \DateTime())->sub(new \DateInterval('PT1H'));

        $qb->select('r')
            ->from(Request::class, 'r')
            ->where('r.ipAddress = :ip')
            ->andWhere('r.lastRequest >= :cutoff')
            ->setParameter('ip', $ip)
            ->setParameter('cutoff', $oneHourAgo);
        $requestHistory = $qb->getQuery()->getResult();

        if (empty($requestHistory)) {
            $this->deleteOldRequestCounts($ip, $oneHourAgo);

            $requestItem = new Request();
            $requestItem->setIpAddress($ip);
        } else {
            $requestItem = $requestHistory[0];
        }

        $requestItem->addRequest();<commit_msg>Add a note to possibly follow up on 1 rare error log observed<commit_after><?php

namespace Outlandish\Wpackagist\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;

class RequestRepository extends EntityRepository
{
    /**
     * Get a count of sensitive requests for an IP, incrementing the counter as a side effect.
     *
     * @param string $ip
     * @return int The number of requests within the past 24 hours
     * @throws \Doctrine\DBAL\DBALException
     */
    public function getRequestCountByIp(string $ip): int
    {
        $em = $this->getEntityManager();
        $qb = new QueryBuilder($em);

        $oneHourAgo = (new \DateTime())->sub(new \DateInterval('PT1H'));

        $qb->select('r')
            ->from(Request::class, 'r')
            ->where('r.ipAddress = :ip')
            ->andWhere('r.lastRequest >= :cutoff')
            ->setParameter('ip', $ip)
            ->setParameter('cutoff', $oneHourAgo);
        $requestHistory = $qb->getQuery()->getResult();

        if (empty($requestHistory)) {
            $this->deleteOldRequestCounts($ip, $oneHourAgo);

            $requestItem = new Request();
            $requestItem->setIpAddress($ip);
        } else {
            $requestItem = $requestHistory[0];
        }

        $requestItem->addRequest();
        // TODO (low priority) this can – very rarely – crash if another request persisted a record
        // with the same IP since the lookup to check for existing ones. Ideally the select and
        // insert would be atomic in a txn?
{'fraction_non_alphanumeric': 0.12935323383084577, 'fraction_numerical': 0.0031982942430703624, 'mean_word_length': 2.483910891089109, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '224731', 'n_tokens_mistral': 811, 'n_tokens_neox': 795, 'n_words': 214}