From b3d76c7de0d55b56d2ec85e9f19c4bf703b88fbf Mon Sep 17 00:00:00 2001 From: Andre Lorbach Date: Thu, 5 Nov 2009 17:17:50 +0100 Subject: [PATCH] Further changes in report area --- src/classes/logstream.class.php | 7 ++ src/classes/logstreamdb.class.php | 72 ++++++++++++++ src/classes/logstreampdo.class.php | 95 +++++++++++++++++++ .../reports/report.eventlog.monilog.class.php | 13 ++- src/reportgenerator.php | 2 +- 5 files changed, 183 insertions(+), 6 deletions(-) diff --git a/src/classes/logstream.class.php b/src/classes/logstream.class.php index 27431b2..4371920 100644 --- a/src/classes/logstream.class.php +++ b/src/classes/logstream.class.php @@ -202,6 +202,13 @@ abstract class LogStream { */ public abstract function GetCountSortedByField($szFieldId, $nFieldType, $nRecordLimit); + /** + * This functions is used by reports to consolidate data + * + * @return integer Error stat + */ + public abstract function ConsolidateDataByField($szFieldId, $nRecordLimit); + /** * Gets a property and checks if the class is able to sort the records diff --git a/src/classes/logstreamdb.class.php b/src/classes/logstreamdb.class.php index f1ba45c..53b1f43 100644 --- a/src/classes/logstreamdb.class.php +++ b/src/classes/logstreamdb.class.php @@ -689,6 +689,78 @@ class LogStreamDB extends LogStream { } + /** + * Implementation of ConsolidateDataByField + * + * In the native MYSQL Logstream, the database will do most of the work + * + * @return integer Error stat + */ + public function ConsolidateDataByField($szFieldId, $nRecordLimit) + { + global $content, $dbmapping, $fields; + + // Copy helper variables, this is just for better readability + $szTableType = $this->_logStreamConfigObj->DBTableType; + + if ( isset($dbmapping[$szTableType]['DBMAPPINGS'][$szFieldId]) ) + { + // 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 ) + { + // Helper variable for the select statement + $mySelectFieldName = $mySelectFieldName . "Grouped"; + $myDBQueryFieldName = "DATE( " . $myDBFieldName . ") AS " . $mySelectFieldName ; + } + + // 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)) + { + $aResult[ $myRow[$mySelectFieldName] ] = $myRow; + } +// $aResult[] = $myRow; +// $aResult[ $myRow[$mySelectFieldName] ] = $myRow['TotalCount']; + + // return finished array + if ( count($aResult) > 0 ) + return $aResult; + else + return ERROR_NOMORERECORDS; + } + else + { + // return error code, field mapping not found + return ERROR_DB_DBFIELDNOTFOUND; + } + } + + + /** * Implementation of GetCountSortedByField * diff --git a/src/classes/logstreampdo.class.php b/src/classes/logstreampdo.class.php index 77d2549..a0f9918 100644 --- a/src/classes/logstreampdo.class.php +++ b/src/classes/logstreampdo.class.php @@ -681,6 +681,101 @@ class LogStreamPDO extends LogStream { } + /** + * Implementation of ConsolidateDataByField + * + * In the PDO DB Logstream, the database will do most of the work + * + * @return integer Error stat + */ + public function ConsolidateDataByField($szFieldId, $nRecordLimit) + { + global $content, $dbmapping; + + // Copy helper variables, this is just for better readability + $szTableType = $this->_logStreamConfigObj->DBTableType; + + if ( isset($dbmapping[$szTableType]['DBMAPPINGS'][$szFieldId]) ) + { + // 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 ) + { + if ( $this->_logStreamConfigObj->DBType == DB_MYSQL || + $this->_logStreamConfigObj->DBType == DB_PGSQL ) + { + // Helper variable for the select statement + $mySelectFieldName = $mySelectFieldName . "grouped"; + $myDBQueryFieldName = "DATE( " . $myDBFieldName . ") AS " . $mySelectFieldName ; + } + else if($this->_logStreamConfigObj->DBType == DB_MSSQL ) + { + // TODO FIND A WAY FOR MSSQL! + } + } + + // Create SQL String now! + $szSql = "SELECT " . + $myDBQueryFieldName . ", " . + "count(" . $myDBFieldName . ") as totalcount " . + " FROM " . $this->_logStreamConfigObj->DBTableName . + " GROUP BY " . $mySelectFieldName . + " ORDER BY totalcount DESC"; + // Append LIMIT in this case! + if ( $this->_logStreamConfigObj->DBType == DB_MYSQL || + $this->_logStreamConfigObj->DBType == DB_PGSQL ) + $szSql .= " LIMIT " . $nRecordLimit; + + // Perform Database Query + $this->_myDBQuery = $this->_dbhandle->query($szSql); + if ( !$this->_myDBQuery ) + return ERROR_DB_QUERYFAILED; + + if ( $this->_myDBQuery->rowCount() == 0 ) + { + $this->_myDBQuery = null; + return ERROR_NOMORERECORDS; + } + + // Initialize Array variable + $aResult = array(); + + // read data records + $iCount = 0; + while ( ($myRow = $this->_myDBQuery->fetch(PDO::FETCH_ASSOC)) && $iCount < $nRecordLimit) + { + if ( isset($myRow[$mySelectFieldName]) ) + { + $aResult[] = $myRow; +// $aResult[ $myRow[$mySelectFieldName] ] = $myRow['totalcount']; + $iCount++; + } + } + + // Delete handle + $this->_myDBQuery = null; + + // return finished array + if ( count($aResult) > 0 ) + return $aResult; + else + return ERROR_NOMORERECORDS; + } + else + { + // return error code, field mapping not found + return ERROR_DB_DBFIELDNOTFOUND; + } + } + + /** * Implementation of GetCountSortedByField * diff --git a/src/classes/reports/report.eventlog.monilog.class.php b/src/classes/reports/report.eventlog.monilog.class.php index 25b40c2..bb8ca63 100644 --- a/src/classes/reports/report.eventlog.monilog.class.php +++ b/src/classes/reports/report.eventlog.monilog.class.php @@ -107,18 +107,21 @@ class Report_monilog extends Report { // Step 1: Gather Summaries // Obtain data from the logstream! - $reportData = $this->_streamObj->GetCountSortedByField( SYSLOG_SEVERITY, FILTER_TYPE_NUMBER, 10 ); -echo "wtf"; - -print_r ( $reportData ); -exit; + $reportData = $this->_streamObj->ConsolidateDataByField( SYSLOG_SEVERITY, 10 ); // 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] ); + } } +print_r ( $reportData ); +exit; + // --- } diff --git a/src/reportgenerator.php b/src/reportgenerator.php index f1a20dc..4812450 100644 --- a/src/reportgenerator.php +++ b/src/reportgenerator.php @@ -203,7 +203,7 @@ if ( !$content['error_occured'] ) } else { - // Perform report output + // Perform report output InitTemplateParser(); echo $myReportObj->_baseFileName; exit;