Skip to content

Update stats gathering functions#11844

Merged
mekarpeles merged 4 commits into
internetarchive:masterfrom
jimchamp:update-stats
Apr 15, 2026
Merged

Update stats gathering functions#11844
mekarpeles merged 4 commits into
internetarchive:masterfrom
jimchamp:update-stats

Conversation

@jimchamp

@jimchamp jimchamp commented Feb 10, 2026

Copy link
Copy Markdown
Collaborator

Supports #11714

Affects /stats page

Makes the following updates to our stats gathering functions:

  1. Removes unimplemented functions
  2. Updates comments that are stale or otherwise misleading
  3. Adds new function that fetches all thing IDs associated with bot accounts
  4. Simplifies functions that query for human and bot edit counts
  5. Functions that query for bot/human edits now strictly count edit transactions (not records edited)

Important Note: Functions that count the number of edits on the platform continue to count every transaction type as an edit. To fix this, we'll have to filter by action type when querying the transaction table. I'm unsure if this filtering should be included in this PR. Some transactions are being misidentified today -- perhaps those should be fixed before filters are added to the queries.

Technical

Testing

Can be tested by running scripts/store_counts.py with the expected arguments. Before running, it's best to update the following values in admin/stats.py::store_data:


uid = "counts-%s" % date

Change to something like "test-counts-%s" % date to avoid clobbering existing data.


doc['type'] = 'admin-stats'

Change to something like "test-admin-stats" for easy clean-up after testing. With this change in place, you can delete the entries like this:

test_entry_keys = web.ctx.site.store.keys(type="test-admin-stats")
for key in test_entry_keys:
    web.ctx.site.store.delete(key)

The store entry created during testing can be compared to the most recent production store_counts entry. Expect the total_* counts to be different -- human edits may be off a fair amount, while bot edits may differ slightly.

Screenshot

Stakeholders

Removes all functions from `numbers.py` that return `0`.

As all `admin_delta__*` functions have been removed, the
code that calls these functions has been removed from
`admin/stats.py`.
total_edits = result[0].count
bot_ids = _get_cached_bot_accounts(thingdb=kargs['thingdb'])
q1 = (
"SELECT count(DISTINCT t.id) AS count FROM transaction t, version v WHERE "

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Note that this was counting distinct transaction IDs. The bot edits query uses count(*), which would count the same transaction ID multiple times.

This should now be moot, as the version table is no longer queried for these counts.

@jimchamp jimchamp changed the title Update stats gathering functions [DO NOT MERGE] Update stats gathering functions Feb 18, 2026
Comment thread openlibrary/admin/numbers.py Outdated
Comment thread openlibrary/admin/numbers.py Outdated
@jimchamp jimchamp marked this pull request as ready for review February 24, 2026 01:08
Copilot AI review requested due to automatic review settings February 24, 2026 01:08
@jimchamp jimchamp marked this pull request as draft February 24, 2026 01:09
Comment thread openlibrary/admin/numbers.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the stats gathering functions in the admin module to improve accuracy and maintainability. It removes unimplemented stub functions, updates misleading documentation, adds bot account identification functionality, and simplifies the human/bot edit counting queries. The PR is marked as "DO NOT MERGE" and is a draft because it contains test values that must be replaced before production deployment.

Changes:

  • Removes unimplemented admin_delta__ functions and their corresponding gathering logic
  • Adds new get_bot_accounts() function to fetch bot account IDs from the database
  • Refactors admin_range__human_edits() and admin_range__bot_edits() to use the new bot account fetching mechanism
  • Updates documentation to fix typos and clarify function behavior

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.

File Description
openlibrary/admin/stats.py Adds test prefixes to data storage (test-counts and test-admin-stats) and removes admin_delta__ function calls
openlibrary/admin/numbers.py Adds bot account fetching functions, refactors human/bot edit counting, removes unimplemented functions, and updates documentation

Comment thread openlibrary/admin/numbers.py Outdated
Comment thread openlibrary/admin/numbers.py Outdated
Comment thread openlibrary/admin/numbers.py Outdated
Comment thread openlibrary/admin/numbers.py Outdated
Comment thread openlibrary/admin/stats.py Outdated
Comment thread openlibrary/admin/stats.py Outdated
Comment thread openlibrary/admin/numbers.py
@jimchamp jimchamp marked this pull request as ready for review February 24, 2026 01:18
Removes notion of querying for bot accounts via the
defunct `account` table.  New functions that query the
store for bot accounts have been created.

`admin_range__human_edits` and `admin_range__bot_edits`
have been updated to fetch bot accounts using the new functions.  Additionally, a redundant DB query has been
removed from `admin_range__human_edits`.
@jimchamp jimchamp changed the title [DO NOT MERGE] Update stats gathering functions Update stats gathering functions Feb 25, 2026
@jimchamp

jimchamp commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator Author

Data comparison

data generated with this updated script:

{"total_authors": 15069255, "total_covers": 14851352, "total_editions": 55613409, "total_lists": 253670, "total_members": 13382371, "total_works": 40792246, "type": "test-admin-stats", "authors": 1296, "bot_edits": 44791, "covers": 256, "editions": 3530, "human_edits": 7741, "lists": 118, "loans": 10382, "members": 4657, "works": 3050}

data generated with prod script:

{"total_authors": 15069258, "total_covers": 14851354, "total_ebooks": 0, "total_editions": 55613073, "total_lists": 253672, "total_members": 13382514, "total_subjects": 0, "total_works": 40791925, "ebooks": 0, "subjects": 0, "type": "admin-stats", "authors": 1292, "bot_edits": 0, "covers": 253, "editions": 3520, "human_edits": 51386, "lists": 115, "loans": 10255, "members": 4612, "works": 3041}

@jimchamp

Copy link
Copy Markdown
Collaborator Author

Noting that it took ~1 hour and 10 minutes to run this updated script for the previous 7 day's worth of counts.

@mekarpeles mekarpeles self-assigned this Mar 2, 2026
@mekarpeles mekarpeles added the Priority: 1 Do this week, receiving emails, time sensitive, . [managed] label Mar 20, 2026
@mekarpeles mekarpeles added this to the Sprint 2026-03 milestone Mar 23, 2026
@cdrini cdrini removed the On Testing label Mar 31, 2026
@mekarpeles mekarpeles added Priority: 0 Fix now: Issue prevents users from using the site or active data corruption. [managed] and removed Priority: 1 Do this week, receiving emails, time sensitive, . [managed] labels Apr 3, 2026
@mekarpeles mekarpeles merged commit 42afb76 into internetarchive:master Apr 15, 2026
4 checks passed
jack-wines pushed a commit to jack-wines/openlibrary that referenced this pull request Apr 18, 2026
* Remove unimplemented stats functions

Removes all functions from `numbers.py` that return `0`.

As all `admin_delta__*` functions have been removed, the
code that calls these functions has been removed from
`admin/stats.py`.

* Updates functions which count bot and human edits

Removes notion of querying for bot accounts via the
defunct `account` table.  New functions that query the
store for bot accounts have been created.

`admin_range__human_edits` and `admin_range__bot_edits`
have been updated to fetch bot accounts using the new functions.  Additionally, a redundant DB query has been
removed from `admin_range__human_edits`.
@jimchamp jimchamp deleted the update-stats branch April 23, 2026 17:09
IvanPisquiy06 pushed a commit to IvanPisquiy06/openlibrary that referenced this pull request Apr 27, 2026
* Remove unimplemented stats functions

Removes all functions from `numbers.py` that return `0`.

As all `admin_delta__*` functions have been removed, the
code that calls these functions has been removed from
`admin/stats.py`.

* Updates functions which count bot and human edits

Removes notion of querying for bot accounts via the
defunct `account` table.  New functions that query the
store for bot accounts have been created.

`admin_range__human_edits` and `admin_range__bot_edits`
have been updated to fetch bot accounts using the new functions.  Additionally, a redundant DB query has been
removed from `admin_range__human_edits`.
IvanPisquiy06 pushed a commit to IvanPisquiy06/openlibrary that referenced this pull request Apr 27, 2026
* Remove unimplemented stats functions

Removes all functions from `numbers.py` that return `0`.

As all `admin_delta__*` functions have been removed, the
code that calls these functions has been removed from
`admin/stats.py`.

* Updates functions which count bot and human edits

Removes notion of querying for bot accounts via the
defunct `account` table.  New functions that query the
store for bot accounts have been created.

`admin_range__human_edits` and `admin_range__bot_edits`
have been updated to fetch bot accounts using the new functions.  Additionally, a redundant DB query has been
removed from `admin_range__human_edits`.
Sadashii pushed a commit to Sadashii/openlibrary that referenced this pull request May 11, 2026
* Remove unimplemented stats functions

Removes all functions from `numbers.py` that return `0`.

As all `admin_delta__*` functions have been removed, the
code that calls these functions has been removed from
`admin/stats.py`.

* Updates functions which count bot and human edits

Removes notion of querying for bot accounts via the
defunct `account` table.  New functions that query the
store for bot accounts have been created.

`admin_range__human_edits` and `admin_range__bot_edits`
have been updated to fetch bot accounts using the new functions.  Additionally, a redundant DB query has been
removed from `admin_range__human_edits`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: 0 Fix now: Issue prevents users from using the site or active data corruption. [managed]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants