-
Notifications
You must be signed in to change notification settings - Fork 0
[JAY-656] Add a way to access the Elasticsearch cluster's node-related statistics #30
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
89128b2
[JAY-656] Rename Stats#response to #indices_stats
sergio-bobillier 44d28ad
[JAY-656] Add Elasticsearch::Stats::Node::Storage
sergio-bobillier 82bebbd
[JAY-656] Add the Elasticsearch::Stats::Node class
sergio-bobillier c601619
[JAY-656] Add Elasticsearch::Stats::Nodes class
sergio-bobillier 04139af
[JAY-656] Add the #nodes method to Elasticsearch::Stats
sergio-bobillier 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
15 changes: 15 additions & 0 deletions
15
lib/jay_api/elasticsearch/stats/errors/stats_data_not_available.rb
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,15 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative '../../../errors/error' | ||
|
|
||
| module JayAPI | ||
| module Elasticsearch | ||
| class Stats | ||
| module Errors | ||
| # An error to be raised when a particular Stats element is requested for | ||
| # which there is no data in the response received from the cluster. | ||
| class StatsDataNotAvailable < ::JayAPI::Errors::Error; end | ||
| end | ||
| end | ||
| end | ||
| end |
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,45 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'errors/stats_data_not_available' | ||
| require_relative 'node/storage' | ||
|
|
||
| module JayAPI | ||
| module Elasticsearch | ||
| class Stats | ||
| # Holds information about one of the nodes in the Elasticsearch cluster. | ||
| class Node | ||
| attr_reader :name | ||
|
|
||
| # @param [String] name The name of the node. | ||
| # @param [Hash] data Information about the node. | ||
| def initialize(name, data) | ||
| @name = name | ||
| @data = data | ||
| end | ||
|
|
||
| # @return [JayAPI::Elasticsearch::Stats::Node::Storage] Storage | ||
| # information about the node. | ||
| # @raise [JayAPI::Elasticsearch::Stats::Errors::StatsDataNotAvailable] | ||
| # If there is no storage information for the node. | ||
| def storage | ||
| @storage ||= ::JayAPI::Elasticsearch::Stats::Node::Storage.new(fs_totals) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :data | ||
|
|
||
| # @return [Hash] Aggregated information about the +Node+'s | ||
| # filesystem. | ||
| # @raise [JayAPI::Elasticsearch::Stats::Errors::StatsDataNotAvailable] | ||
| # If there is no filesystem information for the node. | ||
| def fs_totals | ||
| @fs_totals ||= data.dig('fs', 'total') || raise( | ||
| ::JayAPI::Elasticsearch::Stats::Errors::StatsDataNotAvailable, | ||
| "Filesystem data not available for node #{name}" | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
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,55 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module JayAPI | ||
| module Elasticsearch | ||
| class Stats | ||
| class Node | ||
| # Holds storage information related to one of the nodes in the | ||
| # Elasticsearch cluster. | ||
| class Storage | ||
|
sergio-bobillier marked this conversation as resolved.
|
||
| TOTAL_KEY = 'total_in_bytes' | ||
| FREE_KEY = 'free_in_bytes' | ||
| AVAILABLE_KEY = 'available_in_bytes' | ||
|
sergio-bobillier marked this conversation as resolved.
|
||
|
|
||
| # @param [Hash] data Data about the Node's storage. | ||
| def initialize(data) | ||
| @data = data | ||
| end | ||
|
|
||
| # @return [Integer] The total size of the storage (in bytes) | ||
| def total | ||
| @total ||= data[TOTAL_KEY] | ||
| end | ||
|
|
||
| # @return [Integer] The total number of bytes that are free on the | ||
| # node. | ||
| def free | ||
| @free ||= data[FREE_KEY] | ||
| end | ||
|
|
||
| # @return [Integer] The total number of bytes that are available on | ||
| # the node. In general this is equal to #free, but not always. | ||
|
sergio-bobillier marked this conversation as resolved.
|
||
| def available | ||
| @available ||= data[AVAILABLE_KEY] | ||
| end | ||
|
|
||
| # @return [self] A new instance of the class with the added storage of | ||
| # the receiver and +other+. | ||
| def +(other) | ||
| raise ArgumentError, "Cannot add #{self.class} and #{other.class} together" unless other.is_a?(self.class) | ||
|
|
||
| self.class.new( | ||
| TOTAL_KEY => total + other.total, | ||
| FREE_KEY => free + other.free, | ||
| AVAILABLE_KEY => available + other.available | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :data | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
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,46 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'forwardable' | ||
|
|
||
| require_relative 'node' | ||
|
|
||
| module JayAPI | ||
| module Elasticsearch | ||
| class Stats | ||
| # Provides access to the list of nodes returned by Elasticsearch's Stats API | ||
| class Nodes | ||
| extend Forwardable | ||
|
|
||
| def_delegator :nodes, :size | ||
|
|
||
| # @param [Hash] nodes Information about the nodes in the Elasticsearch | ||
| # cluster. | ||
| def initialize(nodes) | ||
| @nodes = nodes | ||
| end | ||
|
|
||
| # @return [Enumerator::Lazy<JayAPI::Elasticsearch::Stats::Node>] A lazy | ||
| # enumerator of +Node+ objects, one for each of the nodes. | ||
| def all | ||
| @all ||= with_lazy_instantiation { nodes } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :nodes | ||
|
|
||
| def build_node(args) | ||
|
Check failure on line 32 in lib/jay_api/elasticsearch/stats/nodes.rb
|
||
| ::JayAPI::Elasticsearch::Stats::Node.new(*args) | ||
| end | ||
|
|
||
| # Calls the given block and turns its return value into a lazy | ||
| # enumerator that instantiates a +Node+ object for each of the | ||
| # elements of the collection returned by the block. | ||
| # @return [Enumerator::Lazy<JayAPI::Elasticsearch::Stats::Node>] | ||
|
sergio-bobillier marked this conversation as resolved.
|
||
| def with_lazy_instantiation(&block) | ||
| block.call.lazy.map(&method(:build_node)) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
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,90 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'jay_api/elasticsearch/stats/node/storage' | ||
|
|
||
| RSpec.describe JayAPI::Elasticsearch::Stats::Node::Storage do | ||
| subject(:storage) { described_class.new(data) } | ||
|
|
||
| let(:data) do | ||
| { | ||
| 'total_in_bytes' => 5_126_127_616, | ||
| 'free_in_bytes' => 4_576_702_464, | ||
| 'available_in_bytes' => 4_559_925_248 | ||
| } | ||
| end | ||
|
|
||
| describe '#total' do | ||
| subject(:method_call) { storage.total } | ||
|
|
||
| it 'returns the total number of bytes' do | ||
| expect(method_call).to eq(5_126_127_616) | ||
| end | ||
| end | ||
|
|
||
| describe '#free' do | ||
| subject(:method_call) { storage.free } | ||
|
|
||
| it 'returns the total number of free bytes' do | ||
| expect(method_call).to eq(4_576_702_464) | ||
| end | ||
| end | ||
|
|
||
| describe '#available' do | ||
| subject(:method_call) { storage.available } | ||
|
|
||
| it 'returns the total number of available bytes' do | ||
| expect(method_call).to eq(4_559_925_248) | ||
| end | ||
| end | ||
|
|
||
| describe '#+' do | ||
| subject(:method_call) { storage + other } | ||
|
|
||
| let(:other) do | ||
| described_class.new( | ||
| 'total_in_bytes' => 316_863_741_952, | ||
| 'free_in_bytes' => 25_923_690_496, | ||
| 'available_in_bytes' => 25_906_913_280 | ||
| ) | ||
| end | ||
|
|
||
| context "when 'other' is not an instance of #{described_class}" do | ||
| let(:other) do | ||
| { | ||
| 'total_in_bytes' => 316_863_741_952, | ||
| 'free_in_bytes' => 25_923_690_496, | ||
| 'available_in_bytes' => 25_906_913_280 | ||
| } | ||
| end | ||
|
|
||
| it 'raises an ArgumentError' do | ||
| expect { method_call }.to raise_error( | ||
| ArgumentError, | ||
| 'Cannot add JayAPI::Elasticsearch::Stats::Node::Storage and Hash together' | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| it 'does not return the receiver nor other' do | ||
| expect(method_call).not_to be(storage) | ||
| expect(method_call).not_to be(other) | ||
| method_call | ||
| end | ||
|
|
||
| it "returns a new instance of #{described_class}" do | ||
| expect(method_call).to be_a(described_class) | ||
| end | ||
|
|
||
| it "returns an instance of #{described_class} with the expected number of total bytes" do | ||
| expect(method_call.total).to eq(321_989_869_568) | ||
| end | ||
|
|
||
| it "returns an instance of #{described_class} with the expected number of free bytes" do | ||
| expect(method_call.free).to eq(30_500_392_960) | ||
| end | ||
|
|
||
| it "returns an instance of #{described_class} with the expected number of available bytes" do | ||
| expect(method_call.available).to eq(30_466_838_528) | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.