From 96e31c2479ab8aa4526e78c8cefc27ca25e0e26d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20M=C3=BCller=20Madsen?= Date: Tue, 23 May 2017 00:09:10 +0200 Subject: [PATCH] add bandwidth to table --- bandwidth.rb | 23 +++++++++++++++++++++++ dcav-index.erb | 5 ++++- gen_index.rb | 10 +++++++--- 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 bandwidth.rb diff --git a/bandwidth.rb b/bandwidth.rb new file mode 100644 index 0000000..c935281 --- /dev/null +++ b/bandwidth.rb @@ -0,0 +1,23 @@ +require 'server_log_parser' +require 'filesize' +require 'json' + +load 'config.rb' + +parser = ServerLogParser::Parser.new(ServerLogParser::COMBINED) + +bw_sum = Hash.new(0) + +log = `zcat /var/shots/logs/access.log.gz` +log.split("\n").each do |line| + parsed = parser.parse(line) + request = parsed['%r'].split(" ")[1] + bytes = parsed['%b'].to_i + status = parsed['%>s'] + + user = request[1] + + bw_sum[user] += bytes unless user == nil or status != "200" +end + +puts JSON.generate(bw_sum.map{|k,v| [NAMES_SHO[k],Filesize.from(v.to_s + 'B').pretty]}.to_h) diff --git a/dcav-index.erb b/dcav-index.erb index c35126d..ddbf207 100644 --- a/dcav-index.erb +++ b/dcav-index.erb @@ -5,6 +5,7 @@ > +

Screenshot statistics for dcav.pw

@@ -16,10 +17,11 @@ Count Size Average size + Bandwidth <% order = 0 users.sort.each do |user| - order += 1 + order += 1 %> <%= %Q{ @@ -28,6 +30,7 @@ #{user.count} #{Filesize.from(user.size.to_s + "B").pretty} #{Filesize.from(user.avgsize.to_s + "B").pretty} + #{user.bandwidth.to_s} } %> <% end %> diff --git a/gen_index.rb b/gen_index.rb index 9d806a1..a4f685b 100644 --- a/gen_index.rb +++ b/gen_index.rb @@ -1,5 +1,6 @@ require 'erb' require 'filesize' +require 'json' load 'config.rb' @@ -8,8 +9,10 @@ shot_dir = SHOT_DIR erb_file = ERB_FILE html_file = HTML_FILE +bw = JSON.parse(`ruby bandwidth.rb`) + class User - attr_accessor :name, :count, :size, :avgsize + attr_accessor :name, :count, :size, :avgsize, :bandwidth def <=> other other.count <=> count end @@ -26,8 +29,9 @@ usernames.each do |username| .delete("\n") .to_i user.avgsize = user.size / user.count - - users << user + user.bandwidth = bw[username] + + users << user end File.open(html_file, 'w') do |f|