audit.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. try
  25. {
  26. require_once('../approot.inc.php');
  27. require_once(APPROOT.'/application/application.inc.php');
  28. require_once(APPROOT.'/application/itopwebpage.class.inc.php');
  29. require_once(APPROOT.'/application/startup.inc.php');
  30. $operation = utils::ReadParam('operation', '');
  31. $oAppContext = new ApplicationContext();
  32. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  33. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  34. $oP = new iTopWebPage(Dict::S('UI:Audit:Title'));
  35. function GetRuleResultSet($iRuleId, $oDefinitionFilter)
  36. {
  37. $oRule = MetaModel::GetObject('AuditRule', $iRuleId);
  38. $sOql = $oRule->Get('query');
  39. $oRuleFilter = DBObjectSearch::FromOQL($sOql);
  40. if ($oRule->Get('valid_flag') == 'false')
  41. {
  42. // The query returns directly the invalid elements
  43. $oFilter = $oRuleFilter;
  44. $oFilter->MergeWith($oDefinitionFilter);
  45. $oErrorObjectSet = new CMDBObjectSet($oFilter);
  46. }
  47. else
  48. {
  49. // The query returns only the valid elements, all the others are invalid
  50. $oFilter = $oRuleFilter;
  51. $oErrorObjectSet = new CMDBObjectSet($oFilter);
  52. $aValidIds = array(0); // Make sure that we have at least one value in the list
  53. while($oObj = $oErrorObjectSet->Fetch())
  54. {
  55. $aValidIds[] = $oObj->GetKey();
  56. }
  57. $oFilter = $oDefinitionFilter;
  58. $oFilter->AddCondition('id', $aValidIds, 'NOTIN');
  59. $oErrorObjectSet = new CMDBObjectSet($oFilter);
  60. }
  61. return $oErrorObjectSet;
  62. }
  63. function GetReportColor($iTotal, $iErrors)
  64. {
  65. $sResult = 'red';
  66. if ( ($iTotal == 0) || ($iErrors / $iTotal) <= 0.05 )
  67. {
  68. $sResult = 'green';
  69. }
  70. else if ( ($iErrors / $iTotal) <= 0.25 )
  71. {
  72. $sResult = 'orange';
  73. }
  74. return $sResult;
  75. }
  76. switch($operation)
  77. {
  78. case 'errors':
  79. $iCategory = utils::ReadParam('category', '');
  80. $iRuleIndex = utils::ReadParam('rule', 0);
  81. $oAuditCategory = MetaModel::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 = MetaModel::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. }
  170. catch(CoreException $e)
  171. {
  172. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  173. $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
  174. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  175. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  176. $oP->output();
  177. if (MetaModel::IsLogEnabledIssue())
  178. {
  179. if (MetaModel::IsValidClass('EventIssue'))
  180. {
  181. $oLog = new EventIssue();
  182. $oLog->Set('message', $e->getMessage());
  183. $oLog->Set('userinfo', '');
  184. $oLog->Set('issue', $e->GetIssue());
  185. $oLog->Set('impact', 'Page could not be displayed');
  186. $oLog->Set('callstack', $e->getTrace());
  187. $oLog->Set('data', $e->getContextData());
  188. $oLog->DBInsertNoReload();
  189. }
  190. IssueLog::Error($e->getMessage());
  191. }
  192. // For debugging only
  193. //throw $e;
  194. }
  195. catch(Exception $e)
  196. {
  197. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  198. $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
  199. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  200. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  201. $oP->output();
  202. if (MetaModel::IsLogEnabledIssue())
  203. {
  204. if (MetaModel::IsValidClass('EventIssue'))
  205. {
  206. $oLog = new EventIssue();
  207. $oLog->Set('message', $e->getMessage());
  208. $oLog->Set('userinfo', '');
  209. $oLog->Set('issue', 'PHP Exception');
  210. $oLog->Set('impact', 'Page could not be displayed');
  211. $oLog->Set('callstack', $e->getTrace());
  212. $oLog->Set('data', array());
  213. $oLog->DBInsertNoReload();
  214. }
  215. IssueLog::Error($e->getMessage());
  216. }
  217. }
  218. ?>