Skip to content

Commit 0b0fa87

Browse files
committed
Update AdminController
1 parent b97e479 commit 0b0fa87

2 files changed

Lines changed: 41 additions & 34 deletions

File tree

app/Console/Commands/InstanceStatsCollectorCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Models\Report;
1111
use App\Models\Video;
1212
use Illuminate\Console\Command;
13+
use Illuminate\Support\Facades\Cache;
1314

1415
class InstanceStatsCollectorCommand extends Command
1516
{
@@ -56,6 +57,10 @@ public function handle()
5657
$count++;
5758
}
5859

60+
if ($count) {
61+
Cache::forget('loops:admin:api:instance-stats');
62+
}
63+
5964
$this->info("Collected stats for {$count} instances");
6065
}
6166
}

app/Http/Controllers/Api/AdminController.php

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -930,40 +930,42 @@ public function instanceSuspend(Request $request, $id)
930930

931931
public function instanceStats(Request $request)
932932
{
933-
$res = [
934-
[
935-
'name' => 'Total',
936-
'value' => Instance::active()->count(),
937-
],
938-
[
939-
'name' => 'New',
940-
'value' => Instance::active()->where('created_at', '>', now()->subHours(24))->count(),
941-
],
942-
[
943-
'name' => 'Blocked',
944-
'value' => Instance::whereFederationState(2)->count(),
945-
],
946-
[
947-
'name' => 'Users',
948-
'value' => Instance::active()->sum('user_count'),
949-
],
950-
[
951-
'name' => 'Videos',
952-
'value' => Instance::active()->sum('video_count'),
953-
],
954-
[
955-
'name' => 'Followers',
956-
'value' => Instance::active()->sum('follower_count'),
957-
],
958-
[
959-
'name' => 'Following',
960-
'value' => Instance::active()->sum('following_count'),
961-
],
962-
[
963-
'name' => 'Comments',
964-
'value' => Instance::active()->sum('comment_count'),
965-
],
966-
];
933+
$res = Cache::remember('loops:admin:api:instance-stats', now()->addHours(4), function () {
934+
return [
935+
[
936+
'name' => 'Total',
937+
'value' => Instance::active()->count(),
938+
],
939+
[
940+
'name' => 'New',
941+
'value' => Instance::active()->where('created_at', '>', now()->subHours(24))->count(),
942+
],
943+
[
944+
'name' => 'Blocked',
945+
'value' => Instance::whereFederationState(2)->count(),
946+
],
947+
[
948+
'name' => 'Users',
949+
'value' => Instance::active()->sum('user_count'),
950+
],
951+
[
952+
'name' => 'Videos',
953+
'value' => Instance::active()->sum('video_count'),
954+
],
955+
[
956+
'name' => 'Followers',
957+
'value' => Instance::active()->sum('follower_count'),
958+
],
959+
[
960+
'name' => 'Following',
961+
'value' => Instance::active()->sum('following_count'),
962+
],
963+
[
964+
'name' => 'Comments',
965+
'value' => Instance::active()->sum('comment_count'),
966+
],
967+
];
968+
});
967969

968970
return $this->data($res);
969971
}

0 commit comments

Comments
 (0)