Skip to content

Commit

Permalink
Results are one convenient table now
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Vartanov committed Dec 31, 2009
1 parent 8a7ab4d commit cbd3edc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 58 deletions.
49 changes: 24 additions & 25 deletions README.textile
Expand Up @@ -24,31 +24,30 @@ h3. Example of unreadable log:


h3. Example of isp-logs-parser output: h3. Example of isp-logs-parser output:


<pre>+---------+--------------------------+-------------------------+ <pre>+-----------------+---------+-----------+----------+
| | internal | world | | host | traffic | day/night | location |
+---------+--------------------------+-------------------------+ +-----------------+---------+-----------+----------+
| daily | 81.20.21.134 [1.94GB] | 86.40.182.30 [54.3MB] | | 212.112.103.210 | 35.3GB | nightly | internal |
| | 217.29.28.102 [294MB] | 212.47.219.83 [358kB] | | 81.20.21.134 | 1.94GB | daily | internal |
| | 217.29.21.21 [10.7MB] | 65.54.167.92 [282kB] | | 91.207.97.23 | 464MB | nightly | internal |
| | 91.205.50.59 [10.6MB] | 212.47.219.86 [260kB] | | 217.29.28.102 | 337MB | nightly | internal |
| | 85.113.0.25 [3.83MB] | 207.46.26.185 [220kB] | | 195.38.161.22 | 334MB | nightly | internal |
| | 212.2.228.113 [1.84MB] | 194.67.52.35 [116kB] | | 217.29.28.102 | 294MB | daily | internal |
| | 81.20.16.4 [433kB] | 78.146.85.59 [102kB] | | 77.95.63.107 | 228MB | nightly | internal |
| | 212.42.103.178 [325kB] | 65.54.172.189 [99.9kB] | | 86.40.182.30 | 101MB | nightly | world |
| | 89.237.225.252 [295kB] | 65.54.172.75 [86.6kB] | | 86.40.182.30 | 54.3MB | daily | world |
| | 77.95.58.175 [231kB] | 207.46.112.75 [56.9kB] | | 87.248.210.200 | 36.1MB | nightly | world |
+---------+--------------------------+-------------------------+ | 87.248.209.186 | 33.4MB | nightly | world |
| nightly | 212.112.103.210 [35.3GB] | 86.40.182.30 [101MB] | | 217.29.21.21 | 10.7MB | daily | internal |
| | 91.207.97.23 [464MB] | 87.248.210.200 [36.1MB] | | 91.205.50.59 | 10.6MB | daily | internal |
| | 217.29.28.102 [337MB] | 87.248.209.186 [33.4MB] | | 82.99.19.11 | 4.03MB | nightly | world |
| | 195.38.161.22 [334MB] | 82.99.19.11 [4.03MB] | | 85.113.0.25 | 3.83MB | daily | internal |
| | 77.95.63.107 [228MB] | 92.122.213.202 [1.59MB] | | 92.122.213.202 | 1.59MB | nightly | world |
| | 212.42.120.60 [193MB] | 195.27.30.8 [1.44MB] | | 212.47.219.83 | 358kB | daily | world |
| | 89.237.225.37 [130MB] | 207.46.112.47 [1.32MB] | | 65.54.167.92 | 282kB | daily | world |
| | 217.29.20.18 [101MB] | 207.46.112.45 [1.03MB] | | 212.47.219.86 | 260kB | daily | world |
| | 85.115.205.44 [90.4MB] | 207.46.112.71 [952kB] | | 207.46.26.185 | 220kB | daily | world |
| | 212.112.98.122 [82.2MB] | 65.54.167.92 [738kB] | +-----------------+---------+-----------+----------+</pre>
+---------+--------------------------+-------------------------+</pre>


h2. Installation h2. Installation


Expand Down
8 changes: 8 additions & 0 deletions lib/host.rb
Expand Up @@ -6,4 +6,12 @@ def amount_of_traffic
end end
summ summ
end end

def daily?
records.first.daily?
end

def internal?
records.first.internal?
end
end end
4 changes: 2 additions & 2 deletions lib/traffic.rb
Expand Up @@ -10,8 +10,8 @@ def local_address
@local_address ||= find_local_address @local_address ||= find_local_address
end end


def highest_ten_hosts def highest_five_hosts
highest_hosts(10) highest_hosts(5)
end end


def hosts def hosts
Expand Down
15 changes: 5 additions & 10 deletions lib/traffic_splitter.rb
@@ -1,15 +1,10 @@
class TrafficSplitter class TrafficSplitter
def self.split_traffic!(traffic) def self.split_traffic!(traffic)
{ hosts = traffic.daily.internal.highest_five_hosts +
:internal => { traffic.nightly.internal.highest_five_hosts +
:daily => traffic.daily.internal.highest_ten_hosts, traffic.daily.world.highest_five_hosts +
:nightly => traffic.nightly.internal.highest_ten_hosts, traffic.nightly.world.highest_five_hosts
},


:world => { hosts.sort { |left, right| right.amount_of_traffic <=> left.amount_of_traffic }
:daily => traffic.daily.world.highest_ten_hosts,
:nightly => traffic.nightly.world.highest_ten_hosts,
}
}
end end
end end
34 changes: 13 additions & 21 deletions parse!.rb
Expand Up @@ -2,44 +2,36 @@


require File.join(File.dirname(__FILE__), 'init') require File.join(File.dirname(__FILE__), 'init')


def pretty_host(host) def result_string(hosts)
"#{host.address} [#{nice_bytes(host.amount_of_traffic)}]"
end

def result_string(split_traffic)
require File.join(File.dirname(__FILE__), 'vendor', 'terminal-table', 'lib', 'terminal-table') require File.join(File.dirname(__FILE__), 'vendor', 'terminal-table', 'lib', 'terminal-table')
require 'terminal-table/import' require 'terminal-table/import'
t = table ['', 'internal', 'world']

t << ['daily', pretty_host(split_traffic[:internal][:daily].first), pretty_host(split_traffic[:world][:daily].first)]


1.upto(9).each do |index| results_table = table ['host', 'traffic', 'day/night', 'location']
t << ['', pretty_host(split_traffic[:internal][:daily][index]), pretty_host(split_traffic[:world][:daily][index])]
end

t.add_separator


t << ['nightly', pretty_host(split_traffic[:internal][:nightly].first), pretty_host(split_traffic[:world][:nightly].first)] hosts.each do |host|

results_table << [host.address,
1.upto(9).each do |index| nice_bytes(host.amount_of_traffic),
t << ['', pretty_host(split_traffic[:internal][:nightly][index]), pretty_host(split_traffic[:world][:nightly][index])] host.daily? ? 'daily' : 'nightly',
host.internal? ? 'internal' : 'world'
]
end end


t results_table
end end


puts ARGV.inspect

if ARGV.size < 1 if ARGV.size < 1
puts "usage: parse\\!.rb log-file" puts "usage: parse\\!.rb log-file"
exit 1 exit 1
end end


log_file_name = ARGV[0] log_file_name = ARGV[0]
puts "Parsing #{log_file_name}..."


parser = Parser.new parser = Parser.new
records = parser.parse_file!(log_file_name) records = parser.parse_file!(log_file_name)
puts "#{records.size} records read from #{log_file_name}\n\n" puts "#{records.size} records read"

puts "Calculating...\n\n"
traffic = Traffic.new records traffic = Traffic.new records
split_traffic = TrafficSplitter.split_traffic!(traffic) split_traffic = TrafficSplitter.split_traffic!(traffic)


Expand Down

0 comments on commit cbd3edc

Please sign in to comment.