Class: Aws::RiKanjoo

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/rikanjo.rb,
lib/aws/rikanjo/mode/ec2.rb,
lib/aws/rikanjo/mode/rds.rb

Defined Under Namespace

Classes: Mode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode = 'ec2', region, instance_type, ri_util, multiaz) ⇒ RiKanjoo

Returns a new instance of RiKanjoo.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/aws/rikanjo.rb', line 14

def initialize(mode='ec2', region, instance_type, ri_util, multiaz)
  if mode == 'ec2'
    @mode_class = Aws::RiKanjoo::Mode::Ec2.new(region, instance_type, ri_util)
  elsif mode == 'rds'
    @mode_class = Aws::RiKanjoo::Mode::Rds.new(region, instance_type, ri_util, multiaz)
  end
  @region        = region
  @instance_type = instance_type
  @ri_util       = ri_util
  @om_info       = Hash.new
  @ri_info       = Hash.new
end

Instance Attribute Details

#instance_typeObject (readonly)

Returns the value of attribute instance_type.



12
13
14
# File 'lib/aws/rikanjo.rb', line 12

def instance_type
  @instance_type
end

#regionObject (readonly)

Returns the value of attribute region.



12
13
14
# File 'lib/aws/rikanjo.rb', line 12

def region
  @region
end

#ri_utilObject (readonly)

Returns the value of attribute ri_util.



12
13
14
# File 'lib/aws/rikanjo.rb', line 12

def ri_util
  @ri_util
end

Instance Method Details

#calc_year_costObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/aws/rikanjo.rb', line 44

def calc_year_cost

  om_hr_price = @om_info[:hr_price].to_f
  ri_hr_price = @ri_info[@ri_util][:hr_price].to_f
  upfront    = @ri_info[@ri_util][:upfront].to_f

  om_day_paid = (om_hr_price * 24).to_f
  ri_day_paid = (ri_hr_price * 24).to_f

  sum_om_price = om_day_paid
  sum_ri_price = ri_day_paid + upfront
  365.times.each do |d|
    d = d + 1
    sum_om_price = sum_om_price + om_day_paid
    sum_ri_price = sum_ri_price + ri_day_paid

    if sum_ri_price < sum_om_price
      @ri_info[@ri_util].store(:sweet_spot_price, sum_ri_price.round(2))
      @ri_info[@ri_util].store(:sweet_spot_start_day, d)
      break 
    end
  end
  total_om_price = (om_day_paid * 365).round(2)
  @om_info.store(:yr_price, total_om_price)
  total_ri_price = ((ri_day_paid * 365) + upfront).round(2)
  @ri_info[@ri_util].store(:yr_price, total_ri_price)

  # exp. @om_info = {:hr_price=>"0.350", :yr_price=>3066.0}
  # exp. @ri_info = {"ri_util"=>{:upfront=>"NNN", :hr_price=>"NNN",
  #                  :sweet_spot_price=>NNN, :sweet_spot_start_day=>78, :yr_price=>2074.56}}
  return @om_info, @ri_info
end

#om_get_hr_priceObject



27
28
29
30
31
32
33
34
# File 'lib/aws/rikanjo.rb', line 27

def om_get_hr_price
   # TODO: merge om and ri
  uri = URI.parse("#{@mode_class.price_url}/#{@mode_class.om_price_file}")
  contents = Net::HTTP.get(uri)

  # parse om info
  @om_info = @mode_class.om_price_from_contents(contents)
end

#ri_get_hr_price_and_upfrontObject



36
37
38
39
40
41
42
# File 'lib/aws/rikanjo.rb', line 36

def ri_get_hr_price_and_upfront
  uri = URI.parse("#{@mode_class.price_url}/#{@mode_class.ri_price_file}")
  contents = Net::HTTP.get(uri)

  # parse ri info
  @ri_info = @mode_class.ri_price_from_contents(contents)
end

#total_cost_yearObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/aws/rikanjo.rb', line 77

def total_cost_year
   om_get_hr_price
   ri_get_hr_price_and_upfront
   calc_year_cost 

   discount_per = (100 - ((@ri_info[@ri_util][:yr_price] / @om_info[:yr_price]) * 100)).round(2)

   # If the Reserved Instance does not become cheaper than the On-demand Instance.
   if @ri_info[@ri_util][:sweet_spot_start_day].nil?
     puts "\"Price of the Reserved Instance is higher than the price of On-Demand Instances.\""
     puts "\"region\" : #{@region}"
     puts "\"instance_type\" : #{@instance_type}"
     puts "\"ri_util\" : #{@ri_util}"
     puts "\"discont percent (percent)\" : #{discount_per}"
     puts "\"ondemand hour price (doller)\" : #{@om_info[:hr_price]}"
     puts "\"reserved hour price (doller)\" : #{@ri_info[@ri_util][:hr_price]}"
     puts "\"ondemand year price (doller)\" : #{@om_info[:yr_price]}"
     puts "\"reserved year price (doller)\" : #{@ri_info[@ri_util][:yr_price]}"
     puts "\"reserved upfront (doller)\" : #{@ri_info[@ri_util][:upfront]}"

     exit 1
   end

   sweet_spot_date = Date.today + @ri_info[@ri_util][:sweet_spot_start_day]

   puts "\"region\" : #{@region}"
   puts "\"instance_type\" : #{@instance_type}"
   puts "\"ri_util\" : #{@ri_util}"
   puts "\"discont percent (percent)\" : #{discount_per}"
   puts "\"ondemand hour price (doller)\" : #{@om_info[:hr_price]}"
   puts "\"reserved hour price (doller)\" : #{@ri_info[@ri_util][:hr_price]}"
   puts "\"ondemand year price (doller)\" : #{@om_info[:yr_price]}"
   puts "\"reserved year price (doller)\" : #{@ri_info[@ri_util][:yr_price]}"
   puts "\"reserved upfront (doller)\" : #{@ri_info[@ri_util][:upfront]}"
   puts "\"sweet spot day (day)\" : #{@ri_info[@ri_util][:sweet_spot_start_day]}"
   puts "\"sweet spot date (date)\" : #{sweet_spot_date}"
   puts "\"sweet spot price (doller)\" : #{@ri_info[@ri_util][:sweet_spot_price]}"
end