Class: Joyent::Cloud::Pricing::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/pricing/reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commit = COMMIT, zones_in_use = []) ⇒ Reporter

Returns a new instance of Reporter.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pricing/reporter.rb', line 10

def initialize(commit = COMMIT, zones_in_use = [])
  @commit = case commit
              when String
                Joyent::Cloud::Pricing::Commit.from_yaml(commit)
              when Joyent::Cloud::Pricing::Commit
                commit
              when nil
                Joyent::Cloud::Pricing::Commit.new
              else
                raise NotImplementedError, "Unknown type of commit passed: #{commit.inspect}"
            end

  @zones_in_use = zones_in_use
  @analyzer = Joyent::Cloud::Pricing::Analyzer.new(@commit, @zones_in_use)
  @formatter = Joyent::Cloud::Pricing::Formatter.new(pricing.config)
  @print_zone_list = true
  @template = File.read(File.expand_path('../report.txt.erb', __FILE__))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Various formatting helpers follow below Note that we delegate to analyzer for majority of calls. Sorry about that method_missing :(



43
44
45
46
47
48
49
# File 'lib/pricing/reporter.rb', line 43

def method_missing(method, *args, &block)
  if @analyzer.respond_to?(method)
    @analyzer.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#analyzerObject

Returns the value of attribute analyzer.



8
9
10
# File 'lib/pricing/reporter.rb', line 8

def analyzer
  @analyzer
end

#commitObject

Returns the value of attribute commit.



8
9
10
# File 'lib/pricing/reporter.rb', line 8

def commit
  @commit
end

#formatterObject

Returns the value of attribute formatter.



8
9
10
# File 'lib/pricing/reporter.rb', line 8

def formatter
  @formatter
end

Returns the value of attribute print_zone_list.



8
9
10
# File 'lib/pricing/reporter.rb', line 8

def print_zone_list
  @print_zone_list
end

#templateObject

Returns the value of attribute template.



8
9
10
# File 'lib/pricing/reporter.rb', line 8

def template
  @template
end

#zones_in_useObject

Returns the value of attribute zones_in_use.



8
9
10
# File 'lib/pricing/reporter.rb', line 8

def zones_in_use
  @zones_in_use
end

Instance Method Details

#excess_zones_for_printObject



59
60
61
# File 'lib/pricing/reporter.rb', line 59

def excess_zones_for_print
  zones_for_print(zone_counts_to_list(analyzer.excess_zone_counts), :yellow)
end

#format_price(*args) ⇒ Object



51
52
53
# File 'lib/pricing/reporter.rb', line 51

def format_price *args
  @formatter.format_price *args
end

#have_commit_pricing?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/pricing/reporter.rb', line 55

def have_commit_pricing?
  commit.reserves.size > 0
end

#over_reserved_zones_for_printObject



63
64
65
# File 'lib/pricing/reporter.rb', line 63

def over_reserved_zones_for_print
  zones_for_print(analyzer.over_reserved_zone_counts)
end

#pricingObject



35
36
37
# File 'lib/pricing/reporter.rb', line 35

def pricing
  Joyent::Cloud::Pricing::Configuration.instance
end

#render(options = {}) ⇒ Object



29
30
31
32
33
# File 'lib/pricing/reporter.rb', line 29

def render(options = {})
  disable_color if (options && options[:disable_color]) || ENV['NO_COLOR']
  @r = self
  ERB.new(template, 0, '-').result(binding)
end

#unknown_zones_for_printObject



67
68
69
# File 'lib/pricing/reporter.rb', line 67

def unknown_zones_for_print
  zone_list_for_print(analyzer.unknown_zone_counts.keys)
end

#zone_counts_to_list(zone_count_hash) ⇒ Object



84
85
86
87
88
89
# File 'lib/pricing/reporter.rb', line 84

def zone_counts_to_list zone_count_hash
  zones = zone_count_hash.each_pair.
      map { |flavor, count| [flavor, count, pricing.monthly(flavor) * count] }.
      sort { |x, y| y[2] <=> x[2] }
  zones
end

#zone_list_for_print(list, format = ' %-40s') ⇒ Object



80
81
82
# File 'lib/pricing/reporter.rb', line 80

def zone_list_for_print(list, format = '    %-40s')
  list.map { |k| sprintf(format, k) }.join("\n")
end

#zone_props_to_string(prop_type, width, suffix = '', divide_by = 1) ⇒ Object



91
92
93
94
95
96
# File 'lib/pricing/reporter.rb', line 91

def zone_props_to_string(prop_type, width, suffix = '', divide_by = 1)
  props = analyzer.send(prop_type)
  [props[:reserved], props[:unreserved], props[:total]].map do |value|
    sprintf("%#{width}d#{suffix}", value / divide_by)
  end
end

#zones_for_print(zone_list, color = nil) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/pricing/reporter.rb', line 71

def zones_for_print(zone_list, color = nil)
  zone_list.map do |tuple|
    flavor, count, monthly = tuple
    price = formatter.format_price(monthly, 16)
    price = price.send(color) if color
    sprintf("     %2d x %-36s   %16s", count, flavor, price)
  end.join("\n")
end