audit.php 11 KB

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