From 8fb6f40462e835bf65346c706135f892bfe65009 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 26 Apr 2017 06:36:19 +0200 Subject: [PATCH] move cleanup_stats() to PMG::DBTools::purge_statistic_database() --- PMG/DBTools.pm | 39 +++++++++++++++++++++++++++++++++++++++ PMG/Statistic.pm | 38 -------------------------------------- bin/pmg-daily | 2 +- 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/PMG/DBTools.pm b/PMG/DBTools.pm index 612a0a7..08f1a52 100644 --- a/PMG/DBTools.pm +++ b/PMG/DBTools.pm @@ -6,6 +6,7 @@ use warnings; use POSIX ":sys_wait_h"; use POSIX ':signal_h'; use DBI; +use Time::Local; use PVE::SafeSyslog; use PVE::Tools; @@ -821,6 +822,44 @@ sub init_masterdb { die $err if $err; } +sub purge_statistic_database { + my ($dbh, $statlifetime) = @_; + + return if $statlifetime <= 0; + + my (undef, undef, undef, $mday, $mon, $year) = localtime(time()); + my $end = timelocal(0, 0, 0, $mday, $mon, $year); + my $start = $end - $statlifetime*86400; + + # delete statistics older than $start + + my $rows = 0; + + eval { + $dbh->begin_work; + + my $sth = $dbh->prepare("DELETE FROM CStatistic WHERE time < $start"); + $sth->execute; + $rows = $sth->rows; + $sth->finish; + + if ($rows > 0) { + $sth = $dbh->prepare( + "DELETE FROM CReceivers WHERE NOT EXISTS " . + "(SELECT * FROM CStatistic WHERE CID = CStatistic_CID AND RID = CStatistic_RID)"); + + $sth->execute; + } + $dbh->commit; + }; + if (my $err = $@) { + $dbh->rollback; + die $err; + } + + return $rows; +} + sub copy_table { my ($ldb, $rdb, $table) = @_; diff --git a/PMG/Statistic.pm b/PMG/Statistic.pm index 5c1b87a..49ed128 100755 --- a/PMG/Statistic.pm +++ b/PMG/Statistic.pm @@ -374,44 +374,6 @@ sub update_stats { while (update_stats_virusinfo ($dbh, $cinfo) > 0) {}; } -sub cleanup_stats { - my ($dbh, $statlifetime) = @_; - - return if $statlifetime <= 0; - - my (undef, undef, undef, $mday, $mon, $year) = localtime (time()); - my $end = timelocal(0, 0, 0, $mday, $mon, $year); - my $start = $end - $statlifetime*86400; - - # delete statistics older than $start - - my $rows = 0; - - eval { - $dbh->begin_work; - - my $sth = $dbh->prepare("DELETE FROM CStatistic WHERE time < $start"); - $sth->execute; - $rows = $sth->rows; - $sth->finish; - - if ($rows > 0) { - $sth = $dbh->prepare( - "DELETE FROM CReceivers WHERE NOT EXISTS " . - "(SELECT * FROM CStatistic WHERE CID = CStatistic_CID AND RID = CStatistic_RID)"); - - $sth->execute; - } - $dbh->commit; - }; - if (my $err = $@) { - $dbh->rollback; - die $err; - } - - return $rows; -} - sub total_mail_stat { my ($self, $rdb) = @_; diff --git a/bin/pmg-daily b/bin/pmg-daily index e5cec9a..985e7d0 100755 --- a/bin/pmg-daily +++ b/bin/pmg-daily @@ -36,7 +36,7 @@ if (my $statlifetime = $cfg->get ('admin', 'statlifetime')) { my $count = 0; eval { my $dbh = PMG::DBTools::open_ruledb(); - $count = PMG::Statistic::cleanup_stats($dbh, $statlifetime); + $count = PMG::DBTools::purge_statistic_database($dbh, $statlifetime); }; if (my $err = $@) { syslog('err', $err); -- 2.39.2