Class: Maze::MetricsProcessor
- Inherits:
-
Object
- Object
- Maze::MetricsProcessor
- Defined in:
- lib/maze/metrics_processor.rb
Instance Method Summary collapse
-
#collate ⇒ Object
Organises the raw metrics into an Array of hashes representing each row of the CSV, whilst also capturing an overall set of column headings.
-
#initialize(metrics) ⇒ MetricsProcessor
constructor
A new instance of MetricsProcessor.
-
#process ⇒ Object
Collates the metrics given into a CSV-friendly structure and writes the CSV to disk.
- #to_csv_friendly(value) ⇒ Object
- #write_to_disk ⇒ Object
Constructor Details
#initialize(metrics) ⇒ MetricsProcessor
Returns a new instance of MetricsProcessor.
6 7 8 9 10 |
# File 'lib/maze/metrics_processor.rb', line 6 def initialize(metrics) @metrics = metrics @columns = Set.new @rows = [] end |
Instance Method Details
#collate ⇒ Object
Organises the raw metrics into an Array of hashes representing each row of the CSV, whilst also capturing an overall set of column headings
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/maze/metrics_processor.rb', line 22 def collate @metrics.all.each do |request| row = {} metric = request[:body] metric.each do |key, value| @columns.add key row[key] = value end @rows << row end end |
#process ⇒ Object
Collates the metrics given into a CSV-friendly structure and writes the CSV to disk
13 14 15 16 17 18 |
# File 'lib/maze/metrics_processor.rb', line 13 def process return if @metrics.size_all == 0 collate write_to_disk end |
#to_csv_friendly(value) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/maze/metrics_processor.rb', line 60 def to_csv_friendly(value) return value unless value.class == String if value.include?(' ') || value.include?(',') "\"#{value}\"" else value end end |
#write_to_disk ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/maze/metrics_processor.rb', line 34 def write_to_disk filepath = File.join(Dir.pwd, 'maze_output', 'metrics.csv') puts "Call with #{filepath}" File.open(filepath, 'w') do |file| # Write the header, with columns ordered alphabetically sorted_columns = @columns.to_a.sort! header = sorted_columns.join ',' file.puts header # Write the rows @rows.each do |row| row_values = sorted_columns.map do |column| if row.has_key? column to_csv_friendly(row[column]) else '' end end file.puts row_values.join ',' end end end |