Module: Influxer::TimestampQuoting

Included in:
Metrics, Relation
Defined in:
lib/influxer/metrics/quoting/timestamp.rb

Overview

:nodoc:

Constant Summary collapse

TIME_FACTORS =
{
  "ns" => 1_000_000_000,
  "ms" => 1_000,
  "s" => 1
}.freeze
DEFAULT_PRECISION =
"ns"

Instance Method Summary collapse

Instance Method Details

#factorize_timestamp(val, factor) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/influxer/metrics/quoting/timestamp.rb', line 55

def factorize_timestamp(val, factor)
  case val
  when Numeric
    (val.to_f * factor).to_i
  when String
    (Time.parse(val).to_r * factor).to_i
  when Date, DateTime
    (val.to_time.to_r * factor).to_i
  when Time
    (val.to_r * factor).to_i
  end
end

#quote_timestamp(val, client) ⇒ Object

Quote timestamp rubocop: disable Metrics/MethodLength, Metrics/AbcSize



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/influxer/metrics/quoting/timestamp.rb', line 15

def quote_timestamp(val, client)
  if Influxer.config.time_duration_suffix_enabled
    precision = if TIME_FACTORS.key?(client.time_precision)
      client.time_precision
    else
      DEFAULT_PRECISION
    end
    return quote_timestamp_with_suffix(val, precision)
  end

  if !TIME_FACTORS.key?(client.time_precision) &&
      !val.is_a?(Numeric)
    warn(
      "Influxer doesn't automatically cast Time and String values " \
      "to '#{client.time_precision}' precision. " \
      "Please, convert to numeric value yourself"
    )
    return val
  end

  factorize_timestamp(val, TIME_FACTORS.fetch(client.time_precision))
end

#quote_timestamp_for_write(val, client) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/influxer/metrics/quoting/timestamp.rb', line 43

def quote_timestamp_for_write(val, client)
  if !TIME_FACTORS.key?(client.time_precision) &&
      !val.is_a?(Numeric)
    raise ArgumentError,
          "Influxer doesn't support quoting #{val} " \
          " with '#{client.time_precision}' precision. " \
          "Please, convert to numeric value yourself"
  end

  factorize_timestamp(val, TIME_FACTORS.fetch(client.time_precision))
end

#quote_timestamp_with_suffix(val, precision) ⇒ Object

rubocop: enable Metrics/MethodLength, Metrics/AbcSize



39
40
41
# File 'lib/influxer/metrics/quoting/timestamp.rb', line 39

def quote_timestamp_with_suffix(val, precision)
  "#{factorize_timestamp(val, TIME_FACTORS.fetch(precision))}#{precision}"
end