gitlab-org--gitlab-foss/lib/gitlab/ci/trace/section_parser.rb

100 lines
2.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-09-25 12:54:08 -04:00
module Gitlab
module Ci
class Trace
class SectionParser
def initialize(lines)
@lines = lines
end
def parse!
@markers = {}
@lines.each do |line, pos|
parse_line(line, pos)
end
end
def sections
sanitize_markers.map do |name, markers|
start_, end_ = markers
{
name: name,
byte_start: start_[:marker],
byte_end: end_[:marker],
date_start: start_[:timestamp],
date_end: end_[:timestamp]
}
end
end
private
def parse_line(line, line_start_position)
s = StringScanner.new(line)
until s.eos?
find_next_marker(s) do |scanner|
marker_begins_at = line_start_position + scanner.pointer
if scanner.scan(Gitlab::Regex.build_trace_section_regex)
marker_ends_at = line_start_position + scanner.pointer
handle_line(scanner[1], scanner[2].to_i, scanner[3], marker_begins_at, marker_ends_at)
true
else
false
end
end
end
end
def sanitize_markers
@markers.select do |_, markers|
markers.size == 2 && markers[0][:action] == :start && markers[1][:action] == :end
end
end
def handle_line(action, time, name, marker_start, marker_end)
action = action.to_sym
timestamp = Time.at(time).utc
marker = if action == :start
marker_end
else
marker_start
end
@markers[name] ||= []
@markers[name] << {
name: name,
action: action,
timestamp: timestamp,
marker: marker
}
end
def beginning_of_section_regex
@beginning_of_section_regex ||= /section_/.freeze
end
2018-07-04 10:02:01 -04:00
def find_next_marker(scanner)
2017-09-25 12:54:08 -04:00
beginning_of_section_len = 8
2018-07-04 10:02:01 -04:00
maybe_marker = scanner.exist?(beginning_of_section_regex)
2017-09-25 12:54:08 -04:00
if maybe_marker.nil?
2018-07-04 10:02:01 -04:00
scanner.terminate
2017-09-25 12:54:08 -04:00
else
# repositioning at the beginning of the match
2018-07-04 10:02:01 -04:00
scanner.pos += maybe_marker - beginning_of_section_len
2017-09-25 12:54:08 -04:00
if block_given?
2018-07-04 10:02:01 -04:00
good_marker = yield(scanner)
2017-09-25 12:54:08 -04:00
# if not a good marker: Consuming the matched beginning_of_section_regex
2018-07-04 10:02:01 -04:00
scanner.pos += beginning_of_section_len unless good_marker
2017-09-25 12:54:08 -04:00
end
end
end
end
end
end
end