From 0e1114846d1739c1f47d8e4e067f81be52b887ea Mon Sep 17 00:00:00 2001 From: Vitaly X Date: Tue, 3 Jan 2023 23:39:16 +0200 Subject: [PATCH] charts orderby config; see readme for more details --- .gitignore | 2 ++ README.md | 4 +++ src/admin/charts.php | 20 +++++++++++---- src/chartgenerator.php | 21 +++++++++------ src/classes/logstreamdb.class.php | 37 +++++++++------------------ src/include/db_update_v14.txt | 7 +++++ src/include/functions_common.php | 2 ++ src/include/functions_config.php | 3 ++- src/index.php | 5 +++- src/statistics.php | 8 +++++- src/templates/admin/admin_charts.html | 11 +++++--- src/templates/statistics.html | 13 ++++++++-- 12 files changed, 87 insertions(+), 46 deletions(-) create mode 100644 .gitignore create mode 100644 src/include/db_update_v14.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3809eae --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +src/config.php + diff --git a/README.md b/README.md index 453860b..67b9b40 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,10 @@ Adiscon LogAnalyzer, a web frontend to log data from the same folks the created rsyslog # changes + - Maximize view by default + - fix chart double adding same key into search + - allow to configure ORDER BY clause + - add custom filter to chart value redirection link if such exists - support number ranges in filter, i.e. severity:3-6 -> where severity in (3,4,5,6) - support quoted filters, i.e. syslogtag:-="dhcp,info",-="wireless,info",-"system%,account" -> where (syslogtag <> 'dhcp,info' AND syslogtag <> 'wireless,info' AND syslogtag NOT LIKE '%system%,account%' ) - support TRACE syslog level; diff --git a/src/admin/charts.php b/src/admin/charts.php index e4d1ed2..75bd537 100644 --- a/src/admin/charts.php +++ b/src/admin/charts.php @@ -154,7 +154,8 @@ if ( isset($_GET['op']) ) $content['CHECKED_ISSHOWPERCENT'] = ""; $content['chart_defaultfilter'] = ""; // Chart Field - $content['chart_field'] = SYSLOG_HOST; + $content['chart_field'] = SYSLOG_HOST; + $content['chart_orderby'] = ''; CreateChartFields($content['chart_field']); // COMMON Fields @@ -194,7 +195,6 @@ if ( isset($_GET['op']) ) { // Get Source reference $myChart = $content['Charts'][ $content['CHARTID'] ]; - // Copy basic properties $content['Name'] = $myChart['DisplayName']; $content['chart_type'] = $myChart['chart_type']; @@ -216,7 +216,8 @@ if ( isset($_GET['op']) ) // Chart Field $content['chart_field'] = $myChart['chart_field']; CreateChartFields($content['chart_field']); - + $content['chart_orderby'] = $myChart['chart_orderby']; + // COMMON Fields $content['userid'] = $myChart['userid']; if ( $content['userid'] != null ) @@ -317,6 +318,7 @@ if ( isset($_POST['op']) ) if ( isset($_POST['chart_type']) ) { $content['chart_type'] = intval(DB_RemoveBadChars($_POST['chart_type'])); } if ( isset($_POST['chart_width']) ) { $content['chart_width'] = intval(DB_RemoveBadChars($_POST['chart_width'])); } else {$content['chart_width'] = 400; } if ( isset($_POST['chart_field']) ) { $content['chart_field'] = DB_RemoveBadChars($_POST['chart_field']); } + if ( isset($_POST['chart_orderby']) ) { $content['chart_orderby'] = DB_RemoveBadChars($_POST['chart_orderby']); } if ( isset($_POST['maxrecords']) ) { $content['maxrecords'] = intval(DB_RemoveBadChars($_POST['maxrecords'])); } if ( isset($_POST['showpercent']) ) { $content['showpercent'] = intval(DB_RemoveBadChars($_POST['showpercent'])); } else {$content['showpercent'] = 0; } if ( isset($_POST['chart_defaultfilter']) ) { $content['chart_defaultfilter'] = DB_RemoveBadChars($_POST['chart_defaultfilter']); } @@ -369,12 +371,13 @@ if ( isset($_POST['op']) ) if ( $_POST['op'] == "addnewchart" ) { // Add custom search now! - $sqlquery = "INSERT INTO " . DB_CHARTS . " (DisplayName, chart_enabled, chart_type, chart_width, chart_field, chart_defaultfilter, maxrecords, showpercent, userid, groupid) + $sqlquery = "INSERT INTO " . DB_CHARTS . " (DisplayName, chart_enabled, chart_type, chart_width, chart_field, chart_orderby, chart_defaultfilter, maxrecords, showpercent, userid, groupid) VALUES ('" . $content['Name'] . "', " . $content['chart_enabled'] . ", " . $content['chart_type'] . ", " . $content['chart_width'] . ", '" . $content['chart_field'] . "', + '" . $content['chart_orderby'] . "', '" . $content['chart_defaultfilter'] . "', " . $content['maxrecords'] . ", " . $content['showpercent'] . ", @@ -405,6 +408,7 @@ if ( isset($_POST['op']) ) chart_type = " . $content['chart_type'] . ", chart_width = " . $content['chart_width'] . ", chart_field = '" . $content['chart_field'] . "', + chart_orderby = '" . $content['chart_orderby'] . "', chart_defaultfilter = '" . $content['chart_defaultfilter'] . "', maxrecords = " . $content['maxrecords'] . ", showpercent = " . $content['showpercent'] . ", @@ -514,6 +518,12 @@ if ( !isset($_POST['op']) && !isset($_GET['op']) ) $myChart['chart_defaultfilter_urldecoded'] = urlencode($myChart['chart_defaultfilter']); else $myChart['chart_defaultfilter_urldecoded'] = ""; + + if ( ( isset($myChart['chart_orderby']) ) && ( strlen($myChart['chart_orderby']) > 0 ) ) + $myChart['chart_orderby_urldecoded'] = urlencode($myChart['chart_orderby']); + else + $myChart['chart_orderby_urldecoded'] = ""; + // --- // --- Set CSS Class @@ -544,4 +554,4 @@ $page -> parser($content, "admin/admin_charts.html"); $page -> output(); // --- -?> \ No newline at end of file +?> diff --git a/src/chartgenerator.php b/src/chartgenerator.php index d52e9a9..20bd30d 100644 --- a/src/chartgenerator.php +++ b/src/chartgenerator.php @@ -98,6 +98,11 @@ else $content['error_details'] = $content['LN_GEN_ERROR_MISSINGCHARTFIELD']; } +if ( isset($_GET['orderby']) ) +{ + $content['chart_orderby'] = $_GET['orderby']; +}else { $content['chart_orderby'] = NULL; } + if ( isset($_GET['maxrecords']) ) { // read and verify value @@ -148,7 +153,7 @@ if ( !$content['error_occured'] ) { // Obtain and get the Config Object $stream_config = $content['Sources'][$currentSourceID]['ObjRef']; - + //echo "filter:".$content['chart_defaultfilter']."
"; // Create LogStream Object $stream = $stream_config->LogStreamFactory($stream_config); $stream->SetFilter($content['chart_defaultfilter']); @@ -160,7 +165,7 @@ if ( !$content['error_occured'] ) if ( $aFilterFields != null ) $content['ChartColumns'] = $aFilterFields; - // Append mandetory fields + // Append mandatory fields if ( !in_array(SYSLOG_UID, $content['ChartColumns']) ) $content['ChartColumns'][] = SYSLOG_UID; if ( !in_array($content['chart_field'], $content['ChartColumns']) ) @@ -182,9 +187,9 @@ if ( !$content['error_occured'] ) // Split key and value $tmpArray = explode(":", $myFilter, 2); - // Check if keyfield is valid and add to our Columns array if available + // Check if keyfield is valid and add to our Columns array only if it's not there yet $newField = $stream->ReturnFilterKeyBySearchField($tmpArray[FILTER_TMP_KEY]); - if ( $newField ) + if ( $newField && !in_array($newField,$content['ChartColumns'])) $content['ChartColumns'][] = $newField; } } @@ -195,8 +200,8 @@ if ( !$content['error_occured'] ) if ( $res == SUCCESS ) { // Obtain data from the logstream! - $chartData = $stream->GetCountSortedByField($content['chart_field'], $content['chart_fieldtype'], $content['maxrecords']); - + $chartData = $stream->GetCountSortedByField($content['chart_field'], $content['chart_fieldtype'], $content['maxrecords'], $content['chart_orderby']); +//echo "
";echo print_r($chartData); echo "

"; // If data is valid, we have an array! if ( is_array($chartData) && count($chartData) > 0 ) { @@ -210,7 +215,7 @@ if ( !$content['error_occured'] ) $YchartData[] = intval($myData); $XchartData[] = strlen($myKey) > 0 ? $myKey : "Unknown"; if ( isset($fields[$content['chart_field']]['SearchField']) && strlen($myKey) > 0 ) - $chartImageMapLinks[] = $content['custombasepath'] . "index.php?filter=" . $fields[$content['chart_field']]['SearchField'] . "%3A%3D" . urlencode($szEncodedKeyStr) . "&search=Search"; + $chartImageMapLinks[] = $content['custombasepath'] . "index.php?filter=" . $fields[$content['chart_field']]['SearchField'] . "%3A%3D" . urlencode($szEncodedKeyStr) .(empty($content['chart_defaultfilter'])?"":urlencode(" ".$content['chart_defaultfilter'])). "&search=Search"; else $chartImageMapLinks[] = ""; @@ -552,4 +557,4 @@ function OutpuCustomErrorMessage() { // Exit in any case exit; } -?> \ No newline at end of file +?> diff --git a/src/classes/logstreamdb.class.php b/src/classes/logstreamdb.class.php index ab3fc8c..86b7062 100644 --- a/src/classes/logstreamdb.class.php +++ b/src/classes/logstreamdb.class.php @@ -1270,8 +1270,11 @@ class LogStreamDB extends LogStream { * * @return integer Error stat */ - public function GetCountSortedByField($szFieldId, $nFieldType, $nRecordLimit) + public function GetCountSortedByField($szFieldId, $nFieldType, $nRecordLimit, $orderBy='') { + if(empty($orderBy)){ + $orderBy = 'totalcount DESC'; + } global $content, $dbmapping; // Copy helper variables, this is just for better readability @@ -1283,13 +1286,14 @@ class LogStreamDB extends LogStream { $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 ; + //$orderBy = $mySelectFieldName." DESC"; } // Create SQL Where Clause! @@ -1307,7 +1311,7 @@ class LogStreamDB extends LogStream { " FROM `" . $this->_logStreamConfigObj->DBTableName . "`" . $this->_SQLwhereClause . " GROUP BY " . $mySelectFieldName . - " ORDER BY totalcount DESC" . + " ORDER BY ".$orderBy. " LIMIT " . $nRecordLimit; // Perform Database Query @@ -1365,6 +1369,7 @@ class LogStreamDB extends LogStream { // --- Build Query Array $arrayQueryProperties = $this->_arrProperties; + //"
".$arrayQueryProperties."

" if ( isset($this->_arrFilterProperties) && $this->_arrFilterProperties != null) { foreach ( $this->_arrFilterProperties as $filterproperty ) @@ -1373,8 +1378,9 @@ class LogStreamDB extends LogStream { $arrayQueryProperties[] = $filterproperty; } } - // --- - + // --- + //echo 'DEBUG
'; print_r($arrayQueryProperties); echo '
'; + //echo 'DEBUG
'; print_r($this->_filters); echo '
'; // Loop through all available properties foreach( $arrayQueryProperties as $propertyname ) { @@ -1496,26 +1502,8 @@ class LogStreamDB extends LogStream { if ( $myfilter[FILTER_DATEMODE] == DATEMODE_LASTX ) { // Get current timestamp - $nNowTimeStamp = time() - (60 * 60 * intval($myfilter[FILTER_VALUE])); - /*if ( $myfilter[FILTER_VALUE] == DATE_LASTX_HOUR ) - $nNowTimeStamp -= 60 * 60; // One Hour! - else if ( $myfilter[FILTER_VALUE] == DATE_LASTX_12HOURS ) - $nNowTimeStamp -= 60 * 60 * 12; // 12 Hours! - else if ( $myfilter[FILTER_VALUE] == DATE_LASTX_24HOURS ) - $nNowTimeStamp -= 60 * 60 * 24; // 24 Hours! - else if ( $myfilter[FILTER_VALUE] == DATE_LASTX_7DAYS ) - $nNowTimeStamp -= 60 * 60 * 24 * 7; // 7 days - else if ( $myfilter[FILTER_VALUE] == DATE_LASTX_31DAYS ) - $nNowTimeStamp -= 60 * 60 * 24 * 31; // 31 days - else - { - // Set filter to unknown and Abort in this case! - $tmpfilters[$propertyname][FILTER_TYPE] = FILTER_TYPE_UNKNOWN; - break; - }*/ - // Append filter $tmpfilters[$propertyname][FILTER_VALUE] .= $dbmapping[$szTableType]['DBMAPPINGS'][$propertyname] . " > '" . date("Y-m-d H:i:s", $nNowTimeStamp) . "'"; } @@ -1554,7 +1542,6 @@ class LogStreamDB extends LogStream { } } } - // Check and combine all filters now! if ( isset($tmpfilters) ) { @@ -1587,7 +1574,7 @@ class LogStreamDB extends LogStream { } } -// echo $this->_SQLwhereClause; +//echo $this->_SQLwhereClause."
"; //$dbmapping[$szTableType][SYSLOG_UID] } else // No filters means nothing to do! diff --git a/src/include/db_update_v14.txt b/src/include/db_update_v14.txt new file mode 100644 index 0000000..27ce130 --- /dev/null +++ b/src/include/db_update_v14.txt @@ -0,0 +1,7 @@ +-- New Database Structure Updates +ALTER TABLE `logcon_charts` ADD `chart_orderby` VARCHAR( 255 ) NULL AFTER `chart_defaultfilter` ; + + +-- Insert data + +-- Updated Data diff --git a/src/include/functions_common.php b/src/include/functions_common.php index cef4820..c30bbf7 100644 --- a/src/include/functions_common.php +++ b/src/include/functions_common.php @@ -510,6 +510,8 @@ function CreateReloadTimesList() $tmpReloadSeconds = GetConfigSetting("ViewEnableAutoReloadSeconds", "", CFGLEVEL_USER); if ( $tmpReloadSeconds > 0 ) { + //by default select predefined value + $_SESSION['AUTORELOAD_ID'] = 1; $content['reloadtimes'][$iCounter] = array( "ID" => $iCounter, "Selected" => "", "DisplayName" => $content['LN_AUTORELOAD_PRECONFIGURED'] . " (" . $tmpReloadSeconds . " " . $content['LN_AUTORELOAD_SECONDS'] . ") ", "Value" => $tmpReloadSeconds ); $iCounter++; } $content['reloadtimes'][$iCounter] = array( "ID" => $iCounter, "Selected" => "", "DisplayName" => " 5 " . $content['LN_AUTORELOAD_SECONDS'], "Value" => 5 ); $iCounter++; diff --git a/src/include/functions_config.php b/src/include/functions_config.php index fd8e647..d5189c3 100644 --- a/src/include/functions_config.php +++ b/src/include/functions_config.php @@ -930,7 +930,8 @@ function LoadChartsFromDatabase() DB_CHARTS . ".chart_enabled, " . DB_CHARTS . ".chart_type, " . DB_CHARTS . ".chart_width, " . - DB_CHARTS . ".chart_field, " . + DB_CHARTS . ".chart_field, " . + DB_CHARTS . ".chart_orderby, " . DB_CHARTS . ".chart_defaultfilter, " . DB_CHARTS . ".maxrecords, " . DB_CHARTS . ".showpercent, " . diff --git a/src/index.php b/src/index.php index bcc18a2..c69fc8a 100644 --- a/src/index.php +++ b/src/index.php @@ -122,11 +122,14 @@ $content['main_pager_last_found'] = false; // Init Sorting variables $content['sorting'] = ""; $content['searchstr'] = ""; -$content['searchstr_htmlform'] = "datelastx:6"; +$content['searchstr_htmlform'] = "datelastx:6 severity:-8"; $content['highlightstr'] = ""; $content['highlightstr_htmlform'] = ""; $content['EXPAND_HIGHLIGHT'] = "false"; +// hide header by default +$_SESSION['SESSION_MAXIMIZED'] = true; + // --- Read and process filters from search dialog! if ( (isset($_POST['search']) || isset($_GET['search'])) || (isset($_POST['filter']) || isset($_GET['filter'])) ) { diff --git a/src/statistics.php b/src/statistics.php index 8e3fe37..1709793 100644 --- a/src/statistics.php +++ b/src/statistics.php @@ -110,6 +110,12 @@ if ( isset($content['Charts']) ) $myChart['chart_defaultfilter_urldecoded'] = urlencode($myChart['chart_defaultfilter']); else $myChart['chart_defaultfilter_urldecoded'] = ""; + + if ( isset($myChart['chart_orderby']) && strlen($myChart['chart_orderby']) > 0 ) + $myChart['chart_orderby_urldecoded'] = urlencode($myChart['chart_orderby']); + else + $myChart['chart_orderby_urldecoded'] = ""; + // --- // --- Set CSS Class @@ -159,4 +165,4 @@ $page -> output(); //include($gl_root_path . 'include/functions_installhelpers.php'); //ConvertCustomCharts(); -?> \ No newline at end of file +?> diff --git a/src/templates/admin/admin_charts.html b/src/templates/admin/admin_charts.html index 1e64b1c..fbadec3 100644 --- a/src/templates/admin/admin_charts.html +++ b/src/templates/admin/admin_charts.html @@ -68,7 +68,7 @@   - \ No newline at end of file + diff --git a/src/templates/statistics.html b/src/templates/statistics.html index 3417ad7..450f2e4 100644 --- a/src/templates/statistics.html +++ b/src/templates/statistics.html @@ -43,6 +43,15 @@ {LN_CHART_FIELD}  {CHART_FIELD_DISPLAYNAME} + + {LN_CHART_ORDERBY} +  {chart_orderby} + + + {LN_CHART_FILTER} +  {chart_defaultfilter} + + {LN_CHART_WIDTH}  {chart_width} @@ -59,7 +68,7 @@
@@ -70,4 +79,4 @@ - \ No newline at end of file +