Class: Conrad::Processors::AddTimestamp

Inherits:
Object
  • Object
show all
Defined in:
lib/conrad/processors/add_timestamp.rb

Overview

Used to add timestamps to an audit event in seconds or milliseconds.

Defined Under Namespace

Classes: Error

Constant Summary collapse

ALLOWED_TIME_UNITS =

Types of units supported for generation.

%i[milliseconds seconds].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(units: :milliseconds, timestamp_key: :timestamp) ⇒ AddTimestamp

Creates a new instance of AddTimestmap processor

Parameters:

  • units (Symbol) (defaults to: :milliseconds)

    type of time units for the timestamp generated. Allows :seconds or :milliseconds.

  • timestamp_key (Symbol) (defaults to: :timestamp)

    key to add to the event hash.

Raises:

  • (ArgumentError)

    if the given units value is not one of ALLOWED_TIME_UNITS



25
26
27
28
29
30
31
32
# File 'lib/conrad/processors/add_timestamp.rb', line 25

def initialize(units: :milliseconds, timestamp_key: :timestamp)
  unless ALLOWED_TIME_UNITS.include? units
    raise ArgumentError, "Provided units of `#{units}` must be one of #{ALLOWED_TIME_UNITS}"
  end

  @generator = generator_from_units(units)
  @timestamp_key = timestamp_key
end

Instance Attribute Details

#generatorObject (readonly)

object used to generate the timestamp



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
49
50
51
52
# File 'lib/conrad/processors/add_timestamp.rb', line 9

class AddTimestamp
  # :nodoc:
  class Error < Conrad::Error; end

  # Types of units supported for generation.
  ALLOWED_TIME_UNITS = %i[milliseconds seconds].freeze

  attr_reader :generator, :timestamp_key

  # Creates a new instance of AddTimestmap processor
  #
  # @param units [Symbol] type of time units for the timestamp generated.
  #   Allows :seconds or :milliseconds.
  # @param timestamp_key [Symbol] key to add to the event hash.
  # @raise [ArgumentError] if the given units value is not one of
  #   ALLOWED_TIME_UNITS
  def initialize(units: :milliseconds, timestamp_key: :timestamp)
    unless ALLOWED_TIME_UNITS.include? units
      raise ArgumentError, "Provided units of `#{units}` must be one of #{ALLOWED_TIME_UNITS}"
    end

    @generator = generator_from_units(units)
    @timestamp_key = timestamp_key
  end

  # Generates and adds a timestamp to the provided Hash.
  #
  # @param event [Hash]
  # @return [Hash]
  def call(event)
    event.merge(timestamp_key => generator.call)
  end

  private

  def generator_from_units(units)
    case units
    when :milliseconds then -> { (Time.now.to_f * 1000).to_i }
    when :seconds then -> { Time.now.to_i }
    else
      raise UnrecognizedTimeUnit, units
    end
  end
end

#timestamp_keyObject (readonly)

Returns the value of attribute timestamp_key.



16
17
18
# File 'lib/conrad/processors/add_timestamp.rb', line 16

def timestamp_key
  @timestamp_key
end

Instance Method Details

#call(event) ⇒ Hash

Generates and adds a timestamp to the provided Hash.

Parameters:

  • event (Hash)

Returns:

  • (Hash)


38
39
40
# File 'lib/conrad/processors/add_timestamp.rb', line 38

def call(event)
  event.merge(timestamp_key => generator.call)
end