public function getLastChild() { return dmArray::last($this->children); }
protected function getData() { if (!($data = $this->getCache('data'))) { $data = array('date' => array(), 'timer' => array()); $requestLogEntries = $this->serviceContainer->getService('request_log')->getEntries(10000, array('hydrate' => false, 'keys' => array('time', 'timer', 'code', 'mem'))); $logDelta = dmArray::get(dmArray::first($requestLogEntries), 'time') - dmArray::get(dmArray::last($requestLogEntries), 'time'); $hours = $logDelta / 3600; if ($hours < 1) { throw new dmException('Not enough log entries'); } $stepFactor = $hours / 40; $step = round(60 * 60 * $stepFactor); $stepDate = $_SERVER['REQUEST_TIME'] - $step; $tmpTimes = array(); $tmpMems = array(); $tmpErrs = 0; $data = array('date' => array(), 'time' => array(), 'nbReq' => array(), 'nbErr' => array(), 'mem' => array()); if (count($requestLogEntries) < 50) { throw new dmException('Not enough log entries'); } foreach ($requestLogEntries as $userLogEntry) { $date = $userLogEntry['time']; $timer = $userLogEntry['timer']; $mem = $userLogEntry['mem']; $err = in_array($userLogEntry['code'], array('500', '404')); if ($date > $stepDate) { $tmpTimes[] = $timer; $tmpMems[] = $mem; $tmpErrs += $err; } else { $nb = count($tmpTimes); $trustableData = $nb >= 10; $data['date'][] = $stepDate; $data['nbReq'][] = $trustableData ? $nb / $stepFactor : ''; $data['nbErr'][] = $trustableData ? $tmpErrs / $stepFactor : ''; $data['time'][] = $trustableData ? array_sum($tmpTimes) / $nb : ''; $data['mem'][] = $trustableData ? array_sum($tmpMems) / $nb : ''; $stepDate -= $step; $tmpTimes = array($timer); $tmpMems = array($mem); $tmpErrs = $err; } } $nb = count($tmpTimes); $trustableData = $nb >= 5; $data['date'][] = $stepDate; $data['nbReq'][] = $nb / $stepFactor; $data['nbErr'][] = $tmpErrs / $stepFactor; $data['time'][] = $trustableData ? array_sum($tmpTimes) / $nb : ""; $data['mem'][] = $trustableData ? array_sum($tmpMems) / $nb : ""; foreach (array_keys($data) as $key) { $data[$key] = array_reverse($data[$key]); } foreach ($data['mem'] as $index => $value) { $data['nbReq'][$index] = $data['nbReq'][$index] / 60; $data['nbErr'][$index] = $data['nbErr'][$index] / 60; if ("" != $data['mem'][$index]) { $data['mem'][$index] = $value / (1024 * 1024); } if ($data['time'][$index] > 5000) { $data['time'][$index] = 5000; } } $events = $this->serviceContainer->getService('event_log')->getEntries(1000, array('filter' => array($this, 'filterEvent'), 'hydrate' => false)); $data['events'] = array(); foreach ($this->eventsFilter as $eventType) { $data['events'][$eventType] = array(); for ($it = 0, $itMax = count($data['date']); $it < $itMax; $it++) { $data['events'][$eventType][] = 0; } } $time = time(); foreach ($events as $event) { $eventType = $event['action'] . ' ' . $event['type']; $timeDelta = $time; $nearestTimeIndex = null; foreach ($data['date'] as $index => $time) { $eventTimeDelta = abs($time - $event['time']); if ($eventTimeDelta < $timeDelta) { $nearestTimeIndex = $index; $timeDelta = $eventTimeDelta; } } if ($timeDelta < $data['date'][1] - $data['date'][0]) { ++$data['events'][$eventType][$nearestTimeIndex]; } } unset($events); $this->setCache('data', $data); } return $data; }
$t->is_deeply(dmArray::firsts(array(), 2), null, 'empty source array'); $t->is_deeply(dmArray::firsts(array('alpha'), 2), array('alpha'), 'not enough values'); $array = array('alpha', 'beta', 'gamma', 'delta'); $t->is_deeply(dmArray::firsts($array, 2), array('alpha', 'beta'), 'first values'); $t->diag('get'); $t->is_deeply(dmArray::get('alpha', null, 'test'), 'test', 'first parameter is not an array'); $t->is_deeply(dmArray::get(array('alpha'), 0, 'test'), 'alpha', 'key exists'); $t->is_deeply(dmArray::get(array('alpha'), 1, 'test'), 'test', 'key not exists'); $t->is_deeply(dmArray::get(array(''), 0, 'test', false), '', 'empty value without default if empty'); $t->is_deeply(dmArray::get(array(''), 0, 'test', true), 'test', 'empty value with default if empty'); $t->is_deeply(dmArray::get(array('alpha'), 0, 'test', true), 'alpha', 'key exists with default if empty'); $t->diag('last'); $t->is_deeply(dmArray::last('alpha'), 'alpha', 'first parameter is not an array'); $t->is_deeply(dmArray::last(array()), null, 'empty source array'); $array = array('alpha', 'beta', 'gamma', 'delta'); $t->is_deeply(dmArray::last($array), 'delta', 'last value'); $t->diag('toHtmlCssClasses'); $classes = array('class1', '', ' class2', ' ', ' class3 '); $cleanClasses = 'class1 class2 class3'; $t->is(dmArray::toHtmlCssClasses($classes), $cleanClasses, 'clean classes : ' . $cleanClasses); $classes = array('class1'); $cleanClasses = 'class1'; $t->is(dmArray::toHtmlCssClasses($classes), $cleanClasses, 'clean classes : ' . $cleanClasses); $classes = array(' class1 class2 class3 '); $cleanClasses = 'class1 class2 class3'; $t->is(dmArray::toHtmlCssClasses($classes), $cleanClasses, 'clean classes : ' . $cleanClasses); $t->diag('unsetEmpty'); $array = array(1 => '', 2 => 'delta', 3 => '', 'alpha' => 'gamma', 'gamma' => '', 'delta' => ''); $t->is_deeply(dmArray::unsetEmpty($array, array(3, 'gamma')), array('1' => '', '2' => 'delta', 'alpha' => 'gamma', 'delta' => ''), 'check'); $t->diag('valueToKey'); $array = array('alpha', 'beta', 'gamma', 'delta');