Class: AwsReporting::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-reporting/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#forceObject

Returns the value of attribute force.



3
4
5
# File 'lib/aws-reporting/generator.rb', line 3

def force
  @force
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/aws-reporting/generator.rb', line 3

def path
  @path
end

Instance Method Details

#build_alarm_tree(alarms) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/aws-reporting/generator.rb', line 85

def build_alarm_tree(alarms)
  alarm_tree = {}
  alarms.each{|alarm|
    key = {:region => alarm[:region],
           :namespace => alarm[:namespace],
           :dimensions => alarm[:dimensions],
           :metric_name => alarm[:metric_name]}
    alarm_tree[key] = merge_status(alarm_tree[key], alarm[:status])
  }
  alarm_tree
end

#copy_template(path, force) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/aws-reporting/generator.rb', line 147

def copy_template(path, force)
  report_path = File.expand_path(path)
  if force != true and File.exist?(path)
    error = AwsReporting::Error::OverwriteError.new
    error.path = report_path
    raise error
  end

  template_path = File.expand_path('../../../template', __FILE__)
  template_files = Dir.glob(template_path + '/*')
  FileUtils.mkdir_p(report_path)
  FileUtils.cp_r(template_files, report_path)
end

#download(base_path, plan, start_time, end_time, period, name_resolvers, timestamp) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/aws-reporting/generator.rb', line 105

def download(base_path, plan, start_time, end_time, period, name_resolvers, timestamp)
  metrics = Hash.new{|h, k| h[k] = Hash.new{|h, k| h[k] = []}}

  alarms = AwsReporting::Alarm.get_alarms()
  alarm_tree = build_alarm_tree(alarms)

  mutex = Mutex.new
  started_at = Time.now
  num_of_metrics = plan.length
  num_of_downloaded = 0
  Parallel.each_with_index(plan, :in_threads => 8){|entry, i|
    namespace = entry[:namespace]
    metric_name = entry[:metric_name]
    dimensions = entry[:dimensions]
    statistics = entry[:statistics]
    region = entry[:region]

    mutex.synchronize do
      num_of_downloaded += 1
      Formatador.redisplay_progressbar(num_of_downloaded, num_of_metrics, {:started_at => started_at})
    end

    data = AwsReporting::Statistics.get(region, namespace, metric_name, start_time, end_time, period, dimensions, statistics)
    set_status(data, alarm_tree)
    file = AwsReporting::Store.save(base_path, data)

    identification = {:dimensions => dimensions, :region => region}
    metrics[namespace][identification] << file
  }

  report_info = {:start_time => start_time.to_s,
                 :end_time => end_time.to_s,
                 :period => period.to_s,
                 :timestamp => timestamp.to_s,
                 :num_of_metrics => plan.length.to_s,
                 :version => Version.get}

  open(base_path + '/metrics.json', 'w'){|f|
    f.print JSON.dump({:report_info => report_info, :metrics => transform(metrics, name_resolvers), :alarms => alarms})
  }
end

#expand(dimension_type, dimensions) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/aws-reporting/generator.rb', line 59

def expand(dimension_type, dimensions)
  dimension_hash = {}

  dimensions.each{|dimension|
    dimension_hash[dimension[:name]] = dimension[:value]
  }

  values = []
  dimension_type.each{|dimension_name|
    values << dimension_hash[dimension_name] 
  }
  values.join(' ')
end

#format_path(path) ⇒ Object



5
6
7
8
# File 'lib/aws-reporting/generator.rb', line 5

def format_path(path)
  path.split('/')[-2..-1]
      .join('/')
end

#generateObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/aws-reporting/generator.rb', line 161

def generate
  timestamp = Time.now

  AwsReporting::Config.load()

  puts 'Report generating started.'
  copy_template(@path, @force)
  data_dir = @path + '/data'

  puts '(1/3) Planning...'
  plan = Plan.generate
  puts "Planning complete."
  start_time = Time.now - 24 * 60 * 60 * 14
  end_time = Time.now
  period = 60 * 60
  puts '(2/3) Building name tables...'
  name_resolvers = AwsReporting::Resolvers.new
  name_resolvers.init
  puts 'Name tables were builded.'
  puts '(3/3) Downloading metrics...'
  download(data_dir, plan, start_time, end_time, period, name_resolvers, timestamp)
  puts 'Downloading metrics done.'
  puts 'Report generating complete!'
end

#merge_status(s0, s1) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/aws-reporting/generator.rb', line 73

def merge_status(s0, s1)
  if s0 == nil or s1 == nil
    s0 || s1
  else
    if s0 == :ALARM or s1 == :ALARM
      'ALARM'
    else
      'OK'
    end
  end
end

#set_status(data, alarm_tree) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/aws-reporting/generator.rb', line 97

def set_status(data, alarm_tree)
  key = {:region => data[:info][:region],
         :namespace => data[:info][:namespace],
         :dimensions => data[:info][:dimensions],
         :metric_name => data[:info][:metric_name]}
  data[:info][:status] = alarm_tree[key] if alarm_tree[key]
end

#sort_result!(result) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/aws-reporting/generator.rb', line 45

def sort_result!(result)
  result.sort_by!{|namespace_table| namespace_table[:namespace]}
  result.each{|namespace_table|
    namespace_table[:dimension_table].sort_by!{|dimension_table| dimension_table[:dimension_type].join(' ')}
  }

  result.each{|namespace_table|
    namespace_table[:dimension_table].each{|dimension_table|
      dimension_type = dimension_table[:dimension_type]
      dimension_table[:elements].sort_by!{|element| expand(dimension_type, element[:dimensions]) }
    }
  }
end

#transform(metrics, name_resolvers) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/aws-reporting/generator.rb', line 10

def transform(metrics, name_resolvers)
  temp = Hash.new{|h, k| h[k] = Hash.new{|h, k| h[k] = [] }}
  metrics.each{|namespace, value|
    value.each{|identification, files|
      element =  {:region => identification[:region],
                  :namespace => namespace,
                  :dimensions => identification[:dimensions],
                  :files => files.sort_by{|entry| entry[:info][:metric_name]}.map{|entry| format_path(entry[:path])}}

      element[:name] = name_resolvers.get_name(element)

      dimension_type = identification[:dimensions].map{|item| item[:name]}.sort

      temp[namespace][dimension_type] << element
    }
  }

  temp2 = Hash.new{|h, k| h[k] = [] }
  temp.each{|namespace, dimension_table|
    dimension_table.each{|dimension_type, elements| 
      temp2[namespace] << {:dimension_type => dimension_type,
                           :elements => elements}
    }
  }

  result = []
  temp2.each{|namespace, dimension_table|
    result << {:namespace => namespace, :dimension_table => dimension_table}
  }

  sort_result!(result)

  result
end