<commit_before><?php namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * Торрент
 *
 * @property integer $id
 * @property integer $user_id
 * @property integer $category_id
 * @property integer $rto_id
 * @property string  $title
 * @property string  $html
 * @property integer $size
 * @property integer $seeders
 * @property string  $info_hash
 * @property string  $announcer
 * @property integer $clicks
 * @property \Carbon\Carbon $registered_at
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 *
 * @property-read \App\User $user
 */
class Torrent extends Model
{
    const STATUS_0 = 1; // не проверено
    const STATUS_1 = 1; // закрыто
    const STATUS_2 = 2; // проверено
    const STATUS_3 = 3; // недооформлено
    const STATUS_4 = 4; // не оформлено
    const STATUS_DUPLICATE = 5; // повтор
    const STATUS_6 = 6; // закрыто правообладателем
    const STATUS_7 = 7; // поглощено
    const STATUS_8 = 8; // сомнительно
    const STATUS_9 = 9; // проверяется
    const STATUS_10 = 10; // временная
    const STATUS_11 = 11; // премодерация

    protected $guarded = ['created_at', 'updated_at', 'goto'];
    protected $dates = ['registered_at'];
    protected $perPage = 50;

    public function comments()
    {
        return $this->morphMany(Comment::class, 'rel');
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function externalLink()
    {
        return "http://maintracker.org/forum/viewtopic.php?t={$this->rto_id}";
    }

    public function fullDate()
    {
        $format = $this->registered_at->year == date('Y') ? '%e %B' : '%e %B %Y';

        if ($this->registered_at->isToday()) {
            return trans('torrents.today').", ".$this->registered_at->formatLocalized($format);
        } elseif ($this->registered_at->isYesterday()) {
            return trans('torrents.yesterday').", ".$this->registered_at->formatLocalized($format);
        }

        return $this->registered_at->formatLocalized($format);
    }

    public function magnet()
    {
        return "magnet:?xt=urn:btih:{$this->info_hash}&tr=" . urlencode($this->announcer) . "&dn=" . rawurlencode($this->title);
    }

    public static function statsByCategories()
    {
        $result = self::selectRaw('category_id, COUNT(*) as total')
            ->groupBy('category_id')
            ->get();

        $stats = [];

        foreach ($result as $row) {
            $stats[$row->category_id] = $row->total;
        }

        return $stats;
    }
}<commit_msg>Кэш количества раздач в рубриках<commit_after><?php namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * Торрент
 *
 * @property integer $id
 * @property integer $user_id
 * @property integer $category_id
 * @property integer $rto_id
 * @property string  $title
 * @property string  $html
 * @property integer $size
 * @property integer $seeders
 * @property string  $info_hash
 * @property string  $announcer
 * @property integer $clicks
 * @property \Carbon\Carbon $registered_at
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 *
 * @property-read \App\User $user
 */
class Torrent extends Model
{
    const STATUS_0 = 1; // не проверено
    const STATUS_1 = 1; // закрыто
    const STATUS_2 = 2; // проверено
    const STATUS_3 = 3; // недооформлено
    const STATUS_4 = 4; // не оформлено
    const STATUS_DUPLICATE = 5; // повтор
    const STATUS_6 = 6; // закрыто правообладателем
    const STATUS_7 = 7; // поглощено
    const STATUS_8 = 8; // сомнительно
    const STATUS_9 = 9; // проверяется
    const STATUS_10 = 10; // временная
    const STATUS_11 = 11; // премодерация

    protected $guarded = ['created_at', 'updated_at', 'goto'];
    protected $dates = ['registered_at'];
    protected $perPage = 50;

    public function comments()
    {
        return $this->morphMany(Comment::class, 'rel');
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function externalLink()
    {
        return "http://maintracker.org/forum/viewtopic.php?t={$this->rto_id}";
    }

    public function fullDate()
    {
        $format = $this->registered_at->year == date('Y') ? '%e %B' : '%e %B %Y';

        if ($this->registered_at->isToday()) {
            return trans('torrents.today').", ".$this->registered_at->formatLocalized($format);
        } elseif ($this->registered_at->isYesterday()) {
            return trans('torrents.yesterday').", ".$this->registered_at->formatLocalized($format);
        }

        return $this->registered_at->formatLocalized($format);
    }

    public function magnet()
    {
        return "magnet:?xt=urn:btih:{$this->info_hash}&tr=" . urlencode($this->announcer) . "&dn=" . rawurlencode($this->title);
    }

    public static function statsByCategories()
    {
        return \Cache::remember('torrents.stats-by-categories', 15, function () {
            return self::selectRaw('category_id, COUNT(*) as total')
                ->groupBy('category_id')
                ->get()
                ->pluck('total', 'category_id');
        });
    }
}
{'fraction_non_alphanumeric': 0.14691022153128644, 'fraction_numerical': 0.011659541391371939, 'mean_word_length': 2.804138950480414, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, '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': '1724300', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1698, 'n_words': 369}