|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
|
8
|
|
|
|
|
9
|
|
|
class ReleaseStat extends Model |
|
10
|
|
|
{ |
|
11
|
|
|
use HasFactory; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
protected $guarded = []; |
|
14
|
|
|
|
|
15
|
|
|
public static function insertRecentlyAdded(): void |
|
16
|
|
|
{ |
|
17
|
|
|
$categories = Category::query() |
|
18
|
|
|
->with('parent') |
|
19
|
|
|
->where('r.adddate', '>', now()->subWeek()) |
|
20
|
|
|
->select([ |
|
21
|
|
|
'categories.id', |
|
22
|
|
|
'root_categories_id', |
|
23
|
|
|
DB::raw('COUNT(r.id) as count'), |
|
24
|
|
|
'categories.title', |
|
25
|
|
|
]) |
|
26
|
|
|
->join('releases as r', 'r.categories_id', '=', 'categories.id') |
|
27
|
|
|
->whereNotIn('categories.id', [10, 20]) // Exclude OTHER_MISC and OTHER_HASHED |
|
28
|
|
|
->groupBy('categories.id', 'root_categories_id', 'categories.title') |
|
29
|
|
|
->orderByDesc('count') |
|
30
|
|
|
->get(); |
|
31
|
|
|
|
|
32
|
|
|
foreach ($categories as $category) { |
|
33
|
|
|
// Build the category display name with root category prefix |
|
34
|
|
|
$categoryDisplay = $category->parent |
|
35
|
|
|
? $category->parent->title.' > '.$category->title |
|
36
|
|
|
: $category->title; |
|
37
|
|
|
|
|
38
|
|
|
// Check if we already have the information and if we do just update the count |
|
39
|
|
|
if (self::query()->where('category', $categoryDisplay)->exists()) { |
|
40
|
|
|
self::query()->where('category', $categoryDisplay)->update(['count' => $category->count]); |
|
41
|
|
|
|
|
42
|
|
|
continue; |
|
43
|
|
|
} |
|
44
|
|
|
self::query()->create(['category' => $categoryDisplay, 'count' => $category->count]); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function getRecentlyAdded(): array |
|
49
|
|
|
{ |
|
50
|
|
|
return self::query()->select(['category', 'count'])->get()->toArray(); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|