However, I??™ve spelled referrer correctly in the schema, since the field in the hits
table does not necessarily refer to the header but to the concept of the referrer in general. The LogParser
class, on the other hand, uses the incorrect spelling so that it adheres to the standard.
The routine creates a new Hit object, sets the fields to the various values from the
hash, and saves it. Then you add one to the count variable.
The ActiveRecord::Extensions code looks like this:
real_time_elapsed = Benchmark.realtime do
columns = [:user_agent, :path_info, :remote_addr,
:http_referrer, :status, :visited_at]
values = []
LogParser.new.parse_io_stream(params[:log][:file]) do |l|
values <<
[ l['HTTP_USER_AGENT'],
l['PATH_INFO'],
l['REMOTE_ADDR'],
l['HTTP_REFERER'],
l['STATUS'],
Date.parse(l['DATETIME']) ]
count = count + 1
end
Hit.import columns, values, :validate=>false if values.length>0
end
end
CHAPTER 10 n CALCULATING COSTS BY ANALYZING APACHE WEB LOGS 207
This code parses the log using LogParser, just as the plain Active Record code does,
but it automatically uses the import method that ActiveRecord::Extensions adds to the Hit
model.
Pages:
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298