Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // We have an array of incident domain objects
- $downPeriods = array();
- foreach ($incidents as $incident) {
- $start = strtotime($incident->getCreatedTimestamp());
- $end = strtotime($incident->getStableTimestamp()); // that doesn't exist but imagine it
- $found = false;
- foreach ($downPeriods as $startDown => $endDown) {
- if ($startDown > $start) { // new down period previous to the rest
- $downPeriods[$start] = $end;
- $found = true;
- break;
- }
- if ($endDown > $start) { // belongs to this period
- if ($end > $endDown) {
- $endDown = $end;
- }
- $downPeriods[$startDown] = $endDown;
- $found = true;
- break;
- }
- }
- if (!$found) {
- $downPeriods[$start] = $end;
- }
- ksort($downPeriods);
- }
- $mtbf = getMtbf($startPeriod, $endPeriod, $downPeriods);
- function getMtbf($startPeriod, $endPeriod, array $downPeriods) {
- $totalTimeDown = 0;
- $previousEnd = null;
- foreach ($downPeriods as $start => $end) {
- if ($previousEnd === null) { // first up period
- $totalTimeDown += $start - strtotime($startPeriod);
- } else {
- $totalTimeDown += $start - $previousEnd;
- }
- $previousEnd = $end;
- }
- $totalTimeDown += strtotime($endPeriod) - $previousEnd;
- return $totalTimeDown / (count($downPeriods) + 2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement