module LogStash::Inputs::FriendlyDurations

Constants

DAYS
HOURS
KILO
MEGA
NUMBERS_RE
ValidatedStruct

Public Class Methods

call(value, unit = "sec") click to toggle source
# 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