fluentd-ui/lib/regexp_preview/single_line.rb
Kenji Okimoto 1c1be7bfc7 Use new plugin API for parser plugins
This commit will fail after release Fluentd v1.2.0.
Because new version of Fluentd will update the regular expression of
some plugins.

Signed-off-by: Kenji Okimoto <okimoto@clear-code.com>
2018-05-22 10:00:42 +09:00

66 lines
1.8 KiB
Ruby

module RegexpPreview
class SingleLine
attr_reader :file, :format, :params, :regexp, :time_format
def initialize(file, format, params = {})
@file = file
@format = format
@time_format = params[:time_format]
@params = params
case format
when "regexp"
@regexp = Regexp.new(params[:regexp])
@time_format = nil
when "ltsv", "json", "csv", "tsv"
@regexp = nil
@time_format = nil
else # apache, nginx, etc
parser_plugin = Fluent::Plugin.new_parser(format)
raise "Unknown format '#{format}'" unless parser_plugin
parser_plugin.configure(Fluent::Config::Element.new('ROOT', '', {}, [])) # NOTE: SyslogParser define @regexp in configure method so call it to grab Regexp object
@regexp = parser_plugin.instance_variable_get(:@regexp)
@time_format = parser_plugin.time_format
end
end
def matches_json
{
params: {
setting: {
# NOTE: regexp and time_format are used when format == 'apache' || 'nginx' || etc.
regexp: regexp.try(:source),
time_format: time_format,
}
},
matches: matches.compact,
}
end
private
def matches
return [] unless @regexp # such as ltsv, json, etc
reader = FileReverseReader.new(File.open(file))
matches = reader.tail(Settings.in_tail_preview_line_count).map do |line|
result = {
:whole => line,
:matches => [],
}
match = line.match(regexp)
next result unless match
match.names.each_with_index do |name, index|
result[:matches] << {
key: name,
matched: match[name],
pos: match.offset(index + 1),
}
end
result
end
matches
end
end
end