protected function getDeleteDBSizeEstimate($getSettingsFromQuery = false, $forceEstimate = false)
{
$this->checkDataPurgeAdminSettingsIsEnabled();
// get the purging settings & create two purger instances
if ($getSettingsFromQuery) {
$settings = $this->getPurgeSettingsFromRequest();
} else {
$settings = PrivacyManager::getPurgeDataSettings();
}
$doDatabaseSizeEstimate = PiwikConfig::getInstance()->Deletelogs['enable_auto_database_size_estimate'];
// determine the DB size & purged DB size
$metadataProvider = StaticContainer::get('Piwik\\Plugins\\DBStats\\MySQLMetadataProvider');
$tableStatuses = $metadataProvider->getAllTablesStatus();
$totalBytes = 0;
foreach ($tableStatuses as $status) {
$totalBytes += $status['Data_length'] + $status['Index_length'];
}
$formatter = new Formatter();
$result = array('currentSize' => $formatter->getPrettySizeFromBytes($totalBytes));
// if the db size estimate feature is enabled, get the estimate
if ($doDatabaseSizeEstimate || $forceEstimate == 1) {
// maps tables whose data will be deleted with number of rows that will be deleted
// if a value is -1, it means the table will be dropped.
$deletedDataSummary = PrivacyManager::getPurgeEstimate($settings);
$totalAfterPurge = $totalBytes;
foreach ($tableStatuses as $status) {
$tableName = $status['Name'];
if (isset($deletedDataSummary[$tableName])) {
$tableTotalBytes = $status['Data_length'] + $status['Index_length'];
// if dropping the table
if ($deletedDataSummary[$tableName] === ReportsPurger::DROP_TABLE) {
$totalAfterPurge -= $tableTotalBytes;
} else {
if ($status['Rows'] > 0) {
$totalAfterPurge -= $tableTotalBytes / $status['Rows'] * $deletedDataSummary[$tableName];
}
}
}
}
$result['sizeAfterPurge'] = $formatter->getPrettySizeFromBytes($totalAfterPurge);
$result['spaceSaved'] = $formatter->getPrettySizeFromBytes($totalBytes - $totalAfterPurge);
}
return $result;
}