Repository navigation
Consolidateion metadata summary API - #14031
Conversation
📝 WalkthroughWalkthroughMoved the metadata summary endpoint from the legacy web/API layer to a dataset-scoped RESTful handler (POST → GET), updated client SDK and frontend calls to use the new GET route with query params, and adjusted tests to reflect the new route and its behaviors. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Auth as Auth/ZAuth (decorators)
participant REST as REST Handler (/datasets/:id/metadata/summary)
participant KB as KnowledgebaseService
participant DocMeta as DocMetadataService
Client->>REST: GET /datasets/{dataset_id}/metadata/summary?doc_ids=1,2
REST->>Auth: validate login & add tenant_id
Auth-->>REST: tenant_id
REST->>KB: accessible(kb_id=dataset_id, user_id=tenant_id)
KB-->>REST: accessible? (true/false)
alt accessible
REST->>DocMeta: get_metadata_summary(dataset_id, doc_ids)
DocMeta-->>REST: summary
REST-->>Client: 200 { code:0, data:{ summary } }
else not accessible
REST-->>Client: error (ownership/access failure)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
api/apps/restful_apis/document_api.py (1)
150-185: Add basic logging to this new API flow.Please add debug/warn logs for access-denied and request-shape events (and optionally successful summary retrieval size) in this new endpoint to align with repo standards.
♻️ Suggested logging additions
async def metadata_summary(dataset_id, tenant_id): @@ - if not KnowledgebaseService.accessible(kb_id=dataset_id, user_id=tenant_id): + if not KnowledgebaseService.accessible(kb_id=dataset_id, user_id=tenant_id): + logging.warning("Metadata summary access denied. dataset_id=%s tenant_id=%s", dataset_id, tenant_id) return get_error_data_result(message=f"You don't own the dataset {dataset_id}. ") @@ - doc_ids_param = request.args.get("doc_ids", "") + doc_ids_param = request.args.get("doc_ids", "") + logging.debug("Metadata summary request. dataset_id=%s tenant_id=%s doc_ids_param=%s", dataset_id, tenant_id, doc_ids_param) @@ - summary = DocMetadataService.get_metadata_summary(dataset_id, doc_ids) + summary = DocMetadataService.get_metadata_summary(dataset_id, doc_ids) + logging.debug("Metadata summary success. dataset_id=%s fields=%s", dataset_id, len(summary or {})) return get_result(data={"summary": summary})As per coding guidelines
**/*.py: Add logging for new flows.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@api/apps/restful_apis/document_api.py` around lines 150 - 185, Add logging to the metadata_summary flow: in the metadata_summary function, log the incoming request shape (e.g., dataset_id and doc_ids_param) at debug level before access checks, log an access-denied warning when KnowledgebaseService.accessible(kb_id=dataset_id, user_id=tenant_id) returns false, and after successfully calling DocMetadataService.get_metadata_summary(dataset_id, doc_ids) log a debug/info line with the size/summary keys returned; use the module logger (create a logger = logging.getLogger(__name__) at top if missing) and include contextual fields (dataset_id, tenant_id, doc_ids) in all log entries.test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_summary.py (1)
52-89: Add a test fordoc_idsquery filtering behavior.The new endpoint contract includes optional
doc_idsfiltering, but current cases only validate unfiltered summary. Add one@pytest.mark.p2case that callsmetadata_summary(..., params={"doc_ids": "<subset>"})and asserts excluded docs don’t affect counts.Based on learnings
Applies to tests/**/*.py : Add/adjust tests for behavior changes.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_summary.py` around lines 52 - 89, Add a new pytest (mark p2) test in test_metadata_summary.py that verifies the metadata_summary endpoint honors the optional doc_ids filter: create documents via add_documents_func, set meta_fields with update_document for each doc, then call metadata_summary(HttpApiAuth, dataset_id, params={"doc_ids": "<comma-separated-subset-of-document_ids>"}) and assert res["code"] == 0 and that _summary_to_counts(res["data"]["summary"]) only includes counts from the provided subset (i.e., excluded doc_ids do not change tag/author counts).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@api/apps/restful_apis/document_api.py`:
- Around line 179-180: The code currently builds doc_ids from request.args via
doc_ids_param and forwards raw split tokens to
DocMetadataService.get_metadata_summary, which can include whitespace or empty
strings; normalize by trimming whitespace and removing empty tokens (e.g., build
doc_ids as a list comprehension that strips each token and filters out
falsy/empty results), and convert to None if the resulting list is empty before
passing to DocMetadataService.get_metadata_summary.
---
Nitpick comments:
In `@api/apps/restful_apis/document_api.py`:
- Around line 150-185: Add logging to the metadata_summary flow: in the
metadata_summary function, log the incoming request shape (e.g., dataset_id and
doc_ids_param) at debug level before access checks, log an access-denied warning
when KnowledgebaseService.accessible(kb_id=dataset_id, user_id=tenant_id)
returns false, and after successfully calling
DocMetadataService.get_metadata_summary(dataset_id, doc_ids) log a debug/info
line with the size/summary keys returned; use the module logger (create a logger
= logging.getLogger(__name__) at top if missing) and include contextual fields
(dataset_id, tenant_id, doc_ids) in all log entries.
In
`@test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_summary.py`:
- Around line 52-89: Add a new pytest (mark p2) test in test_metadata_summary.py
that verifies the metadata_summary endpoint honors the optional doc_ids filter:
create documents via add_documents_func, set meta_fields with update_document
for each doc, then call metadata_summary(HttpApiAuth, dataset_id,
params={"doc_ids": "<comma-separated-subset-of-document_ids>"}) and assert
res["code"] == 0 and that _summary_to_counts(res["data"]["summary"]) only
includes counts from the provided subset (i.e., excluded doc_ids do not change
tag/author counts).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 266006d8-4f9e-4ea3-9819-18c9fea12b21
📒 Files selected for processing (8)
api/apps/document_app.pyapi/apps/restful_apis/document_api.pyapi/apps/sdk/doc.pytest/testcases/test_http_api/test_file_management_within_dataset/test_doc_sdk_routes_unit.pytest/testcases/test_http_api/test_file_management_within_dataset/test_metadata_summary.pytest/testcases/test_web_api/test_document_app/test_document_metadata.pyweb/src/services/knowledge-service.tsweb/src/utils/api.ts
💤 Files with no reviewable changes (3)
- test/testcases/test_web_api/test_document_app/test_document_metadata.py
- api/apps/sdk/doc.py
- api/apps/document_app.py
| doc_ids_param = request.args.get("doc_ids", "") | ||
| doc_ids = doc_ids_param.split(",") if doc_ids_param else None |
There was a problem hiding this comment.
Normalize doc_ids query values before service call.
Line 180 currently forwards raw split values; whitespace/empty tokens can leak into DocMetadataService.get_metadata_summary(...) and cause avoidable filtering mismatches.
🛠️ Suggested normalization
- doc_ids_param = request.args.get("doc_ids", "")
- doc_ids = doc_ids_param.split(",") if doc_ids_param else None
+ doc_ids_param = request.args.get("doc_ids", "")
+ doc_ids = [doc_id.strip() for doc_id in doc_ids_param.split(",") if doc_id.strip()] or None📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| doc_ids_param = request.args.get("doc_ids", "") | |
| doc_ids = doc_ids_param.split(",") if doc_ids_param else None | |
| doc_ids_param = request.args.get("doc_ids", "") | |
| doc_ids = [doc_id.strip() for doc_id in doc_ids_param.split(",") if doc_id.strip()] or None |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@api/apps/restful_apis/document_api.py` around lines 179 - 180, The code
currently builds doc_ids from request.args via doc_ids_param and forwards raw
split tokens to DocMetadataService.get_metadata_summary, which can include
whitespace or empty strings; normalize by trimming whitespace and removing empty
tokens (e.g., build doc_ids as a list comprehension that strips each token and
filters out falsy/empty results), and convert to None if the resulting list is
empty before passing to DocMetadataService.get_metadata_summary.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #14031 +/- ##
==========================================
- Coverage 98.11% 96.66% -1.45%
==========================================
Files 10 10
Lines 690 690
Branches 108 108
==========================================
- Hits 677 667 -10
- Misses 4 8 +4
- Partials 9 15 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@yuzhichang should be good this time. Thanks. |
|
LGTM |
### What problem does this PR solve? Consolidation WEB API & HTTP API for document metadata summary Before consolidation Web API: POST /api/v1/document/metadata/summary Http API - GET /v1/datasets/<dataset_id>/metadata/summary After consolidation, Restful API -- GET /v1/datasets/<dataset_id>/metadata/summary ### Type of change - [x] Refactoring
### What problem does this PR solve? Consolidation WEB API & HTTP API for document metadata summary Before consolidation Web API: POST /api/v1/document/metadata/summary Http API - GET /v1/datasets/<dataset_id>/metadata/summary After consolidation, Restful API -- GET /v1/datasets/<dataset_id>/metadata/summary ### Type of change - [x] Refactoring
### What problem does this PR solve? Consolidation WEB API & HTTP API for document metadata summary Before consolidation Web API: POST /api/v1/document/metadata/summary Http API - GET /v1/datasets/<dataset_id>/metadata/summary After consolidation, Restful API -- GET /v1/datasets/<dataset_id>/metadata/summary ### Type of change - [x] Refactoring
### What problem does this PR solve? Consolidation WEB API & HTTP API for document metadata summary Before consolidation Web API: POST /api/v1/document/metadata/summary Http API - GET /v1/datasets/<dataset_id>/metadata/summary After consolidation, Restful API -- GET /v1/datasets/<dataset_id>/metadata/summary ### Type of change - [x] Refactoring
What problem does this PR solve?
Consolidation WEB API & HTTP API for document metadata summary
Before consolidation
Web API: POST /api/v1/document/metadata/summary
Http API - GET /v1/datasets/<dataset_id>/metadata/summary
After consolidation, Restful API -- GET /v1/datasets/<dataset_id>/metadata/summary
Type of change