add bandwidth to table

This commit is contained in:
Christoffer Müller Madsen 2017-05-23 00:09:10 +02:00
parent d27fc0d210
commit 96e31c2479
3 changed files with 34 additions and 4 deletions

23
bandwidth.rb Normal file
View File

@ -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)

View File

@ -5,6 +5,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href=<%="#{RES_BASE}style.css"%>> <link rel="stylesheet" type="text/css" href=<%="#{RES_BASE}style.css"%>>
<link rel="icon" type="image/png" href="/favicon.png">
</head> </head>
<body> <body>
<h1>Screenshot statistics for dcav.pw</h1> <h1>Screenshot statistics for dcav.pw</h1>
@ -16,10 +17,11 @@
<th>Count</th> <th>Count</th>
<th>Size</th> <th>Size</th>
<th>Average size</th> <th>Average size</th>
<th>Bandwidth</th>
</tr> </tr>
<% order = 0 <% order = 0
users.sort.each do |user| users.sort.each do |user|
order += 1 order += 1
%> %>
<%= %Q{ <%= %Q{
<tr> <tr>
@ -28,6 +30,7 @@
<td class=\"right-align\">#{user.count}</td> <td class=\"right-align\">#{user.count}</td>
<td class=\"right-align\">#{Filesize.from(user.size.to_s + "B").pretty}</td> <td class=\"right-align\">#{Filesize.from(user.size.to_s + "B").pretty}</td>
<td class=\"right-align\">#{Filesize.from(user.avgsize.to_s + "B").pretty}</td> <td class=\"right-align\">#{Filesize.from(user.avgsize.to_s + "B").pretty}</td>
<td class=\"right-align\">#{user.bandwidth.to_s}</td>
</tr>} </tr>}
%> %>
<% end %> <% end %>

View File

@ -1,5 +1,6 @@
require 'erb' require 'erb'
require 'filesize' require 'filesize'
require 'json'
load 'config.rb' load 'config.rb'
@ -8,8 +9,10 @@ shot_dir = SHOT_DIR
erb_file = ERB_FILE erb_file = ERB_FILE
html_file = HTML_FILE html_file = HTML_FILE
bw = JSON.parse(`ruby bandwidth.rb`)
class User class User
attr_accessor :name, :count, :size, :avgsize attr_accessor :name, :count, :size, :avgsize, :bandwidth
def <=> other def <=> other
other.count <=> count other.count <=> count
end end
@ -26,8 +29,9 @@ usernames.each do |username|
.delete("\n") .delete("\n")
.to_i .to_i
user.avgsize = user.size / user.count user.avgsize = user.size / user.count
user.bandwidth = bw[username]
users << user
users << user
end end
File.open(html_file, 'w') do |f| File.open(html_file, 'w') do |f|