-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics_calculator.rb
59 lines (45 loc) · 1.38 KB
/
statistics_calculator.rb
1
2
3
4
5
6
7
8
9
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'terminal-table'
require 'date'
class StatisticsCalculator
def initialize(n_subscribers)
@n_subscribers = n_subscribers
end
def calculate
table = Terminal::Table.new :headings => ['Id', 'Time Elapsed'], :rows => rows
File.write(output_file, table.to_s + "\n\n#{average_time_elapsed_log}")
puts average_time_elapsed_log
puts "Statistics persisted to: #{file_path}"
end
private
def average_time_elapsed_log
average_time_elapsed = (rows.map(&:last).sum)/rows.size
"Average time elapsed: #{average_time_elapsed} seconds"
end
def file_path
"./output/logs/output_#{@n_subscribers}.log"
end
def logs
@logs ||= File.read(file_path).split("\n").map { |l| l.split(',') }
end
def output_file
"./output/statistics/time_elapsed_#{@n_subscribers}.txt"
end
def parsed_publish_time
_, _, publish_time = publish_log
@parsed_publish_time ||= DateTime.parse(publish_time)
end
def publish_log
@publish_log ||= logs.find { |l| l.first == 'publish' }
end
def receive_logs
logs.select { |l| l.first == 'receive' }
end
def rows
@rows ||= receive_logs.map do |_, identifier, receipt_time|
time_elapsed = DateTime.parse(receipt_time).to_time - parsed_publish_time.to_time
[identifier, time_elapsed]
end
end
end
n_subscribers = ARGV.first
StatisticsCalculator.new(n_subscribers).calculate