Improvements and updates

Improved Crapview and DbQuery
This commit is contained in:
Valentino Orlandi 2024-02-04 21:56:06 +01:00
parent 7df22305da
commit cbabccfe0c
Signed by: elB4RTO
GPG Key ID: 1719E976DB2D4E71
6 changed files with 113 additions and 117 deletions

View File

@ -1,6 +1,8 @@
#include "crapview.h"
#include "globals/db_names.h"
#include "utilities/printables.h"
#include "utilities/strings.h"
#include "utilities/vectors.h"
@ -30,29 +32,11 @@ void Crapview::setDialogsLevel( const DialogsLevel new_level ) noexcept
void Crapview::setDbPath( const std::string& path ) noexcept
{
this->dbQuery.setDbPath( path + "/collection.db" );
this->dbQuery.setDbPath( path + "/" + DatabasesNames::data );
}
QString Crapview::getLogFieldString ( const size_t field_id ) const noexcept
{
return TR::tr( this->dbQuery.FIELDS.at( field_id ).c_str() );
}
int Crapview::getLogFieldID ( const QString& field_str ) const noexcept
{
int f{ 0 };
for ( const auto& [id,str] : this->dbQuery.FIELDS ) {
if ( TR::tr(str.c_str()) == field_str ) {
f = id;
break;
}
}
return f;
}
int Crapview::getMonthNumber( const QString& month_str ) const noexcept
{
const auto pos{ std::find_if( this->dbQuery.MONTHS.cbegin(), this->dbQuery.MONTHS.cend(),
@ -143,14 +127,38 @@ QStringList Crapview::getHours() const noexcept
return hours;
}
QStringList Crapview::getFields( const std::string& tab ) const noexcept
QStringList Crapview::getWarnHeaderColumns() const noexcept
{
QStringList list;
const auto& f{ this->fields.at( tab ) };
std::transform( f.cbegin(), f.cend(),
std::back_inserter( list ),
[](const auto& field){ return TR::tr( field.c_str() ); } );
return list;
return {
TR::tr( FIELDS__WARNING.c_str() ),
TR::tr( WORDS__DATE.c_str() ),
TR::tr( WORDS__TIME.c_str() ),
TR::tr( FIELDS__PROTOCOL.c_str() ),
TR::tr( FIELDS__METHOD.c_str() ),
TR::tr( FIELDS__URI.c_str() ),
TR::tr( FIELDS__QUERY.c_str() ),
TR::tr( FIELDS__RESPONSE_CODE.c_str() ),
TR::tr( FIELDS__USER_AGENT.c_str() ),
TR::tr( FIELDS__CLIENT.c_str() ),
TR::tr( FIELDS__COOKIE.c_str() ),
TR::tr( FIELDS__REFERRER.c_str() ),
TR::tr( FIELDS__BYTES_RECEIVED.c_str() ),
TR::tr( FIELDS__BYTES_SENT.c_str() ),
TR::tr( FIELDS__TIME_TAKEN.c_str() )
};
}
QStringList Crapview::getSpeedHeaderColumns() const noexcept
{
return {
TR::tr( FIELDS__TIME_TAKEN.c_str() ),
TR::tr( FIELDS__URI.c_str() ),
TR::tr( FIELDS__QUERY.c_str() ),
TR::tr( FIELDS__METHOD.c_str() ),
TR::tr( FIELDS__PROTOCOL.c_str() ),
TR::tr( FIELDS__RESPONSE_CODE.c_str() ),
TR::tr( WORDS__TIME.c_str() )
};
}
@ -615,7 +623,7 @@ void Crapview::drawCount( QTableWidget* table, QChartView* chart, const QChart::
void Crapview::drawDay( QChartView* chart, const QChart::ChartTheme& theme, const QString web_server, const QString from_year, const QString from_month, const QString from_day, const QString to_year, const QString to_month, const QString to_day, const QString field , const QString filter ) const
void Crapview::drawDay( QChartView* chart, const QChart::ChartTheme& theme, const QString web_server, const QString from_year, const QString from_month, const QString from_day, const QString to_year, const QString to_month, const QString to_day, const QString field_str, const LogField field, const QString filter ) const
{
std::optional<stats_day_items_t> result;
@ -712,7 +720,7 @@ void Crapview::drawDay( QChartView* chart, const QChart::ChartTheme& theme, cons
QChart* b_chart{ new QChart() };
b_chart->setTheme( theme );
b_chart->addSeries( bars );
b_chart->setTitle( QStringLiteral("%1: %2").arg( TR::tr( "Time of Day Count" ), field ) );
b_chart->setTitle( QStringLiteral("%1: %2").arg( TR::tr( "Time of Day Count" ), field_str ) );
b_chart->legend()->setVisible( false );
/*b_chart->legend()->setAlignment( Qt::AlignBottom );
b_chart->setAnimationOptions( QChart::SeriesAnimations );*/
@ -742,7 +750,7 @@ void Crapview::drawDay( QChartView* chart, const QChart::ChartTheme& theme, cons
void Crapview::drawRelat( QChartView* chart, const QChart::ChartTheme& theme, const QString web_server, const QString from_year, const QString from_month, const QString from_day, const QString to_year, const QString to_month, const QString to_day, const QString field_1, const QString filter_1, const QString field_2, const QString filter_2 ) const
void Crapview::drawRelat( QChartView* chart, const QChart::ChartTheme& theme, const QString web_server, const QString from_year, const QString from_month, const QString from_day, const QString to_year, const QString to_month, const QString to_day, const QString field_1_str, const LogField field_1, const QString filter_1, const QString field_2_str, const LogField field_2, const QString filter_2 ) const
{
const bool period{ from_day != to_day || from_month != to_month || from_year != to_year };
@ -822,7 +830,7 @@ void Crapview::drawRelat( QChartView* chart, const QChart::ChartTheme& theme, co
a_chart->setTheme( theme );
a_chart->addSeries( area );
a_chart->addSeries( area_ );
a_chart->setTitle( QStringLiteral("%1: %2 -> %3").arg( TR::tr( "Time of Day Count" ), field_1, field_2) );
a_chart->setTitle( QStringLiteral("%1: %2 -> %3").arg( TR::tr( "Time of Day Count" ), field_1_str, field_2_str) );
a_chart->legend()->setVisible( false );
/*a_chart->legend()->setFont( fonts.at( "main_small" ) );
a_chart->legend()->setAlignment( Qt::AlignBottom );*/

View File

@ -43,22 +43,6 @@ public:
void setDbPath( const std::string& path ) noexcept;
//! Returns the printable log field corresponding to the given ID
/*!
The field gets translated to be printable before being returned
\param field_id The ID of the log fiels
\return The printable field
*/
QString getLogFieldString ( const size_t field_id ) const noexcept;
//! Returns the log field ID corresponding to the given printable field
/*!
\param field_str The log field
\return The ID of the log field
*/
int getLogFieldID ( const QString& field_str ) const noexcept;
//! Returns the month number corresponding to the given printable month
/*!
\param month_Str The printable month name
@ -105,12 +89,19 @@ public:
QStringList getHours() const noexcept;
//! Returns a list of the fields for the given tab
//! Returns a list of the translated columns
/*!
\param tab The stats tab
To be inserted in a header
\return The list of fields
*/
QStringList getFields( const std::string& tab ) const noexcept;
QStringList getWarnHeaderColumns() const noexcept;
//! Returns a list of the translated columns
/*!
To be inserted in a header
\return The list of fields
*/
QStringList getSpeedHeaderColumns() const noexcept;
//! Draws the chart and fills the table for the Warnings stats
@ -198,7 +189,7 @@ public:
const QString web_server,
const QString from_year, const QString from_month, const QString from_day,
const QString to_year, const QString to_month, const QString to_day,
const QString field, const QString filter
const QString field_str, const LogField field, const QString filter
) const;
@ -224,8 +215,8 @@ public:
const QString web_server,
const QString from_year, const QString from_month, const QString from_day,
const QString to_year, const QString to_month, const QString to_day,
const QString field_1, const QString filter_1,
const QString field_2, const QString filter_2
const QString field_1_str, const LogField field_1, const QString filter_1,
const QString field_2_str, const LogField field_2, const QString filter_2
) const;
@ -269,36 +260,11 @@ private:
// { web_server_id : { year : { month : [ days ] } } }
database_dates_t dates;
// collection of available fields, for tabs which needs them
// { tab : [ fields ] }
const std::unordered_map<std::string, std::vector<std::string>> fields{
{"Daytime", {
this->dbQuery.FIELDS.at(10),this->dbQuery.FIELDS.at(11),this->dbQuery.FIELDS.at(12),this->dbQuery.FIELDS.at(13),this->dbQuery.FIELDS.at(14),this->dbQuery.FIELDS.at(18),this->dbQuery.FIELDS.at(22),this->dbQuery.FIELDS.at(21),this->dbQuery.FIELDS.at(20)} },
{"Relational", {
this->dbQuery.FIELDS.at(10),this->dbQuery.FIELDS.at(11),this->dbQuery.FIELDS.at(12),this->dbQuery.FIELDS.at(13),this->dbQuery.FIELDS.at(14),this->dbQuery.FIELDS.at(15),this->dbQuery.FIELDS.at(16),this->dbQuery.FIELDS.at(17),this->dbQuery.FIELDS.at(18),this->dbQuery.FIELDS.at(22),this->dbQuery.FIELDS.at(21),this->dbQuery.FIELDS.at(20)} }
};
// converr Web Servers names to Web Server IDs
const QHash<QString, int> WebServer_s2i{
{"apache",11}, {"nginx",12}, {"iis",13} };
// convert log fields to log fields IDs
const QHash<QString, int> LogFields_s2i{
{QString::fromStdString(this->dbQuery.FIELDS.at( 0)), 0},
{QString::fromStdString(this->dbQuery.FIELDS.at(10)), 10},
{QString::fromStdString(this->dbQuery.FIELDS.at(11)), 11},
{QString::fromStdString(this->dbQuery.FIELDS.at(12)), 12},
{QString::fromStdString(this->dbQuery.FIELDS.at(13)), 13},
{QString::fromStdString(this->dbQuery.FIELDS.at(14)), 14},
{QString::fromStdString(this->dbQuery.FIELDS.at(15)), 15},
{QString::fromStdString(this->dbQuery.FIELDS.at(16)), 16},
{QString::fromStdString(this->dbQuery.FIELDS.at(17)), 17},
{QString::fromStdString(this->dbQuery.FIELDS.at(18)), 18},
{QString::fromStdString(this->dbQuery.FIELDS.at(20)), 20},
{QString::fromStdString(this->dbQuery.FIELDS.at(21)), 21},
{QString::fromStdString(this->dbQuery.FIELDS.at(22)), 22}};
// convert months names to months numbers
const QHash<QString, int> Months_s2i{
{QString::fromStdString(this->dbQuery.MONTHS.at(1)), 1},

View File

@ -1,5 +1,5 @@
#ifndef LOGDOCTOR__CRAPVIEW__FILTERS_H
#define LOGDOCTOR__CRAPVIEW__FILTERS_H
#ifndef LOGDOCTOR__CRAPVIEW__MODULES__FILTERS_H
#define LOGDOCTOR__CRAPVIEW__MODULES__FILTERS_H
#include <optional>
@ -48,4 +48,4 @@ std::optional<QString> parseTextualFilter( const QString& filter_str ) noexcept;
}
#endif // LOGDOCTOR__CRAPVIEW__FILTERS_H
#endif // LOGDOCTOR__CRAPVIEW__MODULES__FILTERS_H

View File

@ -1,5 +1,5 @@
#ifndef LOGDOCTOR__CRAPVIEW__LIB_H
#define LOGDOCTOR__CRAPVIEW__LIB_H
#ifndef LOGDOCTOR__CRAPVIEW__MODULES__LIB_H
#define LOGDOCTOR__CRAPVIEW__MODULES__LIB_H
#include <array>
@ -65,4 +65,4 @@ struct GlobalsData final
Q_DISABLE_COPY(GlobalsData)
};
#endif // LOGDOCTOR__CRAPVIEW__LIB_H
#endif // LOGDOCTOR__CRAPVIEW__MODULES__LIB_H

View File

@ -203,11 +203,16 @@ int DbQuery::countMonths( QStringView from_year, QStringView from_month, QString
}
const QString& DbQuery::getDbField( const LogField fld ) const
{
return this->LogFields_to_DbFields.at( fld );
}
const QString& DbQuery::getDbField( QStringView tr_fld ) const
{
for ( const auto& [id,str] : this->FIELDS ) {
if ( TR::tr(str.c_str()) == tr_fld ) {
return this->LogFields_to_DbFields.at( str );
return this->LogFields_to_DbFields.at( id );
}
}
throw DatabaseException( std::move(QStringLiteral("Unexpected DbField: ").append(tr_fld)) );
@ -603,7 +608,7 @@ void DbQuery::getItemsCount( std::optional<stats_count_items_t>& result, QString
// get and count items with a 10 minutes gap for every hour of the day
void DbQuery::getDaytimeCounts( std::optional<stats_day_items_t>& result, QStringView web_server, QStringView from_year_, QStringView from_month_, QStringView from_day_, QStringView to_year_, QStringView to_month_, QStringView to_day_, QStringView log_field_, QStringView field_filter ) const
void DbQuery::getDaytimeCounts( std::optional<stats_day_items_t>& result, QStringView web_server, QStringView from_year_, QStringView from_month_, QStringView from_day_, QStringView to_year_, QStringView to_month_, QStringView to_day_, const LogField log_field_, QStringView field_filter ) const
{
stats_day_items_t data{ // std::unordered_map<int, std::unordered_map<int, int>>
{0, {{0,0},{10,0},{20,0},{30,0},{40,0},{50,0}}}, {1, {{0,0},{10,0},{20,0},{30,0},{40,0},{50,0}}},
@ -736,7 +741,7 @@ void DbQuery::getDaytimeCounts( std::optional<stats_day_items_t>& result, QStrin
// get and count how many times a specific item value brought to another
void DbQuery::getRelationalCountsDay( std::optional<stats_relat_items_t>& result, QStringView web_server, QStringView year_, QStringView month_, QStringView day_, QStringView log_field_1_, QStringView field_filter_1, QStringView log_field_2_, QStringView field_filter_2 ) const
void DbQuery::getRelationalCountsDay( std::optional<stats_relat_items_t>& result, QStringView web_server, QStringView year_, QStringView month_, QStringView day_, const LogField log_field_1_, QStringView field_filter_1, const LogField log_field_2_, QStringView field_filter_2 ) const
{
stats_relat_items_t data; // std::vector<std::tuple<qint64, int>>
int gap = 20;
@ -881,7 +886,7 @@ void DbQuery::getRelationalCountsDay( std::optional<stats_relat_items_t>& result
void DbQuery::getRelationalCountsPeriod( std::optional<stats_relat_items_t>& result, QStringView web_server, QStringView from_year_, QStringView from_month_, QStringView from_day_, QStringView to_year_, QStringView to_month_, QStringView to_day_, QStringView log_field_1_, QStringView field_filter_1, QStringView log_field_2_, QStringView field_filter_2 ) const
void DbQuery::getRelationalCountsPeriod( std::optional<stats_relat_items_t>& result, QStringView web_server, QStringView from_year_, QStringView from_month_, QStringView from_day_, QStringView to_year_, QStringView to_month_, QStringView to_day_, const LogField log_field_1_, QStringView field_filter_1, const LogField log_field_2_, QStringView field_filter_2 ) const
{
stats_relat_items_t data; // std::vector<std::tuple<qint64, int>>

View File

@ -1,5 +1,5 @@
#ifndef LOGDOCTOR__CRAPVIEW__QUERY_H
#define LOGDOCTOR__CRAPVIEW__QUERY_H
#ifndef LOGDOCTOR__CRAPVIEW__MODULES__QUERY_H
#define LOGDOCTOR__CRAPVIEW__MODULES__QUERY_H
#include "lib.h"
@ -7,6 +7,8 @@
#include "modules/shared.h"
#include "modules/crapview/lib.h"
#include <unordered_map>
#include <optional>
@ -17,14 +19,21 @@ class DbQuery final
public:
// convert log fields IDs to log fields
const std::unordered_map<int, std::string> FIELDS{
{0, FIELDS__WARNING},
{10,FIELDS__PROTOCOL}, {11,FIELDS__METHOD},
{12,FIELDS__URI}, {13,FIELDS__QUERY}, {14,FIELDS__RESPONSE_CODE},
{15,FIELDS__TIME_TAKEN}, {16,FIELDS__BYTES_SENT}, {17,FIELDS__BYTES_RECEIVED},
{18,FIELDS__REFERRER}, {22,FIELDS__COOKIE},
{20,FIELDS__CLIENT}, {21,FIELDS__USER_AGENT} };
// log fields enums to log fields strings
const std::unordered_map<LogField, std::string> FIELDS{
{LogField::Warning, FIELDS__WARNING},
{LogField::Protocol, FIELDS__PROTOCOL},
{LogField::Method, FIELDS__METHOD},
{LogField::Uri, FIELDS__URI},
{LogField::Query, FIELDS__QUERY},
{LogField::ResponseCode, FIELDS__RESPONSE_CODE},
{LogField::TimeTaken, FIELDS__TIME_TAKEN},
{LogField::BytesSent, FIELDS__BYTES_SENT},
{LogField::BytesReceived, FIELDS__BYTES_RECEIVED},
{LogField::Referrer, FIELDS__REFERRER},
{LogField::Cookie, FIELDS__COOKIE},
{LogField::Client, FIELDS__CLIENT},
{LogField::UserAgent, FIELDS__USER_AGENT} };
// convert month numbers to month names
const std::unordered_map<int, std::string> MONTHS{
@ -177,7 +186,7 @@ public:
QStringView web_server,
QStringView from_year_, QStringView from_month_, QStringView from_day_,
QStringView to_year_, QStringView to_month_, QStringView to_day_,
QStringView log_field_, QStringView field_filter
const LogField log_field_, QStringView field_filter
) const;
@ -203,8 +212,8 @@ public:
std::optional<stats_relat_items_t>& result,
QStringView web_server,
QStringView year_, QStringView month_, QStringView day_,
QStringView log_field_1_, QStringView field_filter_1,
QStringView log_field_2_, QStringView field_filter_2
const LogField log_field_1_, QStringView field_filter_1,
const LogField log_field_2_, QStringView field_filter_2
) const;
//! Retrieves the data needed for the Relational statistics
@ -233,8 +242,8 @@ public:
QStringView web_server,
QStringView from_year_, QStringView from_month_, QStringView from_day_,
QStringView to_year_, QStringView to_month_, QStringView to_day_,
QStringView log_field_1_, QStringView field_filter_1,
QStringView log_field_2_, QStringView field_filter_2
const LogField log_field_1_, QStringView field_filter_1,
const LogField log_field_2_, QStringView field_filter_2
) const;
@ -274,22 +283,30 @@ private:
QString db_name;
// convert log fields to database fields
const std::unordered_map<std::string, QString> LogFields_to_DbFields{
{this->FIELDS.at( 0), "warning"},
{this->FIELDS.at(10), "protocol"},
{this->FIELDS.at(11), "method"},
{this->FIELDS.at(12), "uri"},
{this->FIELDS.at(13), "query"},
{this->FIELDS.at(14), "response"},
{this->FIELDS.at(15), "time_taken"},
{this->FIELDS.at(16), "bytes_sent"},
{this->FIELDS.at(17), "bytes_received"},
{this->FIELDS.at(18), "referrer"},
{this->FIELDS.at(20), "client"},
{this->FIELDS.at(21), "user_agent"},
{this->FIELDS.at(22), "cookie"}
const std::unordered_map<LogField, QString> LogFields_to_DbFields{
{LogField::Warning, "warning"},
{LogField::Protocol, "protocol"},
{LogField::Method, "method"},
{LogField::Uri, "uri"},
{LogField::Query, "query"},
{LogField::ResponseCode, "response"},
{LogField::TimeTaken, "time_taken"},
{LogField::BytesSent, "bytes_sent"},
{LogField::BytesReceived, "bytes_received"},
{LogField::Referrer, "referrer"},
{LogField::Client, "client"},
{LogField::UserAgent, "user_agent"},
{LogField::Cookie, "cookie"}
};
//! Returns the database field corresponding to the relative log field
/*!
\param fld The log field
\return The database field
\throw CrapviewException
*/
const QString& getDbField( const LogField fld ) const;
//! Returns the database field corresponding to the relative log field
/*!
\param tr_fld The log field, hendles translated text
@ -359,4 +376,4 @@ private:
};
#endif // LOGDOCTOR__CRAPVIEW__QUERY_H
#endif // LOGDOCTOR__CRAPVIEW__MODULES__QUERY_H