Skip to content

Add a /stats/tags page showing how Book Tags grow over time #14656

Open

Description

@openlibrary-bot

Goal

Add a /stats/tags page that shows how Book Tags are growing over time:

  • how many Tags were created per day, week, month, year and all time
  • how many works carry a Tag, broken down by Tag type (genres, subgenres, literary forms, audiences, content warnings, content formats, awards)

It should look and work like the existing /stats/tasks page, and use the same nightly stats job, so the page itself does no expensive work.

How the existing stats work

A nightly job (openlibrary/admin/stats.py) runs every function in openlibrary/admin/numbers.py whose name starts with admin_range__ or admin_total__, and saves the results as one document per day in the store (counts-YYYY-MM-DD). Stats pages read those daily documents; they never query the big tables directly.

  • admin_range__foo counts what happened that day and is saved under the key foo.
  • admin_total__foo counts the total as of that day and is saved under the key total_foo.

Good examples to copy:

What Where
Count things of one type created in a day admin_range__works = functools.partial(single_thing_skeleton, type="work") in openlibrary/admin/numbers.py
A counter stored as a dict, one entry per field admin_range__tasks_saves_by_field_1d in openlibrary/admin/numbers.py
Reading the daily docs into rows and charts get_rolling_counts in openlibrary/core/admin.py and build_saves_by_field in openlibrary/views/loanstats.py
The page itself the tasks_stats view in openlibrary/views/loanstats.py and openlibrary/templates/stats/tasks.html.jinja
Tests openlibrary/admin/tests/test_numbers.py and openlibrary/plugins/openlibrary/tests/test_stats.py

The commit that added /stats/tasks is a useful walkthrough: 2c49c29.

What to build

1. Tags created per day. One line in numbers.py:

admin_range__tags = functools.partial(single_thing_skeleton, type="tag")

thing.type and thing.created are both indexed, so this is cheap.

2. Works tagged, by Tag type. A new admin_total__tagged_works that returns a dict such as {"genres": 1069, "awards": 0, ...}.

When a work links to a Tag ("genres": [{"key": "/tags/OL167T"}]), Infobase stores one row per link in the work_ref table, keyed by the field's id in the property table. So, for each Tag field on a work:

SELECT p.name, count(DISTINCT r.thing_id) AS works, count(*) AS links
FROM work_ref r JOIN property p ON p.id = r.key_id
WHERE p.type = (SELECT id FROM thing WHERE key = '/type/work')
  AND p.name IN ('genres', 'subgenres', 'literary_forms', 'audiences',
                 'content_warnings', 'content_formats', 'awards')
GROUP BY p.name

work_ref has an index on (key_id, value), so this reads only the matching rows. Plain-text values in those fields (older records store some genres as strings) live in a different table and are correctly left out. Decide whether to store works, links or both, and say which in the PR.

3. The page. /stats/tags shows:

  • Tags created: last 7 days, last 28 days, last 365 days and all time, each with a small trend chart like /stats/tasks
  • Works tagged, one row per Tag type: the current total and the change over the last 7, 28 and 365 days (today's total minus the total that many days ago)

Link it from /admin next to the existing Tasks link (openlibrary/templates/admin/index.html).

Please don't

  • Query transaction_details or other large edit-history tables from the page or from the nightly job. They have no index on date, so any date-range count there scans the whole table.
  • Use Solr for these counts. The nightly job talks to the database.

Done when

  • admin_range__tags and admin_total__tagged_works added, with tests in openlibrary/admin/tests/test_numbers.py
  • /stats/tags renders from the daily documents, with a test like the ones for /stats/tasks
  • Linked from /admin
  • A screenshot from your local dev instance in the PR. Run the stats script there first, otherwise the page will show "—" because no counts have been written yet.

Not in this issue

Counting subjects that were merged or removed (distinct subject spellings, not works). The edit history records which field changed on a work, not which subject strings were merged away, so this needs the subject tools to report their own counts. It will be a separate issue.

Activity

  1. added
    Priority: 3
    Good First Issue
    Lead: @mekarpeles
    Theme: Book Tags
    on Nov 22, 2026
  2. github-actions commented on Nov 22, 2026

    @github-actions

    🤖 Assigned to @DeviousCardi automatically: they opened #14657 after commenting here. (This is a Good First Issue, so nobody has to assign it by hand.)

  3. dhruvwarriorr commented on Nov 23, 2026

    @dhruvwarriorr
    Contributor

    I’ve implemented and tested the Book Tag Stats page in #14665. I’m taking this on and will mark the PR ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Good First IssueLead: @mekarpelesPriority: 3Theme: Book Tags

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions