Module: TestBench::Telemetry::EventData::Serialization

Defined in:
lib/test_bench/telemetry/event_data/serialization.rb

Class Method Summary collapse

Class Method Details

.dump(event_data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/test_bench/telemetry/event_data/serialization.rb', line 5

def self.dump(event_data)
  data = event_data.data.map do |value|
    case value
    in Time => time
      timestamp(time)
    else
      value
    end
  end

  type = event_data.type
  process_id = event_data.process_id

  time = event_data.time
  if not time.nil?
    timestamp = timestamp(time)
  end

  json_text = JSON.generate([type, process_id, timestamp, data])
  json_text << "\n"
  json_text
end

.load(text) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/test_bench/telemetry/event_data/serialization.rb', line 28

def self.load(text)
  type, process_id, timestamp, data = JSON.parse(text)

  type = type.to_sym

  if not timestamp.nil?
    time = time(timestamp)
  end

  event_data = EventData.new
  event_data.type = type
  event_data.process_id = process_id
  event_data.time = time
  event_data.data = data

  event_data
end

.time(timestamp) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/test_bench/telemetry/event_data/serialization.rb', line 50

def self.time(timestamp)
  match_data = time_pattern.match(timestamp)

  year = match_data['year'].to_i
  month = match_data['month'].to_i
  day = match_data['day'].to_i
  hour = match_data['hour'].to_i
  minute = match_data['minute'].to_i
  second = match_data['second'].to_i

  nanosecond = match_data['nanosecond'].to_i
  usec = Rational(nanosecond, 1_000)

  Time.utc(year, month, day, hour, minute, second, usec)
end

.time_patternObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/test_bench/telemetry/event_data/serialization.rb', line 66

def self.time_pattern
  @time_pattern ||=
    begin
      year = %r{(?<year>[[:digit:]]{4})}
      month = %r{(?<month>[[:digit:]]{2})}
      day = %r{(?<day>[[:digit:]]{2})}
      hour = %r{(?<hour>[[:digit:]]{2})}
      minute = %r{(?<minute>[[:digit:]]{2})}
      second = %r{(?<second>[[:digit:]]{2})}
      nanosecond = %r{(?<nanosecond>[[:digit:]]{9})}

      %r{\A#{year}-#{month}-#{day}T#{hour}:#{minute}:#{second}\.#{nanosecond}Z\z}
    end
end

.timestamp(time) ⇒ Object



46
47
48
# File 'lib/test_bench/telemetry/event_data/serialization.rb', line 46

def self.timestamp(time)
  time.strftime('%Y-%m-%dT%H:%M:%S.%NZ')
end