Skip to content

Commit

Permalink
Adopting scopes-n-groups
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielVartanov committed Dec 21, 2009
1 parent a573a10 commit f87d6c0
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 152 deletions.
4 changes: 4 additions & 0 deletions init.rb
@@ -1,4 +1,8 @@
require 'rubygems' require 'rubygems'
require 'active_support' require 'active_support'

__DIR__ = File.dirname(__FILE__) __DIR__ = File.dirname(__FILE__)

require File.join(__DIR__, 'vendor', 'scopes-n-groups', 'lib', 'scope')

Dir.glob(File.join(__DIR__, 'lib', '**', '*.rb')).each { |file| require file } Dir.glob(File.join(__DIR__, 'lib', '**', '*.rb')).each { |file| require file }
16 changes: 8 additions & 8 deletions lib/host.rb
@@ -1,9 +1,9 @@
class Host < Struct.new(:address, :records) class Host < Struct.new(:address, :records)
def amount_of_traffic def amount_of_traffic
summ = 0 summ = 0
records.each do |record| records.each do |record|
summ += record.bytes summ += record.bytes
end end
summ summ
end end
end end
77 changes: 37 additions & 40 deletions lib/traffic.rb
@@ -1,44 +1,41 @@
class Traffic class Traffic < Scope
attr_accessor :local_address attr_accessor :local_address
attr_reader :records

define_scope :internal, proc { |record| record.internal? }
def initialize(records, local_address) define_scope :world, proc { |record| record.world? }
@records = records define_scope :daily, proc { |record| record.daily? }
@local_address = local_address define_scope :nightly, proc { |record| record.nightly? }
end define_scope :outcoming, proc { |record| record.source_address == local_address }
define_scope :incoming, proc { |record| record.destination_address == local_address }


def incoming def highest_ten_hosts
@incoming ||= self.records.select { |record| record.destination_address == self.local_address } highest_hosts(10)
self.class.new(@incoming, self.local_address) end
end


def outcoming protected
@outcoming ||= self.records.select { |record| record.source_address == self.local_address }
self.class.new(@outcoming, self.local_address)
end

def hosts
@hosts ||= grouped_by_host.map { |host_address, records| Host.new(host_address, records) }
end

def highest_hosts(limit=nil)
sorted_hosts = hosts.sort { |left, right| right.amount_of_traffic <=> left.amount_of_traffic }
limit.nil? ? sorted_hosts : sorted_hosts[0..limit-1]
end

def grouped_by_host
return @grouped_by_host if @grouped_by_host

@grouped_by_host = {}

incoming.records.each do |record|
(@grouped_by_host[record.source_address] ||= []) << record
end


outcoming.records.each do |record| def hosts
(@grouped_by_host[record.destination_address] ||= []) << record @hosts ||= grouped_by_host.map { |host_address, records| Host.new(host_address, records) }
end end


@grouped_by_host def highest_hosts(limit=0)
end sorted_hosts = hosts.sort { |left, right| right.amount_of_traffic <=> left.amount_of_traffic }
limit.nil? ? sorted_hosts : sorted_hosts[0..limit-1]
end

def grouped_by_host
return @grouped_by_host if @grouped_by_host

@grouped_by_host = {}

incoming.records.each do |record|
(@grouped_by_host[record.source_address] ||= []) << record
end

outcoming.records.each do |record|
(@grouped_by_host[record.destination_address] ||= []) << record
end

@grouped_by_host
end
end end
52 changes: 0 additions & 52 deletions lib/traffic_calculator.rb

This file was deleted.

15 changes: 15 additions & 0 deletions lib/traffic_splitter.rb
@@ -0,0 +1,15 @@
class TrafficSplitter < Struct.new(:local_address)
def split_traffic!(records)
{
:internal => {
:daily => traffic.internal.daily.highest_ten_hosts,
:nightly => traffic.internal.nightly.highest_ten_hosts,
},

:world => {
:daily => traffic.world.daily.highest_ten_hosts,
:nightly => traffic.world.nightly.highest_ten_hosts,
}
}
end
end
32 changes: 28 additions & 4 deletions parse!.rb
Expand Up @@ -3,9 +3,33 @@
__DIR__ = File.dirname(__FILE__) __DIR__ = File.dirname(__FILE__)
require File.join(__DIR__, 'init') require File.join(__DIR__, 'init')


def print_results(split_traffic)

end

def results_string
result = ''

result << "\n=== Incoming traffic ===\n"
@traffic.incoming.highest_hosts(10).each do |host|
result << "#{self.user_address} <- #{host.address} [#{nice_bytes(host.amount_of_traffic)}]\n"
end

result << "\n=== Outcoming traffic ===\n"
@traffic.outcoming.highest_hosts(10).each do |host|
result << "#{self.user_address} -> #{host.address} [#{nice_bytes(host.amount_of_traffic)}]\n"
end

result
end

user_address = '77.235.9.36' user_address = '77.235.9.36'
file_name = 'sample.log' log_file_name = 'sample.log'

parser = Parser.new
parser.parse_file!(log_file_name)

traffic_calculator = TrafficCalculator.new local_address, records
split_traffic = traffic_calculator.split_traffic!


traffic_calculator = TrafficCalculator.new file_name, user_address print_results(spit_traffic)
traffic_calculator.calculate!
traffic_calculator.print_results
96 changes: 48 additions & 48 deletions spec/traffic/scopes_spec.rb
@@ -1,54 +1,54 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper') require File.join(File.dirname(__FILE__), '..', 'spec_helper')


describe Traffic do describe Traffic do
describe "given a bunch of records" do describe "given a bunch of records" do
before :each do before :each do
@local_address = '192.168.0.1' @local_address = '192.168.0.1'
@facebook = '77.235.14.9' @facebook = '77.235.14.9'
@gmail = '77.235.14.10' @gmail = '77.235.14.10'


@records = [ @records = [
Record.new(@local_address, @facebook), Record.new(@local_address, @facebook),
Record.new(@local_address, @gmail), Record.new(@local_address, @gmail),
Record.new(@facebook, @local_address), Record.new(@facebook, @local_address),
Record.new(@gmail, @local_address) Record.new(@gmail, @local_address)
] ]
end end



describe "given a Traffic instance loaded with records" do
describe "given a Traffic instance loaded with records" do before :each do
before :each do @traffic = Traffic.new @records
@traffic = Traffic.new(@records, @local_address) @traffic.local_address = @local_address
end end


describe "when #incoming is called" do describe "when #incoming is called" do
before :each do before :each do
@return_value = @traffic.incoming @return_value = @traffic.incoming
end end


it "should return a Traffic instance" do it "should return a Traffic instance" do
@return_value.should be_kind_of(Traffic) @return_value.should be_kind_of(Traffic)
end end


it "should narrow records to incoming traffic only" do it "should narrow records to incoming traffic only" do
@return_value.records.to_set.should == [Record.new(@facebook, @local_address), Record.new(@gmail, @local_address)].to_set @return_value.to_set.should == [Record.new(@facebook, @local_address), Record.new(@gmail, @local_address)].to_set
end end
end end


describe "when #outcoming is called" do describe "when #outcoming is called" do
before :each do before :each do
@return_value = @traffic.outcoming @return_value = @traffic.outcoming
end end


it "should return a Traffic instance" do it "should return a Traffic instance" do
@return_value.should be_kind_of(Traffic) @return_value.should be_kind_of(Traffic)
end end


it "should narrow records to outcoming traffic only" do it "should narrow records to outcoming traffic only" do
@return_value.records.to_set.should == [Record.new(@local_address, @facebook), Record.new(@local_address, @gmail)].to_set @return_value.to_set.should == [Record.new(@local_address, @facebook), Record.new(@local_address, @gmail)].to_set
end
end
end
end end
end
end
end
end end


0 comments on commit f87d6c0

Please sign in to comment.