Skip to content
This repository has been archived by the owner on Sep 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #11 from 1ma/backend-decoupling
Browse files Browse the repository at this point in the history
Backend decoupling
  • Loading branch information
glensc authored Apr 24, 2019
2 parents 0d0e010 + d0e9d1e commit 1388249
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/composer.lock
/composer.phar
/tags
/vendor
config/config.php

# phpstorm files
.idea/
.idea/
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
}
},
"require": {
"php": ">=5.3.0"
"php": ">=5.3.0",
"paragonie/random_compat": ">=2.0"
},
"suggest": {
"ext-xhprof": "You need to install either xhprof or uprofiler to use XHGui.",
"ext-uprofiler": "You need to install either xhprof or uprofiler to use XHGui.",
"ext-mongo": "Mongo is needed to store profiler results for PHP < 7.",
"ext-mongodb": "Mongo is needed to store profiler results for PHP > 7.",
"ext-curl": "You need to install the curl extension to upload saved files",
"ext-pdo": "You need to install the pdo extension to store profiler results to a relational database",
"alcaeus/mongo-php-adapter": "Mongo PHP Adapter is required for PHP >7 (when using ext-mongodb)"
}
}
7 changes: 7 additions & 0 deletions config/config.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
'db.host' => sprintf('mongodb://%s', $mongoUri),
'db.db' => $mongoDb,

'pdo' => array(
'dsn' => 'sqlite:/tmp/xhgui.sqlite3',
'user' => null,
'pass' => null,
'table' => 'results'
),

// Allows you to pass additional options like replicaSet to MongoClient.
// 'username', 'password' and 'db' (where the user is added)
'db.options' => array(),
Expand Down
9 changes: 2 additions & 7 deletions external/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,8 @@ function () {
$requestTimeFloat[1] = 0;
}

if (Xhgui_Config::read('save.handler') === 'mongodb') {
$requestTs = new MongoDate($time);
$requestTsMicro = new MongoDate($requestTimeFloat[0], $requestTimeFloat[1]);
} else {
$requestTs = array('sec' => $time, 'usec' => 0);
$requestTsMicro = array('sec' => $requestTimeFloat[0], 'usec' => $requestTimeFloat[1]);
}
$requestTs = array('sec' => $time, 'usec' => 0);
$requestTsMicro = array('sec' => $requestTimeFloat[0], 'usec' => $requestTimeFloat[1]);

$data['meta'] = array(
'url' => $uri,
Expand Down
13 changes: 11 additions & 2 deletions src/Xhgui/Saver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ class Xhgui_Saver
* Get a saver instance based on configuration data.
*
* @param array $config The configuration data.
* @return Xhgui_Saver_File|Xhgui_Saver_Mongo|Xhgui_Saver_Upload
* @return Xhgui_Saver_Interface
*/
public static function factory($config)
{
switch ($config['save.handler']) {

case 'file':
return new Xhgui_Saver_File($config['save.handler.filename']);

Expand All @@ -30,6 +29,16 @@ public static function factory($config)
$timeout
);

case 'pdo':
return new Xhgui_Saver_Pdo(
new PDO(
$config['pdo']['dsn'],
$config['pdo']['user'],
$config['pdo']['pass']
),
$config['pdo']['table']
);

case 'mongodb':
default:
$mongo = new MongoClient($config['db.host'], $config['db.options']);
Expand Down
16 changes: 15 additions & 1 deletion src/Xhgui/Saver/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@ public function __construct(MongoCollection $collection)

public function save(array $data)
{
$data['_id'] = self::getLastProfilingId();
if (!isset($data['_id'])) {
$data['_id'] = self::getLastProfilingId();
}

if (isset($data['meta']['request_ts'])) {
$data['meta']['request_ts'] = new MongoDate($data['meta']['request_ts']['sec']);
}

if (isset($data['meta']['request_ts_micro'])) {
$data['meta']['request_ts_micro'] = new MongoDate(
$data['meta']['request_ts_micro']['sec'],
$data['meta']['request_ts_micro']['usec']
);
}


return $this->_collection->insert($data, array('w' => 0));
}
Expand Down
105 changes: 105 additions & 0 deletions src/Xhgui/Saver/Pdo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

class Xhgui_Saver_Pdo implements Xhgui_Saver_Interface
{
const TABLE_DDL = <<<SQL
CREATE TABLE IF NOT EXISTS "%s" (
id TEXT PRIMARY KEY,
profile TEXT NOT NULL,
url TEXT NULL,
SERVER TEXT NULL,
GET TEXT NULL,
ENV TEXT NULL,
simple_url TEXT NULL,
request_ts INTEGER NOT NULL,
request_ts_micro NUMERIC(15, 4) NOT NULL,
request_date DATE NOT NULL,
main_wt INTEGER NOT NULL,
main_ct INTEGER NOT NULL,
main_cpu INTEGER NOT NULL,
main_mu INTEGER NOT NULL,
main_pmu INTEGER NOT NULL
);
SQL;

const INSERT_DML = <<<SQL
INSERT INTO "%s" (
id,
profile,
url,
SERVER,
GET,
ENV,
simple_url,
request_ts,
request_ts_micro,
request_date,
main_wt,
main_ct,
main_cpu,
main_mu,
main_pmu
) VALUES (
:id,
:profile,
:url,
:SERVER,
:GET,
:ENV,
:simple_url,
:request_ts,
:request_ts_micro,
:request_date,
:main_wt,
:main_ct,
:main_cpu,
:main_mu,
:main_pmu
);
SQL;

/**
* @var PDOStatement
*/
private $stmt;

/**
* @param PDO $pdo
* @param string $table
*/
public function __construct(PDO $pdo, $table)
{
$pdo->exec(sprintf(self::TABLE_DDL, $table));

$this->stmt = $pdo->prepare(sprintf(self::INSERT_DML, $table));
}

public function save(array $data)
{
$main = $data['profile']['main()'];

$this->stmt->execute(array(
'id' => Xhgui_Util::generateId(),
'profile' => json_encode($data['profile']),
'url' => $data['meta']['url'],
'SERVER' => json_encode($data['meta']['SERVER']),
'GET' => json_encode($data['meta']['get']),
'ENV' => json_encode($data['meta']['env']),
'simple_url' => $data['meta']['simple_url'],
'request_ts' => $data['meta']['request_ts']['sec'],
'request_ts_micro' => "{$data['meta']['request_ts_micro']['sec']}.{$data['meta']['request_ts_micro']['usec']}",
'request_date' => $data['meta']['request_date'],
'main_wt' => $main['wt'],
'main_ct' => $main['ct'],
'main_cpu' => $main['cpu'],
'main_mu' => $main['mu'],
'main_pmu' => $main['pmu'],
));

$this->stmt->closeCursor();
}
}
14 changes: 14 additions & 0 deletions src/Xhgui/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,18 @@ public static function simpleUrl($url)
return preg_replace('/\=\d+/', '', $url);
}

/**
* Returns an new ObjectId-like string, where its first 8
* characters encode the current unix timestamp and the
* next 16 are random.
*
* @see http://php.net/manual/en/mongodb-bson-objectid.construct.php
*
* @return string
*/
public static function generateId()
{
return dechex(time()) . bin2hex(random_bytes(8));
}

}

0 comments on commit 1388249

Please sign in to comment.