Prev | Current Page 285 | Next

David Berube

"Practical Reporting with Ruby and Rails"


real_time_elapsed = Benchmark.realtime do
LogParser.new.parse_io_stream(params[:log][:file]) do |l|
Hit.new do |h|
h.user_agent = l['HTTP_USER_AGENT']
h.path_info = l['PATH_INFO']
h.remote_addr = l['REMOTE_ADDR']
h.http_referrer = l['HTTP_REFERER']
h.status = l['STATUS']
h.visited_at = l['DATETIME']
h.save
end
count = count + 1
end
end
CHAPTER 10 n CALCULATING COSTS BY ANALYZING APACHE WEB LOGS 206
The Benchmark.realtime call measures how long it takes to load the information, and
the LogParser class parses the log for you. Specifically, the parse_io_stream method takes
the uploaded file, parses it, and calls your block once for each record. The block is passed
a hash, which looks like this:
---
HTTP_USER_AGENT: Mozilla/4.0 (compatible; a browser user agent here)
PATH_INFO: /some_url
HTTP_HOST: localhost
REMOTE_ADDR: 101.184.128.38
HTTP_REFERER: http://small-site.com.example/
STATUS: "200"
DATETIME: 1/Oct/2007:04:49:03 -0500
nNote HTTP_REFERER is intentionally spelled this way in the code, because that??™s the way it??™s spelled in
the official HTTP standard.


Pages:
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
Dorota Miśkiewicz LMC Glenn Medeiros Manic Street Preachers Drukarnia