main.itop-sla-computation.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. // Copyright (C) 2010-2014 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Module itop-sla-computation: implements an extensible mechanism
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. /**
  25. * Implements the public interface for utilities
  26. * related to the SLA computation
  27. */
  28. class SLAComputation implements iWorkingTimeComputer
  29. {
  30. protected static $m_oAddOn;
  31. /**
  32. * Generic "extensibility" method: select which extension is actually used
  33. * @param $sClassName string The name of the class (derived from SLAComputationAddOnAPI) to use
  34. * @return void
  35. */
  36. public static function SelectModule($sClassName)
  37. {
  38. if (!class_exists($sClassName))
  39. {
  40. throw new CoreException("Could not select this module, '$sClassName' in not a valid class name");
  41. return;
  42. }
  43. if (($sClassName != 'SLAComputationAddOnAPI') && !is_subclass_of($sClassName, 'SLAComputationAddOnAPI'))
  44. {
  45. throw new CoreException("Could not select this module, the class '$sClassName' is not derived from SLAComputationAddOnAPI (parent class:".get_parent_class($sClassName)." )");
  46. return;
  47. }
  48. self::$m_oAddOn = new $sClassName;
  49. self::$m_oAddOn->Init();
  50. }
  51. /**
  52. * Get the class of the extension actually used
  53. * @return string The name of the extension class used
  54. */
  55. public static function GetModuleInstance()
  56. {
  57. return self::$m_oAddOn;
  58. }
  59. public static function GetDescription()
  60. {
  61. return "SLA computation (depends on the installed module)";
  62. }
  63. /**
  64. * Get the date/time corresponding to a given delay in the future from the present
  65. * considering only the valid (open) hours for a specified object
  66. * @param $oObject DBObject The object for which to compute the deadline
  67. * @param $iDuration integer The duration (in seconds) in the future
  68. * @param $oStartDate DateTime The starting point for the computation
  69. * @return DateTime The date/time for the deadline
  70. */
  71. public function GetDeadline($oObject, $iDuration, DateTime $oStartDate)
  72. {
  73. if (class_exists('WorkingTimeRecorder'))
  74. {
  75. WorkingTimeRecorder::Trace(WorkingTimeRecorder::TRACE_DEBUG, __class__.'::'.__function__);
  76. }
  77. $oEndDate = self::$m_oAddOn->GetDeadline($oObject, $iDuration, $oStartDate);
  78. if (class_exists('WorkingTimeRecorder'))
  79. {
  80. WorkingTimeRecorder::SetValues($oStartDate->format('U'), $oEndDate->format('U'), $iDuration, WorkingTimeRecorder::COMPUTED_END);
  81. }
  82. return $oEndDate;
  83. }
  84. /**
  85. * Get duration (considering only open hours) elapsed bewteen two given DateTimes
  86. * @param $oObject DBObject The object for which to compute the duration
  87. * @param $oStartDate DateTime The starting point for the computation (default = now)
  88. * @param $oEndDate DateTime The ending point for the computation (default = now)
  89. * @return integer The duration (number of seconds) of open hours elapsed between the two dates
  90. */
  91. public function GetOpenDuration($oObject, DateTime $oStartDate, DateTime $oEndDate)
  92. {
  93. if (class_exists('WorkingTimeRecorder'))
  94. {
  95. WorkingTimeRecorder::Trace(WorkingTimeRecorder::TRACE_DEBUG, __class__.'::'.__function__);
  96. }
  97. $iDuration = self::$m_oAddOn->GetOpenDuration($oObject, $oStartDate, $oEndDate);
  98. if (class_exists('WorkingTimeRecorder'))
  99. {
  100. WorkingTimeRecorder::SetValues($oStartDate->format('U'), $oEndDate->format('U'), $iDuration, WorkingTimeRecorder::COMPUTED_DURATION);
  101. }
  102. return $iDuration;
  103. }
  104. }
  105. /**
  106. * Base class for extensions to the SLA computation mechanism
  107. * This class implements a default behavior, suitable for a simple
  108. * 24x7 (no holiday) computation. To override this behavior, implement
  109. * a derived class from this one, overloading the behavior, and call
  110. * SLAComputation::SetExtension()
  111. */
  112. class SLAComputationAddOnAPI
  113. {
  114. /**
  115. * Called when the module is loaded, used for one time initialization (if needed)
  116. */
  117. public function Init()
  118. {
  119. }
  120. /**
  121. * Get the date/time corresponding to a given delay in the future from the present
  122. * considering only the valid (open) hours for a specified ticket
  123. * @param $oTicket Ticket The ticket for which to compute the deadline
  124. * @param $iDuration integer The duration (in seconds) in the future
  125. * @param $oStartDate DateTime The starting point for the computation
  126. * @return DateTime The date/time for the deadline
  127. */
  128. public static function GetDeadline($oTicket, $iDuration, DateTime $oStartDate)
  129. {
  130. if (class_exists('WorkingTimeRecorder'))
  131. {
  132. WorkingTimeRecorder::Trace(WorkingTimeRecorder::TRACE_DEBUG, __class__.'::'.__function__);
  133. }
  134. // Default implementation: 24x7, no holidays: to compute the deadline, just add
  135. // the specified duration to the given date/time
  136. $oResult = clone $oStartDate;
  137. $oResult->modify('+'.$iDuration.' seconds');
  138. return $oResult;
  139. }
  140. /**
  141. * Get duration (considering only open hours) elapsed bewteen two given DateTimes
  142. * @param $oTicket Ticket The ticket for which to compute the duration
  143. * @param $oStartDate DateTime The starting point for the computation (default = now)
  144. * @param $oEndDate DateTime The ending point for the computation (default = now)
  145. * @return integer The duration (number of seconds) of open hours elapsed between the two dates
  146. */
  147. public static function GetOpenDuration($oTicket, DateTime $oStartDate, DateTime $oEndDate)
  148. {
  149. if (class_exists('WorkingTimeRecorder'))
  150. {
  151. WorkingTimeRecorder::Trace(WorkingTimeRecorder::TRACE_DEBUG, __class__.'::'.__function__);
  152. }
  153. return abs($oEndDate->format('U') - $oStartDate->format('U'));
  154. }
  155. }
  156. SLAComputation::SelectModule('SLAComputationAddOnAPI');
  157. ?>