Module: Datadog::Core::Utils::Duration
- Defined in:
- lib/datadog/core/utils/duration.rb
Overview
Helper methods for parsing string values into Numeric
Class Method Summary collapse
Class Method Details
.call(value, base: :s) ⇒ Object
8 9 10 11 12 13 14 15 16 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 44 45 46 47 48 |
# File 'lib/datadog/core/utils/duration.rb', line 8 def self.call(value, base: :s) cast = if value.include?('.') method(:Float) else method(:Integer) end scale = case base when :s 1_000_000_000 when :ms 1_000_000 when :us 1000 when :ns 1 else raise ArgumentError, "invalid base: #{base.inspect}" end result = case value when /^(\d+(?:\.\d+)?)h$/ cast.call(Regexp.last_match(1)) * 1_000_000_000 * 60 * 60 / scale when /^(\d+(?:\.\d+)?)m$/ cast.call(Regexp.last_match(1)) * 1_000_000_000 * 60 / scale when /^(\d+(?:\.\d+)?)s$/ cast.call(Regexp.last_match(1)) * 1_000_000_000 / scale when /^(\d+(?:\.\d+)?)ms$/ cast.call(Regexp.last_match(1)) * 1_000_000 / scale when /^(\d+(?:\.\d+)?)us$/ cast.call(Regexp.last_match(1)) * 1_000 / scale when /^(\d+(?:\.\d+)?)ns$/ cast.call(Regexp.last_match(1)) / scale when /^(\d+(?:\.\d+)?)$/ cast.call(Regexp.last_match(1)) else raise ArgumentError, "invalid duration: #{value.inspect}" end # @type var result: Numeric result.round end |