fluentd-ui/lib/regexp_preview.rb
2014-07-08 13:26:51 +09:00

45 lines
1.1 KiB
Ruby

# for read Fluent::TextParser::TEMPLATE_REGISTRY
require "fluent/registry"
require "fluent/configurable"
require "fluent/parser"
class RegexpPreview
attr_reader :file, :format, :time_format, :regexp
def initialize(file, format, options = {})
@file = file
@format = format
case format
when "regexp"
@regexp = Regexp.new(options[:regexp])
else
definition = Fluent::TextParser::TEMPLATE_REGISTRY.lookup(format)
raise "Unknown format '#{format}'" unless definition
@regexp = definition.patterns["format"]
@time_format = options[:time_format] || definition.patterns["format"]
end
end
def matches
reader = FileReverseReader.new(File.open(file))
matches = reader.tail.map do |line|
result = {
:whole => line,
:matches => [],
}
m = line.match(regexp)
next result unless m
m.names.each_with_index do |name, index|
result[:matches] << {
key: name,
matched: m[name],
pos: m.offset(index + 1),
}
end
result
end
matches
end
end