Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ reset_displayed_attributes_1: |-
compact_index_1: |-
client.index('INDEX_UID').compact
get_index_stats_1: |-
client.index('movies').stats
client.index('movies').stats(show_internal_database_sizes: true, size_format: 'human')
get_indexes_stats_1: |-
client.stats
client.stats(show_internal_database_sizes: true, size_format: 'human')
get_health_1: |-
client.health
get_version_1: |-
Expand Down
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Metrics/AbcSize:
# Offense count: 4
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 541
Max: 542

# Offense count: 4
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Expand Down
13 changes: 11 additions & 2 deletions lib/meilisearch/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,20 @@ def version

# Get stats of all indexes in instance.
#
# @param show_internal_database_sizes [Boolean] When +true+, each index stat object includes an
# +internalDatabaseSizes+ map. The keys in this map are subject to change.
# @param size_format [String, nil] Controls how size fields are returned.
# Use <tt>'human'</tt> for human-readable strings (e.g. <tt>"2.3 MiB"</tt>),
# or <tt>'raw'</tt> (default) for numeric byte counts.
# @see Index#stats
# @see https://www.meilisearch.com/docs/reference/api/stats#get-stats-of-all-indexes Meilisearch API reference
# @return [Hash{String => Object}] see {stats object}[https://www.meilisearch.com/docs/reference/api/stats#stats-object]
def stats
http_get '/stats'
def stats(show_internal_database_sizes: nil, size_format: nil)
params = {
showInternalDatabaseSizes: show_internal_database_sizes,
sizeFormat: size_format
}.compact
http_get '/stats', params
end

### DUMPS
Expand Down
11 changes: 9 additions & 2 deletions lib/meilisearch/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -675,10 +675,17 @@ def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)

# Get stats of this index.
#
# @param show_internal_database_sizes [Boolean] When +true+, the response includes an
# +internalDatabaseSizes+ map of internal database names to their sizes.
# The keys in this map are subject to change.
# @param size_format [String, nil] Controls how size fields are returned.
# Use <tt>'human'</tt> for human-readable strings (e.g. <tt>"2.3 MiB"</tt>),
# or <tt>'raw'</tt> (default) for numeric byte counts.
# @return [Hash{String => Object}]
# @see https://www.meilisearch.com/docs/reference/api/stats#get-stats-of-an-index Meilisearch API Reference
def stats
http_get "/indexes/#{@uid}/stats"
def stats(show_internal_database_sizes: nil, size_format: nil)
params = { showInternalDatabaseSizes: show_internal_database_sizes, sizeFormat: size_format }
http_get "/indexes/#{@uid}/stats", params.compact
end

# Get the number of documents in the index.
Expand Down
16 changes: 16 additions & 0 deletions spec/meilisearch/client/stats_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,20 @@
response = client.stats
expect(response).to have_key('databaseSize')
end

it 'gets stats with human-readable size format' do
response = client.stats(size_format: 'human')
expect(response).to have_key('databaseSize')
expect(response['databaseSize']).to be_a(String)
end

it 'gets stats with internal database sizes' do
client.create_index('books').await
response = client.stats(show_internal_database_sizes: true)

expect(response).to have_key('indexes')
response['indexes'].each_value do |index_stats|
expect(index_stats).to have_key('internalDatabaseSizes')
end
end
Comment on lines +23 to +31
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Look for RSpec cleanup hooks that remove test indexes

# Check for after hooks in this spec file
rg -n -A 3 -B 1 'after\(:(each|all)\)' spec/meilisearch/client/stats_spec.rb

# Check for shared cleanup contexts
rg -n -A 5 'RSpec\.configure.*after' spec/spec_helper.rb spec/support/

# Check for index deletion patterns in spec helpers
rg -n 'delete.*index|clear.*index' spec/spec_helper.rb spec/support/

Repository: meilisearch/meilisearch-ruby

Length of output: 274


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# 1) See where clear_all_indexes is used
rg -n "clear_all_indexes\(" spec | head -n 50

# 2) Inspect stats_spec.rb for hooks / shared contexts
rg -n "shared_context|default_shared_context|include.*default|after\(|before\(" spec/meilisearch/client/stats_spec.rb

# 3) Print stats_spec.rb with line numbers (only the relevant region if short)
wc -l spec/meilisearch/client/stats_spec.rb
cat -n spec/meilisearch/client/stats_spec.rb

# 4) Inspect helper implementations referenced by the initial search
wc -l spec/support/indexes_helpers.rb spec/support/default_shared_context.rb
cat -n spec/support/indexes_helpers.rb
cat -n spec/support/default_shared_context.rb

Repository: meilisearch/meilisearch-ruby

Length of output: 209


🏁 Script executed:

#!/bin/bash
set -u
# Don't fail on missing matches; we need absence info for verification.
set +e

# A) Confirm how stats_spec.rb sets up/tears down the client/indexes
echo "== stats_spec.rb =="
wc -l spec/meilisearch/client/stats_spec.rb
sed -n '1,160p' spec/meilisearch/client/stats_spec.rb

echo
echo "== usages of clear_all_indexes in spec/ =="
rg -n "clear_all_indexes\(" spec | head -n 50

echo
echo "== inclusion of default_shared_context in specs =="
rg -n "default_shared_context" spec | head -n 50
rg -n "include.*default_shared_context|include.*indexes_helpers|include.*shared_context" spec | head -n 50

echo
echo "== after/before hooks in stats_spec.rb =="
rg -n "before\(|after\(|around\(|shared_context" spec/meilisearch/client/stats_spec.rb

echo
echo "== helper implementations =="
wc -l spec/support/indexes_helpers.rb spec/support/default_shared_context.rb
cat -n spec/support/indexes_helpers.rb
cat -n spec/support/default_shared_context.rb

echo
echo "== RSpec configure after hooks =="
rg -n "RSpec\.configure|config\.after|config\.before" spec/spec_helper.rb spec/spec_helper.rb spec/support/ | head -n 80

Repository: meilisearch/meilisearch-ruby

Length of output: 2637


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== find where shared context 'test defaults' is included =="
rg -n "include_context\s+['\"]test defaults['\"]|shared_context\s+['\"]test defaults['\"]" spec | head -n 100

echo
echo "== inspect spec/spec_helper.rb around RSpec.configure =="
sed -n '1,140p' spec/spec_helper.rb
sed -n '140,260p' spec/spec_helper.rb

echo
echo "== confirm whether stats_spec.rb includes default shared context =="
rg -n "include_context|shared_context|default_shared_context" spec/meilisearch/client/stats_spec.rb

echo
echo "== check for global metadata including shared context =="
rg -n "use.*shared_context|before.*include_context|RSpec\.configure" spec/spec_helper.rb spec/support | head -n 200

Repository: meilisearch/meilisearch-ruby

Length of output: 3314


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Locate delete_index implementation and any usage patterns
rg -n "def delete_index\b" lib spec | head -n 50
rg -n "delete_index\(" lib spec | head -n 50
rg -n "delete_index\(.+\)\.await" lib spec | head -n 50

# Print the delete_index method (first match) with surrounding context
file="$(rg -l "def delete_index\b" lib spec | head -n 1)"
echo "== delete_index file: $file =="
if [ -n "${file:-}" ]; then
  # show a window around the method
  line="$(rg -n "def delete_index\b" "$file" | head -n 1 | cut -d: -f1)"
  start=$((line-20))
  end=$((line+60))
  sed -n "${start},${end}p" "$file"
fi

Repository: meilisearch/meilisearch-ruby

Length of output: 349


🏁 Script executed:

#!/bin/bash
set +e

echo "== delete_index implementation =="
file="lib/meilisearch/client.rb"
line="$(rg -n "def delete_index\b" "$file" | head -n 1 | cut -d: -f1)"
echo "found at line: $line"
start=$((line-20))
end=$((line+80))
sed -n "${start},${end}p" "$file"

echo
echo "== usages of delete_index(...).await =="
rg -n "delete_index\([^)]+\)\.await" lib spec | head -n 50

echo
echo "== usages of delete_index =="
rg -n "delete_index\(" lib spec | head -n 80

echo
echo "== inspect indexes_spec.rb around delete_index usage =="
spec_file="spec/meilisearch/client/indexes_spec.rb"
line2="$(rg -n "task = client\.delete_index" "$spec_file" | head -n 1 | cut -d: -f1)"
echo "found at line: $line2"
start2=$((line2-40))
end2=$((line2+60))
sed -n "${start2},${end2}p" "$spec_file"

Repository: meilisearch/meilisearch-ruby

Length of output: 7628


🏁 Script executed:

#!/bin/bash
set +euo pipefail

# Find clear_all_keys implementation and how it's called
rg -n "def clear_all_keys|clear_all_keys\(" spec/support spec | head -n 50

# Inspect the helper file that defines clear_all_keys (if found)
file="$(rg -l "def clear_all_keys\b" spec/support spec | head -n 1)"
echo "== clear_all_keys file: ${file:-<none>} =="
if [ -n "${file:-}" ]; then
  sed -n '1,200p' "$file"
fi

# Inspect if any cleanup helpers await deletion tasks
rg -n "clear_all_indexes|delete_index\(.+?\)\.await|.delete_index\(.+?\)" spec/support | head -n 80

Repository: meilisearch/meilisearch-ruby

Length of output: 783


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Check delete_key implementation
file="$(rg -l "def delete_key\b" lib/meilisearch/client.rb lib spec | head -n 1)"
echo "== delete_key file: ${file:-<none>} =="
if [ -n "${file:-}" ]; then
  line="$(rg -n "def delete_key\b" "$file" | head -n 1 | cut -d: -f1)"
  start=$((line-20))
  end=$((line+70))
  sed -n "${start},${end}p" "$file"
fi

# Check usage patterns of delete_key(...).await
rg -n "delete_key\([^)]+\)\.await" lib spec | head -n 50
rg -n "delete_key\(" lib spec | head -n 50

# Also check whether delete_index returns an async task (already saw return type in comment)
# but let's inspect the Index#delete used underneath
file2="$(rg -l "def delete\b" lib/meilisearch/index.rb lib/meilisearch -g'*.rb' | head -n 20)"
echo
echo "== candidate delete methods files (showing first few): =="
echo "$file2" | head -n 5

Repository: meilisearch/meilisearch-ruby

Length of output: 3890


Await async index deletion in shared test cleanup to prevent race conditions

spec/meilisearch/client/stats_spec.rb doesn’t clean up the 'books' index itself, but the suite includes the shared context test defaults (via config.include_context 'test defaults') that calls clear_all_indexes(client) before each example. However, clear_all_indexes calls client.delete_index(uid) without awaiting the returned Models::Task, so index deletions may still be in progress when the next example starts. Update spec/support/indexes_helpers.rb to await each delete_index task (e.g., client.delete_index(uid).await) or otherwise ensure cleanup completes before the example runs.

🤖 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 `@spec/meilisearch/client/stats_spec.rb` around lines 23 - 31, The shared
cleanup helper clear_all_indexes in spec/support/indexes_helpers.rb issues
client.delete_index(uid) without awaiting the returned Models::Task, which
allows deletions to run asynchronously and causes race conditions; update
clear_all_indexes to await each deletion (e.g., call
client.delete_index(uid).await or otherwise wait for task completion) so all
index deletions complete before the next example runs, ensuring tests like
stats_spec that rely on a clean state no longer race.

end
12 changes: 12 additions & 0 deletions spec/meilisearch/index/stats_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
expect(response).not_to be_empty
end

it 'returns stats with human-readable size format' do
response = index.stats(size_format: 'human')
expect(response).to have_key('rawDocumentDbSize')
expect(response['rawDocumentDbSize']).to be_a(String)
end

it 'returns stats with internal database sizes' do
response = index.stats(show_internal_database_sizes: true)
expect(response).to have_key('internalDatabaseSizes')
expect(response['internalDatabaseSizes']).to be_a(Hash)
end

it 'gets the number of documents' do
response = index.number_of_documents
expect(response).to eq(documents.count)
Expand Down