Progress counter in 5% steps added. Comments added.
This commit is contained in:
parent
e895e931fc
commit
2edae8fbc2
1 changed files with 40 additions and 6 deletions
|
|
@ -10,6 +10,8 @@ namespace :debshots do
|
||||||
# Regular expression to match Nginx access log lines
|
# Regular expression to match Nginx access log lines
|
||||||
REGEXP = /\[.+?\] Cache: (?<cache>\S+) (?<backend>\S+) (?<resptime>\S+) (?<code>\d+) (?<size>\d+) \d+\.\d+\.\d+\.\d+(, \d+\.\d+\.\d+\.\d+)* (?<url>\/\S*) \"(?<referer>.*?)\" \"(?<agent>).*\"/
|
REGEXP = /\[.+?\] Cache: (?<cache>\S+) (?<backend>\S+) (?<resptime>\S+) (?<code>\d+) (?<size>\d+) \d+\.\d+\.\d+\.\d+(, \d+\.\d+\.\d+\.\d+)* (?<url>\/\S*) \"(?<referer>.*?)\" \"(?<agent>).*\"/
|
||||||
|
|
||||||
|
# Regular subexpression to find the package name in different
|
||||||
|
# kind of URLs.
|
||||||
REGEXPS_URL = [
|
REGEXPS_URL = [
|
||||||
%r{/thumbnail/(?<package>.+)},
|
%r{/thumbnail/(?<package>.+)},
|
||||||
%r{/thumbnail-404/(?<package>.+)},
|
%r{/thumbnail-404/(?<package>.+)},
|
||||||
|
|
@ -21,14 +23,33 @@ namespace :debshots do
|
||||||
%r{/json/package/(?<package>.+)},
|
%r{/json/package/(?<package>.+)},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# Hash of packages. The key is the package name as found
|
||||||
|
# in the URLs of the Nginx access log. The value is the
|
||||||
|
# number of occurences that the package was found.
|
||||||
packages={}
|
packages={}
|
||||||
|
|
||||||
|
# Get the logfile's path from the rake command line
|
||||||
logfile_name = args[:logfile]
|
logfile_name = args[:logfile]
|
||||||
Rails.logger.info "Parsing log file #{logfile_name}"
|
Rails.logger.info "Parsing log file #{logfile_name}"
|
||||||
f = open(logfile_name, 'r')
|
f = open(logfile_name, 'r')
|
||||||
|
|
||||||
|
# Get the total file size so that we know how far
|
||||||
|
# we are through parsing the log file.
|
||||||
file_size = f.size
|
file_size = f.size
|
||||||
|
last_percent_done = 0
|
||||||
|
|
||||||
f.each_line do |line|
|
f.each_line do |line|
|
||||||
pkgname = nil
|
pkgname = nil
|
||||||
|
|
||||||
|
# Show progress every 5%
|
||||||
|
percent_done = (f.pos.to_f / f.size.to_f * 100).to_i
|
||||||
|
if percent_done % 5 == 0
|
||||||
|
if percent_done > last_percent_done
|
||||||
|
last_percent_done = percent_done
|
||||||
|
Rails.logger.info "Parsing... #{percent_done}%"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if match = REGEXP.match(line)
|
if match = REGEXP.match(line)
|
||||||
#p match['code']
|
#p match['code']
|
||||||
url = match['url'].squeeze('/')
|
url = match['url'].squeeze('/')
|
||||||
|
|
@ -43,26 +64,39 @@ namespace :debshots do
|
||||||
end
|
end
|
||||||
Rails.logger.debug "#{pkgname} / url=#{url}"
|
Rails.logger.debug "#{pkgname} / url=#{url}"
|
||||||
else
|
else
|
||||||
Rails.logger.error "No match: #{line}"
|
Rails.logger.error "Line did not match: #{line}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: URLs auswerten (thumbnail, …with-version, package…) -> paket-views-zähler sammeln
|
|
||||||
# TODO: Cache-Hit-Ratio errechnen (nach größe der Response)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Update packages with the count per package we summed up
|
# Update packages with the count per package we summed up
|
||||||
|
Rails.logger.info "Updating visits count in packages table"
|
||||||
|
|
||||||
|
last_percent_done = 0
|
||||||
|
updated_packages = 0
|
||||||
Package.transaction do
|
Package.transaction do
|
||||||
packages.each do |pkg, count|
|
packages.each do |pkg, count|
|
||||||
Rails.logger.info "Package: #{pkg} Count: #{count}"
|
|
||||||
|
# Show progress every 5%
|
||||||
|
percent_done = (updated_packages.to_f / packages.count.to_f * 100).to_i
|
||||||
|
if percent_done % 5 == 0
|
||||||
|
if percent_done > last_percent_done
|
||||||
|
last_percent_done = percent_done
|
||||||
|
Rails.logger.info "Updating... #{percent_done}%"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
updated_packages += 1
|
||||||
|
|
||||||
|
Rails.logger.debug "Package: #{pkg} Count: #{count}"
|
||||||
db_pkg = Package.find_by_name pkg
|
db_pkg = Package.find_by_name pkg
|
||||||
if db_pkg
|
if db_pkg
|
||||||
db_pkg.visits += count
|
db_pkg.visits += count
|
||||||
db_pkg.save!
|
db_pkg.save!
|
||||||
else
|
else
|
||||||
Rails.logger.error "Package #{pkg} not found in database."
|
Rails.logger.debug "Package #{pkg} not found in database."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Rails.logger.info "Done."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue