-
Notifications
You must be signed in to change notification settings - Fork 427
(Add) Extended user statistics to API endpoint #5181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
(Add) Extended user statistics to API endpoint #5181
Conversation
PR Summary
|
Roardom
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good PR. I'm currently working on a more refined compartmentalized framework for api usage/keys and will consider this PR as soon as that framework is in place.
| $bonusUploaded = BonTransactions::query() | ||
| ->where('sender_id', '=', $user->id) | ||
| ->where('name', 'like', '%Upload%') | ||
| ->sum('cost'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $bonusUploaded = BonTransactions::query() | |
| ->where('sender_id', '=', $user->id) | |
| ->where('name', 'like', '%Upload%') | |
| ->sum('cost'); | |
| $bonusUploaded = BonTransactions::query() | |
| ->where('sender_id', '=', $user->id) | |
| ->where('name', 'like', '%Upload%') | |
| ->sum('cost'); |
Wildcard searches shouldn't be permitted in the api.
| $history = DB::table('history') | ||
| ->where('user_id', '=', $user->id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $history = DB::table('history') | |
| ->where('user_id', '=', $user->id) | |
| $history = History::query() | |
| ->withTrashed() | |
| ->where('user_id', '=', $user->id) |
app/Http/Resources/UserResource.php
Outdated
| 'real_uploaded' => str_replace("\u{00A0}", ' ', StringHelper::formatBytes((int) ($this->history?->upload_sum ?? 0), 2)), | ||
| 'real_downloaded' => str_replace("\u{00A0}", ' ', StringHelper::formatBytes((int) ($this->history?->download_sum ?? 0), 2)), | ||
| 'credited_uploaded' => str_replace("\u{00A0}", ' ', StringHelper::formatBytes((int) ($this->history?->credited_upload_sum ?? 0), 2)), | ||
| 'credited_downloaded' => str_replace("\u{00A0}", ' ', StringHelper::formatBytes((int) ($this->history?->credited_download_sum ?? 0), 2)), | ||
| 'average_seedtime' => StringHelper::timeElapsed($avgSeedtime), | ||
| 'seeding_size' => str_replace("\u{00A0}", ' ', StringHelper::formatBytes($this->seedingSize, 2)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be raw values, not human ones.
app/Http/Resources/UserResource.php
Outdated
| 'fl_tokens' => $this->fl_tokens, | ||
| 'uploads_count' => $this->uploadsCount, | ||
| 'downloads_count' => (int) ($this->history?->download_count ?? 0), | ||
| 'bonus_uploaded' => str_replace("\u{00A0}", ' ', StringHelper::formatBytes($this->bonusUploaded, 2)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
app/Http/Resources/UserResource.php
Outdated
| $avgSeedtime = $this->history?->count > 0 | ||
| ? (int) (($this->history->seedtime_sum ?? 0) / $this->history->count) | ||
| : 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to use resource loading instead of manual db queries and passing them through: https://laravel.com/docs/12.x/eloquent-resources#conditional-relationships
- Use History::query()->withTrashed() instead of DB facade
- Replace wildcard LIKE with whereRelation('exchange', 'upload', true)
- Return raw integers instead of formatted strings
- Remove custom constructor, use standard resource pattern
Roardom
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My review at this point is still the same:
This is a good PR. I'm currently working on a more refined compartmentalized framework for api usage/keys and will consider this PR as soon as that framework is in place.
Summary
Extends the
/api/userendpoint with additional statistics already available on the profile page:real_uploaded/real_downloaded- actual transfer amountscredited_uploaded/credited_downloaded- with bonus multipliers appliedaverage_seedtime- average seed time per torrentseeding_size- total size of torrents currently seedingfl_tokens- freeleech tokensuploads_count/downloads_count- torrent countsbonus_uploaded- upload purchased with bonus pointsChanges
app/Http/Controllers/API/UserController.php- added history aggregation queryapp/Http/Resources/UserResource.php- added new fields to responsebook/src/torrent_api.md- updated API documentation