Skip to content

Commit

Permalink
Merge pull request #230 from comandi/add-profile-import-over-network
Browse files Browse the repository at this point in the history
Allow a Run to be imported over the network
  • Loading branch information
markstory committed Mar 11, 2018
2 parents b5f8012 + 9f99698 commit c564cb3
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 75 deletions.
9 changes: 8 additions & 1 deletion src/Xhgui/Controller.php
@@ -1,10 +1,17 @@
<?php

use Slim\Slim;

class Xhgui_Controller
{
protected $_templateVars = array();
protected $_template = null;

/**
* @var Slim
*/
protected $app;

public function set($vars)
{
$this->_templateVars = array_merge($this->_templateVars, $vars);
Expand All @@ -17,7 +24,7 @@ public function templateVars()

public function render()
{
$this->_app->render($this->_template, $this->_templateVars);
$this->app->render($this->_template, $this->_templateVars);
}

}
28 changes: 16 additions & 12 deletions src/Xhgui/Controller/Custom.php
@@ -1,14 +1,18 @@
<?php

use Slim\Slim;

class Xhgui_Controller_Custom extends Xhgui_Controller
{
protected $_app;
protected $_profiles;
/**
* @var Xhgui_Profiles
*/
protected $profiles;

public function __construct($app, $profiles)
public function __construct(Slim $app, Xhgui_Profiles $profiles)
{
$this->_app = $app;
$this->_profiles = $profiles;
$this->app = $app;
$this->profiles = $profiles;
}

public function get()
Expand All @@ -18,11 +22,11 @@ public function get()

public function help()
{
$request = $this->_app->request();
$request = $this->app->request();
if ($request->get('id')) {
$res = $this->_profiles->get($request->get('id'));
$res = $this->profiles->get($request->get('id'));
} else {
$res = $this->_profiles->latest();
$res = $this->profiles->latest();
}
$this->_template = 'custom/help.twig';
$this->set(array(
Expand All @@ -32,8 +36,8 @@ public function help()

public function query()
{
$request = $this->_app->request();
$response = $this->_app->response();
$request = $this->app->request();
$response = $this->app->response();
$response['Content-Type'] = 'application/json';

$query = json_decode($request->post('query'), true);
Expand All @@ -52,9 +56,9 @@ public function query()
return $response->body($json);
}

$perPage = $this->_app->config('page.limit');
$perPage = $this->app->config('page.limit');

$res = $this->_profiles->query($query, $retrieve)
$res = $this->profiles->query($query, $retrieve)
->limit($perPage);
$r = iterator_to_array($res);
return $response->body(json_encode($r));
Expand Down
111 changes: 68 additions & 43 deletions src/Xhgui/Controller/Run.php
@@ -1,17 +1,29 @@
<?php

use Slim\Slim;

class Xhgui_Controller_Run extends Xhgui_Controller
{
public function __construct($app, $profiles, $watches)
/**
* @var Xhgui_Profiles
*/
private $profiles;

/**
* @var Xhgui_WatchFunctions
*/
private $watches;

public function __construct(Slim $app, Xhgui_Profiles $profiles, Xhgui_WatchFunctions $watches)
{
$this->_app = $app;
$this->_profiles = $profiles;
$this->_watches = $watches;
$this->app = $app;
$this->profiles = $profiles;
$this->watches = $watches;
}

public function index()
{
$request = $this->_app->request();
$request = $this->app->request();

$search = array();
$keys = array('date_start', 'date_end', 'url');
Expand All @@ -22,11 +34,11 @@ public function index()
}
$sort = $request->get('sort');

$result = $this->_profiles->getAll(array(
$result = $this->profiles->getAll(array(
'sort' => $sort,
'page' => $request->get('page'),
'direction' => $request->get('direction'),
'perPage' => $this->_app->config('page.limit'),
'perPage' => $this->app->config('page.limit'),
'conditions' => $search,
'projection' => true,
));
Expand All @@ -53,7 +65,7 @@ public function index()
'paging' => $paging,
'base_url' => 'home',
'runs' => $result['results'],
'date_format' => $this->_app->config('date.format'),
'date_format' => $this->app->config('date.format'),
'search' => $search,
'has_search' => strlen(implode('', $search)) > 0,
'title' => $title
Expand All @@ -62,9 +74,9 @@ public function index()

public function view()
{
$request = $this->_app->request();
$detailCount = $this->_app->config('detail.count');
$result = $this->_profiles->get($request->get('id'));
$request = $this->app->request();
$detailCount = $this->app->config('detail.count');
$result = $this->profiles->get($request->get('id'));

$result->calculateSelf();

Expand All @@ -76,7 +88,7 @@ public function view()

// Watched Functions Block
$watchedFunctions = array();
foreach ($this->_watches->getAll() as $watch) {
foreach ($this->watches->getAll() as $watch) {
$matches = $result->getWatched($watch['name']);
if ($matches) {
$watchedFunctions = array_merge($watchedFunctions, $matches);
Expand All @@ -92,18 +104,18 @@ public function view()
'wall_time' => $timeChart,
'memory' => $memoryChart,
'watches' => $watchedFunctions,
'date_format' => $this->_app->config('date.format'),
'date_format' => $this->app->config('date.format'),
));
}

public function url()
{
$request = $this->_app->request();
$request = $this->app->request();
$pagination = array(
'sort' => $request->get('sort'),
'direction' => $request->get('direction'),
'page' => $request->get('page'),
'perPage' => $this->_app->config('page.limit'),
'perPage' => $this->app->config('page.limit'),
);

$search = array();
Expand All @@ -112,7 +124,7 @@ public function url()
$search[$key] = $request->get($key);
}

$runs = $this->_profiles->getForUrl(
$runs = $this->profiles->getForUrl(
$request->get('url'),
$pagination,
$search
Expand All @@ -122,7 +134,7 @@ public function url()
$search['limit'] = $search['limit_custom'];
}

$chartData = $this->_profiles->getPercentileForUrl(
$chartData = $this->profiles->getPercentileForUrl(
90,
$request->get('url'),
$search
Expand All @@ -142,30 +154,30 @@ public function url()
'runs' => $runs['results'],
'url' => $request->get('url'),
'chart_data' => $chartData,
'date_format' => $this->_app->config('date.format'),
'date_format' => $this->app->config('date.format'),
'search' => array_merge($search, array('url' => $request->get('url'))),
));
}

public function compare()
{
$request = $this->_app->request();
$request = $this->app->request();

$baseRun = $headRun = $candidates = $comparison = null;
$paging = array();

if ($request->get('base')) {
$baseRun = $this->_profiles->get($request->get('base'));
$baseRun = $this->profiles->get($request->get('base'));
}

if ($baseRun && !$request->get('head')) {
$pagination = array(
'direction' => $request->get('direction'),
'sort' => $request->get('sort'),
'page' => $request->get('page'),
'perPage' => $this->_app->config('page.limit'),
'perPage' => $this->app->config('page.limit'),
);
$candidates = $this->_profiles->getForUrl(
$candidates = $this->profiles->getForUrl(
$baseRun->getMeta('simple_url'),
$pagination
);
Expand All @@ -179,7 +191,7 @@ public function compare()
}

if ($request->get('head')) {
$headRun = $this->_profiles->get($request->get('head'));
$headRun = $this->profiles->get($request->get('head'));
}

if ($baseRun && $headRun) {
Expand All @@ -193,7 +205,7 @@ public function compare()
'head_run' => $headRun,
'candidates' => $candidates,
'url_params' => $request->get(),
'date_format' => $this->_app->config('date.format'),
'date_format' => $this->app->config('date.format'),
'comparison' => $comparison,
'paging' => $paging,
'search' => array(
Expand All @@ -205,11 +217,11 @@ public function compare()

public function symbol()
{
$request = $this->_app->request();
$request = $this->app->request();
$id = $request->get('id');
$symbol = $request->get('symbol');

$profile = $this->_profiles->get($id);
$profile = $this->profiles->get($id);
$profile->calculateSelf();
list($parents, $current, $children) = $profile->getRelatives($symbol);

Expand All @@ -226,13 +238,13 @@ public function symbol()

public function symbolShort()
{
$request = $this->_app->request();
$request = $this->app->request();
$id = $request->get('id');
$threshold = $request->get('threshold');
$symbol = $request->get('symbol');
$metric = $request->get('metric');

$profile = $this->_profiles->get($id);
$profile = $this->profiles->get($id);
$profile->calculateSelf();
list($parents, $current, $children) = $profile->getRelatives($symbol, $metric, $threshold);

Expand All @@ -249,21 +261,21 @@ public function symbolShort()

public function callgraph()
{
$request = $this->_app->request();
$profile = $this->_profiles->get($request->get('id'));
$request = $this->app->request();
$profile = $this->profiles->get($request->get('id'));

$this->_template = 'runs/callgraph.twig';
$this->set(array(
'profile' => $profile,
'date_format' => $this->_app->config('date.format'),
'date_format' => $this->app->config('date.format'),
));
}

public function callgraphData()
{
$request = $this->_app->request();
$response = $this->_app->response();
$profile = $this->_profiles->get($request->get('id'));
$request = $this->app->request();
$response = $this->app->response();
$profile = $this->profiles->get($request->get('id'));
$metric = $request->get('metric') ?: 'wt';
$threshold = (float)$request->get('threshold') ?: 0.01;
$callgraph = $profile->getCallgraph($metric, $threshold);
Expand All @@ -274,21 +286,21 @@ public function callgraphData()

public function flamegraph()
{
$request = $this->_app->request();
$profile = $this->_profiles->get($request->get('id'));
$request = $this->app->request();
$profile = $this->profiles->get($request->get('id'));

$this->_template = 'runs/flamegraph.twig';
$this->set(array(
'profile' => $profile,
'date_format' => $this->_app->config('date.format'),
'date_format' => $this->app->config('date.format'),
));
}

public function flamegraphData()
{
$request = $this->_app->request();
$response = $this->_app->response();
$profile = $this->_profiles->get($request->get('id'));
$request = $this->app->request();
$response = $this->app->response();
$profile = $this->profiles->get($request->get('id'));
$metric = $request->get('metric') ?: 'wt';
$threshold = (float)$request->get('threshold') ?: 0.01;
$flamegraph = $profile->getFlamegraph($metric, $threshold);
Expand All @@ -299,14 +311,27 @@ public function flamegraphData()

public function callgraphDataDot()
{
$request = $this->_app->request();
$response = $this->_app->response();
$profile = $this->_profiles->get($request->get('id'));
$request = $this->app->request();
$response = $this->app->response();
$profile = $this->profiles->get($request->get('id'));
$metric = $request->get('metric') ?: 'wt';
$threshold = (float)$request->get('threshold') ?: 0.01;
$callgraph = $profile->getCallgraphNodes($metric, $threshold);

$response['Content-Type'] = 'application/json';
return $response->body(json_encode($callgraph));
}

public function import()
{
$request = $this->app->request();

$data = json_decode($request->getBody(), true);

$container = Xhgui_ServiceContainer::instance();
/* @var Xhgui_Saver_Mongo $saver */
$saver = $container['saverMongo'];

$saver->save($data);
}
}

0 comments on commit c564cb3

Please sign in to comment.