Class: Legion::CLI::Cost

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/cost_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/legion/cli/cost_command.rb', line 8

def self.exit_on_failure?
  true
end

Instance Method Details

#exportObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/cli/cost_command.rb', line 57

def export
  data = build_client.summary(period: options[:period])
  case options[:format]
  when 'json'
    say Legion::JSON.dump(data)
  when 'csv'
    say 'period,today,week,month,workers'
    say "#{options[:period]},#{data[:today]},#{data[:week]},#{data[:month]},#{data[:workers]}"
  end
end

#summaryObject



16
17
18
19
20
21
22
23
24
# File 'lib/legion/cli/cost_command.rb', line 16

def summary
  data = build_client.summary(period: options[:period])
  say 'Cost Summary', :green
  say '-' * 30
  say format('  Today:      $%.2f', data[:today] || 0)
  say format('  This Week:  $%.2f', data[:week] || 0)
  say format('  This Month: $%.2f', data[:month] || 0)
  say "  Workers:    #{data[:workers] || 0}"
end

#topObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/legion/cli/cost_command.rb', line 40

def top
  consumers = build_client.top_consumers(limit: options[:limit])
  if consumers.empty?
    say 'No cost data available', :yellow
    return
  end
  say 'Top Cost Consumers', :green
  say '-' * 40
  consumers.each_with_index do |c, i|
    cost = c.dig(:cost, :total_cost_usd) || 0
    say format('  %<rank>d. %-25<name>s $%<cost>.2f', rank: i + 1, name: c[:worker_id], cost: cost)
  end
end

#worker(id) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/legion/cli/cost_command.rb', line 27

def worker(id)
  data = build_client.worker_cost(id)
  if data.empty?
    say "No cost data for worker #{id}", :yellow
    return
  end
  say "Worker: #{id}", :green
  say '-' * 30
  data.each { |k, v| say "  #{k}: #{v}" }
end