Add showInternalDatabaseSizes and sizeFormat params to stats endpoints#1235
Add showInternalDatabaseSizes and sizeFormat params to stats endpoints#1235thakoreh wants to merge 1 commit into
Conversation
📝 WalkthroughWalkthroughThis PR adds support for two new optional query parameters to the Meilisearch Python SDK's stats methods, aligning with v1.44.0 API changes: ChangesStats Query Parameters Enhancement
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
tests/index/test_index_stats_meilisearch.py (1)
20-40: ⚡ Quick winStrengthen assertions so these tests actually validate the new query params.
Right now, the new tests still pass if params are dropped. Add assertions on
internal_database_sizesand format-sensitive value types.Suggested test tightening
def test_get_stats_with_internal_database_sizes(empty_index): """Tests getting stats with showInternalDatabaseSizes parameter.""" response = empty_index().get_stats(show_internal_database_sizes=True) assert isinstance(response, IndexStats) assert response.number_of_documents == 0 + assert response.internal_database_sizes is not None + assert isinstance(response.internal_database_sizes, dict) @@ def test_get_stats_with_all_params(empty_index): @@ assert isinstance(response, IndexStats) assert response.number_of_documents == 0 + assert response.internal_database_sizes is not None + assert isinstance(response.internal_database_sizes, dict) + assert all(isinstance(v, str) for v in response.internal_database_sizes.values())🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/index/test_index_stats_meilisearch.py` around lines 20 - 40, The tests currently only assert number_of_documents and don't validate the new query params; update test_get_stats_with_internal_database_sizes to assert that calling empty_index().get_stats(show_internal_database_sizes=True) returns response.internal_database_sizes present and of the expected type (e.g., a dict/mapping and contains expected keys like "main" or at least non-empty), update test_get_stats_with_size_format to call get_stats(size_format="human") and assert that size-related fields (e.g., response.index_size, response.total_size or whichever size properties exist on IndexStats) are human-readable strings (type str and match a simple pattern like ending with "B" or containing space), and update test_get_stats_with_all_params to assert both that internal_database_sizes is present and that size fields are string-formatted when both show_internal_database_sizes=True and size_format="human".tests/client/test_client_stats_meilisearch.py (1)
20-26: ⚡ Quick winMake this test fail when the new params are not honored.
The current conditional check allows a full pass even when
internalDatabaseSizesis absent. Assert presence for at least one index and validate human formatting output.Suggested assertion update
def test_get_all_stats_with_params(client): @@ assert "databaseSize" in response assert "indexes" in response - for index_stats in response["indexes"].values(): - if "internalDatabaseSizes" in index_stats: - assert isinstance(index_stats["internalDatabaseSizes"], dict) + assert isinstance(response["databaseSize"], str) + assert any("internalDatabaseSizes" in s for s in response["indexes"].values()) + for index_stats in response["indexes"].values(): + if "internalDatabaseSizes" in index_stats: + assert isinstance(index_stats["internalDatabaseSizes"], dict)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/client/test_client_stats_meilisearch.py` around lines 20 - 26, Update the test so it fails when the new parameters are not honored by asserting that at least one index contains the "internalDatabaseSizes" key and that those sizes are returned in human-readable string format; specifically, after calling client.get_all_stats(show_internal_database_sizes=True, size_format="human") and getting response, assert that any(index_stats for index_stats in response["indexes"].values() if "internalDatabaseSizes" in index_stats) is True and additionally validate that the values in that internalDatabaseSizes dict are strings matching human-size formatting (e.g., end with "B", "KB", "MB", etc.) using the existing response and index_stats variables.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@meilisearch/client.py`:
- Around line 356-364: The boolean query param show_internal_database_sizes is
being placed into params and encoded with urllib.parse.urlencode which yields
Python-style "True"/"False"; change the assignment in the code that builds
params so that when show_internal_database_sizes is a bool you store a lowercase
JSON-style string ("true" or "false") instead (e.g., set
params["showInternalDatabaseSizes"] = str(show_internal_database_sizes).lower()
when isinstance(show_internal_database_sizes, bool)), leaving size_format
handling and the rest of the path construction (self.config.paths.stat,
parse.urlencode, and the final self.http.get call) unchanged.
---
Nitpick comments:
In `@tests/client/test_client_stats_meilisearch.py`:
- Around line 20-26: Update the test so it fails when the new parameters are not
honored by asserting that at least one index contains the
"internalDatabaseSizes" key and that those sizes are returned in human-readable
string format; specifically, after calling
client.get_all_stats(show_internal_database_sizes=True, size_format="human") and
getting response, assert that any(index_stats for index_stats in
response["indexes"].values() if "internalDatabaseSizes" in index_stats) is True
and additionally validate that the values in that internalDatabaseSizes dict are
strings matching human-size formatting (e.g., end with "B", "KB", "MB", etc.)
using the existing response and index_stats variables.
In `@tests/index/test_index_stats_meilisearch.py`:
- Around line 20-40: The tests currently only assert number_of_documents and
don't validate the new query params; update
test_get_stats_with_internal_database_sizes to assert that calling
empty_index().get_stats(show_internal_database_sizes=True) returns
response.internal_database_sizes present and of the expected type (e.g., a
dict/mapping and contains expected keys like "main" or at least non-empty),
update test_get_stats_with_size_format to call get_stats(size_format="human")
and assert that size-related fields (e.g., response.index_size,
response.total_size or whichever size properties exist on IndexStats) are
human-readable strings (type str and match a simple pattern like ending with "B"
or containing space), and update test_get_stats_with_all_params to assert both
that internal_database_sizes is present and that size fields are
string-formatted when both show_internal_database_sizes=True and
size_format="human".
🪄 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: e27ee6ff-6383-422e-8897-c57d80f64af2
📒 Files selected for processing (6)
.code-samples.meilisearch.yamlmeilisearch/client.pymeilisearch/index.pymeilisearch/models/index.pytests/client/test_client_stats_meilisearch.pytests/index/test_index_stats_meilisearch.py
| if show_internal_database_sizes is not None: | ||
| params["showInternalDatabaseSizes"] = show_internal_database_sizes | ||
| if size_format is not None: | ||
| params["sizeFormat"] = size_format | ||
|
|
||
| path = self.config.paths.stat | ||
| if params: | ||
| path = f"{path}?{parse.urlencode(params)}" | ||
| return self.http.get(path) |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
python - <<'PY'
from urllib import parse
print(parse.urlencode({"showInternalDatabaseSizes": True}))
print(parse.urlencode({"showInternalDatabaseSizes": False}))
PYRepository: meilisearch/meilisearch-python
Length of output: 137
Fix boolean query serialization casing for showInternalDatabaseSizes.
urllib.parse.urlencode serializes Python bool values as True/False (capitalized) in the query string (e.g., showInternalDatabaseSizes=True), so normalize to lowercase to avoid API parsing mismatches.
Proposed fix
params: Dict[str, Any] = {}
if show_internal_database_sizes is not None:
- params["showInternalDatabaseSizes"] = show_internal_database_sizes
+ params["showInternalDatabaseSizes"] = str(show_internal_database_sizes).lower()
if size_format is not None:
params["sizeFormat"] = size_format🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@meilisearch/client.py` around lines 356 - 364, The boolean query param
show_internal_database_sizes is being placed into params and encoded with
urllib.parse.urlencode which yields Python-style "True"/"False"; change the
assignment in the code that builds params so that when
show_internal_database_sizes is a bool you store a lowercase JSON-style string
("true" or "false") instead (e.g., set params["showInternalDatabaseSizes"] =
str(show_internal_database_sizes).lower() when
isinstance(show_internal_database_sizes, bool)), leaving size_format handling
and the rest of the path construction (self.config.paths.stat, parse.urlencode,
and the final self.http.get call) unchanged.
Related issue
Fixes #1234
What does this PR do?
showInternalDatabaseSizesandsizeFormatoptional keyword-only parameters toClient.get_all_stats()andIndex.get_stats()IndexStatsmodel to include optionalinternal_database_sizesfield.code-samples.meilisearch.yamlwith examples forget_index_stats_1andget_indexes_stats_1PR checklist
Note: AI assistance was used (GitHub Copilot) for autocompletion during development.
Overview
This PR adds support for two new query parameters introduced in Meilisearch v1.44.0 to the stats endpoints:
showInternalDatabaseSizesandsizeFormat. These parameters allow clients to request internal database size breakdowns and human-readable size formatting.Changes
API Updates
Client.get_all_stats(): Added optional keyword-only parameters:
show_internal_database_sizes: Optional[bool]- includes internal database sizes in the responsesize_format: Optional[str]- specifies "human" for readable sizes or "raw" for byte countsIndex.get_stats(): Added the same optional keyword-only parameters with matching behavior
Model Updates
internal_database_sizes: Optional[Dict[str, Any]]field to represent dynamically-keyed internal database size mappingsTests
test_get_all_stats_with_params()to verify client stats calls with query parametersshow_internal_database_sizesparametersize_formatparameterDocumentation
.code-samples.meilisearch.yamlwith examples forget_index_stats_1andget_indexes_stats_1demonstrating the new parameters (show_internal_database_sizes=True, size_format='human')Related Issues
Fixes
#1234: Add human-formatted sizes and detailed DB sizes in statsNotes
AI assistance (GitHub Copilot) was used for autocompletion during development.