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:
parent
7722ea85b5
commit
beb9f9c5aa
@ -3,22 +3,47 @@
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use JSON;
|
||||
use POSIX qw(mktime strftime);
|
||||
use DBI;
|
||||
|
||||
my $limit = "datetime('now', '-13 months')";
|
||||
my ($limit,$tnYear,$datadir,$dbf,$dsn,$sth,$dbh,$sql,@dbInfo,$dbInfo);
|
||||
|
||||
# directory cointains databases
|
||||
my $datadir = "./data";
|
||||
my $dbf = "$datadir/fhem_statistics_2017.sqlite";
|
||||
my $dsn = "dbi:SQLite:dbname=$dbf";
|
||||
my $sth;
|
||||
my $dbh = DBI->connect($dsn,"","", { RaiseError => 1, ShowErrorStatement => 1 }) ||
|
||||
# define limits for outdated data
|
||||
$limit = "datetime('now', '-13 months')";
|
||||
$tnYear = strftime("%Y", localtime)-2; # delete all entries (current Year -2)
|
||||
|
||||
# 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";
|
||||
$dbh->do("DELETE FROM jsonNodes where lastSeen < $limit");
|
||||
print "VACUUM...\n";
|
||||
|
||||
# delete outdated statistics data for submissionsPerDay
|
||||
print "Deleting submissionsPerDay in $tnYear\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";
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user