audit.php 10 KB

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