#!/usr/bin/ruby -Ku
require 'mysql'
require 'rexml/document'
#-------------------------------------------------------------------------------
limit = 20 # TOP 20
interval = 7 # 過去何日分を集計する?
node = '^node/[[:digit:]]+$' # 集計するノードのパターン(正規表現)
filter = [ # 集計から除外するIPアドレス("'と'"で囲む)
"'192.168.100.200'",
"'192.168.100.201'",
"'192.168.100.202'"
]
#-------------------------------------------------------------------------------
summary = [
"set character set utf8",
"start transaction",
"delete from node_ranking_old",
"insert into node_ranking_old select * from node_ranking_new",
"delete from node_ranking_new",
"set @rank=0",
"insert into node_ranking_new (nid, path, title, count, rank)
select tmp2.nid, tmp2.path, node.title, tmp2.count, tmp2.rank from node,
(select replace(path, 'node/', '') nid, path, count(*) count, (@rank:=@rank+1) rank from
(select path from accesslog
where hostname not in (#{filter.join(",")})
and path regexp '#{node}'
and timestamp between unix_timestamp(subdate(curdate(), #{interval})) and unix_timestamp(curdate())
group by path,hostname) as tmp1
group by path
order by count desc
limit 0, #{limit}) as tmp2
where node.nid = tmp2.nid",
"commit"
]
#-------------------------------------------------------------------------------
sio = open(File.dirname(File.expand_path(__FILE__)) + '/../config.xml')
doc = REXML::Document.new(sio.readlines.to_s)
cfg = doc.elements['config/drupal']
database = cfg.elements['database'].text
username = cfg.elements['username'].text
password = cfg.elements['password'].text
mysql = Mysql::new('localhost', username, password, database)
summary.each { |sql|
mysql.query(sql)
}
|