fix(bookstack-analytics): fix concurrent-write race condition and show unique users#104
Merged
amrit110 merged 2 commits intoJun 17, 2026
Merged
Conversation
…w unique users Replace the unsafe read-modify-write on the GCS activity log with a compare-and-swap loop using GCS object generation preconditions (if_generation_match). On an HTTP 412 conflict the logger reloads the latest log and retries up to 5 times, so simultaneous queries from different users no longer silently overwrite each other. Replace the misleading "Unique Sessions" dashboard metric (which was essentially equal to total queries since every page load creates a new session) with "Unique Users" computed from distinct user_email values. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
if_generation_match). On an HTTP 412 conflict, the logger reloads the latest log and retries up to 5 times.user_emailvalues. Currently shows 5 unique users vs the misleading 25 sessions.Root cause investigation
Confirmed via
gsutil caton the live GCS bucket: the raw data IS stored correctly (different users asking the same question produce separate entries with distinctsession_id,user_email, andtimestamp). The issues were:activity_logger.py)Changes
src/aieng_bot/bookstack/activity_logger.py_load_activity_logreturns(data, generation),_save_activity_logtakesif_generation_matchtests/bookstack/test_activity_logger.pyPreconditionFailedre-raisebookstack_agent/ui/lib/bookstack-types.tsunique_users: numbertoBookstackMetricsbookstack_agent/ui/lib/bookstack-data-fetcher.tsunique_usersfrom distinct non-nulluser_emailvaluesbookstack_agent/ui/app/analytics/components/query-metrics.tsxTest plan
uv run pytest)PreconditionFailedre-raisetsc --noEmit)ruff check)