class StatusPageRuby::Services::BuildStatsTable
Constants
- HEADINGS
- SECONDS_IN_DAY
- SECONDS_IN_HOUR
- SECONDS_IN_MINUTE
Attributes
status_repository[R]
Public Class Methods
new(status_repository)
click to toggle source
# File lib/status_page_ruby/services/build_stats_table.rb, line 11 def initialize(status_repository) @status_repository = status_repository end
Public Instance Methods
call(service = nil)
click to toggle source
# File lib/status_page_ruby/services/build_stats_table.rb, line 15 def call(service = nil) Terminal::Table.new( headings: HEADINGS, rows: build_rows(service) ).to_s end
Private Instance Methods
build_rows(service)
click to toggle source
# File lib/status_page_ruby/services/build_stats_table.rb, line 24 def build_rows(service) find_records(service) .group_by(&:service) .each_with_object([]) do |(key, records), results| results << [ key, readable_time_amount(calculate_up_since(records)), readable_time_amount(calculate_down_time(records)) ] end end
calculate_down_time(records)
click to toggle source
# File lib/status_page_ruby/services/build_stats_table.rb, line 37 def calculate_down_time(records) records .sort_by { |record| record.time.to_i } .each_cons(2) .inject(0) { |result, (first, second)| result + duration_between(first, second) } end
calculate_up_since(records)
click to toggle source
# File lib/status_page_ruby/services/build_stats_table.rb, line 50 def calculate_up_since(records) record = take_up_since(records) return if record.nil? Time.now.to_i - record.time.to_i end
duration_between(first, second)
click to toggle source
# File lib/status_page_ruby/services/build_stats_table.rb, line 44 def duration_between(first, second) return 0 if first.up? (second.nil? ? Time.now.to_i : second.time.to_i) - first.time.to_i end
find_records(service)
click to toggle source
# File lib/status_page_ruby/services/build_stats_table.rb, line 72 def find_records(service) return status_repository.all if service.nil? status_repository.where(service: service) end
readable_time_amount(duration_sec)
click to toggle source
# File lib/status_page_ruby/services/build_stats_table.rb, line 57 def readable_time_amount(duration_sec) return 'N/A' if duration_sec.nil? return "#{duration_sec / SECONDS_IN_MINUTE} minutes" if duration_sec < SECONDS_IN_HOUR return "#{duration_sec / SECONDS_IN_HOUR} hours" if duration_sec < SECONDS_IN_DAY "#{duration_sec / SECONDS_IN_DAY} days" end
take_up_since(records)
click to toggle source
# File lib/status_page_ruby/services/build_stats_table.rb, line 65 def take_up_since(records) records .sort_by { |record| -record.time.to_i } .take_while(&:up?) .first end