audit.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. /**
  36. * Adds the context parameters to the audit query
  37. */
  38. function FilterByContext(DBObjectSearch &$oFilter, ApplicationContext $oAppContext)
  39. {
  40. $sObjClass = $oFilter->GetClass();
  41. $aContextParams = $oAppContext->GetNames();
  42. if (is_callable("$sObjClass::MapContextParam"))
  43. {
  44. foreach($aContextParams as $sParamName)
  45. {
  46. $sValue = $oAppContext->GetCurrentValue($sParamName, null);
  47. if ($sValue != null)
  48. {
  49. $sAttCode = eval("return $sObjClass::MapContextParam('$sParamName');"); // Returns null when there is no mapping for this parameter
  50. if ($sAttCode != null)
  51. {
  52. $oFilter->AddCondition($sAttCode, $sValue);
  53. }
  54. }
  55. }
  56. }
  57. }
  58. function GetRuleResultSet($iRuleId, $oDefinitionFilter, $oAppContext)
  59. {
  60. $oRule = MetaModel::GetObject('AuditRule', $iRuleId);
  61. $sOql = $oRule->Get('query');
  62. $oRuleFilter = DBObjectSearch::FromOQL($sOql);
  63. FilterByContext($oRuleFilter, $oAppContext); // Not needed since this filter is a subset of the definition filter, but may speedup things
  64. if ($oRule->Get('valid_flag') == 'false')
  65. {
  66. // The query returns directly the invalid elements
  67. $oFilter = $oRuleFilter;
  68. $oFilter->MergeWith($oDefinitionFilter);
  69. $oErrorObjectSet = new CMDBObjectSet($oFilter);
  70. }
  71. else
  72. {
  73. // The query returns only the valid elements, all the others are invalid
  74. $oFilter = $oRuleFilter;
  75. $oErrorObjectSet = new CMDBObjectSet($oFilter);
  76. $aValidIds = array(0); // Make sure that we have at least one value in the list
  77. while($oObj = $oErrorObjectSet->Fetch())
  78. {
  79. $aValidIds[] = $oObj->GetKey();
  80. }
  81. $oFilter = $oDefinitionFilter;
  82. $oFilter->AddCondition('id', $aValidIds, 'NOTIN');
  83. $oErrorObjectSet = new CMDBObjectSet($oFilter);
  84. }
  85. return $oErrorObjectSet;
  86. }
  87. function GetReportColor($iTotal, $iErrors)
  88. {
  89. $sResult = 'red';
  90. if ( ($iTotal == 0) || ($iErrors / $iTotal) <= 0.05 )
  91. {
  92. $sResult = 'green';
  93. }
  94. else if ( ($iErrors / $iTotal) <= 0.25 )
  95. {
  96. $sResult = 'orange';
  97. }
  98. return $sResult;
  99. }
  100. switch($operation)
  101. {
  102. case 'errors':
  103. $iCategory = utils::ReadParam('category', '');
  104. $iRuleIndex = utils::ReadParam('rule', 0);
  105. $oAuditCategory = MetaModel::GetObject('AuditCategory', $iCategory);
  106. $oDefinitionFilter = DBObjectSearch::FromOQL($oAuditCategory->Get('definition_set'));
  107. FilterByContext($oDefinitionFilter, $oAppContext);
  108. $oDefinitionSet = new CMDBObjectSet($oDefinitionFilter);
  109. $oErrorObjectSet = GetRuleResultSet($iRuleIndex, $oDefinitionFilter, $oAppContext);
  110. $oAuditRule = MetaModel::GetObject('AuditRule', $iRuleIndex);
  111. $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>');
  112. $oP->p('<a href="./audit.php?'.$oAppContext->GetForLink().'">[Back to audit results]</a>');
  113. $sBlockId = 'audit_errors';
  114. $oP->p("<div id=\"$sBlockId\" style=\"clear:both\">\n");
  115. $oBlock = DisplayBlock::FromObjectSet($oErrorObjectSet, 'list');
  116. $oBlock->Display($oP, 1);
  117. $oP->p("</div>\n");
  118. break;
  119. case 'audit':
  120. default:
  121. $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>');
  122. $oAuditFilter = new CMDBSearchFilter('AuditCategory');
  123. $oCategoriesSet = new DBObjectSet($oAuditFilter);
  124. $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");
  125. $oP->add("<tr><td>\n");
  126. $oP->add("<table>\n");
  127. $oP->add("<tr>\n");
  128. $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");
  129. $oP->add("</tr>\n");
  130. while($oAuditCategory = $oCategoriesSet->fetch())
  131. {
  132. $oDefinitionFilter = DBObjectSearch::FromOQL($oAuditCategory->Get('definition_set'));
  133. FilterByContext($oDefinitionFilter, $oAppContext);
  134. $aObjectsWithErrors = array();
  135. if (!empty($currentOrganization))
  136. {
  137. if (MetaModel::IsValidFilterCode($oDefinitionFilter->GetClass(), 'org_id'))
  138. {
  139. $oDefinitionFilter->AddCondition('org_id', $currentOrganization, '=');
  140. }
  141. }
  142. $aResults = array();
  143. $oDefinitionSet = new CMDBObjectSet($oDefinitionFilter);
  144. $iCount = $oDefinitionSet->Count();
  145. $oRulesFilter = new CMDBSearchFilter('AuditRule');
  146. $oRulesFilter->AddCondition('category_id', $oAuditCategory->GetKey(), '=');
  147. $oRulesSet = new DBObjectSet($oRulesFilter);
  148. while($oAuditRule = $oRulesSet->fetch() )
  149. {
  150. $aRow = array();
  151. $aRow['description'] = $oAuditRule->Get('name');
  152. if ($iCount == 0)
  153. {
  154. // nothing to check, really !
  155. $aRow['nb_errors'] = "<a href=\"?operation=errors&category=".$oAuditCategory->GetKey()."&rule=".$oAuditRule->GetKey()."\">0</a>";
  156. $aRow['percent_ok'] = '100.00';
  157. $aRow['class'] = GetReportColor($iCount, 0);
  158. }
  159. else
  160. {
  161. $oRuleFilter = DBObjectSearch::FromOQL($oAuditRule->Get('query'));
  162. $oErrorObjectSet = GetRuleResultSet($oAuditRule->GetKey(), $oDefinitionFilter, $oAppContext);
  163. $iErrorsCount = $oErrorObjectSet->Count();
  164. while($oObj = $oErrorObjectSet->Fetch())
  165. {
  166. $aObjectsWithErrors[$oObj->GetKey()] = true;
  167. }
  168. $aRow['nb_errors'] = ($iErrorsCount == 0) ? '0' : "<a href=\"?operation=errors&category=".$oAuditCategory->GetKey()."&rule=".$oAuditRule->GetKey()."&".$oAppContext->GetForLink()."\">$iErrorsCount</a>";
  169. $aRow['percent_ok'] = sprintf('%.2f', 100.0 * (($iCount - $iErrorsCount) / $iCount));
  170. $aRow['class'] = GetReportColor($iCount, $iErrorsCount);
  171. }
  172. $aResults[] = $aRow;
  173. $iTotalErrors = count($aObjectsWithErrors);
  174. $sOverallPercentOk = ($iCount == 0) ? '100.00' : sprintf('%.2f', 100.0 * (($iCount - $iTotalErrors) / $iCount));
  175. $sClass = GetReportColor($iCount, $iTotalErrors);
  176. }
  177. $oP->add("<tr>\n");
  178. $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");
  179. $oP->add("</tr>\n");
  180. foreach($aResults as $aRow)
  181. {
  182. $oP->add("<tr>\n");
  183. $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");
  184. $oP->add("</tr>\n");
  185. }
  186. }
  187. $oP->add("</table>\n");
  188. $oP->add("</td></tr>\n");
  189. $oP->add("</table>\n");
  190. }
  191. $oP->output();
  192. }
  193. catch(CoreException $e)
  194. {
  195. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  196. $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
  197. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  198. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  199. $oP->output();
  200. if (MetaModel::IsLogEnabledIssue())
  201. {
  202. if (MetaModel::IsValidClass('EventIssue'))
  203. {
  204. $oLog = new EventIssue();
  205. $oLog->Set('message', $e->getMessage());
  206. $oLog->Set('userinfo', '');
  207. $oLog->Set('issue', $e->GetIssue());
  208. $oLog->Set('impact', 'Page could not be displayed');
  209. $oLog->Set('callstack', $e->getTrace());
  210. $oLog->Set('data', $e->getContextData());
  211. $oLog->DBInsertNoReload();
  212. }
  213. IssueLog::Error($e->getMessage());
  214. }
  215. // For debugging only
  216. //throw $e;
  217. }
  218. catch(Exception $e)
  219. {
  220. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  221. $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
  222. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  223. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  224. $oP->output();
  225. if (MetaModel::IsLogEnabledIssue())
  226. {
  227. if (MetaModel::IsValidClass('EventIssue'))
  228. {
  229. $oLog = new EventIssue();
  230. $oLog->Set('message', $e->getMessage());
  231. $oLog->Set('userinfo', '');
  232. $oLog->Set('issue', 'PHP Exception');
  233. $oLog->Set('impact', 'Page could not be displayed');
  234. $oLog->Set('callstack', $e->getTrace());
  235. $oLog->Set('data', array());
  236. $oLog->DBInsertNoReload();
  237. }
  238. IssueLog::Error($e->getMessage());
  239. }
  240. }
  241. ?>