Skip to content
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

[back][front] perf: stats API can be filtered by poll #1830

Merged
merged 9 commits into from
Jan 18, 2024

Conversation

GresilleSiffle
Copy link
Collaborator

@GresilleSiffle GresilleSiffle commented Nov 7, 2023

related issues #1829


Description

This PR adds a query parameter to the /stats/ API that allows to retrieve the stats of a single poll, instead of all at once.

Checklist

  • I added the related issue(s) id in the related issues section (if any)
    • if not, delete the related issues section
  • I described my changes and my decisions in the PR description
  • I read the development guidelines of the CONTRIBUTING.md
  • The tests pass and have been updated if relevant
  • The code quality check pass

This will reduce the amount of SQL queries when only
the stats of a single poll are required.
@GresilleSiffle GresilleSiffle changed the title [back][front] perf: stats API can be filtered by poll WIP / [back][front] perf: stats API can be filtered by poll Nov 7, 2023
@GresilleSiffle GresilleSiffle changed the title WIP / [back][front] perf: stats API can be filtered by poll [back][front] perf: stats API can be filtered by poll Nov 9, 2023
@GresilleSiffle GresilleSiffle self-assigned this Nov 9, 2023
@GresilleSiffle GresilleSiffle added the Backend Back-end code of Tournesol label Nov 10, 2023
@GresilleSiffle GresilleSiffle marked this pull request as ready for review November 10, 2023 15:09
Comment on lines 63 to 65
if (
stats.polls.length === 0 ||
currentTime - lastRefreshAt.current >= EXPIRATION_TIME
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a condition to force refresh when current poll is not present in stats.polls ?

Copy link
Collaborator Author

@GresilleSiffle GresilleSiffle Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by 9df3994

I finally opted for a ref, instead of checking if the poll was present in stats.polls. This allows to handle the specific case where getStats is called two times in a row in less than 4 seconds, a first time with a single poll, and a second time with no poll.

Comment on lines 60 to 62
if (loading.current) {
return stats;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some cases, this means getStats() could return stats about the wrong poll, because a concurrent request is running. This is probably not a problem for now, as there is a single one active poll, but it would be worth adding a FIXME.

Suggested change
if (loading.current) {
return stats;
}
// FIXME: this function may return stats about the wrong poll here.
// Maybe `useStats()` should always depend on `useCurrentPoll()` to simplify this logic.
if (loading.current) {
return stats;
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by 094f030

@GresilleSiffle GresilleSiffle marked this pull request as draft November 28, 2023 08:53
@GresilleSiffle GresilleSiffle marked this pull request as ready for review January 8, 2024 09:47
@GresilleSiffle GresilleSiffle merged commit e3dd3e9 into main Jan 18, 2024
7 checks passed
@GresilleSiffle GresilleSiffle deleted the 1829-add_poll_filter_to_stats branch January 18, 2024 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend Back-end code of Tournesol
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants