Further enhancements of the consolidate functions in logstreamdb

This commit is contained in:
Andre Lorbach 2009-11-06 12:36:36 +01:00
parent b3d76c7de0
commit 7a494d2e92
5 changed files with 109 additions and 49 deletions

View File

@ -207,7 +207,7 @@ abstract class LogStream {
*
* @return integer Error stat
*/
public abstract function ConsolidateDataByField($szFieldId, $nRecordLimit);
public abstract function ConsolidateDataByField($szConsFieldId, $nRecordLimit, $szSortFieldId, $nSortingOrder, $bIncludeLogStreamFields = false);
/**

View File

@ -696,67 +696,124 @@ class LogStreamDB extends LogStream {
*
* @return integer Error stat
*/
public function ConsolidateDataByField($szFieldId, $nRecordLimit)
public function ConsolidateDataByField($szConsFieldId, $nRecordLimit, $szSortFieldId, $nSortingOrder, $aIncludeCustomFields = null, $bIncludeLogStreamFields = false)
{
global $content, $dbmapping, $fields;
// Copy helper variables, this is just for better readability
$szTableType = $this->_logStreamConfigObj->DBTableType;
// Check if fields are available
if ( !isset($dbmapping[$szTableType]['DBMAPPINGS'][$szConsFieldId]) || !isset($dbmapping[$szTableType]['DBMAPPINGS'][$szSortFieldId]) )
return ERROR_DB_DBFIELDNOTFOUND;
if ( isset($dbmapping[$szTableType]['DBMAPPINGS'][$szFieldId]) )
// --- Set Options
$nConsFieldType = $fields[$szConsFieldId]['FieldType'];
if ( $nSortingOrder == SORTING_ORDER_DESC )
$szSortingOrder = "DESC";
else
$szSortingOrder = "ASC";
// ---
// --- Set DB Field names
$myDBConsFieldName = $dbmapping[$szTableType]['DBMAPPINGS'][$szConsFieldId];
$myDBGroupByFieldName = $myDBConsFieldName;
// Check which fields to include
if ( $aIncludeCustomFields != null )
{
// Get FieldType
$nFieldType = $fields[$szFieldId]['FieldType'];
// Set DB Field name first!
$myDBFieldName = $dbmapping[$szTableType]['DBMAPPINGS'][$szFieldId];
$myDBQueryFieldName = $myDBFieldName;
$mySelectFieldName = $myDBFieldName;
// Special handling for date fields
if ( $nFieldType == FILTER_TYPE_DATE )
$myDBQueryFields = "";
foreach ( $aIncludeCustomFields as $myFieldName )
{
// Helper variable for the select statement
$mySelectFieldName = $mySelectFieldName . "Grouped";
$myDBQueryFieldName = "DATE( " . $myDBFieldName . ") AS " . $mySelectFieldName ;
if ( isset($dbmapping[$szTableType]['DBMAPPINGS'][$myFieldName]) )
$myDBQueryFields .= $dbmapping[$szTableType]['DBMAPPINGS'][$myFieldName] . ", ";
}
// Create SQL String now!
$szSql = "SELECT " .
$myDBQueryFieldName . ", " .
"count(" . $myDBFieldName . ") as TotalCount " .
" FROM " . $this->_logStreamConfigObj->DBTableName .
" GROUP BY " . $mySelectFieldName .
" ORDER BY TotalCount DESC" .
" LIMIT " . $nRecordLimit;
// Perform Database Query
$myquery = mysql_query($szSql, $this->_dbhandle);
if ( !$myquery )
return ERROR_DB_QUERYFAILED;
// Initialize Array variable
$aResult = array();
// read data records
while ($myRow = mysql_fetch_array($myquery, MYSQL_ASSOC))
// Append Sortingfield
if ( !in_array($szConsFieldId, $aIncludeCustomFields) )
$myDBQueryFields .= $myDBConsFieldName . ", ";
}
else if ( $bIncludeLogStreamFields )
{
$myDBQueryFields = "";
foreach ( $this->_arrProperties as $myFieldName )
{
$aResult[ $myRow[$mySelectFieldName] ] = $myRow;
if ( isset($dbmapping[$szTableType]['DBMAPPINGS'][$myFieldName]) )
$myDBQueryFields .= $dbmapping[$szTableType]['DBMAPPINGS'][$myFieldName] . ", ";
}
}
else // Only Include ConsolidateField
$myDBQueryFields = $myDBConsFieldName . ", ";
if ( $szConsFieldId == $szSortFieldId )
$myDBSortedFieldName = "ConsolidatedField";
else
$myDBSortedFieldName = $szSortFieldId;
// ---
// Special handling for date fields
if ( $nConsFieldType == FILTER_TYPE_DATE )
{
// Helper variable for the select statement
$mySelectFieldName = $myDBGroupByFieldName . "Grouped";
$myDBQueryFieldName = "DATE( " . $myDBConsFieldName . ") AS " . $myDBGroupByFieldName ;
}
// Create SQL String now!
$szSql = "SELECT " .
$myDBQueryFields .
"count(" . $myDBConsFieldName . ") as ConsolidatedField " .
" FROM " . $this->_logStreamConfigObj->DBTableName .
" GROUP BY " . $myDBGroupByFieldName .
" ORDER BY " . $myDBSortedFieldName . " " . $szSortingOrder .
" LIMIT " . $nRecordLimit;
// Perform Database Query
$myquery = mysql_query($szSql, $this->_dbhandle);
if ( !$myquery )
return ERROR_DB_QUERYFAILED;
// Initialize Array variable
$aResult = array();
// read data records
while ($myRow = mysql_fetch_array($myquery, MYSQL_ASSOC))
{
// Create new row
$aNewRow = array();
foreach ( $myRow as $myFieldName => $myFieldValue )
{
if ( $myFieldName == $dbmapping[$szTableType]['DBMAPPINGS'][$szConsFieldId] )
$aNewRow[$szConsFieldId] = $myFieldValue;
else
$aNewRow[$myFieldName] = $myFieldValue;
// if ( in_array($myFieldName, $this->_arrProperties) )
// echo "!!";
//print_r ( $this->_arrProperties );
// echo $myFieldName;
// exit;
}
// Add new row to result
$aResult[] = $aNewRow;
// $aResult[ $myRow[$myDBGroupByFieldName] ] = $myRow;
}
// $aResult[] = $myRow;
// $aResult[ $myRow[$mySelectFieldName] ] = $myRow['TotalCount'];
// return finished array
if ( count($aResult) > 0 )
return $aResult;
else
return ERROR_NOMORERECORDS;
}
// return finished array
if ( count($aResult) > 0 )
return $aResult;
else
{
// return error code, field mapping not found
return ERROR_DB_DBFIELDNOTFOUND;
}
return ERROR_NOMORERECORDS;
}

View File

@ -688,7 +688,7 @@ class LogStreamPDO extends LogStream {
*
* @return integer Error stat
*/
public function ConsolidateDataByField($szFieldId, $nRecordLimit)
public function ConsolidateDataByField($szConsFieldId, $nRecordLimit, $szSortFieldId, $nSortingOrder, $bIncludeLogStreamFields = false)
{
global $content, $dbmapping;

View File

@ -107,14 +107,14 @@ class Report_monilog extends Report {
// Step 1: Gather Summaries
// Obtain data from the logstream!
$reportData = $this->_streamObj->ConsolidateDataByField( SYSLOG_SEVERITY, 10 );
$reportData = $this->_streamObj->ConsolidateDataByField( SYSLOG_SEVERITY, 10, SYSLOG_SEVERITY, SORTING_ORDER_DESC, null, false );
// If data is valid, we have an array!
if ( is_array($reportData) && count($reportData) > 0 )
{
foreach ($reportData as &$tmpReportData )
{
$tmpReportData['DisplayName'] = GetFacilityDisplayName( $tmpReportData[SYSLOG_SEVERITY] );
$tmpReportData['DisplayName'] = GetSeverityDisplayName( $tmpReportData[SYSLOG_SEVERITY] );
}
}

View File

@ -86,6 +86,9 @@ define('ALIGN_RIGHT', 'right');
define('REPORT_OUTPUT_HTML', 'html');
define('REPORT_OUTPUT_PDF', 'pdf');
// Defines for sorting
define('SORTING_ORDER_ASC', 'asc');
define('SORTING_ORDER_DESC', 'desc');
// --- Predefine fields array!
$fields[SYSLOG_UID]['FieldID'] = SYSLOG_UID;