Skip to content

Commit 7499f81

Browse files
committed
Add InstanceService
1 parent 0b0fa87 commit 7499f81

3 files changed

Lines changed: 113 additions & 62 deletions

File tree

app/Console/Commands/InstanceStatsCollectorCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use App\Models\Profile;
1010
use App\Models\Report;
1111
use App\Models\Video;
12+
use App\Services\InstanceService;
1213
use Illuminate\Console\Command;
13-
use Illuminate\Support\Facades\Cache;
1414

1515
class InstanceStatsCollectorCommand extends Command
1616
{
@@ -58,7 +58,7 @@ public function handle()
5858
}
5959

6060
if ($count) {
61-
Cache::forget('loops:admin:api:instance-stats');
61+
app(InstanceService::class)->flushStats();
6262
}
6363

6464
$this->info("Collected stats for {$count} instances");

app/Http/Controllers/Api/AdminController.php

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use App\Services\AccountSuggestionService;
3030
use App\Services\AdminAuditLogService;
3131
use App\Services\ExploreService;
32+
use App\Services\InstanceService;
3233
use App\Services\NodeinfoCrawlerService;
3334
use App\Services\SanitizeService;
3435
use App\Services\VersionCheckService;
@@ -881,6 +882,7 @@ public function updateInstanceSettings(Request $request, $id)
881882
$instance->update($validated);
882883

883884
app(AdminAuditLogService::class)->logInstanceUpdateSettings($request->user(), $instance, ['old' => $oldValues, 'new' => $validated]);
885+
app(InstanceService::class)->flushStats();
884886

885887
return $this->success();
886888
}
@@ -898,6 +900,7 @@ public function updateInstanceRefreshData(Request $request, $id)
898900
$instance->save();
899901

900902
FetchInstanceNodeinfo::dispatch($instance)->onQueue('actor-update');
903+
app(InstanceService::class)->flushStats();
901904

902905
return $this->success();
903906
}
@@ -911,6 +914,7 @@ public function instanceActivate(Request $request, $id)
911914

912915
app(AdminAuditLogService::class)->logInstanceActivated($request->user(), $instance);
913916
app(SanitizeService::class)->getBannedDomains(true);
917+
app(InstanceService::class)->flushStats();
914918

915919
return $this->success();
916920
}
@@ -924,79 +928,23 @@ public function instanceSuspend(Request $request, $id)
924928

925929
app(AdminAuditLogService::class)->logInstanceSuspended($request->user(), $instance);
926930
app(SanitizeService::class)->getBannedDomains(true);
931+
app(InstanceService::class)->flushStats();
927932

928933
return $this->success();
929934
}
930935

931936
public function instanceStats(Request $request)
932937
{
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-
});
938+
$res = app(InstanceService::class)->getStats();
969939

970940
return $this->data($res);
971941
}
972942

973943
public function instanceAdvancedStats()
974944
{
975-
$totalInstances = Instance::active()->count();
976-
$activeInstances = Instance::active()->count();
977-
$allowedVideoPosts = Instance::active()->where('allow_video_posts', true)->count();
978-
$allowedInFyf = Instance::active()->where('allow_videos_in_fyf', true)->count();
979-
980-
$softwareStats = Instance::select('software')
981-
->selectRaw('COUNT(*) as count')
982-
->selectRaw('SUM(CASE WHEN allow_video_posts = 1 THEN 1 ELSE 0 END) as allow_video_posts_count')
983-
->where('is_blocked', false)
984-
->active()
985-
->groupBy('software')
986-
->orderByDesc('count')
987-
->get();
945+
$res = app(InstanceService::class)->getAdvancedStats();
988946

989-
return response()->json([
990-
'data' => [
991-
'stats' => [
992-
['name' => 'Total Instances', 'value' => $totalInstances],
993-
['name' => 'Active Instances', 'value' => $activeInstances],
994-
['name' => 'Video Posts Allowed', 'value' => $allowedVideoPosts],
995-
['name' => 'FYF Allowed', 'value' => $allowedInFyf],
996-
],
997-
'software_stats' => $softwareStats,
998-
],
999-
]);
947+
return response()->json($res);
1000948
}
1001949

1002950
public function manageInstanceToggleBySoftware(Request $request)
@@ -1012,6 +960,7 @@ public function manageInstanceToggleBySoftware(Request $request)
1012960
->update(['allow_video_posts' => $request->allow_video_posts]);
1013961

1014962
app(AdminAuditLogService::class)->logInstanceSoftwareUpdateAllowVideoPosts($request->user(), ['software' => $software, 'allow_video_posts' => $allowVideoPosts]);
963+
app(InstanceService::class)->flushStats();
1015964

1016965
return response()->json([
1017966
'data' => [
@@ -1036,6 +985,7 @@ public function manageInstanceToggleByDomains(Request $request)
1036985
->update(['allow_video_posts' => $request->allow_video_posts]);
1037986

1038987
app(AdminAuditLogService::class)->logInstanceDomainsUpdateAllowVideoPosts($request->user(), ['domains' => $domains, 'allow_video_posts' => $request->allow_video_posts]);
988+
app(InstanceService::class)->flushStats();
1039989

1040990
return response()->json([
1041991
'data' => [
@@ -1082,6 +1032,7 @@ public function instanceCreate(Request $request)
10821032

10831033
app(AdminAuditLogService::class)->logInstanceDomainAdded($request->user(), $instance);
10841034
app(SanitizeService::class)->getBannedDomains(true);
1035+
app(InstanceService::class)->flushStats();
10851036

10861037
return $this->success();
10871038
}

app/Services/InstanceService.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use App\Models\Instance;
6+
use Illuminate\Support\Facades\Cache;
7+
8+
class InstanceService
9+
{
10+
const CACHE_STATS_KEY = 'loops:admin:api:instance-stats';
11+
12+
const CACHE_ADVANCED_STATS_KEY = 'loops:admin:api:instance-advanced-stats';
13+
14+
public function flushStats()
15+
{
16+
$this->getStats(true);
17+
$this->getAdvancedStats(true);
18+
19+
return 1;
20+
}
21+
22+
public function getStats($flush = false)
23+
{
24+
if ($flush) {
25+
Cache::forget(self::CACHE_STATS_KEY);
26+
}
27+
28+
return Cache::remember(self::CACHE_STATS_KEY, now()->addHours(12), function () {
29+
return [
30+
[
31+
'name' => 'Total',
32+
'value' => Instance::active()->count(),
33+
],
34+
[
35+
'name' => 'New',
36+
'value' => Instance::active()->where('created_at', '>', now()->subHours(24))->count(),
37+
],
38+
[
39+
'name' => 'Blocked',
40+
'value' => Instance::whereFederationState(2)->count(),
41+
],
42+
[
43+
'name' => 'Users',
44+
'value' => Instance::active()->sum('user_count'),
45+
],
46+
[
47+
'name' => 'Videos',
48+
'value' => Instance::active()->sum('video_count'),
49+
],
50+
[
51+
'name' => 'Followers',
52+
'value' => Instance::active()->sum('follower_count'),
53+
],
54+
[
55+
'name' => 'Following',
56+
'value' => Instance::active()->sum('following_count'),
57+
],
58+
[
59+
'name' => 'Comments',
60+
'value' => Instance::active()->sum('comment_count'),
61+
],
62+
];
63+
});
64+
}
65+
66+
public function getAdvancedStats($flush = false)
67+
{
68+
if ($flush) {
69+
Cache::forget(self::CACHE_ADVANCED_STATS_KEY);
70+
}
71+
72+
return Cache::remember(self::CACHE_ADVANCED_STATS_KEY, now()->addDays(7), function () {
73+
$totalInstances = Instance::active()->count();
74+
$activeInstances = Instance::active()->count();
75+
$allowedVideoPosts = Instance::active()->where('allow_video_posts', true)->count();
76+
$allowedInFyf = Instance::active()->where('allow_videos_in_fyf', true)->count();
77+
78+
$softwareStats = Instance::select('software')
79+
->selectRaw('COUNT(*) as count')
80+
->selectRaw('SUM(CASE WHEN allow_video_posts = 1 THEN 1 ELSE 0 END) as allow_video_posts_count')
81+
->where('is_blocked', false)
82+
->active()
83+
->groupBy('software')
84+
->orderByDesc('count')
85+
->get();
86+
87+
return [
88+
'data' => [
89+
'stats' => [
90+
['name' => 'Total Instances', 'value' => $totalInstances],
91+
['name' => 'Active Instances', 'value' => $activeInstances],
92+
['name' => 'Video Posts Allowed', 'value' => $allowedVideoPosts],
93+
['name' => 'FYF Allowed', 'value' => $allowedInFyf],
94+
],
95+
'software_stats' => $softwareStats,
96+
],
97+
];
98+
});
99+
}
100+
}

0 commit comments

Comments
 (0)