Sha256: 980eab86f8c11fa67a0acb933488e533c570ed00a4ac76b047a15d89f402e6c2

Contents?: true

Size: 1.49 KB

Versions: 30

Compression:

Stored size: 1.49 KB

Contents

# encoding: utf-8

module LogStash module Inputs
  module FriendlyDurations
    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

    ValidatedStruct = Struct.new(:value, :error_message) do
      def to_a
        error_message.nil? ? [true, value] : [false, error_message]
      end
    end

    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
  end
end end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
logstash-input-file-4.4.6 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.4.5 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.4.4 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.4.3 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.4.2 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.4.1 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.4.0 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.3.1 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.3.0 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.2.4 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.2.3 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.2.2 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.2.1 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.2.0 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.1.18 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.1.17 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.1.16 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.1.15 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.1.14 lib/logstash/inputs/friendly_durations.rb
logstash-input-file-4.1.13 lib/logstash/inputs/friendly_durations.rb