← 返回 instacart 的题目列表Fix Incorrect Metrics Caused by Applying Filters After Aggregation
类型:online_judge
Problem: Fix Incorrect Metrics Caused by Applying Filters After Aggregation
You are working on an existing library management system. The application has a metrics page showing aggregate values, such as:
Number of borrowed items
Number of available items
Number of overdue items
Aggregated statistics by branch or category
The metrics page supports filters such as:
branch
category
status
date range
Users report that one value on the metrics page is incorrect after filters are selected.
Known Information
The frontend is written in JavaScript.
The backend is written in Python using FastAPI.
The actual issue is in the backend.
The bug is that the backend computes metrics before applying filters, then filters the data afterward. Therefore the returned metric is computed from unfiltered data.
Requirements
Fix the backend logic so metrics are always computed from the filtered dataset.
Constraints
Do not change the API response shape.
Preserve the existing filtering semantics.
Add or fix tests covering:
Correct metrics with no filters.
Correct metrics with one filter.
Correct metrics with multiple combined filters.
Correct metrics when the filtered result is empty.
Example
Input
Items: 3 available, 2 borrowed; no filters
Output
Metrics report available=3, borrowed=2.