Skip to content

Commit

Permalink
Clean up pruning
Browse files Browse the repository at this point in the history
Now we only keep the top 21 (number of talkers in the grid) scores
Add id column to talkerpair (id is just src/dst concatenated) to
use in the subselect in the prune.
  • Loading branch information
skarfacegc committed Feb 12, 2015
1 parent a5b0a2c commit 40c2f65
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
18 changes: 11 additions & 7 deletions FT/Reporting.pm
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ sub getFlowsByTalkerPair
my $reporting_interval = $duration // $config->{reporting_interval};
my $ret_struct;

my $flows = $self->getFlowsForLast( $reporting_interval );
my $flows = $self->getFlowsForLast($reporting_interval);

foreach my $flow (@$flows)
{
Expand Down Expand Up @@ -195,9 +195,9 @@ sub updateRecentTalkers

$update_sql = qq{
INSERT OR REPLACE INTO
recent_talkers (internal_ip, external_ip, score, last_update)
recent_talkers (id, internal_ip, external_ip, score, last_update)
VALUES
(?,?,?,?)
(?,?,?,?,?)
};

# Age the scores
Expand Down Expand Up @@ -228,7 +228,6 @@ sub updateRecentTalkers

my @flow_bytes = map $_->{bytes}, @{ $recent_flows->{$recent_pair}{flows} };


unless ( List::Util::sum(@flow_bytes) < 500 )
{
# Add the average traffic for the recent flows to the score
Expand All @@ -245,7 +244,8 @@ sub updateRecentTalkers

foreach my $scored_flow ( keys %$scored_flows )
{
$sth->execute( $scored_flows->{$scored_flow}{internal_ip},
$sth->execute( $scored_flows->{$scored_flow}{internal_ip} . $scored_flows->{$scored_flow}{external_ip},
$scored_flows->{$scored_flow}{internal_ip},
$scored_flows->{$scored_flow}{external_ip},
$scored_flows->{$scored_flow}{score}, time )
or $logger->warning( "Couldn't execute: " . $dbh->errstr );
Expand All @@ -266,7 +266,7 @@ sub getRecentTalkers
my $ret_struct = {};

my $sql = qq{
SELECT * FROM recent_talkers ORDER BY score, last_update
SELECT * FROM recent_talkers ORDER BY score DESC, last_update DESC
};

my $sth = $dbh->prepare($sql) or $logger->warn( "Couldn't prepare:\n $sql\n" . $dbh->errstr );
Expand Down Expand Up @@ -319,7 +319,11 @@ sub purgeRecentTalkers
my $dbh = $self->_initDB();
my $rows_deleted;
my $sql = qq {
DELETE FROM recent_talkers WHERE score <= 0
DELETE FROM recent_talkers WHERE id NOT IN (
SELECT id FROM recent_talkers
ORDER BY score DESC
LIMIT 21
)
};

my $sth = $dbh->prepare($sql) or $logger->logconfess( 'failed to prepare:' . $DBI::errstr );
Expand Down
8 changes: 7 additions & 1 deletion FT/Schema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ $TABLES->{raw_flow} = [
];

$TABLES->{recent_talkers} = [

# ID field (auto inc)
{
name => 'id',
type => 'INT',
},
{
name => 'internal_ip',
type => 'INT',
Expand All @@ -110,7 +116,7 @@ $TABLES->{recent_talkers} = [
# TODO: Make primary key setup less dumb.
{
name => 'last_update',
type => 'INT, PRIMARY KEY (internal_ip, external_ip)'
type => 'INT, PRIMARY KEY (id)'
}

];
Expand Down

0 comments on commit 40c2f65

Please sign in to comment.