mirror of
https://github.com/fluent/fluentd-ui.git
synced 2025-08-12 17:27:09 +02:00
Fix #208 for multiline preview
This commit is contained in:
parent
bd37b6393c
commit
33656e29f4
@ -27,24 +27,34 @@ module RegexpPreview
|
|||||||
reader = FileReverseReader.new(File.open(file))
|
reader = FileReverseReader.new(File.open(file))
|
||||||
result = []
|
result = []
|
||||||
target_lines = reader.tail(Settings.in_tail_preview_line_count).map{|line| line << "\n" }
|
target_lines = reader.tail(Settings.in_tail_preview_line_count).map{|line| line << "\n" }
|
||||||
target_lines.each_with_index do |line, line_no|
|
whole_string = target_lines.join
|
||||||
if line.match(params[:format_firstline])
|
re_firstline = Regexp.new(params[:format_firstline])
|
||||||
lines = target_lines[line_no, patterns.length]
|
indexes = []
|
||||||
next if lines.length < patterns.length
|
cur = 0
|
||||||
ret = detect_chunk(lines)
|
while first_index = whole_string.index(re_firstline, cur)
|
||||||
next unless ret
|
indexes << first_index
|
||||||
result << ret
|
cur = first_index + 1
|
||||||
end
|
end
|
||||||
|
indexes.each_with_index do |index, i|
|
||||||
|
next_index = indexes[i + 1] || -1
|
||||||
|
chunk = whole_string[index...next_index]
|
||||||
|
ret = detect_chunk(chunk)
|
||||||
|
next unless ret
|
||||||
|
result << ret
|
||||||
end
|
end
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
def detect_chunk(lines)
|
def detect_chunk(chunk)
|
||||||
whole = ""
|
whole = ""
|
||||||
matches = []
|
matches = []
|
||||||
lines.each_with_index do |line, i|
|
offset = 0
|
||||||
match = line.match(patterns[i])
|
patterns.each do |pat|
|
||||||
|
match = chunk.match(pat)
|
||||||
return nil unless match
|
return nil unless match
|
||||||
|
offset = chunk.index(pat)
|
||||||
|
return nil if offset > 0
|
||||||
|
chunk = chunk[match[0].length..-1]
|
||||||
match.names.each_with_index do |name, index|
|
match.names.each_with_index do |name, index|
|
||||||
matches << {
|
matches << {
|
||||||
key: name,
|
key: name,
|
||||||
@ -52,7 +62,7 @@ module RegexpPreview
|
|||||||
pos: match.offset(index + 1).map{|pos| pos + whole.length},
|
pos: match.offset(index + 1).map{|pos| pos + whole.length},
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
whole << line
|
whole << match[0]
|
||||||
end
|
end
|
||||||
{
|
{
|
||||||
whole: whole,
|
whole: whole,
|
||||||
@ -63,7 +73,7 @@ module RegexpPreview
|
|||||||
def patterns
|
def patterns
|
||||||
@patterns ||= (1..20).map do |n|
|
@patterns ||= (1..20).map do |n|
|
||||||
params["format#{n}"].presence
|
params["format#{n}"].presence
|
||||||
end.compact.map {|pattern| Regexp.new(pattern)}
|
end.compact.map {|pattern| Regexp.new(pattern, Regexp::MULTILINE)}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -2,39 +2,115 @@ require 'spec_helper'
|
|||||||
|
|
||||||
describe RegexpPreview::MultiLine do
|
describe RegexpPreview::MultiLine do
|
||||||
describe "#matches_json" do
|
describe "#matches_json" do
|
||||||
subject { RegexpPreview::MultiLine.new(File.expand_path("./spec/support/fixtures/error0.log", Rails.root), "multiline", params).matches_json }
|
subject { parser.matches_json }
|
||||||
|
let(:parser) { RegexpPreview::MultiLine.new(target_path, "multiline", params) }
|
||||||
|
|
||||||
let :params do
|
describe "simple usage" do
|
||||||
params = {
|
let(:target_path) { File.expand_path("./spec/support/fixtures/error0.log", Rails.root) }
|
||||||
format_firstline: ".+",
|
|
||||||
time_format: "time_format",
|
let :params do
|
||||||
}
|
params = {
|
||||||
params["format1"] = "(?<foo>foo)"
|
format_firstline: "foo",
|
||||||
params["format2"] = "(?<bar>bar)"
|
time_format: "time_format",
|
||||||
3.upto(Fluentd::Setting::InTail::MULTI_LINE_MAX_FORMAT_COUNT) do |i|
|
}
|
||||||
params["format#{i}"] = ""
|
params["format1"] = "(?<foo>foo)\n"
|
||||||
|
params["format2"] = "(?<bar>bar)"
|
||||||
|
3.upto(Fluentd::Setting::InTail::MULTI_LINE_MAX_FORMAT_COUNT) do |i|
|
||||||
|
params["format#{i}"] = ""
|
||||||
|
end
|
||||||
|
{ params: params }
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should not have regexp and time_format in [:params][:setting]' do
|
||||||
|
expect(subject[:params][:setting]).to eq({ regexp: nil, time_format: nil })
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should include matches info" do
|
||||||
|
matches_info = {
|
||||||
|
whole: "foo\nbar",
|
||||||
|
matches: [
|
||||||
|
{
|
||||||
|
key: "foo", matched: "foo", pos: [0, 3]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "bar", matched: "bar", pos: [4, 7]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(subject[:matches]).to include matches_info
|
||||||
end
|
end
|
||||||
{ params: params }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should not have regexp and time_format in [:params][:setting]' do
|
describe "detect only continuos patterns" do
|
||||||
expect(subject[:params][:setting]).to eq({ regexp: nil, time_format: nil })
|
let(:target_path) { File.expand_path("./spec/support/fixtures/error0.log", Rails.root) }
|
||||||
|
let(:params) do
|
||||||
|
params = {
|
||||||
|
format_firstline: "foo",
|
||||||
|
time_format: "time_format",
|
||||||
|
}
|
||||||
|
params["format1"] = "(?<foo>foo)\n"
|
||||||
|
params["format2"] = "(?<bar>baz)"
|
||||||
|
3.upto(Fluentd::Setting::InTail::MULTI_LINE_MAX_FORMAT_COUNT) do |i|
|
||||||
|
params["format#{i}"] = ""
|
||||||
|
end
|
||||||
|
{ params: params }
|
||||||
|
end
|
||||||
|
|
||||||
|
it "shouldn't match" do
|
||||||
|
expect(subject[:matches]).to eq []
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should include matches info" do
|
describe "example on document" do
|
||||||
matches_info = {
|
# http://docs.fluentd.org/articles/in_tail
|
||||||
whole: "foo\nbar\n",
|
let(:target_path) { File.expand_path("./spec/support/fixtures/multiline_example.log", Rails.root) }
|
||||||
matches: [
|
|
||||||
{
|
let :params do
|
||||||
key: "foo", matched: "foo", pos: [0, 3]
|
params = {
|
||||||
},
|
format_firstline: "\\d{4}-\\d{1,2}-\\d{1,2}",
|
||||||
{
|
"format1" => "^(?<time>\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}) \\[(?<thread>.*)\\] (?<level>[^\\s]+)(?<message>.*)",
|
||||||
key: "bar", matched: "bar", pos: [4, 7]
|
time_format: "time_format",
|
||||||
}
|
}
|
||||||
|
2.upto(Fluentd::Setting::InTail::MULTI_LINE_MAX_FORMAT_COUNT) do |i|
|
||||||
|
params["format#{i}"] = ""
|
||||||
|
end
|
||||||
|
{ params: params }
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should include matches info" do
|
||||||
|
matches_info =
|
||||||
|
[
|
||||||
|
{
|
||||||
|
:whole => "2013-3-03 14:27:33 [main] INFO Main - Start\n",
|
||||||
|
:matches => [
|
||||||
|
{:key => "time", :matched => "2013-3-03 14:27:33", :pos => [0, 18]},
|
||||||
|
{:key => "thread", :matched => "main", :pos => [20, 24]},
|
||||||
|
{:key => "level", :matched => "INFO", :pos => [26, 30]},
|
||||||
|
{:key => "message", :matched => " Main - Start\n", :pos => [30, 45]}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
:whole => "2013-3-03 14:27:33 [main] ERROR Main - Exception\njavax.management.RuntimeErrorException: null\n at Main.main(Main.java:16) ~[bin/:na]\n",
|
||||||
|
:matches => [
|
||||||
|
{:key => "time", :matched => "2013-3-03 14:27:33", :pos => [0, 18]},
|
||||||
|
{:key => "thread", :matched => "main", :pos => [20, 24]},
|
||||||
|
{:key => "level", :matched => "ERROR", :pos => [26, 31]},
|
||||||
|
{:key => "message", :matched => " Main - Exception\njavax.management.RuntimeErrorException: null\n at Main.main(Main.java:16) ~[bin/:na]\n", :pos => [31, 136]},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
:whole => "2013-3-03 14:27:33 [main] INFO Main - End",
|
||||||
|
:matches => [
|
||||||
|
{:key => "time", :matched => "2013-3-03 14:27:33", :pos => [0, 18]},
|
||||||
|
{:key => "thread", :matched => "main", :pos => [20, 24]},
|
||||||
|
{:key => "level", :matched => "INFO", :pos => [26, 30]},
|
||||||
|
{:key => "message", :matched => " Main - End", :pos => [30, 42]},
|
||||||
|
]
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
expect(subject[:matches]).to eq matches_info
|
||||||
|
end
|
||||||
expect(subject[:matches]).to include matches_info
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
5
spec/support/fixtures/multiline_example.log
Normal file
5
spec/support/fixtures/multiline_example.log
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
2013-3-03 14:27:33 [main] INFO Main - Start
|
||||||
|
2013-3-03 14:27:33 [main] ERROR Main - Exception
|
||||||
|
javax.management.RuntimeErrorException: null
|
||||||
|
at Main.main(Main.java:16) ~[bin/:na]
|
||||||
|
2013-3-03 14:27:33 [main] INFO Main - End
|
Loading…
Reference in New Issue
Block a user