From 3b20be05cb2bbf1ebea4bb3ca16a42676a86fa0f Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 21 Jul 2020 14:08:09 +0200 Subject: [PATCH] 3189 - DBTools enhancements * Add CLI command bin/report.php to generate report offline * Keep the latest report in log/dbtools-report.log in order to visualize it with "Log management" menu --- .../2.x/combodo-db-tools/bin/report.php | 29 +++++++ datamodels/2.x/combodo-db-tools/dbtools.php | 71 +++------------- .../module.combodo-db-tools.php | 3 +- .../src/Service/DBAnalyzerUtils.php | 81 +++++++++++++++++++ 4 files changed, 123 insertions(+), 61 deletions(-) create mode 100644 datamodels/2.x/combodo-db-tools/bin/report.php create mode 100644 datamodels/2.x/combodo-db-tools/src/Service/DBAnalyzerUtils.php diff --git a/datamodels/2.x/combodo-db-tools/bin/report.php b/datamodels/2.x/combodo-db-tools/bin/report.php new file mode 100644 index 000000000..2428ce779 --- /dev/null +++ b/datamodels/2.x/combodo-db-tools/bin/report.php @@ -0,0 +1,29 @@ +CheckIntegrity([]); + +if (empty($aResults)) +{ + echo "Database OK\n"; + exit(0); +} + +$sReportFile = DBAnalyzerUtils::GenerateReport($aResults); + +echo "Report generated: {$sReportFile}.log\n"; diff --git a/datamodels/2.x/combodo-db-tools/dbtools.php b/datamodels/2.x/combodo-db-tools/dbtools.php index 72eaad23e..869c4d337 100644 --- a/datamodels/2.x/combodo-db-tools/dbtools.php +++ b/datamodels/2.x/combodo-db-tools/dbtools.php @@ -17,6 +17,8 @@ * You should have received a copy of the GNU Affero General Public License */ +use Combodo\iTop\DBTools\Service\DBAnalyzerUtils; + @include_once('../../approot.inc.php'); require_once(APPROOT.'application/startup.inc.php'); @@ -53,7 +55,7 @@ function DisplayDBInconsistencies(iTopWebPage &$oP, ApplicationContext &$oAppCon $bRunAnalysis = intval(utils::ReadParam('run_analysis', '0')); if ($bRunAnalysis) { - $oDBAnalyzer = new DatabaseAnalyzer(); + $oDBAnalyzer = new DatabaseAnalyzer(0); $aResults = $oDBAnalyzer->CheckIntegrity($aClassSelection); if (empty($aResults)) { @@ -199,74 +201,23 @@ function DisplayDBInconsistencies(iTopWebPage &$oP, ApplicationContext &$oAppCon */ function DisplayInconsistenciesReport($aResults) { - $sDBToolsFolder = str_replace("\\", '/', APPROOT.'log/'); - $sReportFile = 'dbtools-report-'.date('Y-m-d-H-i-s'); - - $fReport = fopen($sDBToolsFolder.$sReportFile.'.txt', 'w'); - fwrite($fReport, 'Database Maintenance tools: '.date('Y-m-d H:i:s')."\r\n"); - foreach($aResults as $sClass => $aErrorList) - { - fwrite($fReport, ''); - foreach($aErrorList as $sErrorLabel => $aError) - { - fwrite($fReport, "\r\n----------\r\n"); - fwrite($fReport, 'Class: '.MetaModel::GetName($sClass).' ('.$sClass.")\r\n"); - $iCount = $aError['count']; - fwrite($fReport, 'Count: '.$iCount."\r\n"); - fwrite($fReport, 'Error: '.$sErrorLabel."\r\n"); - $sQuery = $aError['query']; - fwrite($fReport, 'Query: '.$sQuery."\r\n"); - - if (isset($aError['fixit'])) - { - fwrite($fReport, "\r\nFix it (indication):\r\n\r\n"); - $aFixitQueries = $aError['fixit']; - foreach($aFixitQueries as $sFixitQuery) - { - fwrite($fReport, "$sFixitQuery\r\n"); - } - fwrite($fReport, "\r\n"); - } - - $sQueryResult = ''; - $aIdList = array(); - foreach($aError['res'] as $aRes) - { - foreach($aRes as $sKey => $sValue) - { - $sQueryResult .= "'$sKey'='$sValue' "; - if ($sKey == 'id') - { - $aIdList[] = $sValue; - } - } - $sQueryResult .= "\r\n"; - - } - fwrite($fReport, "Result: \r\n".$sQueryResult); - $sIdList = '('.implode(',', $aIdList).')'; - fwrite($fReport, 'Ids: '.$sIdList."\r\n"); - } - } - fclose($fReport); + $sReportFile = DBAnalyzerUtils::GenerateReport($aResults); + $sZipReport = "{$sReportFile}.zip"; $oArchive = new ZipArchive(); - $oArchive->open($sDBToolsFolder.$sReportFile.'.zip', ZipArchive::CREATE); - $oArchive->addFile($sDBToolsFolder.$sReportFile.'.txt', $sReportFile.'.txt'); + $oArchive->open($sZipReport, ZipArchive::CREATE); + $oArchive->addFile($sReportFile.'.log', basename($sReportFile.'.log')); $oArchive->close(); - unlink($sDBToolsFolder.$sReportFile.'.txt'); - $sReportFile = $sDBToolsFolder.$sReportFile.'.zip'; - header('Content-Description: File Transfer'); header('Content-Type: multipart/x-zip'); - header('Content-Disposition: inline; filename="'.basename($sReportFile).'"'); + header('Content-Disposition: inline; filename="'.basename($sZipReport).'"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); - header('Content-Length: '.filesize($sReportFile)); - readfile($sReportFile); - unlink($sReportFile); + header('Content-Length: '.filesize($sZipReport)); + readfile($sZipReport); + unlink($sZipReport); exit(0); } diff --git a/datamodels/2.x/combodo-db-tools/module.combodo-db-tools.php b/datamodels/2.x/combodo-db-tools/module.combodo-db-tools.php index f71a05de1..3cd01a400 100644 --- a/datamodels/2.x/combodo-db-tools/module.combodo-db-tools.php +++ b/datamodels/2.x/combodo-db-tools/module.combodo-db-tools.php @@ -42,7 +42,8 @@ SetupWebPage::AddModule( // 'datamodel' => array( 'model.combodo-db-tools.php', - 'src/Service/DBToolsUtils.php' + 'src/Service/DBToolsUtils.php', + 'src/Service/DBAnalyzerUtils.php', ), 'webservice' => array(), 'data.struct' => array(), diff --git a/datamodels/2.x/combodo-db-tools/src/Service/DBAnalyzerUtils.php b/datamodels/2.x/combodo-db-tools/src/Service/DBAnalyzerUtils.php new file mode 100644 index 000000000..725b45d55 --- /dev/null +++ b/datamodels/2.x/combodo-db-tools/src/Service/DBAnalyzerUtils.php @@ -0,0 +1,81 @@ + $aErrorList) + { + fwrite($fReport, ''); + foreach ($aErrorList as $sErrorLabel => $aError) + { + fwrite($fReport, "\r\n----------\r\n"); + fwrite($fReport, 'Class: '.MetaModel::GetName($sClass).' ('.$sClass.")\r\n"); + $iCount = $aError['count']; + fwrite($fReport, 'Count: '.$iCount."\r\n"); + fwrite($fReport, 'Error: '.$sErrorLabel."\r\n"); + $sQuery = $aError['query']; + fwrite($fReport, 'Query: '.$sQuery."\r\n"); + + if (isset($aError['fixit'])) + { + fwrite($fReport, "\r\nFix it (indication):\r\n\r\n"); + $aFixitQueries = $aError['fixit']; + foreach ($aFixitQueries as $sFixitQuery) + { + fwrite($fReport, "$sFixitQuery\r\n"); + } + fwrite($fReport, "\r\n"); + } + + $sQueryResult = ''; + $aIdList = array(); + foreach ($aError['res'] as $aRes) + { + foreach ($aRes as $sKey => $sValue) + { + $sQueryResult .= "'$sKey'='$sValue' "; + if ($sKey == 'id') + { + $aIdList[] = $sValue; + } + } + $sQueryResult .= "\r\n"; + + } + fwrite($fReport, "Result: \r\n".$sQueryResult); + $sIdList = '('.implode(',', $aIdList).')'; + fwrite($fReport, 'Ids: '.$sIdList."\r\n"); + } + } + fclose($fReport); + + + $sReportFile = $sDBToolsFolder.$sReportFile; + + return $sReportFile; + } +}