<commit_before><?php
namespace Metagist\ServerBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Repository for package ratings.
 * 
 * @author <NAME> <<EMAIL>>
 */
class RatingRepository extends EntityRepository
{
    /**
     * Retrieves all stored meta info for the given package.
     * 
     * @param \Metagist\ServerBundle\Entity\Package $package
     * @param integer           $offset
     * @param integer           $limit
     * @return \Doctrine\Common\Collections\Collection
     */
    public function byPackage(Package $package, $offset = 0, $limit = 25)
    {
        if ($package->getId() === null) {
            throw new \RuntimeException('Package has no id.');
        }
        $builder = $this->createQueryBuilder('r')
            ->where('r.package = :package')
            ->setFirstResult($offset)
            ->setMaxResults($limit);
        
        return new ArrayCollection($builder->getQuery()->execute(array('package' => $package)));
    }
    
    /**
     * Retrieves the rating of a package by the given user.
     * 
     * @param Package $package
     * @param User    $user
     * @return Rating|null
     */
    public function byPackageAndUser(Package $package, User $user)
    {
        return $this->findOneBy(array('package' => $package, 'user' => $user));
    }
    
    /**
     * Returns all the user's ratings.
     * 
     * @param \Metagist\ServerBundle\Entity\User $user
     * @return ArrayCollection
     */
    public function byUser(User $user)
    {
        return new ArrayCollection($this->findBy(array('user' => $user)));
    }
    
    /**
     * Retrieve the latest ratings.
     * 
     * @param int $limit
     * @return \Doctrine\Common\Collections\ArrayCollection
     */
    public function latest($limit = 1)
    {
        $builder = $this->createQueryBuilder('r')
            ->orderBy('r.timeUpdated', 'DESC')
            ->setMaxResults($limit);
        
        return new ArrayCollection($builder->getQuery()->execute());
    }
    
    /**
     * Saves (inserts) a single info.
     * 
     * @param \Metagist\Rating $rating
     * @return int
     */
    public function save(Rating $rating)
    {
        $this->getEntityManager()->persist($rating);
        $this->getEntityManager()->flush();
    }
    
    /**
     * Retrieves metainfo that has been updated lately.
     * 
     * @param int $limit
     * @return \Doctrine\Common\Collections\ArrayCollection
     */
    public function best($limit = 25)
    {
        $builder = $this->createQueryBuilder('r')
            ->select('avg(r.rating) rateavg, r')
            ->join('r.package', 'p')
            ->groupBy('p.id')
            ->orderBy('rateavg', 'DESC')
            ->setMaxResults($limit);
        
        $result = $builder->getQuery()->execute();
        $collection = new ArrayCollection();
        foreach ($result as $data) {
            foreach ($data as $entry) {
                if ($entry instanceof Rating) {
                    $collection->add($entry->getPackage());
                }
            }
        }
        return $collection;
    }
    
    /**
     * Retrieves the average rating for a package.
     * 
     * @param \Metagist\ServerBundle\Entity\Package $package
     * @return float
     * @todo select rateavg without the package
     */
    public function getAverageForPackage(Package $package)
    {
        $builder = $this->createQueryBuilder('r')
            ->select('avg(r.rating) rateavg, r')
            ->join('r.package', 'p')
            ->groupBy('p.id')
            ->where('r.package = :package')
            ->orderBy('rateavg', 'DESC');
        
        $result = $builder->getQuery()->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_SCALAR)->execute(array('package' => $package));
        return (float)$result[0]['rateavg'];
    }
}<commit_msg>rating repo orders rating by time upated (desc)<commit_after><?php
namespace Metagist\ServerBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Repository for package ratings.
 * 
 * @author <NAME> <<EMAIL>>
 */
class RatingRepository extends EntityRepository
{
    /**
     * Retrieves all stored meta info for the given package.
     * 
     * @param \Metagist\ServerBundle\Entity\Package $package
     * @param integer           $offset
     * @param integer           $limit
     * @return \Doctrine\Common\Collections\Collection
     */
    public function byPackage(Package $package, $offset = 0, $limit = 25)
    {
        if ($package->getId() === null) {
            throw new \RuntimeException('Package has no id.');
        }
        $builder = $this->createQueryBuilder('r')
            ->where('r.package = :package')
            ->setFirstResult($offset)
            ->orderBy('r.timeUpdated', 'DESC')
            ->setMaxResults($limit);
        
        return new ArrayCollection($builder->getQuery()->execute(array('package' => $package)));
    }
    
    /**
     * Retrieves the rating of a package by the given user.
     * 
     * @param Package $package
     * @param User    $user
     * @return Rating|null
     */
    public function byPackageAndUser(Package $package, User $user)
    {
        return $this->findOneBy(array('package' => $package, 'user' => $user));
    }
    
    /**
     * Returns all the user's ratings.
     * 
     * @param \Metagist\ServerBundle\Entity\User $user
     * @return ArrayCollection
     */
    public function byUser(User $user)
    {
        return new ArrayCollection($this->findBy(array('user' => $user)));
    }
    
    /**
     * Retrieve the latest ratings.
     * 
     * @param int $limit
     * @return \Doctrine\Common\Collections\ArrayCollection
     */
    public function latest($limit = 1)
    {
        $builder = $this->createQueryBuilder('r')
            ->orderBy('r.timeUpdated', 'DESC')
            ->setMaxResults($limit);
        
        return new ArrayCollection($builder->getQuery()->execute());
    }
    
    /**
     * Saves (inserts) a single info.
     * 
     * @param \Metagist\Rating $rating
     * @return int
     */
    public function save(Rating $rating)
    {
        $this->getEntityManager()->persist($rating);
        $this->getEntityManager()->flush();
    }
    
    /**
     * Retrieves metainfo that has been updated lately.
     * 
     * @param int $limit
     * @return \Doctrine\Common\Collections\ArrayCollection
     */
    public function best($limit = 25)
    {
        $builder = $this->createQueryBuilder('r')
            ->select('avg(r.rating) rateavg, r')
            ->join('r.package', 'p')
            ->groupBy('p.id')
            ->orderBy('rateavg', 'DESC')
            ->setMaxResults($limit);
        
        $result = $builder->getQuery()->execute();
        $collection = new ArrayCollection();
        foreach ($result as $data) {
            foreach ($data as $entry) {
                if ($entry instanceof Rating) {
                    $collection->add($entry->getPackage());
                }
            }
        }
        return $collection;
    }
    
    /**
     * Retrieves the average rating for a package.
     * 
     * @param \Metagist\ServerBundle\Entity\Package $package
     * @return float
     * @todo select rateavg without the package
     */
    public function getAverageForPackage(Package $package)
    {
        $builder = $this->createQueryBuilder('r')
            ->select('avg(r.rating) rateavg, r')
            ->join('r.package', 'p')
            ->groupBy('p.id')
            ->where('r.package = :package')
            ->orderBy('rateavg', 'DESC');
        
        $result = $builder->getQuery()->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_SCALAR)->execute(array('package' => $package));
        return (float)$result[0]['rateavg'];
    }
}
{'fraction_non_alphanumeric': 0.14407534682448772, 'fraction_numerical': 0.0017818505791014382, 'mean_word_length': 2.0743348982785603, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 94, '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': '9220', 'n_tokens_mistral': 2239, 'n_tokens_neox': 2200, 'n_words': 479}