InstanceManager.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Instance;
  5. use App\Profile;
  6. use App\Services\InstanceService;
  7. use App\Jobs\InstancePipeline\FetchNodeinfoPipeline;
  8. use function Laravel\Prompts\select;
  9. use function Laravel\Prompts\confirm;
  10. use function Laravel\Prompts\progress;
  11. use function Laravel\Prompts\search;
  12. use function Laravel\Prompts\table;
  13. class InstanceManager extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'app:instance-manager';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = 'Manage Instances';
  27. /**
  28. * Execute the console command.
  29. */
  30. public function handle()
  31. {
  32. $action = select(
  33. 'What action do you want to perform?',
  34. [
  35. 'Recalculate Stats',
  36. 'Ban Instance',
  37. 'Unlist Instance',
  38. 'Unlisted Instances',
  39. 'Banned Instances',
  40. 'Unban Instance',
  41. 'Relist Instance',
  42. ],
  43. );
  44. switch($action) {
  45. case 'Recalculate Stats':
  46. return $this->recalculateStats();
  47. break;
  48. case 'Unlisted Instances':
  49. return $this->viewUnlistedInstances();
  50. break;
  51. case 'Banned Instances':
  52. return $this->viewBannedInstances();
  53. break;
  54. case 'Unlist Instance':
  55. return $this->unlistInstance();
  56. break;
  57. case 'Ban Instance':
  58. return $this->banInstance();
  59. break;
  60. case 'Unban Instance':
  61. return $this->unbanInstance();
  62. break;
  63. case 'Relist Instance':
  64. return $this->relistInstance();
  65. break;
  66. }
  67. }
  68. protected function recalculateStats()
  69. {
  70. $instanceCount = Instance::count();
  71. $confirmed = confirm('Do you want to recalculate stats for all ' . $instanceCount . ' instances?');
  72. if(!$confirmed) {
  73. $this->error('Aborting...');
  74. exit;
  75. }
  76. $users = progress(
  77. label: 'Updating instance stats...',
  78. steps: Instance::all(),
  79. callback: fn ($instance) => $this->updateInstanceStats($instance),
  80. );
  81. }
  82. protected function updateInstanceStats($instance)
  83. {
  84. FetchNodeinfoPipeline::dispatch($instance)->onQueue('intbg');
  85. }
  86. protected function unlistInstance()
  87. {
  88. $id = search(
  89. 'Search by domain',
  90. fn (string $value) => strlen($value) > 0
  91. ? Instance::whereUnlisted(false)->where('domain', 'like', "%{$value}%")->pluck('domain', 'id')->all()
  92. : []
  93. );
  94. $instance = Instance::find($id);
  95. if(!$instance) {
  96. $this->error('Oops, an error occured');
  97. exit;
  98. }
  99. $tbl = [
  100. [
  101. $instance->domain,
  102. number_format($instance->status_count),
  103. number_format($instance->user_count),
  104. ]
  105. ];
  106. table(
  107. ['Domain', 'Status Count', 'User Count'],
  108. $tbl
  109. );
  110. $confirmed = confirm('Are you sure you want to unlist this instance?');
  111. if(!$confirmed) {
  112. $this->error('Aborting instance unlisting');
  113. exit;
  114. }
  115. $instance->unlisted = true;
  116. $instance->save();
  117. InstanceService::refresh();
  118. $this->info('Successfully unlisted ' . $instance->domain . '!');
  119. exit;
  120. }
  121. protected function relistInstance()
  122. {
  123. $id = search(
  124. 'Search by domain',
  125. fn (string $value) => strlen($value) > 0
  126. ? Instance::whereUnlisted(true)->where('domain', 'like', "%{$value}%")->pluck('domain', 'id')->all()
  127. : []
  128. );
  129. $instance = Instance::find($id);
  130. if(!$instance) {
  131. $this->error('Oops, an error occured');
  132. exit;
  133. }
  134. $tbl = [
  135. [
  136. $instance->domain,
  137. number_format($instance->status_count),
  138. number_format($instance->user_count),
  139. ]
  140. ];
  141. table(
  142. ['Domain', 'Status Count', 'User Count'],
  143. $tbl
  144. );
  145. $confirmed = confirm('Are you sure you want to re-list this instance?');
  146. if(!$confirmed) {
  147. $this->error('Aborting instance re-listing');
  148. exit;
  149. }
  150. $instance->unlisted = false;
  151. $instance->save();
  152. InstanceService::refresh();
  153. $this->info('Successfully re-listed ' . $instance->domain . '!');
  154. exit;
  155. }
  156. protected function banInstance()
  157. {
  158. $id = search(
  159. 'Search by domain',
  160. fn (string $value) => strlen($value) > 0
  161. ? Instance::whereBanned(false)->where('domain', 'like', "%{$value}%")->pluck('domain', 'id')->all()
  162. : []
  163. );
  164. $instance = Instance::find($id);
  165. if(!$instance) {
  166. $this->error('Oops, an error occured');
  167. exit;
  168. }
  169. $tbl = [
  170. [
  171. $instance->domain,
  172. number_format($instance->status_count),
  173. number_format($instance->user_count),
  174. ]
  175. ];
  176. table(
  177. ['Domain', 'Status Count', 'User Count'],
  178. $tbl
  179. );
  180. $confirmed = confirm('Are you sure you want to ban this instance?');
  181. if(!$confirmed) {
  182. $this->error('Aborting instance ban');
  183. exit;
  184. }
  185. $instance->banned = true;
  186. $instance->save();
  187. InstanceService::refresh();
  188. $this->info('Successfully banned ' . $instance->domain . '!');
  189. exit;
  190. }
  191. protected function unbanInstance()
  192. {
  193. $id = search(
  194. 'Search by domain',
  195. fn (string $value) => strlen($value) > 0
  196. ? Instance::whereBanned(true)->where('domain', 'like', "%{$value}%")->pluck('domain', 'id')->all()
  197. : []
  198. );
  199. $instance = Instance::find($id);
  200. if(!$instance) {
  201. $this->error('Oops, an error occured');
  202. exit;
  203. }
  204. $tbl = [
  205. [
  206. $instance->domain,
  207. number_format($instance->status_count),
  208. number_format($instance->user_count),
  209. ]
  210. ];
  211. table(
  212. ['Domain', 'Status Count', 'User Count'],
  213. $tbl
  214. );
  215. $confirmed = confirm('Are you sure you want to unban this instance?');
  216. if(!$confirmed) {
  217. $this->error('Aborting instance unban');
  218. exit;
  219. }
  220. $instance->banned = false;
  221. $instance->save();
  222. InstanceService::refresh();
  223. $this->info('Successfully un-banned ' . $instance->domain . '!');
  224. exit;
  225. }
  226. protected function viewBannedInstances()
  227. {
  228. $data = Instance::whereBanned(true)
  229. ->get(['domain', 'user_count', 'status_count'])
  230. ->map(function($d) {
  231. return [
  232. 'domain' => $d->domain,
  233. 'user_count' => number_format($d->user_count),
  234. 'status_count' => number_format($d->status_count),
  235. ];
  236. })
  237. ->toArray();
  238. table(
  239. ['Domain', 'User Count', 'Status Count'],
  240. $data
  241. );
  242. }
  243. protected function viewUnlistedInstances()
  244. {
  245. $data = Instance::whereUnlisted(true)
  246. ->get(['domain', 'user_count', 'status_count', 'banned'])
  247. ->map(function($d) {
  248. return [
  249. 'domain' => $d->domain,
  250. 'user_count' => number_format($d->user_count),
  251. 'status_count' => number_format($d->status_count),
  252. 'banned' => $d->banned ? '✅' : null
  253. ];
  254. })
  255. ->toArray();
  256. table(
  257. ['Domain', 'User Count', 'Status Count', 'Banned'],
  258. $data
  259. );
  260. }
  261. }