audit.php 11 KB

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