InstanceUpdateTotalLocalPosts.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\ConfigCacheService;
  4. use Cache;
  5. use DB;
  6. use Illuminate\Console\Command;
  7. use Storage;
  8. class InstanceUpdateTotalLocalPosts extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'app:instance-update-total-local-posts';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Update the total number of local statuses/post count';
  22. /**
  23. * Execute the console command.
  24. */
  25. public function handle()
  26. {
  27. $cached = $this->checkForCache();
  28. if (! $cached) {
  29. $this->initCache();
  30. return;
  31. }
  32. $cache = $this->getCached();
  33. if (! $cache || ! isset($cache['count'])) {
  34. $this->error('Problem fetching cache');
  35. return;
  36. }
  37. $this->updateAndCache();
  38. Cache::forget('api:nodeinfo');
  39. }
  40. protected function checkForCache()
  41. {
  42. return Storage::exists('total_local_posts.json');
  43. }
  44. protected function initCache()
  45. {
  46. $res = [
  47. 'count' => $this->getTotalLocalPosts(),
  48. ];
  49. Storage::put('total_local_posts.json', json_encode($res, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
  50. ConfigCacheService::put('instance.stats.total_local_posts', $res['count']);
  51. }
  52. protected function getCached()
  53. {
  54. return Storage::json('total_local_posts.json');
  55. }
  56. protected function updateAndCache()
  57. {
  58. $res = [
  59. 'count' => $this->getTotalLocalPosts(),
  60. ];
  61. Storage::put('total_local_posts.json', json_encode($res, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
  62. ConfigCacheService::put('instance.stats.total_local_posts', $res['count']);
  63. }
  64. protected function getTotalLocalPosts()
  65. {
  66. if ((bool) config('instance.total_count_estimate') && config('database.default') === 'mysql') {
  67. return DB::select("EXPLAIN SELECT COUNT(*) FROM statuses WHERE deleted_at IS NULL AND uri IS NULL and local = 1 AND type != 'share'")[0]->rows;
  68. }
  69. return DB::table('statuses')
  70. ->whereNull('deleted_at')
  71. ->where('local', true)
  72. ->whereNot('type', 'share')
  73. ->count();
  74. }
  75. }