class ActiveAdmin::ElasticStats

Constants

CONTAINER_STYLE
VALUE_STYLE

Public Instance Methods

build(model, attributes = {}) click to toggle source
Calls superclass method
# File lib/active_admin/elastic_stats.rb, line 8
def build(model, attributes = {})
  super(attributes)

  index_name = model.index_name
  panel("Elastic #{index_name.titleize} Stats") do
    div(style: CONTAINER_STYLE) do
      begin
        if Elasticsearch::Model.client.indices.exists(index: index_name)
          build_fields(index_name)
        else
          show_error(index_name)
        end
      rescue Faraday::ConnectionFailed => e
        h2 e.message
      end
    end
  end
end

Private Instance Methods

average_fetch_time(primaries) click to toggle source
# File lib/active_admin/elastic_stats.rb, line 57
def average_fetch_time(primaries)
  total = primaries.dig('search', 'fetch_total')
  return 'N/A' if total.zero?
  average = primaries.dig('search', 'fetch_time_in_millis').to_f / total
  "#{average.round(2)} ms"
end
average_search_time(primaries) click to toggle source
# File lib/active_admin/elastic_stats.rb, line 50
def average_search_time(primaries)
  total = primaries.dig('search', 'query_total')
  return 'N/A' if total.zero?
  average = primaries.dig('search', 'query_time_in_millis').to_f / total
  "#{average.round(2)} ms"
end
build_fields(index_name) click to toggle source
# File lib/active_admin/elastic_stats.rb, line 29
def build_fields(index_name)
  stats = JSON.parse(Elasticsearch::Model.client.indices.stats(index: index_name).to_json)

  primaries = stats.dig('_all', 'primaries')

  {
    document_count: primaries.dig('docs', 'count'),
    size: number_to_human_size(primaries.dig('store', 'size_in_bytes')),
    index_time: primaries.dig('indexing', 'index_time_in_millis').to_s + ' ms',
    search_time: average_search_time(primaries),
    fetch_time: average_fetch_time(primaries)
  }.each do |name, value|
    div style: VALUE_STYLE do
      h1 value
      span name.to_s.titleize
    end
  end
rescue => e
  div { h3 e.message }
end
show_error(index_name) click to toggle source
# File lib/active_admin/elastic_stats.rb, line 64
def show_error(index_name)
  h2 "Index #{index_name} does not exist"
end