audit.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. $oContext = new UserContext();
  36. $oRule = $oContext->GetObject('AuditRule', $iRuleId);
  37. $sOql = $oRule->Get('query');
  38. $oRuleFilter = DBObjectSearch::FromOQL($sOql);
  39. if ($oRule->Get('valid_flag') == 'false')
  40. {
  41. // The query returns directly the invalid elements
  42. $oFilter = $oRuleFilter;
  43. $oFilter->MergeWith($oDefinitionFilter);
  44. $oErrorObjectSet = new CMDBObjectSet($oFilter);
  45. }
  46. else
  47. {
  48. // The query returns only the valid elements, all the others are invalid
  49. $oFilter = $oRuleFilter;
  50. $oErrorObjectSet = new CMDBObjectSet($oFilter);
  51. $aValidIds = array(0); // Make sure that we have at least one value in the list
  52. while($oObj = $oErrorObjectSet->Fetch())
  53. {
  54. $aValidIds[] = $oObj->GetKey();
  55. }
  56. $oFilter = $oDefinitionFilter;
  57. $oFilter->AddCondition('id', $aValidIds, 'NOTIN');
  58. $oErrorObjectSet = new CMDBObjectSet($oFilter);
  59. }
  60. return $oErrorObjectSet;
  61. }
  62. function GetReportColor($iTotal, $iErrors)
  63. {
  64. $sResult = 'red';
  65. if ( ($iTotal == 0) || ($iErrors / $iTotal) <= 0.05 )
  66. {
  67. $sResult = 'green';
  68. }
  69. else if ( ($iErrors / $iTotal) <= 0.25 )
  70. {
  71. $sResult = 'orange';
  72. }
  73. return $sResult;
  74. }
  75. switch($operation)
  76. {
  77. case 'errors':
  78. $iCategory = utils::ReadParam('category', '');
  79. $iRuleIndex = utils::ReadParam('rule', 0);
  80. $oContext = new UserContext();
  81. $oAuditCategory = $oContext->GetObject('AuditCategory', $iCategory);
  82. $oDefinitionFilter = DBObjectSearch::FromOQL($oAuditCategory->Get('definition_set'));
  83. if (!empty($currentOrganization))
  84. {
  85. $oDefinitionFilter->AddCondition('org_id', $currentOrganization);
  86. }
  87. $oDefinitionSet = new CMDBObjectSet($oDefinitionFilter);
  88. $oErrorObjectSet = GetRuleResultSet($iRuleIndex, $oDefinitionFilter);
  89. $oAuditRule = $oContext->GetObject('AuditRule', $iRuleIndex);
  90. $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>');
  91. $oP->p('<a href="./audit.php?'.$oAppContext->GetForLink().'">[Back to audit results]</a>');
  92. $sBlockId = 'audit_errors';
  93. $oP->p("<div id=\"$sBlockId\" style=\"clear:both\">\n");
  94. cmdbAbstractObject::DisplaySet($oP, $oErrorObjectSet, array('block_id' => $sBlockId));
  95. $oP->p("</div>\n");
  96. break;
  97. case 'audit':
  98. default:
  99. $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>');
  100. $oAuditFilter = new CMDBSearchFilter('AuditCategory');
  101. $oCategoriesSet = new DBObjectSet($oAuditFilter);
  102. $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");
  103. $oP->add("<tr><td>\n");
  104. $oP->add("<table>\n");
  105. $oP->add("<tr>\n");
  106. $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");
  107. $oP->add("</tr>\n");
  108. while($oAuditCategory = $oCategoriesSet->fetch())
  109. {
  110. $oDefinitionFilter = DBObjectSearch::FromOQL($oAuditCategory->Get('definition_set'));
  111. $aObjectsWithErrors = array();
  112. if (!empty($currentOrganization))
  113. {
  114. if (MetaModel::IsValidFilterCode($oDefinitionFilter->GetClass(), 'org_id'))
  115. {
  116. $oDefinitionFilter->AddCondition('org_id', $currentOrganization);
  117. }
  118. }
  119. $aResults = array();
  120. $oDefinitionSet = new CMDBObjectSet($oDefinitionFilter);
  121. $iCount = $oDefinitionSet->Count();
  122. $oRulesFilter = new CMDBSearchFilter('AuditRule');
  123. $oRulesFilter->AddCondition('category_id', $oAuditCategory->GetKey());
  124. $oRulesSet = new DBObjectSet($oRulesFilter);
  125. while($oAuditRule = $oRulesSet->fetch() )
  126. {
  127. $aRow = array();
  128. $aRow['description'] = $oAuditRule->Get('name');
  129. if ($iCount == 0)
  130. {
  131. // nothing to check, really !
  132. $aRow['nb_errors'] = "<a href=\"?operation=errors&category=".$oAuditCategory->GetKey()."&rule=".$oAuditRule->GetKey()."\">0</a>";
  133. $aRow['percent_ok'] = '100.00';
  134. $aRow['class'] = GetReportColor($iCount, 0);
  135. }
  136. else
  137. {
  138. $oRuleFilter = DBObjectSearch::FromOQL($oAuditRule->Get('query'));
  139. $oErrorObjectSet = GetRuleResultSet($oAuditRule->GetKey(), $oDefinitionFilter);
  140. $iErrorsCount = $oErrorObjectSet->Count();
  141. while($oObj = $oErrorObjectSet->Fetch())
  142. {
  143. $aObjectsWithErrors[$oObj->GetKey()] = true;
  144. }
  145. $aRow['nb_errors'] = ($iErrorsCount == 0) ? '0' : "<a href=\"?operation=errors&category=".$oAuditCategory->GetKey()."&rule=".$oAuditRule->GetKey()."&".$oAppContext->GetForLink()."\">$iErrorsCount</a>";
  146. $aRow['percent_ok'] = sprintf('%.2f', 100.0 * (($iCount - $iErrorsCount) / $iCount));
  147. $aRow['class'] = GetReportColor($iCount, $iErrorsCount);
  148. }
  149. $aResults[] = $aRow;
  150. $iTotalErrors = count($aObjectsWithErrors);
  151. $sOverallPercentOk = ($iCount == 0) ? '100.00' : sprintf('%.2f', 100.0 * (($iCount - $iTotalErrors) / $iCount));
  152. $sClass = GetReportColor($iCount, $iTotalErrors);
  153. }
  154. $oP->add("<tr>\n");
  155. $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");
  156. $oP->add("</tr>\n");
  157. foreach($aResults as $aRow)
  158. {
  159. $oP->add("<tr>\n");
  160. $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");
  161. $oP->add("</tr>\n");
  162. }
  163. }
  164. $oP->add("</table>\n");
  165. $oP->add("</td></tr>\n");
  166. $oP->add("</table>\n");
  167. }
  168. $oP->output();
  169. ?>