Module: Riemann::Tools::Utils

Included in:
Health, TLSCheck::TLSCheckResult
Defined in:
lib/riemann/tools/utils.rb

Overview

:nodoc:

Defined Under Namespace

Classes: StringTokenizer

Instance Method Summary collapse

Instance Method Details

#distance_of_time_in_words_to_utcnow(to_time) ⇒ Object

Stolen from ActionView, to avoid pulling a lot of dependencies



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/riemann/tools/utils.rb', line 75

def distance_of_time_in_words_to_utcnow(to_time)
  distance_in_seconds = (to_time - utcnow).round.abs
  distance_in_minutes = distance_in_seconds / 60

  case distance_in_minutes
  when 0                then 'less than 1 minute'
  when 1...45           then pluralize_string('%d minute', distance_in_minutes)
  when 45...1440        then pluralize_string('about %d hour', (distance_in_minutes.to_f / 60.0).round)
    # 24 hours up to 30 days
  when 1440...43_200    then pluralize_string('%d day', (distance_in_minutes.to_f / 1440.0).round)
    # 30 days up to 60 days
  when 43_200...86_400  then pluralize_string('about %d month', (distance_in_minutes.to_f / 43_200.0).round)
    # 60 days up to 365 days
  when 86_400...525_600 then pluralize_string('%d month', (distance_in_minutes.to_f / 43_200.0).round)
  else
    pluralize_string('about %d year', (distance_in_minutes.to_f / 525_600.0).round)
  end
end

#pluralize_string(string, number) ⇒ Object



94
95
96
# File 'lib/riemann/tools/utils.rb', line 94

def pluralize_string(string, number)
  format(string, number) + (number == 1 ? '' : 's')
end

#reverse_numeric_sort_with_header(data, header: 1, count: 10) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/riemann/tools/utils.rb', line 56

def reverse_numeric_sort_with_header(data, header: 1, count: 10)
  lines = data.chomp.split("\n")
  header = lines.shift(header)

  lines.sort_by!(&:to_f)
  lines.reverse!

  (header + lines[0, count]).join("\n")
end

#utcnowObject

The current date and time, but in UTC



99
100
101
# File 'lib/riemann/tools/utils.rb', line 99

def utcnow
  Time.at(Time.now, in: '+00:00')
end

#when_from_utcnow(date) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/riemann/tools/utils.rb', line 66

def when_from_utcnow(date)
  if date > utcnow
    "in #{distance_of_time_in_words_to_utcnow(date)}"
  else
    "#{distance_of_time_in_words_to_utcnow(date)} ago"
  end
end