Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.45 KB | None | 0 0
  1. <?php
  2.  
  3. // We have an array of incident domain objects
  4. $downPeriods = array();
  5. foreach ($incidents as $incident) {
  6.     $start = strtotime($incident->getCreatedTimestamp());
  7.     $end = strtotime($incident->getStableTimestamp());  // that doesn't exist but imagine it
  8.  
  9.     $found = false;
  10.     foreach ($downPeriods as $startDown => $endDown) {
  11.         if ($startDown > $start) { // new down period previous to the rest
  12.             $downPeriods[$start] = $end;
  13.             $found = true;
  14.             break;
  15.         }
  16.  
  17.         if ($endDown > $start) { // belongs to this period
  18.             if ($end > $endDown) {
  19.                 $endDown = $end;
  20.             }
  21.             $downPeriods[$startDown] = $endDown;
  22.             $found = true;
  23.             break;
  24.         }
  25.     }
  26.  
  27.     if (!$found) {
  28.         $downPeriods[$start] = $end;
  29.     }
  30.  
  31.     ksort($downPeriods);
  32. }
  33.  
  34. $mtbf = getMtbf($startPeriod, $endPeriod, $downPeriods);
  35.  
  36. function getMtbf($startPeriod, $endPeriod, array $downPeriods) {
  37.     $totalTimeDown = 0;
  38.  
  39.     $previousEnd = null;
  40.     foreach ($downPeriods as $start => $end) {
  41.         if ($previousEnd === null) { // first up period
  42.             $totalTimeDown += $start - strtotime($startPeriod);
  43.         } else {
  44.             $totalTimeDown += $start - $previousEnd;
  45.         }
  46.         $previousEnd = $end;
  47.     }
  48.     $totalTimeDown += strtotime($endPeriod) - $previousEnd;
  49.  
  50.     return $totalTimeDown / (count($downPeriods) + 2);
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement