1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Models;
7
8use Carbon\Carbon;
9use DB;
10
11/**
12 * @property Build $build
13 * @property int $build_id
14 * @property \Carbon\Carbon $created_at
15 * @property int $id
16 * @property int $user_count
17 */
18class BuildPropagationHistory extends Model
19{
20 public $timestamps = false;
21
22 protected $casts = [
23 'created_at' => 'datetime',
24 ];
25
26 public function build()
27 {
28 return $this->belongsTo(Build::class, 'build_id');
29 }
30
31 public function scopeChangelog($query, $streamId, $days)
32 {
33 $buildsTable = (new Build())->getTable();
34 $propagationTable = (new self())->getTable();
35 $streamsTable = $GLOBALS['cfg']['database']['connections']['mysql-updates']['database'].'.'.(new UpdateStream())->getTable();
36
37 $query->join($buildsTable, "{$buildsTable}.build_id", '=', "{$propagationTable}.build_id")
38 ->select('created_at')
39 ->where('created_at', '>', Carbon::now()->subDays($days))
40 ->orderBy('created_at', 'asc');
41
42 if ($streamId !== null) {
43 $query->addSelect(DB::raw("user_count, {$buildsTable}.version as label"))
44 ->where("{$buildsTable}.stream_id", $streamId);
45 } else {
46 $query->join($streamsTable, "{$streamsTable}.stream_id", '=', "{$buildsTable}.stream_id")
47 // casting to integer here as the sum aggregate returns a string
48 ->addSelect(DB::raw('cast(sum(user_count) as signed) as user_count, pretty_name as label'))
49 ->whereIn("{$buildsTable}.stream_id", $GLOBALS['cfg']['osu']['changelog']['update_streams'])
50 ->groupBy(['created_at', 'pretty_name']);
51 }
52 }
53}