Skip to content

Conversation

@chance-wnb
Copy link
Contributor

@chance-wnb chance-wnb commented Nov 22, 2025

Add Annotation Queues Stats API

This PR adds a new API endpoint to retrieve statistics for multiple annotation queues in a single batch request. This is needed for the Annotation Queues List page in the frontend to efficiently display progress information for all visible queues.

Motivation

The Annotation Queues List page displays queues in a paginated table with columns showing:

  • Total number of traces/items in each queue
  • Number of completed items (completed or skipped)

Without this API, the frontend would need to make individual requests for each queue or query all queue items separately, which is inefficient for paginated list views.

Changes

Backend (Python)

New API Interface (trace_server_interface.py)

  • AnnotationQueueStatsSchema: Schema for a single queue's stats
    • queue_id: The queue identifier
    • total_items: Count of all items in the queue
    • completed_items: Count of items marked as 'completed' or 'skipped'
  • AnnotationQueuesStatsReq: Request with project_id and list of queue_ids
  • AnnotationQueuesStatsRes: Response with list of stats

Query Builder (annotation_queues_query_builder.py)

  • Added make_queues_stats_query() function that generates an efficient SQL query using CTEs:
    • total_items_per_queue: Counts items from annotation_queue_items table
    • completed_items_per_queue: Counts distinct queue_item_ids with 'completed' or 'skipped' status from annotator_queue_items_progress table
    • Uses LEFT JOINs to return stats even for empty queues (0 items)
    • Uses arrayJoin() to ensure all requested queue_ids are in the result set

ClickHouse Implementation (clickhouse_trace_server_batched.py)

  • Implemented annotation_queues_stats() method
  • Returns empty stats list if no queue_ids provided
  • Uses tuple unpacking from result.result_rows for efficient result parsing

Server Interface Implementations

  • sqlite_trace_server.py: Stub that raises NotImplementedError (annotation queues not supported in SQLite)
  • external_to_internal_trace_server_adapter.py: Converts external project_id to internal format and delegates
  • caching_middleware_trace_server.py: Passthrough to next server
  • remote_http_trace_server.py: HTTP client implementation calling /annotation_queues/stats
  • cross_process_trace_server.py: IPC request handler

Tests

Unit Tests (test_client_annotations.py)
Added three comprehensive test cases:

  1. test_annotation_queues_stats - Main test with partial completion:

    • Creates 3 queues with different numbers of items (3, 5, 7)
    • Uses ClickHouse lightweight UPDATE to mark items as 'completed' or 'skipped'
    • Verifies stats correctly count both completed and skipped items
    • Tests mixed completion states (Queue 0: 2/3, Queue 1: 4/5, Queue 2: 4/7)
  2. test_annotation_queues_stats_empty_queues - Edge case for empty queues:

    • Verifies queues with no items return 0 for both total_items and completed_items
  3. test_annotation_queues_stats_no_queue_ids - Edge case for empty request:

    • Verifies empty queue_ids list returns empty stats list

Test Implementation Details

  • Uses direct ClickHouse client access via client.server._next_trace_server.ch_client
  • Uses hardcoded internal project_id (c2hhd24vdGVzdC1wcm9qZWN0) for database queries
  • Uses ClickHouse lightweight UPDATE syntax: UPDATE table SET ... WHERE ...
  • Simulates annotation workflow by updating annotator_queue_items_progress records

API Usage

Request:

AnnotationQueuesStatsReq(
    project_id="entity/project",
    queue_ids=["queue-id-1", "queue-id-2", "queue-id-3"]
)
Response:
AnnotationQueuesStatsRes(
    stats=[
        AnnotationQueueStatsSchema(
            queue_id="queue-id-1",
            total_items=10,
            completed_items=7
        ),
        AnnotationQueueStatsSchema(
            queue_id="queue-id-2",
            total_items=5,
            completed_items=0
        ),
        AnnotationQueueStatsSchema(
            queue_id="queue-id-3",
            total_items=0,
            completed_items=0
        )
    ]
)

Performance Considerations

  • Single database query for all requested queues (efficient for paginated list views)
  • Uses CTEs for readability and potential query optimization
  • Counts DISTINCT queue_item_id in progress table (handles shared pool mode)
  • Returns results for all requested queues, even if empty (consistent API contract)

@codecov
Copy link

codecov bot commented Nov 22, 2025

Codecov Report

❌ Patch coverage is 94.11765% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
weave/trace_server/sqlite_trace_server.py 50.00% 1 Missing ⚠️
.../trace_server_bindings/remote_http_trace_server.py 50.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@wandbot-3000
Copy link

wandbot-3000 bot commented Nov 22, 2025

@chance-wnb chance-wnb force-pushed the chance/queue_creation_apis branch from 4eaba5e to fd18687 Compare November 22, 2025 03:42
@chance-wnb chance-wnb force-pushed the chance/queue_stats_api branch from 12d4f51 to 474142b Compare November 22, 2025 03:42
@chance-wnb chance-wnb marked this pull request as ready for review November 25, 2025 17:33
@chance-wnb chance-wnb requested a review from a team as a code owner November 25, 2025 17:33
@chance-wnb chance-wnb force-pushed the chance/queue_stats_api branch from 474142b to 18219c7 Compare November 26, 2025 00:55
@chance-wnb chance-wnb force-pushed the chance/queue_creation_apis branch from fd18687 to 157f8ae Compare November 26, 2025 00:55
@chance-wnb chance-wnb force-pushed the chance/queue_creation_apis branch from 157f8ae to 3a126f3 Compare December 16, 2025 21:02
@chance-wnb chance-wnb force-pushed the chance/queue_stats_api branch from 18219c7 to 49d3ccb Compare December 16, 2025 21:02
@chance-wnb chance-wnb force-pushed the chance/queue_creation_apis branch from 3a126f3 to 2f19dd7 Compare December 16, 2025 21:10
@chance-wnb chance-wnb force-pushed the chance/queue_stats_api branch from 49d3ccb to 3f0bd5a Compare December 16, 2025 21:10
@chance-wnb chance-wnb force-pushed the chance/queue_creation_apis branch from 2f19dd7 to 96e922c Compare December 16, 2025 22:17
@chance-wnb chance-wnb force-pushed the chance/queue_stats_api branch 2 times, most recently from a01e4fd to ad4b3bf Compare December 17, 2025 23:07
@chance-wnb chance-wnb force-pushed the chance/queue_creation_apis branch from 96e922c to ab97452 Compare December 17, 2025 23:07
@chance-wnb chance-wnb force-pushed the chance/queue_stats_api branch from ad4b3bf to a2cb043 Compare December 30, 2025 03:29
@chance-wnb chance-wnb force-pushed the chance/queue_creation_apis branch from ab97452 to 4cf94a1 Compare December 30, 2025 03:29
@chance-wnb chance-wnb force-pushed the chance/queue_creation_apis branch from 4cf94a1 to cc94bee Compare December 31, 2025 02:12
@chance-wnb chance-wnb force-pushed the chance/queue_stats_api branch from a2cb043 to 275496b Compare December 31, 2025 02:12
@chance-wnb chance-wnb force-pushed the chance/queue_stats_api branch from fbd1db9 to edcc182 Compare January 8, 2026 00:49
@chance-wnb chance-wnb force-pushed the chance/queue_creation_apis branch from 64c046c to b054358 Compare January 8, 2026 00:49
@chance-wnb chance-wnb force-pushed the chance/queue_stats_api branch from edcc182 to a39069c Compare January 8, 2026 00:53
@chance-wnb chance-wnb force-pushed the chance/queue_creation_apis branch from b054358 to 202982c Compare January 8, 2026 00:53
@chance-wnb chance-wnb force-pushed the chance/queue_stats_api branch from a39069c to a1adb7f Compare January 8, 2026 01:03
@chance-wnb chance-wnb force-pushed the chance/queue_creation_apis branch 2 times, most recently from 459e8ce to a99980c Compare January 8, 2026 01:32
@chance-wnb chance-wnb force-pushed the chance/queue_stats_api branch from a1adb7f to dc13071 Compare January 8, 2026 01:32
Base automatically changed from chance/queue_creation_apis to master January 8, 2026 02:12
@chance-wnb chance-wnb force-pushed the chance/queue_stats_api branch 2 times, most recently from 030b3c8 to 2da24b1 Compare January 8, 2026 02:28
@chance-wnb chance-wnb force-pushed the chance/queue_stats_api branch from 2da24b1 to 7678207 Compare January 8, 2026 02:32
@chance-wnb chance-wnb merged commit 3395e71 into master Jan 8, 2026
342 of 344 checks passed
@chance-wnb chance-wnb deleted the chance/queue_stats_api branch January 8, 2026 02:54
@github-actions github-actions bot locked and limited conversation to collaborators Jan 8, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants