From c0076ab035afbf3764bac0eaf7155157db253b47 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Tue, 12 Jan 2021 21:50:45 -0700 Subject: [PATCH 1/4] Add snowflake id service --- app/Services/SnowflakeService.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 app/Services/SnowflakeService.php diff --git a/app/Services/SnowflakeService.php b/app/Services/SnowflakeService.php new file mode 100644 index 00000000..b04c56b4 --- /dev/null +++ b/app/Services/SnowflakeService.php @@ -0,0 +1,18 @@ +parse($ts)->timestamp : microtime(true); + return ((round($ts * 1000) - 1549756800000) << 22) + | (1 << 17) + | (1 << 12) + | 0; + } + +} \ No newline at end of file From d8d3331f3f2b40b3b0a7ba9bddd12ef09c06a3c4 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Tue, 12 Jan 2021 21:52:07 -0700 Subject: [PATCH 2/4] Update DiscoverController, improve trending api performance --- app/Http/Controllers/DiscoverController.php | 55 ++++++++++++--------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/app/Http/Controllers/DiscoverController.php b/app/Http/Controllers/DiscoverController.php index dbad2f3f..c94935f0 100644 --- a/app/Http/Controllers/DiscoverController.php +++ b/app/Http/Controllers/DiscoverController.php @@ -22,6 +22,8 @@ use League\Fractal; use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Pagination\IlluminatePaginatorAdapter; use App\Services\StatusHashtagService; +use App\Services\SnowflakeService; +use App\Services\StatusService; class DiscoverController extends Controller { @@ -173,33 +175,40 @@ class DiscoverController extends Controller 'range' => 'nullable|string|in:daily,monthly' ]); - $range = $request->filled('range') ? - $request->input('range') == 'alltime' ? '-1' : - ($request->input('range') == 'daily' ? 1 : 31) : 1; + $range = $request->input('range') == 'monthly' ? 31 : 1; - $key = ':api:discover:trending:v2.1:range:' . $range; - $ttl = now()->addHours(2); - $res = Cache::remember($key, $ttl, function() use($range) { - if($range == '-1') { - $res = Status::whereScope('public') - ->whereIn('type', ['photo', 'photo:album', 'video']) + $key = ':api:discover:trending:v2.8:range:' . $range; + $ttl = now()->addMinutes(15); + + $ids = Cache::remember($key, $ttl, function() use($range) { + $days = $range == 1 ? 2 : 31; + $min_id = SnowflakeService::byDate(now()->subDays($days)); + return Status::select( + 'id', + 'scope', + 'type', + 'is_nsfw', + 'likes_count', + 'created_at' + ) + ->where('id', '>', $min_id) + ->whereScope('public') + ->whereIn('type', [ + 'photo', + 'photo:album', + 'video' + ]) ->whereIsNsfw(false) ->orderBy('likes_count','desc') - ->take(12) - ->get(); - } else { - $res = Status::whereScope('public') - ->whereIn('type', ['photo', 'photo:album', 'video']) - ->whereIsNsfw(false) - ->orderBy('likes_count','desc') - ->take(12) - ->where('created_at', '>', now()->subDays($range)) - ->get(); - } - $resource = new Fractal\Resource\Collection($res, new StatusStatelessTransformer()); - return $this->fractal->createData($resource)->toArray(); + ->take(15) + ->pluck('id'); }); - return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); + + $res = $ids->map(function($s) { + return StatusService::get($s); + }); + + return response()->json($res); } public function trendingHashtags(Request $request) From 53322a9378d9d4b87374a2649db5fd560bb8d7d8 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Tue, 12 Jan 2021 21:52:53 -0700 Subject: [PATCH 3/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 217bd560..064ddd2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -154,6 +154,7 @@ - Updated StatusController, fix scope bug. ([7dc3739c](https://github.com/pixelfed/pixelfed/commit/7dc3739c)) - Updated AP helpers, fixed federation bug. ([a52564f3](https://github.com/pixelfed/pixelfed/commit/a52564f3)) - Updated Helpers, cache profiles. ([1f672ecf](https://github.com/pixelfed/pixelfed/commit/1f672ecf)) +- Updated DiscoverController, improve trending api performance. ([d8d3331f](https://github.com/pixelfed/pixelfed/commit/d8d3331f)) ## [v0.10.9 (2020-04-17)](https://github.com/pixelfed/pixelfed/compare/v0.10.8...v0.10.9) ### Added From 74ba326adb3941282d894551d587447b747920f8 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Tue, 12 Jan 2021 22:04:25 -0700 Subject: [PATCH 4/4] Update DiscoverController, limit trending api to local posts --- app/Http/Controllers/DiscoverController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/DiscoverController.php b/app/Http/Controllers/DiscoverController.php index c94935f0..d8e4e7f2 100644 --- a/app/Http/Controllers/DiscoverController.php +++ b/app/Http/Controllers/DiscoverController.php @@ -192,6 +192,7 @@ class DiscoverController extends Controller 'created_at' ) ->where('id', '>', $min_id) + ->whereNull('uri') ->whereScope('public') ->whereIn('type', [ 'photo',