-
Notifications
You must be signed in to change notification settings - Fork 145
Add stats size format options #979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Moustafa-Ameen
wants to merge
1
commit into
meilisearch:main
Choose a base branch
from
Moustafa-Ameen:codex/meilisearch-stats-size-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+302
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
47 changes: 47 additions & 0 deletions
47
src/main/java/com/meilisearch/sdk/model/IndexStatsWithSizeFormat.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package com.meilisearch.sdk.model; | ||
|
|
||
| import java.util.Map; | ||
| import lombok.Getter; | ||
|
|
||
| /** | ||
| * Stats data structure of a Meilisearch Index when database sizes may be raw bytes or human-readable | ||
| * strings. | ||
| * | ||
| * @see <a href="https://www.meilisearch.com/docs/reference/api/indexes/get-stats-of-index">API | ||
| * specification</a> | ||
| */ | ||
| @Getter | ||
| public class IndexStatsWithSizeFormat { | ||
| protected long numberOfDocuments; | ||
| protected boolean isIndexing; | ||
| protected Map<String, Integer> fieldDistribution; | ||
| protected Object rawDocumentDbSize; | ||
| protected Object avgDocumentSize; | ||
| protected Object maxDocumentSize; | ||
| protected Long numberOfEmbeddedDocuments; | ||
| protected Long numberOfEmbeddings; | ||
| protected Map<String, Object> internalDatabaseSizes; | ||
|
|
||
| public IndexStatsWithSizeFormat() {} | ||
|
|
||
| public IndexStatsWithSizeFormat( | ||
| long numberOfDocuments, | ||
| boolean isIndexing, | ||
| Map<String, Integer> fieldDistribution, | ||
| Object rawDocumentDbSize, | ||
| Object avgDocumentSize, | ||
| Object maxDocumentSize, | ||
| Long numberOfEmbeddedDocuments, | ||
| Long numberOfEmbeddings, | ||
| Map<String, Object> internalDatabaseSizes) { | ||
| this.numberOfDocuments = numberOfDocuments; | ||
| this.isIndexing = isIndexing; | ||
| this.fieldDistribution = fieldDistribution; | ||
| this.rawDocumentDbSize = rawDocumentDbSize; | ||
| this.avgDocumentSize = avgDocumentSize; | ||
| this.maxDocumentSize = maxDocumentSize; | ||
| this.numberOfEmbeddedDocuments = numberOfEmbeddedDocuments; | ||
| this.numberOfEmbeddings = numberOfEmbeddings; | ||
| this.internalDatabaseSizes = internalDatabaseSizes; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.meilisearch.sdk.model; | ||
|
|
||
| import com.meilisearch.sdk.http.URLBuilder; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import lombok.experimental.Accessors; | ||
|
|
||
| /** | ||
| * Data structure of the query parameters for the stats routes. | ||
| * | ||
| * @see <a href="https://www.meilisearch.com/docs/reference/api/stats">API specification</a> | ||
| */ | ||
| @Setter | ||
| @Getter | ||
| @Accessors(chain = true) | ||
| public class StatsQuery { | ||
| private Boolean showInternalDatabaseSizes; | ||
| private String sizeFormat; | ||
|
|
||
| public StatsQuery() {} | ||
|
|
||
| public String toQuery() { | ||
| URLBuilder urlb = | ||
| new URLBuilder() | ||
| .addParameter( | ||
| "showInternalDatabaseSizes", | ||
| this.getShowInternalDatabaseSizes()) | ||
| .addParameter("sizeFormat", this.getSizeFormat()); | ||
| return urlb.getURL(); | ||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
src/main/java/com/meilisearch/sdk/model/StatsWithSizeFormat.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.meilisearch.sdk.model; | ||
|
|
||
| import java.util.Date; | ||
| import java.util.Map; | ||
| import lombok.Getter; | ||
|
|
||
| /** | ||
| * Meilisearch stats data structure when database sizes may be raw bytes or human-readable strings. | ||
| * | ||
| * @see <a href="https://www.meilisearch.com/docs/reference/api/stats">API specification</a> | ||
| */ | ||
| @Getter | ||
| public class StatsWithSizeFormat { | ||
| protected Object databaseSize; | ||
| protected Date lastUpdate; | ||
| protected Map<String, IndexStatsWithSizeFormat> indexes; | ||
| protected Object usedDatabaseSize; | ||
|
|
||
| public StatsWithSizeFormat( | ||
| Object databaseSize, | ||
| Date lastUpdate, | ||
| Map<String, IndexStatsWithSizeFormat> indexes, | ||
| Object usedDatabaseSize) { | ||
| this.databaseSize = databaseSize; | ||
| this.lastUpdate = lastUpdate; | ||
| this.indexes = indexes; | ||
| this.usedDatabaseSize = usedDatabaseSize; | ||
| } | ||
|
|
||
| public StatsWithSizeFormat() {} | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package com.meilisearch.sdk; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.*; | ||
|
|
||
| import com.meilisearch.sdk.model.IndexStatsWithSizeFormat; | ||
| import com.meilisearch.sdk.model.StatsQuery; | ||
| import com.meilisearch.sdk.model.StatsWithSizeFormat; | ||
| import okhttp3.mockwebserver.MockResponse; | ||
| import okhttp3.mockwebserver.MockWebServer; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class StatsTest { | ||
|
|
||
| private MockWebServer server; | ||
| private Client client; | ||
|
|
||
| @BeforeEach | ||
| void setup() throws Exception { | ||
| server = new MockWebServer(); | ||
| server.start(); | ||
|
|
||
| client = new Client(new Config(server.url("/").toString(), "masterKey")); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void teardown() throws Exception { | ||
| server.shutdown(); | ||
| } | ||
|
|
||
| @Test | ||
| void getStatsWithQueryParameters() throws Exception { | ||
| String json = | ||
| """ | ||
| { | ||
| "databaseSize": "1.2 KiB", | ||
| "usedDatabaseSize": "1 KiB", | ||
| "lastUpdate": "2019-11-20T09:40:33.711324Z", | ||
| "indexes": { | ||
| "movies": { | ||
| "numberOfDocuments": 10, | ||
| "rawDocumentDbSize": "100 B", | ||
| "maxDocumentSize": "16 B", | ||
| "avgDocumentSize": "10 B", | ||
| "isIndexing": true, | ||
| "fieldDistribution": { | ||
| "genre": 10 | ||
| }, | ||
| "internalDatabaseSizes": { | ||
| "documents": "100 B" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """; | ||
| StatsQuery query = | ||
| new StatsQuery().setShowInternalDatabaseSizes(true).setSizeFormat("human"); | ||
|
|
||
| server.enqueue(new MockResponse().setResponseCode(200).setBody(json)); | ||
|
|
||
| StatsWithSizeFormat stats = client.getStats(query); | ||
|
|
||
| assertThat( | ||
| server.takeRequest().getPath(), | ||
| is("//stats?showInternalDatabaseSizes=true&sizeFormat=human")); | ||
| assertThat(stats.getDatabaseSize(), is("1.2 KiB")); | ||
| assertThat(stats.getUsedDatabaseSize(), is("1 KiB")); | ||
| assertThat(stats.getIndexes().get("movies").getRawDocumentDbSize(), is("100 B")); | ||
| assertThat( | ||
| stats.getIndexes().get("movies").getInternalDatabaseSizes().get("documents"), | ||
| is("100 B")); | ||
| } | ||
|
|
||
| @Test | ||
| void getIndexStatsWithQueryParameters() throws Exception { | ||
| String json = | ||
| """ | ||
| { | ||
| "numberOfDocuments": 10, | ||
| "rawDocumentDbSize": "100 B", | ||
| "maxDocumentSize": "16 B", | ||
| "avgDocumentSize": "10 B", | ||
| "numberOfEmbeddings": 2, | ||
| "numberOfEmbeddedDocuments": 1, | ||
| "isIndexing": false, | ||
| "fieldDistribution": { | ||
| "genre": 10 | ||
| }, | ||
| "internalDatabaseSizes": { | ||
| "documents": "100 B" | ||
| } | ||
| } | ||
| """; | ||
| StatsQuery query = | ||
| new StatsQuery().setShowInternalDatabaseSizes(true).setSizeFormat("human"); | ||
|
|
||
| server.enqueue(new MockResponse().setResponseCode(200).setBody(json)); | ||
|
|
||
| IndexStatsWithSizeFormat stats = client.index("movies").getStats(query); | ||
|
|
||
| assertThat( | ||
| server.takeRequest().getPath(), | ||
| is("//indexes/movies/stats?showInternalDatabaseSizes=true&sizeFormat=human")); | ||
| assertThat(stats.getRawDocumentDbSize(), is("100 B")); | ||
| assertThat(stats.getAvgDocumentSize(), is("10 B")); | ||
| assertThat(stats.getMaxDocumentSize(), is("16 B")); | ||
| assertThat(stats.getInternalDatabaseSizes().get("documents"), is("100 B")); | ||
| assertThat(stats.getNumberOfEmbeddings(), is(2L)); | ||
| assertThat(stats.getNumberOfEmbeddedDocuments(), is(1L)); | ||
| } | ||
|
|
||
| @Test | ||
| void statsQuerySerializesParameters() { | ||
| StatsQuery query = | ||
| new StatsQuery().setShowInternalDatabaseSizes(true).setSizeFormat("raw"); | ||
|
|
||
| assertThat(query.toQuery(), is("?showInternalDatabaseSizes=true&sizeFormat=raw")); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Omit
sizeFormatwhen it is unset.Line 28 always passes
sizeFormatintoURLBuilder, but that helper only skips empty strings. Withnew StatsQuery()ornew StatsQuery().setShowInternalDatabaseSizes(true), this serializes as?sizeFormat=null, which violates the new optional-parameter contract and breaks the request.Proposed fix
public String toQuery() { - URLBuilder urlb = - new URLBuilder() - .addParameter( - "showInternalDatabaseSizes", - this.getShowInternalDatabaseSizes()) - .addParameter("sizeFormat", this.getSizeFormat()); + URLBuilder urlb = + new URLBuilder() + .addParameter( + "showInternalDatabaseSizes", + this.getShowInternalDatabaseSizes()); + if (this.getSizeFormat() != null && !this.getSizeFormat().isEmpty()) { + urlb.addParameter("sizeFormat", this.getSizeFormat()); + } return urlb.getURL(); }📝 Committable suggestion
🤖 Prompt for AI Agents