diff --git a/app/Console/Commands/Stats.php b/app/Console/Commands/Stats.php index b6d28ee..09981e8 100644 --- a/app/Console/Commands/Stats.php +++ b/app/Console/Commands/Stats.php @@ -2,6 +2,7 @@ namespace App\Console\Commands; +use App\Knockout\Stat; use Illuminate\Console\Command; class Stats extends Command { @@ -11,7 +12,12 @@ class Stats extends Command { public function handle() { - echo 'wolo'; + try { + Stat::updateAll(); + echo '[ OK ] Stats cached' . PHP_EOL; + } catch (Exception $e) { + echo '[FAIL] Stats cached' . PHP_EOL; + } } } diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php index 2f2102b..c946556 100644 --- a/app/Http/Controllers/IndexController.php +++ b/app/Http/Controllers/IndexController.php @@ -2,12 +2,12 @@ namespace App\Http\Controllers; -use Illuminate\Http\Request; use App\Knockout\Subforum; +use App\Knockout\Stat; class IndexController extends Controller { - public function get(Request $request) + public function get() { $subforums = Subforum::all(); $records = $subforums->getRecords(); @@ -18,7 +18,8 @@ class IndexController extends Controller { return view('page/index', [ 'leftSubforums' => $leftSubforums, - 'rightSubforums' => $rightSubforums + 'rightSubforums' => $rightSubforums, + 'stat' => Stat::all()->getRecord() ]); } diff --git a/app/Knockout/Dataset.php b/app/Knockout/Dataset.php index 1328bf8..959bba5 100644 --- a/app/Knockout/Dataset.php +++ b/app/Knockout/Dataset.php @@ -13,6 +13,24 @@ class Dataset private $currentPage = 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) { if (!is_array($records)) $records = [$records]; diff --git a/app/Knockout/Event.php b/app/Knockout/Event.php index ce35538..92f4293 100644 --- a/app/Knockout/Event.php +++ b/app/Knockout/Event.php @@ -52,7 +52,7 @@ class Event { public static function all(): Dataset { return Cache::get('events', function() { - return self::requestAll(); + return self::updateAll(); }); } diff --git a/app/Knockout/Stat.php b/app/Knockout/Stat.php new file mode 100644 index 0000000..4867af1 --- /dev/null +++ b/app/Knockout/Stat.php @@ -0,0 +1,48 @@ +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(); + }); + } + +} diff --git a/app/Knockout/Subforum.php b/app/Knockout/Subforum.php index 37bee4f..2d77cd5 100644 --- a/app/Knockout/Subforum.php +++ b/app/Knockout/Subforum.php @@ -54,7 +54,7 @@ class Subforum { public static function all(): Dataset { return Cache::get('subforums', function() { - return self::requestAll(); + return self::updateAll(); }); } diff --git a/resources/views/page/index.blade.php b/resources/views/page/index.blade.php index 8bd7d25..337b667 100644 --- a/resources/views/page/index.blade.php +++ b/resources/views/page/index.blade.php @@ -21,10 +21,10 @@
- Users: 11682 - Threads: 19684 - Posts: 655765 - Ratings: 9013296 + Users: {{ $stat->users }} + Threads: {{ $stat->threads }} + Posts: {{ $stat->posts }} + Ratings: {{ $stat->ratings }}
@endsection