module Kontena::Cli::Grids::Common

Public Instance Methods

calculate_filesystem_stats(nodes) click to toggle source

@param [Array<Hash>] nodes @return [Hash]

# File lib/kontena/cli/grids/common.rb, line 85
def calculate_filesystem_stats(nodes)
  total_fs = {
    'used' => 0.0,
    'total' => 0.0
  }
  nodes.each do |node|
    root_dir = node['engine_root_dir']
    filesystems = node.dig('resource_usage', 'filesystem') || []
    root_fs = filesystems.find{|fs| fs['name'] == root_dir}
    total_fs['used'] += root_fs['used']
    total_fs['total'] += root_fs['total']
  end

  total_fs
end
calculate_loads(nodes, node_count) click to toggle source

@param [Array<Hash>] nodes @param [Fixnum] node_count @return [Hash]

# File lib/kontena/cli/grids/common.rb, line 60
def calculate_loads(nodes, node_count)
  loads = {:'1m' => 0.0, :'5m' => 0.0, :'15m' => 0.0}
  return loads if node_count == 0

  loads[:'1m'] = nodes.map{|n| n.dig('resource_usage', 'load', '1m').to_f }.inject(:+) / node_count
  loads[:'5m'] = nodes.map{|n| n.dig('resource_usage', 'load', '5m').to_f }.inject(:+) / node_count
  loads[:'15m'] = nodes.map{|n| n.dig('resource_usage', 'load', '15m').to_f }.inject(:+) / node_count
  loads
end
calculate_mem_used(nodes) click to toggle source

@param [Array<Hash>] nodes @return [Float]

# File lib/kontena/cli/grids/common.rb, line 72
def calculate_mem_used(nodes)
  nodes.map{|n|
    mem = n.dig('resource_usage', 'memory')
    if mem
      mem['used'] - (mem['cached'] + mem['buffers'])
    else
      0.0
    end
  }.inject(:+)
end
find_grid_by_name(name) click to toggle source
# File lib/kontena/cli/grids/common.rb, line 101
def find_grid_by_name(name)
  grids['grids'].find {|grid| grid['name'] == name }
end
get_grid(name = nil) click to toggle source

@return [Hash] /v1/grids/:grid JSON { … }

# File lib/kontena/cli/grids/common.rb, line 111
def get_grid(name = nil)
  if name
    client(require_token).get("grids/#{name}")
  elsif current_grid
    client(require_token).get("grids/#{current_grid}")
  else
    exit_with_error "No grid given or selected"
  end
end
grids() click to toggle source
# File lib/kontena/cli/grids/common.rb, line 53
def grids
  @grids ||= client.get('grids')
end
print_grid(grid) click to toggle source

@param [Hash] grid

to_gigabytes(amount) click to toggle source
# File lib/kontena/cli/grids/common.rb, line 105
def to_gigabytes(amount)
  return 0.0 if amount.nil?
  (amount.to_f / 1024 / 1024 / 1024).to_f.round(2)
end