Skip to content
Browse files

Adopting scopes-n-groups

  • Loading branch information...
1 parent a573a10 commit f87d6c070c5a0c8a9f9696bf05c3319a68587691 DanielVartanov committed Dec 21, 2009
Showing with 232 additions and 152 deletions.
  1. +4 −0 init.rb
  2. +8 −8 lib/host.rb
  3. +37 −40 lib/traffic.rb
  4. +0 −52 lib/traffic_calculator.rb
  5. +15 −0 lib/traffic_splitter.rb
  6. +28 −4 parse!.rb
  7. +48 −48 spec/traffic/scopes_spec.rb
  8. +91 −0 spec/traffic_splitter_spec.rb
  9. +1 −0 vendor/scopes-n-groups
View
4 init.rb
@@ -1,4 +1,8 @@
require 'rubygems'
require 'active_support'
+
__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 }
View
16 lib/host.rb
@@ -1,9 +1,9 @@
class Host < Struct.new(:address, :records)
- def amount_of_traffic
- summ = 0
- records.each do |record|
- summ += record.bytes
- end
- summ
- end
-end
+ def amount_of_traffic
+ summ = 0
+ records.each do |record|
+ summ += record.bytes
+ end
+ summ
+ end
+end
View
77 lib/traffic.rb
@@ -1,44 +1,41 @@
-class Traffic
- attr_accessor :local_address
- attr_reader :records
-
- def initialize(records, local_address)
- @records = records
- @local_address = local_address
- end
+class Traffic < Scope
+ attr_accessor :local_address
+
+ define_scope :internal, proc { |record| record.internal? }
+ define_scope :world, proc { |record| record.world? }
+ define_scope :daily, proc { |record| record.daily? }
+ define_scope :nightly, proc { |record| record.nightly? }
+ define_scope :outcoming, proc { |record| record.source_address == local_address }
+ define_scope :incoming, proc { |record| record.destination_address == local_address }
- def incoming
- @incoming ||= self.records.select { |record| record.destination_address == self.local_address }
- self.class.new(@incoming, self.local_address)
- end
+ def highest_ten_hosts
+ highest_hosts(10)
+ end
- def outcoming
- @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
+protected
- outcoming.records.each do |record|
- (@grouped_by_host[record.destination_address] ||= []) << record
- end
-
- @grouped_by_host
- end
+ def hosts
+ @hosts ||= grouped_by_host.map { |host_address, records| Host.new(host_address, records) }
+ end
+
+ def highest_hosts(limit=0)
+ 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
View
52 lib/traffic_calculator.rb
@@ -1,52 +0,0 @@
-class TrafficCalculator < Struct.new(:log_file_name, :user_address)
- def calculate!
- parser = Parser.new
- parser.parse_file!(self.log_file_name)
-
- @traffic = Traffic.new(parser.records, self.user_address)
- end
-
- def get_results
- res = Hash.new
- res[:intraffic] = Array.new()
- res[:outtraffic] = Array.new()
-
- @traffic.incoming.highest_hosts(10).each do |host|
- res[:intraffic].push(Hash["IP" => host.address , "size" => nice_bytes(host.amount_of_traffic)])
- end
-
- @traffic.outcoming.highest_hosts(10).each do |host|
- res[:outtraffic].push(Hash["IP" => host.address , "size" => nice_bytes(host.amount_of_traffic)])
- end
-
- res
- end
-
- def print_results
- puts results_string
- 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
-
-protected
-
- def print_first_ten_hosts(traffic)
- traffic.highest_hosts(10).each do |host|
- puts "#{self.user_address} <- #{host.address} [#{nice_bytes(host.amount_of_traffic)}]"
- end
- end
-end
View
15 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
View
32 parse!.rb
@@ -3,9 +3,33 @@
__DIR__ = File.dirname(__FILE__)
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'
-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
-traffic_calculator.calculate!
-traffic_calculator.print_results
+print_results(spit_traffic)
View
96 spec/traffic/scopes_spec.rb
@@ -1,54 +1,54 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
describe Traffic do
- describe "given a bunch of records" do
- before :each do
- @local_address = '192.168.0.1'
- @facebook = '77.235.14.9'
- @gmail = '77.235.14.10'
-
- @records = [
- Record.new(@local_address, @facebook),
- Record.new(@local_address, @gmail),
- Record.new(@facebook, @local_address),
- Record.new(@gmail, @local_address)
- ]
- end
-
-
- describe "given a Traffic instance loaded with records" do
- before :each do
- @traffic = Traffic.new(@records, @local_address)
- end
-
- describe "when #incoming is called" do
- before :each do
- @return_value = @traffic.incoming
- end
-
- it "should return a Traffic instance" do
- @return_value.should be_kind_of(Traffic)
- end
-
- 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
- end
- end
-
- describe "when #outcoming is called" do
- before :each do
- @return_value = @traffic.outcoming
- end
-
- it "should return a Traffic instance" do
- @return_value.should be_kind_of(Traffic)
- end
-
- 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
- end
- end
- end
+ describe "given a bunch of records" do
+ before :each do
+ @local_address = '192.168.0.1'
+ @facebook = '77.235.14.9'
+ @gmail = '77.235.14.10'
+
+ @records = [
+ Record.new(@local_address, @facebook),
+ Record.new(@local_address, @gmail),
+ Record.new(@facebook, @local_address),
+ Record.new(@gmail, @local_address)
+ ]
+ end
+
+ describe "given a Traffic instance loaded with records" do
+ before :each do
+ @traffic = Traffic.new @records
+ @traffic.local_address = @local_address
+ end
+
+ describe "when #incoming is called" do
+ before :each do
+ @return_value = @traffic.incoming
+ end
+
+ it "should return a Traffic instance" do
+ @return_value.should be_kind_of(Traffic)
+ end
+
+ it "should narrow records to incoming traffic only" do
+ @return_value.to_set.should == [Record.new(@facebook, @local_address), Record.new(@gmail, @local_address)].to_set
+ end
+ end
+
+ describe "when #outcoming is called" do
+ before :each do
+ @return_value = @traffic.outcoming
+ end
+
+ it "should return a Traffic instance" do
+ @return_value.should be_kind_of(Traffic)
+ end
+
+ it "should narrow records to outcoming traffic only" do
+ @return_value.to_set.should == [Record.new(@local_address, @facebook), Record.new(@local_address, @gmail)].to_set
end
+ end
+ end
+ end
end
View
91 spec/traffic_splitter_spec.rb
@@ -0,0 +1,91 @@
+require File.join(File.dirname(__FILE__), 'spec_helper')
+
+module TrafficSpecHelper
+ def internal_daily_traffic
+ Record.new @local_address, @internal_address,
+ end
+
+ def internal_nightly_traffic
+ end
+
+ def world_daily_traffic
+ end
+
+ def world_nightly_traffic
+ end
+end
+
+describe TrafficSplitter do
+ before :each do
+ @local_address = '212.112.96.1'
+ @internal_address = '212.112.1.1'
+ @world_address = '8.8.8.8'
+
+ @traffic_splitter = TrafficSplitter.new(@local_address)
+ end
+
+ include TrafficSpecHelper
+
+ describe "given a bunch of records of all types" do
+ before :each do
+ @records = []
+
+ # TODO: 15 записей нужны только для проверки одного теста
+ # Для остальных нужно сузить и проверять нормально, тестовыми данными, а не выполняя алгоритмы реализации
+
+ 15.times { |number| @record << internal_daily_traffic }
+ 15.times { |number| @record << internal_nightly_traffic }
+ 15.times { |number| @record << world_daily_traffic }
+ 15.times { |number| @record << world_nightly_traffic }
+ end
+
+ describe "when #split_traffic! is called" do
+ before :each do
+ @results = @traffic_splitter.split_traffic!(@records)
+ end
+
+ it "should create a two-dimensional hash" do
+ @results.keys.should == [:internal, :world]
+ @results[:internal].keys.should == [:daily, :nightly]
+ @results[:world].keys.should == [:daily, :nightly]
+ end
+
+ it "should put array of Host in each cell" do
+ @results.each_value do |split_results|
+ split_results.each_value do |hosts|
+ hosts.should be_kind_of(Array)
+ hosts.first.should be_kind_of(Host)
+ end
+ end
+ end
+
+ it "should spit records by type correctly" do
+ @results[:internal][:daily].each do |host|
+ host.records.each do |record|
+ record.should be_internal_relatively_to(@local_address)
+ record.should be_daily
+ end
+ end
+ end
+
+ it "should sort hosts by amount of bytes" do
+ hosts = @results[:internal][:daily]
+ previous_host = hosts.first
+ hosts[1..-1].each do |host|
+ (host.amount_of_traffic < previous_host.amount_of_traffic).should be_true
+ previous_host = host
+ end
+ end
+
+ it "should leave only 10 highest hosts" do
+ @results.each_value do |split_results|
+ split_results.each_value do |hosts|
+ hosts.size.should == 10
+ end
+ end
+ end
+ end
+ end
+end
+
+# Record #incoming? #daily? #local?
1 vendor/scopes-n-groups
@@ -0,0 +1 @@
+Subproject commit a9ce5e038d9d3cd569d81d9c340df69a950db013

0 comments on commit f87d6c0

Please sign in to comment.
Something went wrong with that request. Please try again.