2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-03 16:56:54 +00:00

maintainDB.cgi: add deletion for outdated submissionsPerDay

git-svn-id: https://svn.fhem.de/fhem/trunk@14909 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
betateilchen 2017-08-16 17:03:39 +00:00
parent 7722ea85b5
commit beb9f9c5aa

View File

@ -3,23 +3,48 @@
use strict; use strict;
use warnings; use warnings;
use JSON;
use POSIX qw(mktime strftime);
use DBI; use DBI;
my $limit = "datetime('now', '-13 months')"; my ($limit,$tnYear,$datadir,$dbf,$dsn,$sth,$dbh,$sql,@dbInfo,$dbInfo);
# directory cointains databases # define limits for outdated data
my $datadir = "./data"; $limit = "datetime('now', '-13 months')";
my $dbf = "$datadir/fhem_statistics_2017.sqlite"; $tnYear = strftime("%Y", localtime)-2; # delete all entries (current Year -2)
my $dsn = "dbi:SQLite:dbname=$dbf";
my $sth;
my $dbh = DBI->connect($dsn,"","", { RaiseError => 1, ShowErrorStatement => 1 }) ||
die "Cannot connect: $DBI::errstr";
# database connection
print "Establishing database connection...";
$datadir = "./data";
$dbf = "$datadir/fhem_statistics_2017.sqlite";
$dsn = "dbi:SQLite:dbname=$dbf";
$dbh = DBI->connect($dsn,"","", { RaiseError => 1, ShowErrorStatement => 1 }) ||
die "Cannot connect: $DBI::errstr";
print "connected.\n";
# delete records older than limit
print "Deleting records...\n"; print "Deleting records...\n";
$dbh->do("DELETE FROM jsonNodes where lastSeen < $limit"); $dbh->do("DELETE FROM jsonNodes where lastSeen < $limit");
print "VACUUM...\n";
$dbh->do("VACUUM"); # delete outdated statistics data for submissionsPerDay
$dbh->disconnect(); print "Deleting submissionsPerDay in $tnYear\n";
print "Done.\n"; $sql = q(SELECT * from jsonNodes where uniqueID = 'databaseInfo');
$sth = $dbh->prepare( $sql );
$sth->execute();
@dbInfo = $sth->fetchrow_array();
$dbInfo = decode_json $dbInfo[3];
delete $dbInfo->{'submissionsPerDay'}{$tnYear};
$dbInfo = encode_json $dbInfo;
$sth = $dbh->prepare(q{INSERT OR REPLACE INTO jsonNodes(uniqueID,json) VALUES(?,?)});
$sth->execute('databaseInfo',$dbInfo);
$sth->finish();
# shrink database
print "Shrinking database.\n";
$dbh->do("VACUUM");
# Done.
$dbh->disconnect();
print "Done.\n";
1; 1;