Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
DanielVartanov
committed
Dec 21, 2009
1 parent
a573a10
commit f87d6c0
Showing
9 changed files
with
232 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 | ||
|
|
Oops, something went wrong.