This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
app.rb
160 lines (136 loc) · 3.72 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require 'sinatra'
require 'grok-pure'
require 'json'
require 'find'
require 'cgi'
class Application < Sinatra::Base
def grok
if @grok.nil?
@grok = Grok.new
Dir.foreach('./public/patterns/') do |item|
next if item == '.' or item == '..' or item == '.git'
@grok.add_patterns_from_file(("./public/patterns/#{item}"))
end
end
@grok
end
def get_files path
dir_array = Array.new
Find.find(path) do |f|
if !File.directory?(f)
#dir_array << f if !File.directory?.basename(f) # add only non-directories
dir_array << File.basename(f, ".*")
end
end
return dir_array
end
helpers do
def js_array(name, array)
end
end
def add_custom_patterns_from_string(text)
text.each_line do |line|
# Skip comments
next if line =~ /^\s*#/
# File format is: NAME ' '+ PATTERN '\n'
name, pattern = line.gsub(/^\s*/, "").split(/\s+/, 2)
#p name => pattern
# If the line is malformed, skip it.
next if pattern.nil?
# Trim newline and add the pattern.
grok.add_pattern(name, pattern.chomp)
end
end
set :public_folder, File.dirname(__FILE__) + '/public'
post '/grok' do
custom_patterns = params[:custom_patterns]
input = params[:input]
pattern = params[:pattern]
named_captures_only = (params[:named_captures_only] == "true")
singles = (params[:singles] == "true")
keep_empty_captures = (params[:keep_empty_captures] == "true")
if !custom_patterns.empty?
add_custom_patterns_from_string(custom_patterns)
end
begin
grok.compile(params[:pattern])
rescue
return "Compile ERROR"
end
matches = grok.match(params[:input])
return "No Matches" if !matches
fields = {}
matches.captures.each do |key, value|
type_coerce = nil
is_named = false
if key.include?(":")
name, key, type_coerce = key.split(":")
is_named = true
end
case type_coerce
when "int"
value = value.to_i rescue nil
when "float"
value = value.to_f rescue nil
end
if named_captures_only && !is_named
next
end
if fields[key].is_a?(String)
fields[key] = [fields[key]]
end
if keep_empty_captures && fields[key].nil?
fields[key] = []
end
# If value is not nil, or responds to empty and is not empty, add the
# value to the event.
if !value.nil? && (!value.empty? rescue true)
# Store fields as an array unless otherwise instructed with the
# 'singles' config option
if !fields.include?(key) and singles
fields[key] = value
else
fields[key] ||= []
fields[key] << value
end
end
end
if fields
#Keep escaped strings before generating json in escaped_fields dict
escaped_fields = {}
fields.each do |key, value|
if singles
value.map! { |x| CGI.escapeHTML(x) if x != nil }
else
value.each_index do |i|
value[i].map! { |x| CGI.escapeHTML(x) if x != nil}
end
end
escaped_fields[CGI.escapeHTML(key)] = value
end
#pp match.captures
return JSON.pretty_generate(escaped_fields)
end
return "No Matches"
end
post '/discover' do
grok.discover(params[:input])
end
get '/' do
@tags = []
grok.patterns.each do |x,y|
@tags << "%{#{x}"
end
haml :'index'
end
get '/discover' do
haml :'discover'
end
get '/patterns' do
@arr = get_files("./public/patterns/")
haml :'patterns'
end
get '/patterns/*' do
send_file(params[:spat]) unless params[:spat].nil?
end
end