Code improvements

This commit is contained in:
Valentino Orlandi 2022-08-08 22:48:49 +02:00
parent ece3a88104
commit 30a5375d1b
Signed by: elB4RTO
GPG key ID: 1719E976DB2D4E71
4 changed files with 78 additions and 73 deletions

View file

@ -1,6 +1,8 @@
#include "checks.h"
#include "modules/dialogs.h"
#include "utilities/io.h"

View file

@ -1,6 +1,7 @@
#include "store.h"
#include "modules/dialogs.h"
#include "modules/exceptions.h"
#include <QVariant>
@ -80,7 +81,7 @@ bool StoreOps::storeData( QSqlDatabase& db, Craplog& craplog, const std::vector<
if ( check_bl_cli == true ) {
if ( row.find( 20 ) != row.end() ) {
// this row do contains this row item, check if they match
std::string target = row.at( 20 );
const std::string& target = row.at( 20 );
for ( const auto& item : bl_cli_list ) {
if ( StringOps::startsWith( target, item ) ) {
// match found! skip this line
@ -97,7 +98,7 @@ bool StoreOps::storeData( QSqlDatabase& db, Craplog& craplog, const std::vector<
blacklisted_size += str.size();
}
craplog.sumBlacklistededSize( blacklisted_size );
skip=false;
skip = false;
continue;
}
@ -105,11 +106,12 @@ bool StoreOps::storeData( QSqlDatabase& db, Craplog& craplog, const std::vector<
if ( check_wl_cli == true ) {
if ( row.find( 20 ) != row.end() ) {
// this row do contains this row item, check if they match
std::string target = row.at( 20 );
const std::string& target = row.at( 20 );
for ( const auto& item : wl_cli_list ) {
if ( StringOps::startsWith( target, item ) ) {
// match found! put a warning on this line
warning = true;
craplog.sumWarningsSize( item.size() );
break;
}
}
@ -119,11 +121,12 @@ bool StoreOps::storeData( QSqlDatabase& db, Craplog& craplog, const std::vector<
if ( check_wl_ua == true && warning == false ) {
if ( row.find( 21 ) != row.end() ) {
// this row do contains this row item, check if they match
std::string target = row.at( 21 );
const std::string& target = row.at( 21 );
for ( const auto& item : wl_ua_list ) {
if ( StringOps::startsWith( target, item ) ) {
// match found! skip this line
warning = true;
craplog.sumWarningsSize( item.size() );
break;
}
}
@ -133,11 +136,12 @@ bool StoreOps::storeData( QSqlDatabase& db, Craplog& craplog, const std::vector<
if ( check_wl_met == true && warning == false ) {
if ( row.find( 11 ) != row.end() ) {
// this row do contains this row item, check if they match
std::string target = row.at( 11 );
const std::string& target = row.at( 11 );
for ( const auto& item : wl_met_list ) {
if ( item == target ) {
// match found! skip this line
warning = true;
craplog.sumWarningsSize( item.size() );
break;
}
}
@ -147,11 +151,12 @@ bool StoreOps::storeData( QSqlDatabase& db, Craplog& craplog, const std::vector<
if ( check_wl_req == true && warning == false ) {
if ( row.find( 12 ) != row.end() ) {
// this row do contains this row item, check if they match
std::string target = row.at( 12 );
const std::string& target = row.at( 12 );
for ( const auto& item : wl_req_list ) {
if ( StringOps::startsWith( target, item ) ) {
// match found! skip this line
warning = true;
craplog.sumWarningsSize( item.size() );
break;
}
}
@ -174,8 +179,13 @@ bool StoreOps::storeData( QSqlDatabase& db, Craplog& craplog, const std::vector<
query_stmt += "0";
} else {
// value found, bind it
perf_size += row.at( 99 ).size();
query_stmt += QString::fromStdString( row.at( 99 ) ).replace("'","''");
perf_size ++;
if ( warning == false ) {
query_stmt += "0";
} else {
query_stmt += "1";
warning = false;
}
}
// date and time
@ -253,7 +263,7 @@ bool StoreOps::storeData( QSqlDatabase& db, Craplog& craplog, const std::vector<
}
// sum stored data size for the perfs
craplog.sumPerfSize( perf_size );
//craplog.sumPerfSize( perf_size );
// finalize this statement
if ( query.exec() == false ) {

View file

@ -658,10 +658,10 @@ void Crapview::drawDay( QtCharts::QChartView* chart, const QChart::ChartTheme& t
b_30->setLabel( this->printableDate( from_year, this->Months_s2i.value(from_month), from_day ) );
} else {
b_30->setLabel( QString("%1 %2 %3 %4")
.arg( this->LEGEND_FROM )
.arg( this->printableDate( from_year, this->Months_s2i.value(from_month), from_day ))
.arg( this->LEGEND_TO )
.arg( this->printableDate( to_year, this->Months_s2i.value(to_month), to_day )) );
.arg( this->LEGEND_FROM,
this->printableDate( from_year, this->Months_s2i.value(from_month), from_day ),
this->LEGEND_TO,
this->printableDate( to_year, this->Months_s2i.value(to_month), to_day ) ));
}
b_30->setColor( col );
QBarSet *b_40 = new QBarSet( "" );

View file

@ -277,7 +277,7 @@ void DbQuery::updateWarnings( const QString& web_server, const std::vector<std::
} else {
// unexpected WebServer
successful = false;
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg(this->MSG_ERR_UNX_WS).arg( web_server ), true );
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg( this->MSG_ERR_UNX_WS, web_server ), true );
}
if ( successful == true ) {
@ -334,7 +334,7 @@ void DbQuery::getWarnCounts( std::tuple<bool, std::vector<std::vector<std::vecto
} else {
// unexpected WebServer
successful = false;
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg(this->MSG_ERR_UNX_WS).arg( web_server ), true );
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg( this->MSG_ERR_UNX_WS, web_server ), true );
}
int year, month, day, hour;
if ( successful == true ) {
@ -357,9 +357,7 @@ void DbQuery::getWarnCounts( std::tuple<bool, std::vector<std::vector<std::vecto
QSqlQuery query = QSqlQuery( db );
QString stmt = QString("SELECT rowid, * FROM \"%1\" WHERE \"year\"=%2 AND \"month\"=%3 AND \"day\"=%4")
.arg( table )
.arg( year )
.arg( month )
.arg( day );
.arg( year ).arg( month ).arg( day );
if ( hour_.size() == 0 ) {
// entire day
@ -482,7 +480,7 @@ void DbQuery::getSpeedData(std::tuple<bool, std::vector<std::tuple<long long, st
} else {
// unexpected WebServer
successful = false;
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg(this->MSG_ERR_UNX_WS).arg( web_server ), true );
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg( this->MSG_ERR_UNX_WS, web_server ), true );
}
int year, month, day;
if ( successful == true ) {
@ -507,8 +505,7 @@ void DbQuery::getSpeedData(std::tuple<bool, std::vector<std::tuple<long long, st
// prepare the statement
stmt = QString("SELECT \"hour\",\"minute\",\"second\",\"time_taken\",\"uri\",\"query\",\"method\",\"protocol\",\"response\" FROM \"%1\" WHERE \"year\"=%2 AND \"month\"=%3 AND \"day\"=%4 AND \"time_taken\" IS NOT NULL")
.arg( table.replace("'","''") )
.arg( year ).arg( month )
.arg( day );
.arg( year ).arg( month ).arg( day );
// apply a filter if present
if ( protocol_f.size() > 0 ) {
@ -841,18 +838,18 @@ void DbQuery::getItemsCount( std::tuple<bool, std::vector<std::tuple<QString, in
} else {
// unexpected WebServer
successful = false;
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg(this->MSG_ERR_UNX_WS).arg( web_server ), true );
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg( this->MSG_ERR_UNX_WS, web_server ), true );
}
if ( successful == true ) {
// build the query statement
QSqlQuery query = QSqlQuery( db );
QString stmt = QString("SELECT \"%1\" FROM \"%2\" WHERE \"%3\" IS NOT NULL AND \"year\"=%4 AND \"month\"=%5 AND \"day\"=%6;")
.arg( this->LogFields_to_DbFields.value( log_field ))
.arg( table )
.arg( this->LogFields_to_DbFields.value( log_field ))
.arg( year )
.arg( this->Months_s2i.value( month ) )
.arg( day );
.arg( this->LogFields_to_DbFields.value( log_field ),
table,
this->LogFields_to_DbFields.value( log_field ),
year,
QString::fromStdString( std::to_string( this->Months_s2i.value( month ) )),
day );
// quary the database
if ( query.exec( stmt.replace("'","''") ) == false ) {
// error querying database
@ -886,7 +883,7 @@ void DbQuery::getItemsCount( std::tuple<bool, std::vector<std::tuple<QString, in
// sort the list
std::tuple<QString, int> item;
// morph tha HashMap into a Vector of Tuples
for ( const QString& v : aux_items.keys() ) {
foreach ( const QString& v, aux_items.keys() ) {
item = std::make_tuple( v, aux_items.value( v ) );
items.push_back( item );
}
@ -957,7 +954,7 @@ void DbQuery::getDaytimeCounts( std::tuple<bool, std::unordered_map<int, std::un
} else {
// unexpected WebServer
successful = false;
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg(this->MSG_ERR_UNX_WS).arg( web_server ), true );
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg( this->MSG_ERR_UNX_WS, web_server ), true );
}
int from_year, from_month, from_day,
to_year, to_month, to_day;
@ -994,8 +991,7 @@ void DbQuery::getDaytimeCounts( std::tuple<bool, std::unordered_map<int, std::un
// 1 month, no need to loop
stmt = QString("SELECT \"day\", \"hour\", \"minute\" FROM \"%1\" WHERE \"year\"=%2 AND \"month\"=%3 AND \"day\">=%4 AND \"day\"<=%5")
.arg( table.replace("'","''") )
.arg( year )
.arg( month )
.arg( year ).arg( month )
.arg( from_day ).arg( to_day );
// apply a filter if present
@ -1021,13 +1017,13 @@ void DbQuery::getDaytimeCounts( std::tuple<bool, std::unordered_map<int, std::un
filter = QString("=%1").arg( field_filter );
}
stmt += QString(" AND \"%1\"%2")
.arg( log_field.replace("'","''") )
.arg( filter.replace("'","''") );
.arg( log_field.replace("'","''"),
filter.replace("'","''") );
} else {
stmt += QString(" AND \"%1\" LIKE '%2' || '%'")
.arg( log_field.replace("'","''") )
.arg( filter.replace("'","''") );
.arg( log_field.replace("'","''"),
filter.replace("'","''") );
}
}
}
@ -1067,8 +1063,7 @@ void DbQuery::getDaytimeCounts( std::tuple<bool, std::unordered_map<int, std::un
for ( int m=1; m<=n_months; m++ ) {
stmt = QString("SELECT \"day\", \"hour\", \"minute\" FROM \"%1\" WHERE \"year\"=%2 AND \"month\"=%3")
.arg( table )
.arg( year )
.arg( month );
.arg( year ).arg( month );
if ( m == 1 ) {
// first month, only get the day from the beginning day
stmt += QString(" AND \"day\">=%1").arg( from_day );
@ -1101,14 +1096,14 @@ void DbQuery::getDaytimeCounts( std::tuple<bool, std::unordered_map<int, std::un
filter = QString("=%1").arg( field_filter );
}
stmt += QString(" AND \"%1\"%2")
.arg( log_field.replace("'","''") )
.arg( filter.replace("'","''") );
.arg( log_field.replace("'","''"),
filter.replace("'","''") );
} else {
// only values starting-with
stmt += QString(" AND \"%1\" LIKE '%2' || '%'")
.arg( log_field.replace("'","''") )
.arg( filter.replace("'","''") );
.arg( log_field.replace("'","''"),
filter.replace("'","''") );
}
}
}
@ -1203,7 +1198,7 @@ void DbQuery::getRelationalCountsDay(std::tuple<bool, std::vector<std::tuple<lon
} else {
// unexpected WebServer
successful = false;
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg(this->MSG_ERR_UNX_WS).arg( web_server ), true );
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg( this->MSG_ERR_UNX_WS, web_server ), true );
}
int year, month, day;
@ -1231,8 +1226,7 @@ void DbQuery::getRelationalCountsDay(std::tuple<bool, std::vector<std::tuple<lon
// 1 month, no need to loop
stmt = QString("SELECT \"hour\", \"minute\" FROM \"%1\" WHERE \"year\"=%2 AND \"month\"=%3 AND \"day\"=%4")
.arg( table.replace("'","''") )
.arg( year ).arg( month )
.arg( day );
.arg( year ).arg( month ).arg( day );
// apply a filter if present
if ( field_filter_1.size() > 0 ) {
@ -1258,14 +1252,14 @@ void DbQuery::getRelationalCountsDay(std::tuple<bool, std::vector<std::tuple<lon
filter = QString("=%1").arg( field_filter_1 );
}
stmt += QString(" AND \"%1\"%2")
.arg( log_field_1.replace("'","''") )
.arg( filter.replace("'","''") );
.arg( log_field_1.replace("'","''"),
filter.replace("'","''") );
} else {
// only values starting-with
stmt += QString(" AND \"%1\" LIKE '%2' || '%'")
.arg( log_field_1.replace("'","''") )
.arg( filter.replace("'","''") );
.arg( log_field_1.replace("'","''"),
filter.replace("'","''") );
}
}
}
@ -1293,14 +1287,14 @@ void DbQuery::getRelationalCountsDay(std::tuple<bool, std::vector<std::tuple<lon
filter = QString("=%1").arg( field_filter_2 );
}
stmt += QString(" AND \"%1\"%2")
.arg( log_field_2.replace("'","''") )
.arg( filter.replace("'","''") );
.arg( log_field_2.replace("'","''"),
filter.replace("'","''") );
} else {
// only values starting-with
stmt += QString(" AND \"%1\" LIKE '%2' || '%'")
.arg( log_field_2.replace("'","''") )
.arg( QString(field_filter_2).replace("'","''") );
.arg( log_field_2.replace("'","''"),
QString(field_filter_2).replace("'","''") );
}
}
}
@ -1454,10 +1448,10 @@ void DbQuery::getRelationalCountsPeriod(std::tuple<bool, std::vector<std::tuple<
} else {
// unexpected WebServer
successful = false;
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg(this->MSG_ERR_UNX_WS).arg( web_server ), true );
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg( this->MSG_ERR_UNX_WS, web_server ), true );
}
int from_year, from_month, from_day,
to_year, to_month, to_day;
to_year, to_month, to_day;
if ( successful == true ) {
// setup period limits
try {
@ -1510,13 +1504,13 @@ void DbQuery::getRelationalCountsPeriod(std::tuple<bool, std::vector<std::tuple<
filter = QString("=%1").arg( field_filter_1 );
}
stmt += QString(" AND \"%1\"%2")
.arg( log_field_1.replace("'","''") )
.arg( filter.replace("'","''") );
.arg( log_field_1.replace("'","''"),
filter.replace("'","''") );
} else {
stmt += QString(" AND \"%1\" LIKE '%2' || '%'")
.arg( log_field_1.replace("'","''") )
.arg( filter.replace("'","''") );
.arg( log_field_1.replace("'","''"),
filter.replace("'","''") );
}
}
// only select non-NULL values
@ -1535,13 +1529,13 @@ void DbQuery::getRelationalCountsPeriod(std::tuple<bool, std::vector<std::tuple<
filter = QString("=%1").arg( field_filter_2 );
}
stmt += QString(" AND \"%1\"%2")
.arg( log_field_2.replace("'","''") )
.arg( filter.replace("'","''") );
.arg( log_field_2.replace("'","''"),
filter.replace("'","''") );
} else {
stmt += QString(" AND \"%1\" LIKE '%2' || '%'")
.arg( log_field_2.replace("'","''") )
.arg( QString(field_filter_2).replace("'","''") );
.arg( log_field_2.replace("'","''"),
QString(field_filter_2).replace("'","''") );
}
}
@ -1654,8 +1648,7 @@ void DbQuery::getRelationalCountsPeriod(std::tuple<bool, std::vector<std::tuple<
for ( int m=1; m<=n_months; m++ ) {
stmt = QString("SELECT \"day\" FROM \"%1\" WHERE \"year\"=%2 AND \"month\"=%3")
.arg( table.replace("'","''") )
.arg( year )
.arg( month );
.arg( year ).arg( month );
if ( m == 1 ) {
// first month, only get the day from the beginning day
@ -1681,13 +1674,13 @@ void DbQuery::getRelationalCountsPeriod(std::tuple<bool, std::vector<std::tuple<
filter = QString("=%1").arg( field_filter_1 );
}
stmt += QString(" AND \"%1\"%2")
.arg( log_field_1.replace("'","''") )
.arg( filter.replace("'","''") );
.arg( log_field_1.replace("'","''"),
filter.replace("'","''") );
} else {
stmt += QString(" AND \"%1\" LIKE '%2' || '%'")
.arg( log_field_1.replace("'","''") )
.arg( filter.replace("'","''") );
.arg( log_field_1.replace("'","''"),
filter.replace("'","''") );
}
}
// only select non-NULL values
@ -1706,13 +1699,13 @@ void DbQuery::getRelationalCountsPeriod(std::tuple<bool, std::vector<std::tuple<
filter = QString("=%1").arg( field_filter_2 );
}
stmt += QString(" AND \"%1\"%2")
.arg( log_field_2.replace("'","''") )
.arg( filter.replace("'","''") );
.arg( log_field_2.replace("'","''"),
filter.replace("'","''") );
} else {
stmt += QString(" AND \"%1\" LIKE '%2' || '%'")
.arg( log_field_2.replace("'","''") )
.arg( QString(field_filter_2).replace("'","''") );
.arg( log_field_2.replace("'","''"),
QString(field_filter_2).replace("'","''") );
}
}