class Hu::Cli::Scale
Heroku Scale
Constants
- DYNO_TYPES
Public Instance Methods
h()
click to toggle source
# File lib/hu/scale.rb, line 195 def h @h ||= PlatformAPI.connect_oauth(Hu::API_TOKEN) end
heroku_state(force_refresh = false)
click to toggle source
# File lib/hu/scale.rb, line 174 def heroku_state(force_refresh = false) return @heroku_state unless force_refresh || @heroku_state.nil? data = {} app_names = h.app.list.map { |e| e['name'] }.reject { |e| ignored_app?(e) } threads = [] app_names.each_with_index do |app_name, _i| threads << Thread.new do h.formation.list(app_name).each do |dyno| dyno_size = dyno['size'].downcase data[app_name] ||= {} data[app_name][dyno_size] ||= [] data[app_name][dyno_size] << { type: dyno['type'], quantity: dyno['quantity'] } end end end threads.each_with_index do |t, _i| t.join end @heroku_state = data end
ignored_app?(app_name)
click to toggle source
# File lib/hu/scale.rb, line 167 def ignored_app?(app_name) ENV.fetch('HU_IGNORE_APPS', '').split(' ').each do |p| return true if File.fnmatch(p, app_name) end false end
print_state(state)
click to toggle source
# File lib/hu/scale.rb, line 115 def print_state(state) cols = ['$/mo', 'fH12ML', 'formation'] rows = [] total_cost = 0 max_app_name_len = state.keys.reduce(0) { |a, e| [a, e.length].max } state.sort.each do |app_name, dyno_types| next if ignored_app?(app_name) row = { 'app' => app_name, 'fH12ML' => '......', 'formation' => "\e[0mheroku ps:scale -a #{app_name.ljust(max_app_name_len).color(:green).bright}" } cost = 0 dyno_types.each do |dyno_type, dynos| dynos.each do |dyno| row = Marshal.load(Marshal.dump(row)) dyno_type_str = dyno[:type] quant_colon_type = "#{dyno[:quantity]}:#{dyno_type}" if dyno[:quantity] == 0 dyno_type_str = dyno_type_str.color(:black).bright quant_colon_type = quant_colon_type.color(:black).bright else dyno_type_str = dyno_type_str.color(:yellow) quant_colon_type = quant_colon_type.color(:yellow) idx = DYNO_TYPES.find { |e| e[:id] == dyno_type }[:index] row['fH12ML'][idx] = 'fH12ML'[idx] end row['formation'] += " #{dyno_type_str}" + '='.color(:black).bright + quant_colon_type.to_s cost += DYNO_TYPES.find { |e| e[:id] == dyno_type }[:usd] * dyno[:quantity] end end new_row = ''.dup row['fH12ML'].each_char do |l| new_row << (DYNO_TYPES.find {|dt| dt[:letter] == l}[:color] + l + "\e[0;30;1m" rescue '.') end row['fH12ML'] = "\e[0;30;1m"+new_row total_cost += cost row['$/mo'] = format '%4d', cost rows << row end puts puts ' Current Dyno Formation '.inverse puts tpl = "%-5s %6s %s\n" printf tpl.bright, *cols rows.each do |row| printf tpl, *cols.map { |col| row[col] } end puts puts "Total: $#{total_cost} USD/mo" puts end
print_types()
click to toggle source
# File lib/hu/scale.rb, line 103 def print_types cols = %w(id ram cpu dedicated sleeps usd) tpl = "%-16s %5s %4s %11s %7s %5s\n" puts puts ' Available Dyno Types '.inverse puts printf tpl.bright, *cols DYNO_TYPES.each do |dyno_type| printf tpl, *cols.map { |col| dyno_type[col.to_sym] } end end
scale(_cmd, _opts, _args)
click to toggle source
# File lib/hu/scale.rb, line 97 def scale(_cmd, _opts, _args) print_types puts print_state heroku_state end