Module: LogStash::Inputs::FriendlyDurations

Defined in:
lib/logstash/inputs/friendly_durations.rb

Defined Under Namespace

Classes: ValidatedStruct

Constant Summary collapse

NUMBERS_RE =
/^(?<number>\d+(\.\d+)?)\s?(?<units>s((ec)?(ond)?)(s)?|m((in)?(ute)?)(s)?|h(our)?(s)?|d(ay)?(s)?|w(eek)?(s)?|us(ec)?(s)?|ms(ec)?(s)?)?$/
HOURS =
3600
DAYS =
24 * HOURS
MEGA =
10**6
KILO =
10**3

Class Method Summary collapse

Class Method Details

.call(value, unit = "sec") ⇒ Object



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/logstash/inputs/friendly_durations.rb', line 17

def self.call(value, unit = "sec")
  # coerce into seconds
  val_string = value.to_s.strip
  matched = NUMBERS_RE.match(val_string)
  if matched.nil?
    failed_message = "Value '#{val_string}' is not a valid duration string e.g. 200 usec, 250ms, 60 sec, 18h, 21.5d, 1 day, 2w, 6 weeks"
    return ValidatedStruct.new(nil, failed_message)
  end
  multiplier = matched[:units] || unit
  numeric = matched[:number].to_f
  case multiplier
  when "m","min","mins","minute","minutes"
    ValidatedStruct.new(numeric * 60, nil)
  when "h","hour","hours"
    ValidatedStruct.new(numeric * HOURS, nil)
  when "d","day","days"
    ValidatedStruct.new(numeric * DAYS, nil)
  when "w","week","weeks"
    ValidatedStruct.new(numeric * 7 * DAYS, nil)
  when "ms","msec","msecs"
    ValidatedStruct.new(numeric / KILO, nil)
  when "us","usec","usecs"
    ValidatedStruct.new(numeric / MEGA, nil)
  else
    ValidatedStruct.new(numeric, nil)
  end
end