From 979aaa4db5c3547b327786c7fe78967c34e08a1e Mon Sep 17 00:00:00 2001 From: Andre Lorbach Date: Tue, 22 Dec 2009 15:55:17 +0100 Subject: [PATCH] Added command line based report generator --- src/cron/cmdreportgen.php | 195 +++++++++++++++++++++++++++++++ src/include/functions_config.php | 4 +- src/reportgenerator.php | 2 +- 3 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 src/cron/cmdreportgen.php diff --git a/src/cron/cmdreportgen.php b/src/cron/cmdreportgen.php new file mode 100644 index 0000000..76d2e1f --- /dev/null +++ b/src/cron/cmdreportgen.php @@ -0,0 +1,195 @@ + This file is meant to run on command line, or via CRON / task Scheduler + * + * All directives are explained within this file + * + * Copyright (C) 2008-2009 Adiscon GmbH. + * + * This file is part of phpLogCon. + * + * PhpLogCon is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * PhpLogCon is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with phpLogCon. If not, see . + * + * A copy of the GPL can be found in the file "COPYING" in this + * distribution + ********************************************************************* +*/ + +// *** Default includes and procedures *** // +define('IN_PHPLOGCON', true); +$gl_root_path = './../'; + +// Now include necessary include files! +include_once($gl_root_path . 'include/functions_common.php'); +include_once($gl_root_path . 'include/functions_frontendhelpers.php'); +include_once($gl_root_path . 'include/functions_filters.php'); + +// Include LogStream facility +include_once($gl_root_path . 'classes/logstream.class.php'); + +// Set commandline mode for the script +define('IN_PHPLOGCON_COMMANDLINE', true); +$content['IN_PHPLOGCON_COMMANDLINE'] = true; +InitPhpLogCon(); +InitSourceConfigs(); +InitFilterHelpers(); // Helpers for frontend filtering! + +// Firts of all init List of Reports! +InitReportModules(); +// --- + +// --- Helper functions +/* +* Cleans data in desired logstream +*/ +function RunReport() +{ + global $content, $gl_root_path; + + // Get Reference to report! + $myReport = $content['REPORTS'][ $content['reportid'] ]; + + // Get reference to savedreport + $mySavedReport = $myReport['SAVEDREPORTS'][ $content['savedreportid'] ]; + + // Get Objectreference to report + $myReportObj = $myReport["ObjRef"]; + + // Set SavedReport Settings! + $myReportObj->InitFromSavedReport($mySavedReport); + + //Debug Output + PrintHTMLDebugInfo( DEBUG_INFO, "RunReport", GetAndReplaceLangStr($content["LN_CMD_RUNREPORT"], $mySavedReport['customTitle']) ); + + // Perform check + $res = $myReportObj->verifyDataSource(); + if ( $res != SUCCESS ) + { + // Print error and die! + $szError = GetAndReplaceLangStr( $content['LN_REPORTS_ERROR_ERRORCHECKINGSOURCE'], GetAndReplaceLangStr( GetErrorMessage($res), $mySavedReport['sourceid']) ); + if ( isset($extraErrorDescription) ) + $szError .= "

" . GetAndReplaceLangStr( $content['LN_SOURCES_ERROR_EXTRAMSG'], $extraErrorDescription); + DieWithErrorMsg( $szError ); + } + else + { + // Call processing part now! + $res = $myReportObj->startDataProcessing(); + if ( $res != SUCCESS ) + DieWithErrorMsg( GetAndReplaceLangStr( $content['LN_GEN_ERROR_REPORTGENFAILED'], $mySavedReport['customTitle'], GetErrorMessage($res) ); + else + { + // --- Perform report output + + // Init IncludePath + $reportIncludePath = $myReportObj->GetReportIncludePath(); + + // Include Custom language file if available + $myReportObj->InitReportLanguageFile($reportIncludePath); + + // Init template Parser + $page = new Template(); + $page -> set_path ( $reportIncludePath ); + + // Parse template + $page -> parser($content, $myReportObj->GetBaseFileName()); + + // Output the result + $res = $myReportObj->OutputReport( $page ->result(), $szErrorStr ); + if ( $res == SUCCESS && $myReportObj->GetOutputTarget() != REPORT_TARGET_STDOUT ) + { + //Debug Output + PrintHTMLDebugInfo( DEBUG_INFO, "RunReport", GetAndReplaceLangStr($content["LN_GEN_SUCCESS_REPORTWASGENERATED_DETAILS"], $szErrorStr) ); + } + else if ( $res == ERROR ) + { + // Debug Error + PrintHTMLDebugInfo( DEBUG_ERROR, "RunReport", GetAndReplaceLangStr($content["LN_GEN_ERROR_REPORTFAILEDTOGENERATE"], $szErrorStr) ); + } + // --- + } + } +} +// --- + + +// --- BEGIN Custom Code + //Additional Includes + include($gl_root_path . 'include/functions_debugoutput.php'); + + // Run into Commandline part now! + /* Only run if we are in command line mode + * + * Possible Operation Types: + * runreport = To create a report, use this operation type. + * Sample 1: Run report from type "monilog" with savedreportid 3 + * php cmdreportgen.php runreport monilog 3 + * + */ + + // Init DebugHeader + PrintDebugInfoHeader(); + + // --- Now read command line args! + // Operation argv + if ( isset($_SERVER["argv"][1]) ) + $operation = $_SERVER["argv"][1]; + else + DieWithErrorMsg( $content["LN_CMD_NOOP"] ); + + // SavedReportID argv + if ( isset($_SERVER["argv"][2]) ) + { + // Set to SourceID property! + $content['reportid'] = $_SERVER["argv"][2]; + + // Check if exists + if ( !isset($content['REPORTS'][ $content['reportid'] ]) ) + DieWithErrorMsg( GetAndReplaceLangStr($content["LN_CMD_LOGSTREAMNOTFOUND"], $content['reportid']) ); + + // Get Reference to report! + $myReport = $content['REPORTS'][ $content['reportid'] ]; + } + else + DieWithErrorMsg( $content["LN_CMD_NOREPORTID"] ); + + // SavedReportID argv + if ( isset($_SERVER["argv"][3]) ) + { + // Set to SourceID property! + $content['savedreportid'] = intval( $_SERVER["argv"][3] ); + + // Check if exists + if ( !isset($myReport['SAVEDREPORTS'][ $content['savedreportid'] ]) ) + DieWithErrorMsg( GetAndReplaceLangStr($content["LN_CMD_LOGSTREAMNOTFOUND"], $content['savedreportid']) ); + } + else + DieWithErrorMsg( $content["LN_CMD_NOSAVEDREPORTID"] ); + // --- + + // --- Operation Handling now + if ( $operation == "runreport" ) + { + // Create Report + RunReport(); + } + // --- +// --- + +?> \ No newline at end of file diff --git a/src/include/functions_config.php b/src/include/functions_config.php index 5112ffd..bfcf8f5 100644 --- a/src/include/functions_config.php +++ b/src/include/functions_config.php @@ -384,7 +384,9 @@ function InitReportModules() // Add all savedreports foreach ($myrows as &$mySavedReport) { - // TODO: Perform whatever needs to be performed + // Set default properties if not set! + if (!isset($mySavedReport['outputTarget']) || strlen($mySavedReport['outputTarget']) <= 0 ) + $mySavedReport['outputTarget'] = REPORT_TARGET_STDOUT; // Add saved report into global array $content['REPORTS'][$myReportID]['SAVEDREPORTS'][ $mySavedReport['SavedReportID'] ] = $mySavedReport; diff --git a/src/reportgenerator.php b/src/reportgenerator.php index d64955c..82ded07 100644 --- a/src/reportgenerator.php +++ b/src/reportgenerator.php @@ -98,7 +98,7 @@ if ( !$content['error_occured'] ) // Check if report exists if ( isset($content['REPORTS'][ $content['reportid'] ]) ) { - // Get Reference to parser! + // Get Reference to report! $myReport = $content['REPORTS'][ $content['reportid'] ]; // Now check if the saved report is available