Bug 19008: More database cleanups - statistics

misc/cronjobs/cleanup_database.pl provides some database cleanup.
Yet some tables that could need cleanup are not in this script.

This patch adds cleanup for statistics table.

Test plan :
- Count statistics : select count(*),date(datetime) from statistics group by date(datetime);
- Run cleanup : misc/cronjobs/cleanup_database.pl -v --statistics 30
- Recount statistics

Signed-off-by: Mark Tompsett <mtompset@hotmail.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Fridolin Somers 2017-07-28 14:12:22 +02:00 committed by Martin Renvoize
parent 6ac19e02ed
commit eae2dfce06
No known key found for this signature in database
GPG Key ID: 422B469130441A0F
1 changed files with 17 additions and 1 deletions

View File

@ -45,7 +45,7 @@ use Koha::UploadedFiles;
sub usage {
print STDERR <<USAGE;
Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged] [--import DAYS] [--logs DAYS] [--searchhistory DAYS] [--restrictions DAYS] [--all-restrictions] [--fees DAYS] [--temp-uploads] [--temp-uploads-days DAYS] [--uploads-missing 0|1 ]
Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged] [--import DAYS] [--logs DAYS] [--searchhistory DAYS] [--restrictions DAYS] [--all-restrictions] [--fees DAYS] [--temp-uploads] [--temp-uploads-days DAYS] [--uploads-missing 0|1 ] [--statistics DAYS]
-h --help prints this help message, and exits, ignoring all
other options
@ -83,6 +83,7 @@ Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueu
--temp-uploads-days DAYS Override the corresponding preference value.
--uploads-missing FLAG Delete upload records for missing files when FLAG is true, count them otherwise
--oauth-tokens Delete expired OAuth2 tokens
--statistics DAYS Purge entries from statistics older than DAYS days.
USAGE
exit $_[0];
}
@ -109,6 +110,7 @@ my $temp_uploads;
my $temp_uploads_days;
my $uploads_missing;
my $oauth_tokens;
my $pStatistics;
GetOptions(
'h|help' => \$help,
@ -133,6 +135,7 @@ GetOptions(
'temp-uploads-days:i' => \$temp_uploads_days,
'uploads-missing:i' => \$uploads_missing,
'oauth-tokens' => \$oauth_tokens,
'statistics:i' => \$pStatistics,
) || usage(1);
# Use default values
@ -167,6 +170,7 @@ unless ( $sessions
|| $temp_uploads
|| defined $uploads_missing
|| $oauth_tokens
|| $pStatistics
) {
print "You did not specify any cleanup work for the script to do.\n\n";
usage(1);
@ -353,6 +357,18 @@ if ($oauth_tokens) {
say "Removed $count expired OAuth2 tokens" if $verbose;
}
if ($pStatistics) {
print "Purging statistics older than $pStatistics days.\n" if $verbose;
$sth = $dbh->prepare(
q{
DELETE FROM statistics
WHERE datetime < DATE_SUB(CURDATE(), INTERVAL ? DAY)
}
);
$sth->execute($pStatistics);
print "Done with purging statistics.\n" if $verbose;
}
exit(0);
sub RemoveOldSessions {