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

Your Stats makes an expensive GET request on every classification #6588

Open
eatyourgreens opened this issue Dec 19, 2024 · 6 comments · May be fixed by #6589
Open

Your Stats makes an expensive GET request on every classification #6588

eatyourgreens opened this issue Dec 19, 2024 · 6 comments · May be fixed by #6589
Labels
bug Something isn't working

Comments

@eatyourgreens
Copy link
Contributor

eatyourgreens commented Dec 19, 2024

Package

  • app-project

Describe the bug

Every time I press Done now, the Your Stats component makes a request to ERAS that takes ~2s. It's fetching yearly stats for my account, all the way back to 2009, after every new classification.
https://eras.zooniverse.org/classifications/users/149/?end_date=2024-12-19&period=year&start_date=2009-03-25&project_id=8149

You can see the request in the browser network panel. I've highlighted it in Firefox here, where the browser has flagged it as a slow-running request.

Screenshot of the Firefox network panel on a Squirrel Mapper Classify page. The panel is highlighting a stats request that took 2.25s.

To Reproduce

Submit a classification on any project and watch the network requests made by the browser.

Expected behavior

Responses to expensive requests should probably be cached in the browser, or in the React app, and reused eg. the number of classifications I made since 2009 isn't going to change, so doesn't need to be fetched after every classification. You could cache the all-time count when the page first loads, or on visibility change, then simply increment in the browser when the Done button is pressed.

Additional context

@mutabilitie says the stats counts are laggy in this Talk thread. That might be related to the API being slow, or a slow API endpoint being requested too frequently.

What are optimistic updates? explains how to improve responsiveness by showing updates immediately.

useOptimistic in React 19.

@eatyourgreens eatyourgreens added the bug Something isn't working label Dec 19, 2024
@goplayoutside3
Copy link
Contributor

That might be related to the API being slow, or a slow API endpoint being requested too frequently.

Complaints about a slow endpoint with examples should go to the eras repo: https://github.com/zooniverse/eras.

The frontend does what it can to ensure accurate stats displayed to all users. We would welcome a PR on FEM regarding the SWR + mutate strategy to improve the YourProjectStats component.

@eatyourgreens
Copy link
Contributor Author

The frontend does what it can to ensure accurate stats displayed to all users. We would welcome a PR on FEM regarding the SWR + mutate strategy to improve the YourProjectStats component.

If you'd like me to work on this, I'd be happy to do that, but you'll need to get in touch with us and ask Oxford for some of my time.

@eatyourgreens
Copy link
Contributor Author

Complaints about a slow endpoint with examples should go to the eras repo:

The bug here isn't in ERAS (although a faster API would obviously be better.) The frontend is making repeated HTTP requests for data that it already has. That's going to cause performance problems, particularly when the number of incoming classifications is high.

@eatyourgreens
Copy link
Contributor Author

I did take a quick look at the code. This line, which runs after every classification, fetches the user stats from eras.zooniverse.org when the useSWR hook runs.

@eatyourgreens
Copy link
Contributor Author

useSWR there is:

  1. fetching your weekly and all-time project stats from ERAS.
  2. generating a mutate function to update those fetched counts.

The problem is, that code is fetching counts that are already displayed in the Your Stats box.

@eatyourgreens
Copy link
Contributor Author

eatyourgreens commented Dec 20, 2024

#6589 is a very quick PR that shows you how to update the stats with mutate, without rerunning the SWR query every time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants