@@ -2,6 +2,7 @@ | |||||
namespace App\Console\Commands; | namespace App\Console\Commands; | ||||
use App\Knockout\Stat; | |||||
use Illuminate\Console\Command; | use Illuminate\Console\Command; | ||||
class Stats extends Command { | class Stats extends Command { | ||||
@@ -11,7 +12,12 @@ class Stats extends Command { | |||||
public function handle() | public function handle() | ||||
{ | { | ||||
echo 'wolo'; | |||||
try { | |||||
Stat::updateAll(); | |||||
echo '[ OK ] Stats cached' . PHP_EOL; | |||||
} catch (Exception $e) { | |||||
echo '[FAIL] Stats cached' . PHP_EOL; | |||||
} | |||||
} | } | ||||
} | } |
@@ -2,12 +2,12 @@ | |||||
namespace App\Http\Controllers; | namespace App\Http\Controllers; | ||||
use Illuminate\Http\Request; | |||||
use App\Knockout\Subforum; | use App\Knockout\Subforum; | ||||
use App\Knockout\Stat; | |||||
class IndexController extends Controller { | class IndexController extends Controller { | ||||
public function get(Request $request) | |||||
public function get() | |||||
{ | { | ||||
$subforums = Subforum::all(); | $subforums = Subforum::all(); | ||||
$records = $subforums->getRecords(); | $records = $subforums->getRecords(); | ||||
@@ -18,7 +18,8 @@ class IndexController extends Controller { | |||||
return view('page/index', [ | return view('page/index', [ | ||||
'leftSubforums' => $leftSubforums, | 'leftSubforums' => $leftSubforums, | ||||
'rightSubforums' => $rightSubforums | |||||
'rightSubforums' => $rightSubforums, | |||||
'stat' => Stat::all()->getRecord() | |||||
]); | ]); | ||||
} | } | ||||
@@ -13,6 +13,24 @@ class Dataset | |||||
private $currentPage = null; | private $currentPage = null; | ||||
private $recordsPerPage = null; | private $recordsPerPage = null; | ||||
public function __serialize(): array | |||||
{ | |||||
return [ | |||||
'records' => $this->records, | |||||
'totalRecords' => $this->totalRecords, | |||||
'currentPage' => $this->currentPage, | |||||
'recordsPerPage' => $this->recordsPerPage | |||||
]; | |||||
} | |||||
public function __unserialize(array $data): void | |||||
{ | |||||
$this->records = $data['records'] ?? []; | |||||
$this->totalRecords = $data['totalRecords'] ?? null; | |||||
$this->currentPage = $data['currentPage'] ?? null; | |||||
$this->recordsPerPage = $data['recordsPerPage'] ?? null; | |||||
} | |||||
public function __construct($records) | public function __construct($records) | ||||
{ | { | ||||
if (!is_array($records)) $records = [$records]; | if (!is_array($records)) $records = [$records]; | ||||
@@ -52,7 +52,7 @@ class Event { | |||||
public static function all(): Dataset | public static function all(): Dataset | ||||
{ | { | ||||
return Cache::get('events', function() { | return Cache::get('events', function() { | ||||
return self::requestAll(); | |||||
return self::updateAll(); | |||||
}); | }); | ||||
} | } | ||||
@@ -0,0 +1,48 @@ | |||||
<?php | |||||
namespace App\Knockout; | |||||
use Illuminate\Support\Facades\Cache; | |||||
class Stat { | |||||
public $users; | |||||
public $posts; | |||||
public $threads; | |||||
public $ratings; | |||||
public static function unwrap($stat) | |||||
{ | |||||
$s = new self(); | |||||
$s->users = $stat->userCount; | |||||
$s->posts = $stat->postCount; | |||||
$s->threads = $stat->threadCount; | |||||
$s->ratings = $stat->ratingsCount; | |||||
return $s; | |||||
} | |||||
private static function requestAll(): Dataset | |||||
{ | |||||
$data = (new AbstractData)->httpGet('/stats'); | |||||
$json = json_decode($data); | |||||
$record = self::unwrap($json); | |||||
return (new Dataset($record)); | |||||
} | |||||
public static function updateAll(): Dataset | |||||
{ | |||||
$data = self::requestAll(); | |||||
Cache::put('stats', $data, 7200); | |||||
return $data; | |||||
} | |||||
public static function all(): Dataset | |||||
{ | |||||
return Cache::get('stats', function() { | |||||
return self::updateAll(); | |||||
}); | |||||
} | |||||
} |
@@ -54,7 +54,7 @@ class Subforum { | |||||
public static function all(): Dataset | public static function all(): Dataset | ||||
{ | { | ||||
return Cache::get('subforums', function() { | return Cache::get('subforums', function() { | ||||
return self::requestAll(); | |||||
return self::updateAll(); | |||||
}); | }); | ||||
} | } | ||||
@@ -21,10 +21,10 @@ | |||||
</div> | </div> | ||||
<div class="clearfix"></div> | <div class="clearfix"></div> | ||||
<div class="row stats"> | <div class="row stats"> | ||||
<span>Users: 11682</span> | |||||
<span>Threads: 19684</span> | |||||
<span>Posts: 655765</span> | |||||
<span>Ratings: 9013296</span> | |||||
<span>Users: {{ $stat->users }}</span> | |||||
<span>Threads: {{ $stat->threads }}</span> | |||||
<span>Posts: {{ $stat->posts }}</span> | |||||
<span>Ratings: {{ $stat->ratings }}</span> | |||||
</div> | </div> | ||||
@endsection | @endsection | ||||