-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount.rb
More file actions
235 lines (182 loc) · 5.63 KB
/
count.rb
File metadata and controls
235 lines (182 loc) · 5.63 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/ruby
require 'rubygems'
require 'date'
require 'active_support/all'
require 'uri'
require 'cgi'
require 'json'
require 'csv'
require 'trollop'
require 'useragent'
module CountVonCount
class LogParser
def initialize(format_string, options)
@format = format_string.split(',').reduce([]) do |result, token|
result << token.strip
result
end
@options = options
end
def log_line_regex
/(?<ip_address>[0-9\.]+) - - \[(?<timestamp>.+?)\] "(?<uri>.+?)" (?<status>[0-9]+) [0-9]+ "(?<referrer>.+?)" "(?<user_agent>.+?)"/
end
def optional_regex
/(?<dma>\w+?) (?<speed>\w+?)$/
end
def placement_regex
/\/placements\/(?<placement_id>[0-9]+)/
end
def line_matches_to_hash(line, matches)
request = matches.names.reduce({}) {|result, name| result[name] = matches[name]; result}
uri = URI::parse matches[:uri].split(' ')[1]
placement_regex.match(uri.path) do |placement_matches|
request['placement_id'] = placement_matches[:placement_id]
end
query = if uri.query
uri.query.split('&').reduce({}) do |result, str|
key, value = str.split '='
result[key] = value
result
end
else
{}
end
if @options[:extra]
optional_regex.match(line) do |optional_matches|
optional_matches.names.each do |name|
request[name] = optional_matches[name]
end
end
end
request['uri'] = uri.to_s
request['query'] = query
agent = UserAgent.parse(request['user_agent'])
request['browser'] = agent.browser
request['platform'] = agent.platform
time = DateTime.strptime(request['timestamp'], "%d/%b/%Y:%H:%M:%S %z").new_offset(0)
if @options[:hours]
time = time.change(sec: 0).change(min: 0)
end
if @options[:days]
time = time.change(sec: 0).change(min: 0).change(hour: 0)
end
request['timestamp'] = time
hash = @format.reduce({}) do |result, spec|
source = spec.split('.').reduce(request) do |request_obj, key|
request_obj[key] || ''
end
result[spec] ||= source
result
end
{hash: hash}
end
def process_flat(input)
input.readlines.each do |line|
log_line_regex.match(line) do |matches|
hash = line_matches_to_hash(line, matches)
yield hash
end
end
end
def process_with_counts(input)
totals = {}
input.readlines.each do |line|
log_line_regex.match(line) do |matches|
hash = line_matches_to_hash(line, matches)
hash_key = Marshal::dump(hash)
totals[hash_key] ||= {hash: hash, total: 0}
totals[hash_key][:total] += 1
end
end
totals.each do |hash_key, hash|
yield hash
end
end
def process(input, &block)
if @options[:aggregate]
process_with_counts(input, &block)
else
process_flat(input, &block)
end
end
def to_csv(input)
headers = @format
if @options[:aggregate]
headers << 'count'
end
puts headers.to_csv
process(input) do |record|
row = record[:hash].values
if @options[:aggregate]
row << record[:total]
end
puts row.to_csv
end
end
def to_table(input)
output = ""
table = []
widths = {}
process(input) do |row|
row[:hash].each do |spec, value|
string_val = value.class == String ? value : value.to_s
widths[spec] ||= 0
widths[spec] = [widths[spec], spec.size, string_val.size].max
end
end
unless @options[:quiet]
@format.each do |spec|
size = [widths[spec], spec.size].max
output << sprintf("%#{size}s ", spec)
end
output << "count\n"
end
@totals.values.each do |row|
row[:hash].each do |spec, value|
output << sprintf("%#{widths[spec]}s ", value)
end
output << "#{row[:total]}\n"
end
output
end
end
end
p = Trollop::Parser.new do
banner <<-EOS
count counts logline elements.
Usage:
ruby count.rb [options] pathspec [filename]
If no filename is specified, it will read from STDIN.
Pathspec:
ip_address, timestamp, uri, status, referrer, user_agent, query.[query parameter]
A JSON array of strings, each specifying a path. The paths can either be one of the single values above, or a query parameter specified like this:
ruby count.rb '["referrer", "query.type"]' status.log
produces the following output:
referrer query.type count
- IMPRESSION 4
- 10
http://localhost:3000/ad_tags/926000/test 5
http://localhost:3000/ad_tags/926000/test start 3
http://localhost:3000/ad_tags/926000/test complete 1
count will group based on the fields specified.
Options:
EOS
opt :quiet, "Use minimal output", short: 'q'
opt :csv, "Output csv", short: 'c'
opt :extra, "Use extra data", short: 'e'
opt :aggregate, "Aggregate counts", short: 'a'
opt :hours, "Bucket time by hours", short: 'H'
opt :days, "Bucket time by days", short: 'd'
end
opts = Trollop::with_standard_exception_handling p do
raise Trollop::HelpNeeded if ARGV.empty?
p.parse ARGV
end
spec = ARGV[0]
input = ARGV[1] ? open(ARGV[1]) : STDIN
parser = CountVonCount::LogParser.new(spec, opts)
if opts[:csv]
parser.to_csv(input)
else
parser.to_table(input)
end