1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-05 06:07:32 +02:00

Traffic counters tweak.

This commit is contained in:
Cameron
2019-02-10 14:51:51 -08:00
parent 535fef8799
commit 42147e2227
2 changed files with 669 additions and 622 deletions

View File

@@ -17,9 +17,15 @@
+----------------------------------------------------------------------------+
*/
if (!defined('e107_INIT')) { exit; }
if(!defined('e107_INIT'))
{
exit;
}
class e107_db_debug
{
class e107_db_debug {
var $aSQLdetails = array(); // DB query analysis (in pieces for further analysis)
var $aDBbyTable = array();
var $aOBMarks = array(0 => ''); // Track output buffer level at each time mark
@@ -39,10 +45,11 @@ class e107_db_debug {
{
}
function e107_db_debug() {
function e107_db_debug()
{
global $eTimingStart;
$this->aTimeMarks[0] = array(
@@ -65,6 +72,7 @@ class e107_db_debug {
//
function Show_All()
{
$this->ShowIf('Debug Log', $this->Show_Log());
$this->ShowIf('Traffic Counters', e107::getSingleton('e107_traffic')->Display());
$this->ShowIf('Time Analysis', $this->Show_Performance());
@@ -93,11 +101,13 @@ class e107_db_debug {
}
}
function Mark_Time($sMarker) { // Should move to traffic_class?
function Mark_Time($sMarker)
{ // Should move to traffic_class?
$timeNow = microtime();
$nMarks = ++$this->nTimeMarks;
if (!strlen($sMarker)) {
if(!strlen($sMarker))
{
$sMarker = "Mark not set";
}
@@ -135,14 +145,16 @@ class e107_db_debug {
*/
function Mark_Query($query, $rli, $origQryRes, $aTrace, $mytime, $curtable)
{
// global $sql;
$sql = e107::getDb($rli);
// Explain the query, if possible...
list($qtype, $args) = explode(" ", ltrim($query), 2);
unset($args);
$nFields = 0;
$bExplained = FALSE;
$bExplained = false;
$ExplainText = '';
// Note the subtle bracket in the second comparison! Also, strcasecmp() returns zero on match
if(!strcasecmp($qtype, 'SELECT') || !strcasecmp($qtype, '(SELECT'))
@@ -156,7 +168,7 @@ class e107_db_debug {
{
//$nFields = mysql_num_fields($sQryRes);
$nFields = $sql->columnCount($sQryRes); // mysql_num_fields($sQryRes);
$bExplained = TRUE;
$bExplained = true;
}
}
else
@@ -179,13 +191,13 @@ class e107_db_debug {
if($bExplained)
{
$bRowHeaders=FALSE;
$bRowHeaders = false;
// while ($row = @mysql_fetch_assoc($sQryRes))
while($row = $sql->fetch())
{
if(!$bRowHeaders)
{
$bRowHeaders=TRUE;
$bRowHeaders = true;
$t['explain'] = "<tr><td class='forumheader3'><b>" . implode("</b></td><td class='forumheader3'><b>", array_keys($row)) . "</b></td></tr>\n";
}
$t['explain'] .= "<tr><td class='forumheader3'>" . implode("&nbsp;</td><td class='forumheader3'>", array_values($row)) . "&nbsp;</td></tr>\n";
@@ -215,8 +227,9 @@ class e107_db_debug {
}
function Show_SQL_Details($force = false)
{
function Show_SQL_Details($force=false) {
global $sql;
//
// Show stats from aSQLdetails array
@@ -230,7 +243,10 @@ class e107_db_debug {
$text = '';
$nQueries = $sql->db_QueryCount();
if (!$nQueries) return $text;
if(!$nQueries)
{
return $text;
}
//
// ALWAYS summarize query errors
@@ -250,12 +266,14 @@ class e107_db_debug {
}
}
if ($badCount) {
if($badCount)
{
$text .= "\n<table class='fborder table table-striped table-bordered'>\n";
$text .= "<tr><td class='fcaption' colspan='2'><b>$badCount Query Errors!</b></td></tr>\n";
$text .= "<tr><td class='fcaption'><b>Index</b></td><td class='fcaption'><b>Query / Error</b></td></tr>\n";
foreach ($this->aSQLdetails as $idx => $cQuery) {
foreach($this->aSQLdetails as $idx => $cQuery)
{
if(!$cQuery['ok'])
{
$text .= "<tr><td class='forumheader3' rowspan='2' style='text-align:right'>{$idx}&nbsp;</td>
@@ -298,7 +316,6 @@ class e107_db_debug {
}
$text .= "\n</table><br />\n";
}
@@ -313,10 +330,12 @@ class e107_db_debug {
{
$text .= "\n<table class='fborder table table-striped table-bordered' style='width: 100%;'>\n";
$text .= "<tr><td class='forumheader3' colspan='" . $cQuery['nFields'] . "'><b>" . $idx . ") Query:</b> [" . $cQuery['marker'] . " - " . $cQuery['caller'] . "]<br />" . $cQuery['query'] . "</td></tr>\n";
if (isset($cQuery['explain'])) {
if(isset($cQuery['explain']))
{
$text .= $cQuery['explain'];
}
if (strlen($cQuery['error'])) {
if(strlen($cQuery['error']))
{
$text .= "<tr><td class='forumheader3' ><b>Error in query:</b></td></tr>\n<tr><td class='forumheader3'>" . $cQuery['error'] . "</td></tr>\n";
}
@@ -340,6 +359,7 @@ class e107_db_debug {
function countLabel($amount)
{
if($amount < 30)
{
$inc = 'label-success';
@@ -359,6 +379,7 @@ class e107_db_debug {
function save($log)
{
e107::getMessage()->addDebug("Saving a log");
$titles = array_keys($this->aTimeMarks[0]);
@@ -391,6 +412,7 @@ class e107_db_debug {
function Show_Performance()
{
//
// Stats by Time Marker
//
@@ -436,7 +458,6 @@ class e107_db_debug {
}
while(list($tKey, $tMarker) = each($this->aTimeMarks))
{
if(!$bRowHeaders)
@@ -465,7 +486,6 @@ class e107_db_debug {
}
// $tMem = ($tMarker['Memory'] - $aSum['Memory']);
$tMem = ($tMarker['Memory']);
@@ -628,7 +648,8 @@ class e107_db_debug {
return $text;
}
function logDeprecated(){
function logDeprecated()
{
$back_trace = debug_backtrace();
@@ -644,9 +665,10 @@ class e107_db_debug {
function logCode($type, $code, $parm, $details)
{
if(!E107_DBG_BBSC)
{
return FALSE;
return false;
}
$this->scbbcodes[$this->scbcount]['type'] = $type;
@@ -654,10 +676,13 @@ class e107_db_debug {
$this->scbbcodes[$this->scbcount]['parm'] = (string) $parm;
$this->scbbcodes[$this->scbcount]['details'] = $details;
$this->scbcount++;
return null;
}
function Show_SC_BB($force = false)
{
if(!E107_DBG_BBSC && ($force === false))
{
return false;
@@ -691,14 +716,16 @@ class e107_db_debug {
</tr>\n";
}
$text .= "</tbody></table>";
return $text;
}
function Show_PATH($force = false)
{
if(!E107_DBG_PATH && ($force === false))
{
return FALSE;
return false;
}
global $e107;
@@ -752,7 +779,7 @@ class e107_db_debug {
{
$text .= "
<tr>
<td class='forumheader3' colspan='2'><pre>".htmlspecialchars(print_r($e107,TRUE))."</pre></td>
<td class='forumheader3' colspan='2'><pre>" . htmlspecialchars(print_r($e107, true)) . "</pre></td>
</tr>";
}
@@ -776,7 +803,7 @@ class e107_db_debug {
<tr>
<td class='forumheader3' colspan='2'><pre>".htmlspecialchars(print_r($_SESSION,TRUE))."</pre></td>
<td class='forumheader3' colspan='2'><pre>" . htmlspecialchars(print_r($_SESSION, true)) . "</pre></td>
</tr>
</tbody>
@@ -788,6 +815,7 @@ class e107_db_debug {
function Show_DEPRECATED($force = false)
{
if(!E107_DBG_DEPRECATED && ($force === false))
{
return false;
@@ -814,6 +842,7 @@ class e107_db_debug {
</tr>\n";
}
$text .= "</tbody></table>";
return $text;
}
}
@@ -821,10 +850,12 @@ class e107_db_debug {
/**
* var_dump to debug log
*
* @param mixed $message
*/
function dump($message, $TraceLev = 1)
{
ob_start();
var_dump($message);
$content = ob_get_contents();
@@ -842,16 +873,19 @@ class e107_db_debug {
// $this->aLog[] = array ('Message' => $content, 'Function' => '', 'File' => '', 'Line' => '' );
}
//
// Simple debug-level 'console' log
// Record a "nice" debug message with
// $db_debug->log("message");
//
function log($message,$TraceLev=1)
/**
* @desc Simple debug-level 'console' log
* Record a "nice" debug message with
* $db_debug->log("message");
* @param string $message
* @param int $TraceLev
* @return bool true on success , false on error
*/
public function log($message, $TraceLev = 1)
{
if(is_array($message) || is_object($message))
{
$message = "<pre>" . print_r($message, true) . "</pre>";
@@ -871,7 +905,9 @@ class e107_db_debug {
'File' => varset($bt[$TraceLev]['file']),
'Line' => varset($bt[$TraceLev]['line'])
);
} else {
}
else
{
$this->aLog[] = array(
'Message' => $message,
'Function' => '',
@@ -879,13 +915,19 @@ class e107_db_debug {
'Line' => ''
);
}
return true;
}
/**
* @return bool|string
*/
function Show_Log()
{
if(empty($this->aLog))
{
return FALSE;
return false;
}
//
// Dump the debug log
@@ -893,7 +935,7 @@ class e107_db_debug {
$text = "\n<table class='fborder table table-striped'>\n";
$bRowHeaders=FALSE;
$bRowHeaders = false;
foreach($this->aLog as $curLog)
{
@@ -913,8 +955,11 @@ class e107_db_debug {
function Show_Includes($force = false)
{
if (!E107_DBG_INCLUDES && ($force === false)) return false;
if(!E107_DBG_INCLUDES && ($force === false))
{
return false;
}
$text = "<table class='fborder table table-striped'>\n";
@@ -922,15 +967,18 @@ class e107_db_debug {
implode("&nbsp;</td></tr>\n<tr><td class='forumheader3'>", $this->aIncList) .
"&nbsp;</td></tr>\n";
$text .= "</table>\n";
return $text;
}
}
//
// Helper functions (not part of the class)
//
function e107_debug_shutdown()
{
if(e_AJAX_REQUEST) // extra output will break json ajax returns ie. comments
{
return;
@@ -938,7 +986,10 @@ function e107_debug_shutdown()
global $error_handler, $e107_Clean_Exit, $In_e107_Footer, $ADMIN_DIRECTORY;
if (isset($e107_Clean_Exit)) return;
if(isset($e107_Clean_Exit))
{
return;
}
if(!isset($In_e107_Footer))
{
@@ -946,7 +997,8 @@ function e107_debug_shutdown()
{
$filewanted = realpath(dirname(__FILE__)) . '/../' . $ADMIN_DIRECTORY . 'footer.php';
require_once($filewanted);
} else if (defset('USER_AREA'))
}
elseif(defset('USER_AREA'))
{
$filewanted = realpath(dirname(__FILE__)) . '/../' . FOOTERF;
require_once($filewanted);
@@ -955,11 +1007,15 @@ function e107_debug_shutdown()
//
// Error while in the footer, or during startup, or during above processing
//
if (isset($e107_Clean_Exit)) return; // We've now sent a footer...
if(isset($e107_Clean_Exit))
{
return;
} // We've now sent a footer...
// echo isset($In_e107_Footer) ? "In footer" : "In startup".'<br />';
while (ob_get_level() > 0) {
while(ob_get_level() > 0)
{
ob_end_flush();
}
@@ -979,4 +1035,3 @@ function e107_debug_shutdown()
}
?>

View File

@@ -51,6 +51,8 @@ class e_db_pdo implements e_db
private $pdo = true; // using PDO or not.
private $pdoBind = false;
private $traffic;
/**
* Constructor - gets language options from the cookie or session
@@ -58,16 +60,16 @@ class e_db_pdo implements e_db
*/
public function __construct()
{
global $pref, $db_defaultPrefix;
/*
if((PHP_MAJOR_VERSION > 6) || !function_exists('mysql_connect') || (defined('e_PDO') && e_PDO === true))
{
$this->pdo = true;
}
*/e107::getSingleton('e107_traffic')->BumpWho('Create db object', 1);
*/
$this->traffic = e107::getSingleton('e107_traffic');
$this->traffic->BumpWho('Create db object', 1);
$this->mySQLPrefix = MPREFIX; // Set the default prefix - may be overridden
@@ -76,17 +78,7 @@ class e_db_pdo implements e_db
$this->mySQLport = intval($port);
}
/*$langid = (isset($pref['cookie_name'])) ? 'e107language_'.$pref['cookie_name'] : 'e107language_temp';
if (isset($pref['user_tracking']) && ($pref['user_tracking'] == 'session'))
{
if (!isset($_SESSION[$langid])) { return; }
$this->mySQLlanguage = $_SESSION[$langid];
}
else
{
if (!isset($_COOKIE[$langid])) { return; }
$this->mySQLlanguage = $_COOKIE[$langid];
}*/
// Detect is already done in language handler, use it if not too early
if(defined('e_LANGUAGE')) $this->mySQLlanguage = e107::getLanguage()->e_language;
}
@@ -196,7 +188,7 @@ class e_db_pdo implements e_db
{
global $db_ConnectionID, $db_defaultPrefix;
e107::getSingleton('e107_traffic')->BumpWho('db Connect', 1);
$this->traffic->BumpWho('db Connect', 1);
$this->mySQLserver = $mySQLserver;
$this->mySQLuser = $mySQLuser;
@@ -441,8 +433,8 @@ class e_db_pdo implements e_db
$e = microtime();
e107::getSingleton('e107_traffic')->Bump('db_Query', $b, $e);
$mytime = e107::getSingleton('e107_traffic')->TimeDelta($b,$e);
$this->traffic->Bump('db_Query', $b, $e);
$mytime = $this->traffic->TimeDelta($b,$e);
$db_time += $mytime;
$this->mySQLresult = $sQryRes;
@@ -1360,7 +1352,7 @@ class e_db_pdo implements e_db
/** @var PDOStatement $resource */
$resource = $this->mySQLresult;
$row = $resource->fetch($type);
e107::getSingleton('e107_traffic')->Bump('db_Fetch', $b);
$this->traffic->Bump('db_Fetch', $b);
if ($row)
{
$this->dbError('db_Fetch');
@@ -1453,7 +1445,7 @@ class e_db_pdo implements e_db
function close()
{
$this->provide_mySQLaccess();
e107::getSingleton('e107_traffic')->BumpWho('db Close', 1);
$this->traffic->BumpWho('db Close', 1);
$this->mySQLaccess = NULL; // correct way to do it when using shared links.
$this->dbError('dbClose');
}