Create  FrontPage  Index  Search  Changes  Login

Ruby勉強会@関西-4 メモ

Ruby 初級者向けレッスン

資料

配布資料
print.pdf
1 行だけのログファイル
log1.txt
53493 行のログファイル
log3.txt.gz
クラス定義ファイル
apache_log.rb
   1|class ApacheLog
   2|  attr_reader :remote_host, :remote_user, :time
   3|  attr_reader :request_method, :request_uri, :server_protocol
   4|  attr_reader :status, :size, :referer, :user_agent
   5|
   6|  LogFormat  = /\A(\S+) \S+ (\S+) \[([^\]]+)\] "([A-Z]+) (\S+) (\S+)" (\d+) ([\d\-]+)(?: "(.+)" "(.+)")?\Z/
   7|  TimeFormat = %r!\A(\d\d)/([A-Z][a-z][a-z])/(\d\d\d\d):(\d\d):(\d\d):(\d\d)!
   8|  Month      = %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
   9|
  10|
  11|  def initialize( line )
  12|    m = LogFormat.match( line )
  13|    raise "Invalid line: #{line}" unless m
  14|
  15|    @remote_host, @remote_user, @time = m[1..3]
  16|    @request_method, @request_uri, @server_protocol = m[4..6]
  17|    @status, @size, @referer, @user_agent = m[7..10]
  18|    parse_time
  19|  end
  20|
  21|  def parse_time
  22|    m = TimeFormat.match( @time )
  23|    raise "Invalid time: #{@time}" unless m
  24|
  25|    @time = Time.local(m[3].to_i, Month.index(m[2])+1, m[1].to_i,
  26|                       m[4].to_i, m[5].to_i, m[6].to_i)
  27|  end
  28|end

演習問題

  • '/index.rdf'へのアクセスは何回?
  • 最初のMacユーザのアクセスは?
  • Google経由のアクセスは何回?
  • 何曜日のアクセスが一番多い?
  • ブラウザごとのアクセス数ランキングは?

解答例

   1|#!/usr/bin/env ruby
   2|require 'apache_log'
   3|
   4|logs = File.readlines('log3.txt').collect{ |line| ApacheLog.new( line ) }
   5|
   6|# Q.1 ------------------------------------------------------------
   7|index_rdf_list = logs.select{ |log| log.request_uri == '/index.rdf' }
   8|
   9|puts "A.1"
  10|puts  index_rdf_list.size
  11|puts
  12|
  13|# Q.2 ------------------------------------------------------------
  14|mac_access = logs.find { |log| /Mac/ =~ log.user_agent }
  15|
  16|puts "A.2"
  17|puts mac_access.time
  18|puts
  19|
  20|# Q.3 ------------------------------------------------------------
  21|google_list = logs.select { |log| /google/ =~ log.referer }
  22|
  23|puts "A.3"
  24|puts google_list.size
  25|puts
  26|
  27|# Q.4 ------------------------------------------------------------
  28|week_list = Hash.new(0)
  29|logs.each { |log| week_list[log.time.strftime('%a')] += 1 }
  30|
  31|week_list = week_list.sort_by { |wday, count| count }
  32|
  33|puts "A.4"
  34|puts week_list.last[0]
  35|puts
  36|
  37|# Q.5 ------------------------------------------------------------
  38|user_agent_list = Hash.new(0)
  39|logs.each { |log| user_agent_list[log.user_agent] += 1 }
  40|
  41|user_agent_list = user_agent_list.sort_by { |user_agent, count| count }
  42|
  43|puts "A.5"
  44|user_agent_list.reverse[0..10].each do |user_agent, count|
  45|  puts "#{count} ... #{user_agent}"
  46|end
A.1
12323

A.2
Sun Jun 26 06:14:21 JST 2005

A.3
419

A.4
Mon

A.5
3448 ... NATSU-MICAN/3.1b(20010312)
3354 ... rAntenna 0.0.5
2935 ... Mozilla/5.0 (Windows; U; Windows NT 5.1; ja-JP; rv:1.7.8) Gecko/20050511 Firefox/1.0.4
2699 ... TAMATEBAKO/1.1.66 (2002-03-10-12; http://www.wakaba.jp/~ikemo/soft/tama/)
2228 ... Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)
1733 ... Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
1646 ... Mozilla/5.0 (Windows; U; Windows NT 5.0; ja-JP; rv:1.7.8) Gecko/20050511 Firefox/1.0.4
1559 ... Mozilla/2.0 (compatible; Ask Jeeves/Teoma; +http://sp.ask.com/docs/about/tech_crawling.html)
1462 ... -
1361 ... Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)
1084 ... Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)

添削コーナー

みなさんの解答をどうぞ (プログラムの部分は先頭に半角空白を入れてください)。

(bbs plugin is disabled).
Last modified:2005/07/11 21:03:32
Keyword(s):
References:[Ruby勉強会@関西-4]
This page is frozen.