audit.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Execute and shows the data quality audit
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('../application/application.inc.php');
  25. require_once('../application/itopwebpage.class.inc.php');
  26. require_once('../application/startup.inc.php');
  27. $currentOrganization = utils::ReadParam('org_id', '');
  28. $operation = utils::ReadParam('operation', '');
  29. $oAppContext = new ApplicationContext();
  30. require_once('../application/loginwebpage.class.inc.php');
  31. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  32. $oP = new iTopWebPage(Dict::S('UI:Audit:Title'), $currentOrganization);
  33. function GetRuleResultSet($iRuleId, $oDefinitionFilter)
  34. {
  35. $oRule = MetaModel::GetObject('AuditRule', $iRuleId);
  36. $sOql = $oRule->Get('query');
  37. $oRuleFilter = DBObjectSearch::FromOQL($sOql);
  38. if ($oRule->Get('valid_flag') == 'false')
  39. {
  40. // The query returns directly the invalid elements
  41. $oFilter = $oRuleFilter;
  42. $oFilter->MergeWith($oDefinitionFilter);
  43. $oErrorObjectSet = new CMDBObjectSet($oFilter);
  44. }
  45. else
  46. {
  47. // The query returns only the valid elements, all the others are invalid
  48. $oFilter = $oRuleFilter;
  49. $oErrorObjectSet = new CMDBObjectSet($oFilter);
  50. $aValidIds = array(0); // Make sure that we have at least one value in the list
  51. while($oObj = $oErrorObjectSet->Fetch())
  52. {
  53. $aValidIds[] = $oObj->GetKey();
  54. }
  55. $oFilter = $oDefinitionFilter;
  56. $oFilter->AddCondition('id', $aValidIds, 'NOTIN');
  57. $oErrorObjectSet = new CMDBObjectSet($oFilter);
  58. }
  59. return $oErrorObjectSet;
  60. }
  61. function GetReportColor($iTotal, $iErrors)
  62. {
  63. $sResult = 'red';
  64. if ( ($iTotal == 0) || ($iErrors / $iTotal) <= 0.05 )
  65. {
  66. $sResult = 'green';
  67. }
  68. else if ( ($iErrors / $iTotal) <= 0.25 )
  69. {
  70. $sResult = 'orange';
  71. }
  72. return $sResult;
  73. }
  74. switch($operation)
  75. {
  76. case 'errors':
  77. $iCategory = utils::ReadParam('category', '');
  78. $iRuleIndex = utils::ReadParam('rule', 0);
  79. $oAuditCategory = MetaModel::GetObject('AuditCategory', $iCategory);
  80. $oDefinitionFilter = DBObjectSearch::FromOQL($oAuditCategory->Get('definition_set'));
  81. if (!empty($currentOrganization))
  82. {
  83. $oDefinitionFilter->AddCondition('org_id', $currentOrganization, '=');
  84. }
  85. $oDefinitionSet = new CMDBObjectSet($oDefinitionFilter);
  86. $oErrorObjectSet = GetRuleResultSet($iRuleIndex, $oDefinitionFilter);
  87. $oAuditRule = MetaModel::GetObject('AuditRule', $iRuleIndex);
  88. $oP->add('<div class="page_header"><h1>Audit Errors: <span class="hilite">'.$oAuditRule->Get('description').'</span></h1><img style="margin-top: -20px; margin-right: 10px; float: right;" src="../images/stop.png"/></div>');
  89. $oP->p('<a href="./audit.php?'.$oAppContext->GetForLink().'">[Back to audit results]</a>');
  90. $sBlockId = 'audit_errors';
  91. $oP->p("<div id=\"$sBlockId\" style=\"clear:both\">\n");
  92. cmdbAbstractObject::DisplaySet($oP, $oErrorObjectSet, array('block_id' => $sBlockId));
  93. $oP->p("</div>\n");
  94. break;
  95. case 'audit':
  96. default:
  97. $oP->add('<div class="page_header"><h1>'.Dict::S('UI:Audit:InteractiveAudit').'</h1><img style="margin-top: -20px; margin-right: 10px; float: right;" src="../images/clean.png"/></div>');
  98. $oAuditFilter = new CMDBSearchFilter('AuditCategory');
  99. $oCategoriesSet = new DBObjectSet($oAuditFilter);
  100. $oP->add("<table style=\"margin-top: 1em; padding: 0px; border-top: 3px solid #f6f6f1; border-left: 3px solid #f6f6f1; border-bottom: 3px solid #e6e6e1; border-right: 3px solid #e6e6e1;\">\n");
  101. $oP->add("<tr><td>\n");
  102. $oP->add("<table>\n");
  103. $oP->add("<tr>\n");
  104. $oP->add("<th><img src=\"../images/minus.gif\"></th><th class=\"alignLeft\">".Dict::S('UI:Audit:HeaderAuditRule')."</th><th>".Dict::S('UI:Audit:HeaderNbObjects')."</th><th>".Dict::S('UI:Audit:HeaderNbErrors')."</th><th>".Dict::S('UI:Audit:PercentageOk')."</th>\n");
  105. $oP->add("</tr>\n");
  106. while($oAuditCategory = $oCategoriesSet->fetch())
  107. {
  108. $oDefinitionFilter = DBObjectSearch::FromOQL($oAuditCategory->Get('definition_set'));
  109. $aObjectsWithErrors = array();
  110. if (!empty($currentOrganization))
  111. {
  112. if (MetaModel::IsValidFilterCode($oDefinitionFilter->GetClass(), 'org_id'))
  113. {
  114. $oDefinitionFilter->AddCondition('org_id', $currentOrganization, '=');
  115. }
  116. }
  117. $aResults = array();
  118. $oDefinitionSet = new CMDBObjectSet($oDefinitionFilter);
  119. $iCount = $oDefinitionSet->Count();
  120. $oRulesFilter = new CMDBSearchFilter('AuditRule');
  121. $oRulesFilter->AddCondition('category_id', $oAuditCategory->GetKey(), '=');
  122. $oRulesSet = new DBObjectSet($oRulesFilter);
  123. while($oAuditRule = $oRulesSet->fetch() )
  124. {
  125. $aRow = array();
  126. $aRow['description'] = $oAuditRule->Get('name');
  127. if ($iCount == 0)
  128. {
  129. // nothing to check, really !
  130. $aRow['nb_errors'] = "<a href=\"?operation=errors&category=".$oAuditCategory->GetKey()."&rule=".$oAuditRule->GetKey()."\">0</a>";
  131. $aRow['percent_ok'] = '100.00';
  132. $aRow['class'] = GetReportColor($iCount, 0);
  133. }
  134. else
  135. {
  136. $oRuleFilter = DBObjectSearch::FromOQL($oAuditRule->Get('query'));
  137. $oErrorObjectSet = GetRuleResultSet($oAuditRule->GetKey(), $oDefinitionFilter);
  138. $iErrorsCount = $oErrorObjectSet->Count();
  139. while($oObj = $oErrorObjectSet->Fetch())
  140. {
  141. $aObjectsWithErrors[$oObj->GetKey()] = true;
  142. }
  143. $aRow['nb_errors'] = ($iErrorsCount == 0) ? '0' : "<a href=\"?operation=errors&category=".$oAuditCategory->GetKey()."&rule=".$oAuditRule->GetKey()."&".$oAppContext->GetForLink()."\">$iErrorsCount</a>";
  144. $aRow['percent_ok'] = sprintf('%.2f', 100.0 * (($iCount - $iErrorsCount) / $iCount));
  145. $aRow['class'] = GetReportColor($iCount, $iErrorsCount);
  146. }
  147. $aResults[] = $aRow;
  148. $iTotalErrors = count($aObjectsWithErrors);
  149. $sOverallPercentOk = ($iCount == 0) ? '100.00' : sprintf('%.2f', 100.0 * (($iCount - $iTotalErrors) / $iCount));
  150. $sClass = GetReportColor($iCount, $iTotalErrors);
  151. }
  152. $oP->add("<tr>\n");
  153. $oP->add("<th><img src=\"../images/minus.gif\"></th><th class=\"alignLeft\">".$oAuditCategory->GetName()."</th><th class=\"alignRight\">$iCount</th><th class=\"alignRight\">$iTotalErrors</th><th class=\"alignRight $sClass\">$sOverallPercentOk %</th>\n");
  154. $oP->add("</tr>\n");
  155. foreach($aResults as $aRow)
  156. {
  157. $oP->add("<tr>\n");
  158. $oP->add("<td>&nbsp;</td><td colspan=\"2\">".$aRow['description']."</td><td class=\"alignRight\">".$aRow['nb_errors']."</td><td class=\"alignRight ".$aRow['class']."\">".$aRow['percent_ok']." %</td>\n");
  159. $oP->add("</tr>\n");
  160. }
  161. }
  162. $oP->add("</table>\n");
  163. $oP->add("</td></tr>\n");
  164. $oP->add("</table>\n");
  165. }
  166. $oP->output();
  167. ?>