Class: DynamoAutoscale::ScaleReport

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/dynamo-autoscale/scale_report.rb

Constant Summary collapse

TEMPLATE =
File.join(DynamoAutoscale.root, 'templates', 'scale_report_email.erb')

Instance Method Summary collapse

Methods included from Logger

included, logger, #logger, logger=

Constructor Details

#initialize(table) ⇒ ScaleReport

Returns a new instance of ScaleReport.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dynamo-autoscale/scale_report.rb', line 7

def initialize table
  @table = table
  @erb = ERB.new(File.read(TEMPLATE), nil, '-')

  if DynamoAutoscale.config[:dry_run]
    @enabled = false
  elsif config = DynamoAutoscale.config[:email]
    @enabled = true
    Pony.options = config
  else
    @enabled = false
  end
end

Instance Method Details

#email_contentObject



25
26
27
# File 'lib/dynamo-autoscale/scale_report.rb', line 25

def email_content
  @erb.result(binding)
end

#email_subjectObject



21
22
23
# File 'lib/dynamo-autoscale/scale_report.rb', line 21

def email_subject
  "Scale event for #{@table.name}"
end

#formatted_scale_event(scale_event) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dynamo-autoscale/scale_report.rb', line 49

def formatted_scale_event(scale_event)
  max_length = max_metric_length(scale_event)
  
  ['reads', 'writes'].map do |type|
    next unless scale_event.has_key? "#{type}_from".to_sym

    direction = scale_direction( scale_event["#{type}_from".to_sym], scale_event["#{type}_to".to_sym] )
    type_from = scale_event["#{type}_from".to_sym].to_s.rjust(max_length)
    type_to   = scale_event["#{type}_to".to_sym].to_s.rjust(max_length)

    "#{type.capitalize.rjust(6)}: #{direction} from #{type_from} to #{type_to}"
  end.compact
end

#max_metric_length(scale_event) ⇒ Object



63
64
65
# File 'lib/dynamo-autoscale/scale_report.rb', line 63

def max_metric_length(scale_event)
  scale_event.values.max.to_s.length
end

#scale_direction(from, to) ⇒ Object



67
68
69
# File 'lib/dynamo-autoscale/scale_report.rb', line 67

def scale_direction(from, to)
  from > to ? 'DOWN' : ' UP '
end

#sendObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dynamo-autoscale/scale_report.rb', line 29

def send
  return false unless @enabled

  result = Pony.mail({
    subject: email_subject,
    body:    email_content,
  })

  if result
    logger.info "[mailer] Mail sent successfully."
    result
  else
    logger.error "[mailer] Failed to send email. Result: #{result.inspect}"
    false
  end
rescue => e
  logger.error "[mailer] Encountered an error: #{e.class}:#{e.message}"
  false
end