/
cleanup
executable file
·37 lines (26 loc) · 964 Bytes
/
cleanup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- cperl -*-
eval 'exec $CBROOT/bin/perl $0 ${1+"$@"}'
unless $running_under_some_shell;
use strict;
use warnings;
use NP::Model;
use DateTime;
use DateTime::Format::MySQL;
my $dbh = NP::Model->dbh;
my ($min_ts) = $dbh->selectrow_array(q[select min(ts) from log_scores]);
$min_ts = DateTime::Format::MySQL->parse_datetime( $min_ts ) or die "Could not parse date from mysql";
my $archive_date = $min_ts->add( hours => 8 );
my $limit = DateTime->now->subtract( days => 36 );
if ($archive_date > $limit) {
$archive_date = $limit;
}
if ($archive_date < $min_ts) {
# no work todo
exit;
}
print "Moving records from before $archive_date\n" if -t STDIN;
my $rv = $dbh->do(q[insert into log_scores_archive select * from log_scores where ts < ?], {}, $archive_date);
die unless $rv;
print "Inserted $rv records\n" if -t STDIN;
$rv = $dbh->do(q[delete from log_scores where ts < ?], {}, $archive_date);
print "Removed $rv records\n" if -t STDIN;