From 40c2f6530f03015858abd72fa66db940535c4582 Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Wed, 11 Feb 2015 23:20:19 -0500 Subject: [PATCH] Clean up pruning 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. --- FT/Reporting.pm | 18 +++++++++++------- FT/Schema.pm | 8 +++++++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/FT/Reporting.pm b/FT/Reporting.pm index fcda1da..eb337b4 100644 --- a/FT/Reporting.pm +++ b/FT/Reporting.pm @@ -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) { @@ -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 @@ -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 @@ -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 ); @@ -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 ); @@ -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 ); diff --git a/FT/Schema.pm b/FT/Schema.pm index fac8cad..c262d0a 100644 --- a/FT/Schema.pm +++ b/FT/Schema.pm @@ -91,6 +91,12 @@ $TABLES->{raw_flow} = [ ]; $TABLES->{recent_talkers} = [ + + # ID field (auto inc) + { + name => 'id', + type => 'INT', + }, { name => 'internal_ip', type => 'INT', @@ -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)' } ];