暇つぶし

Kikkerのログをrubyで書いた簡単なスクリプトで解析してみた。
これでアクティブなユーザーがある程度見えてきた。


#ruby -Ks

filename = "access.log"
ids = Hash::new
file = open(filename,"r"){|file|
file.each{|line|
finded_id = line.scan(/id=(.*)&type/)
rss = /&type=rss/=~line
html = /&type=html/=~line

unless ids[finded_id[0] ]
ids[finded_id[0] ]= Hash::new
ids[finded_id[0] ]["name"] = finded_id[0].to_s
ids[finded_id[0] ]["count"] = 0
ids[finded_id[0] ]["rss"] = 0
ids[finded_id[0] ]["html"] = 0
end
ids[finded_id[0] ]["count"] += 1 if finded_id[0]
ids[finded_id[0] ]["rss"]+= 1 if html
ids[finded_id[0] ]["html"]+= 1 if rss
}
}

result_arr = ids.values
sorted = result_arr.sort{|a, b|
if a["count"].to_i > b["count"].to_i
-1
else
1
end
}


sorted.each{|name|
p name
}

もっとスマートな書き方があると思うので添削よろ。


追記 6/25 12:15
上のように書いたら添削してもらってこうなった


filename = "access.log"
user_data = Hash.new
File.open(filename,"r").each{|line|
next unless id = line.scan(/id=(.*)&type/).flatten[0]

user_data[id] ||= {
:count => 0,
:rss => 0,
:html => 0
}

user_data[id][:count] += 1
user_data[id][:rss] += 1 if /&type=html/=~line
user_data[id][:html] += 1 if /&type=rss/=~line
}

user_data.to_a.sort{|a,b| b[1][:count] <=> a[1][:count]}.each{|user| p user}

一応添削してくれた記事にリンク
http://d.hatena.ne.jp/harg/20060625/1151191896


http://ryogrid.myhome.cx:1234/