Skip to content

AO3-3730 series stats - #5288

Closed
anmazz wants to merge 27 commits into
otwcode:masterfrom
anmazz:AO3-3730--series-stats
Closed

anmazz wants to merge 27 commits into
otwcode:masterfrom
anmazz:AO3-3730--series-stats

Conversation

@anmazz

@anmazz anmazz commented Jul 26, 2025 •

Copy link
Copy Markdown
Contributor

Pull Request Checklist

Issue

https://otwarchive.atlassian.net/browse/AO3-3730

Purpose

I'm opening this as a draft so I can get feedback on my general approach/design decisions. This will hopefully have a lot of discussion if/when people can review.

Added:

  • Series stats that appear/sort in the same listing as work stats
  • Series stats include Work Count, Bookmarks, and Subscriptions
  • Bookmarks and Subscriptions are sortable among both Series and Works
  • Anything specific to works (kudos, hits, word count) gets set to 0 (NULL when pulled from the db but set to 0 when mapped to the StatItem object -> open to making this get 0 directly from the query)
  • Adds new "Types" filter to stats page which groups by Work and Series (retains Fandoms and Flat view)

Design approach
I began trying to use ActiveRecord but realized that was probably not the best way to go about this. This SQL is pretty big, but I've tried to evolve it so that it's easier to read with the CTEs and comments. That being said, it's still a lot, so sorry 😬.

My main goal was to do a union between the series and work stats and project them onto a single, standard object for ease of sorting. I felt the raw SQL was the best and most performant option for this since it gives me fine-grain control over selects/aggregates, doesn't hydrate a bunch of ActiveRecord entries, and avoids N+1 queries altogether. I've also tried as much as possible to limit Cartesian explosion.

Things to consider

  • I've left a few comments in the code itself. One big question I had was whether or not to use stat_counters for Works as my table for kudos/bookmarks, or retrieve them from kudos/bookmarks directly?
  • I'm not sure what db prod uses. This syntax might not work if it's not the same as local (MariaDB's... interesting)
  • I've tested locally, but not incredibly thoroughly. I've made a bunch of rspec tests but I think I should continue adding more. Wanted to get more eyes on this in case design direction was changed.

There's a bunch of commented stuff/puts statements/linting errors/failed tests galore that I'll clean up later so apologies for that. Open to feedback on any and all parts of this PR!

Testing Instructions

Will fill out later.

References

Are there other relevant issues/pull requests/mailing list discussions? If not, you can remove this section.

Credit

anna (she/her)

Comment thread app/controllers/stats_controller.rb Fixed
Comment thread app/helpers/stats_helper.rb Fixed
@anmazz anmazz changed the title AO3-3730 series stats [DRAFT] AO3-3730 series stats Jul 28, 2025
@anmazz
anmazz marked this pull request as ready for review July 28, 2025 04:11
@Bilka2

Bilka2 commented Aug 4, 2025

Copy link
Copy Markdown
Contributor

We would prefer if this used ActiveRecord instead of such a large amount of handwritten SQL.

@anmazz

anmazz commented Aug 7, 2025

Copy link
Copy Markdown
Contributor Author

#5304

@anmazz anmazz closed this Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants