Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter by methods #265

Merged
merged 10 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/config.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
'db.host' => getenv('XHGUI_MONGO_HOST') ?: 'mongodb://127.0.0.1:27017',
'db.db' => getenv('XHGUI_MONGO_DATABASE') ?: 'xhprof',
'db.options' => array(),
'run.view.filter.names' => array(
'Zend*',
'Composer*',
),

// Whether to instrument a user request.
//
Expand Down
27 changes: 26 additions & 1 deletion src/Xhgui/Controller/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class Xhgui_Controller_Run extends Xhgui_Controller
{
/**
* HTTP GET attribute name for comma separated filters
*/
const FILTER_ARGUMENT_NAME = 'filter';

/**
* @var Xhgui_Profiles
*/
Expand Down Expand Up @@ -95,7 +100,11 @@ public function view()
}
}

$profile = $result->sort('ewt', $result->getProfile());
if (false !== $request->get(self::FILTER_ARGUMENT_NAME, false)) {
$profile = $result->sort('ewt', $result->filter($result->getProfile(), $this->getFilters()));
} else {
$profile = $result->sort('ewt', $result->getProfile());
}

$this->_template = 'runs/view.twig';
$this->set(array(
Expand All @@ -107,6 +116,22 @@ public function view()
'date_format' => $this->app->config('date.format'),
));
}

/**
* @return array
*/
protected function getFilters()
{
$request = $this->app->request();
$filterString = $request->get(self::FILTER_ARGUMENT_NAME);
if (strlen($filterString)) {
$filters = array_map('trim', explode(',', $filterString));
} else {
$filters = $this->app->config('run.view.filter.names');
}

return $filters;
}

public function deleteForm()
{
Expand Down
43 changes: 43 additions & 0 deletions src/Xhgui/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,49 @@ public function sort($dimension, $data)
uasort($data, $sorter);
return $data;
}

/**
* @param array $profileData
* @param array $filters
*
* @return array
*/
public function filter($profileData, $filters = [])
{
$filter = function ($item, $key) use ($filters) {
$nameToFilter = $this->getNameToFilter($key);
return !in_array($nameToFilter, $filters);
};
Mte90 marked this conversation as resolved.
Show resolved Hide resolved

$profileData = array_filter($profileData, $filter, ARRAY_FILTER_USE_BOTH);
foreach ($filters as $key => $item) {
foreach ($profileData as $key_item => $method) {
if (fnmatch($item, $key_item)) {
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
unset($profileData[ $key_item ]);
}
}
}
return $profileData;
}

/**
* @param $name
*
* @return bool|string
*/
protected function getNameToFilter($name)
{
$length = strpos($name, '\\', 1);
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
if (!$length) {
$length = strpos($name, '_', 1);
}
if (!$length) {
$length = strlen($name);
}
$nameToFilter = substr($name, 0, $length);

return $nameToFilter;
}

/**
* Split a key name into the parent==>child format.
Expand Down