forked from elastic/logstash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes elastic#6128
- Loading branch information
Showing
13 changed files
with
380 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module LogStash | ||
module Util | ||
class TimeValue | ||
def initialize(duration, time_unit) | ||
@duration = duration | ||
@time_unit = time_unit | ||
end | ||
|
||
def self.from_value(value) | ||
if value.is_a?(TimeValue) | ||
TimeValue.new(value.duration, value.time_unit) | ||
elsif value.is_a?(::String) | ||
normalized = value.downcase.strip | ||
if normalized.end_with?("nanos") | ||
TimeValue.new(parse(normalized, 5), :nanosecond) | ||
elsif normalized.end_with?("micros") | ||
TimeValue.new(parse(normalized, 6), :microsecond) | ||
elsif normalized.end_with?("ms") | ||
TimeValue.new(parse(normalized, 2), :millisecond) | ||
elsif normalized.end_with?("s") | ||
TimeValue.new(parse(normalized, 1), :second) | ||
elsif normalized.end_with?("m") | ||
TimeValue.new(parse(normalized, 1), :minute) | ||
elsif normalized.end_with?("h") | ||
TimeValue.new(parse(normalized, 1), :hour) | ||
elsif normalized.end_with?("d") | ||
TimeValue.new(parse(normalized, 1), :day) | ||
elsif normalized =~ /^-0*1/ | ||
TimeValue.new(-1, :nanosecond) | ||
else | ||
raise ArgumentError.new("invalid time unit: \"#{value}\"") | ||
end | ||
else | ||
raise ArgumentError.new("value is not a string: #{value} [#{value.class}]") | ||
end | ||
end | ||
|
||
def to_nanos | ||
case @time_unit | ||
when :day | ||
86400000000000 * @duration | ||
when :hour | ||
3600000000000 * @duration | ||
when :minute | ||
60000000000 * @duration | ||
when :second | ||
1000000000 * @duration | ||
when :millisecond | ||
1000000 * @duration | ||
when :microsecond | ||
1000 * @duration | ||
when :nanosecond | ||
@duration | ||
end | ||
end | ||
|
||
def ==(other) | ||
self.duration == other.duration and self.time_unit == other.time_unit | ||
end | ||
|
||
def self.parse(value, suffix) | ||
Integer(value[0..(value.size - suffix - 1)].strip) | ||
end | ||
|
||
private_class_method :parse | ||
attr_reader :duration | ||
attr_reader :time_unit | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# encoding: utf-8 | ||
require "spec_helper" | ||
require "logstash/settings" | ||
|
||
describe LogStash::Setting::TimeValue do | ||
subject { described_class.new("option", "-1") } | ||
describe "#set" do | ||
it "should coerce the default correctly" do | ||
expect(subject.value).to eq(LogStash::Util::TimeValue.new(-1, :nanosecond).to_nanos) | ||
end | ||
|
||
context "when a value is given outside of possible_values" do | ||
it "should raise an ArgumentError" do | ||
expect { subject.set("invalid") }.to raise_error(ArgumentError) | ||
end | ||
end | ||
context "when a value is given as a time value" do | ||
it "should set the value" do | ||
subject.set("18m") | ||
expect(subject.value).to eq(LogStash::Util::TimeValue.new(18, :minute).to_nanos) | ||
end | ||
end | ||
|
||
context "when a value is given as a nanosecond" do | ||
it "should set the value" do | ||
subject.set(5) | ||
expect(subject.value).to eq(LogStash::Util::TimeValue.new(5, :nanosecond).to_nanos) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.