Code improvements

This commit is contained in:
Valentino Orlandi 2022-08-02 21:01:01 +02:00
parent 4eeeb2116e
commit 4c323e0cdc
Signed by: elB4RTO
GPG Key ID: 1719E976DB2D4E71
7 changed files with 16 additions and 32 deletions

View File

@ -81,7 +81,6 @@ bool CheckSec::newStatsDatabase( QSqlDatabase& db, const std::string& db_path, c
} else {
// succesfully creted database file, now create the tables
QSqlQuery query;
int tries;
for ( const QString& ws_name : ws_names ) {
if ( successful == false ) { break; }
// compose the statement with the table name for the access logs
@ -405,7 +404,6 @@ bool CheckSec::newHashesDatabase( QSqlDatabase& db, const std::string& db_path,
} else {
// succesfully creted database file, now create the tables
QSqlQuery query;
int tries;
for ( const QString& ws_name : ws_names ) {
if ( successful == false ) { break; }
// compose the statement with the table name for the access logs

View File

@ -33,13 +33,6 @@ bool HashOps::loadUsedHashesLists( const std::string& db_path )
{
bool successful = true;
const QString db_name = QString::fromStdString( db_path.substr( db_path.find_last_of( '/' ) + 1 ) );
const std::unordered_map<int, QString> ws_names = {
{11, "apache"},
{12, "nginx"},
{13, "iis"} };
const std::unordered_map<int, QString> log_types = {
{1, "access"},
{2, "error"} };
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName( QString::fromStdString( db_path ) );
@ -55,8 +48,8 @@ bool HashOps::loadUsedHashesLists( const std::string& db_path )
} else {
QSqlQuery query = QSqlQuery( db );
for ( const auto& [wid,name] : ws_names ) {
for ( const auto& [tid,type] : log_types ) {
for ( const auto& [wid,name] : this->ws_names ) {
for ( const auto& [tid,type] : this->log_types ) {
if ( query.exec("SELECT hash FROM "+name+"_"+type+";") == false ) {
// error querying database
successful = false;

View File

@ -61,7 +61,7 @@ private:
const FormatOps::LogsFormat& format
);
// temporary vars
int size, lines;
int size=0, lines=0;
};

View File

@ -263,7 +263,6 @@ void DbQuery::updateWarnings( const QString& web_server, const QString& log_type
if ( db.open() == false ) {
// error opening database
successful = false;
QString err_msg = "";
if ( this->dialog_level == 2 ) {
err_msg = db.lastError().text();
@ -364,7 +363,7 @@ void DbQuery::getWarnCounts( std::tuple<bool, std::vector<std::vector<std::vecto
DialogSec::errGeneric( nullptr, QString("%1:\n%2").arg(this->MSG_ERR_UNX_LT).arg( log_type ), true );
}
}
int year, month, day, hour, minute;
int year, month, day, hour;
if ( successful == true ) {
// setup period limits
try {
@ -620,16 +619,16 @@ void DbQuery::getSpeedData(std::tuple<bool, std::vector<std::tuple<long long, st
} else {
try {
// get query data
int hour=-1, aux_hour, prev_hour=0, h,
minute=0, aux_minute, prev_minute=0, m,
second=0, aux_second, prev_second=0, s;
if ( query.size() == 0 ) {
// no result found
;
} else {
// get query data
int hour=-1, aux_hour, prev_hour=0, h,
minute=0, aux_minute, prev_minute=0, m,
second=0, aux_second, prev_second=0, s;
QString tt, ur, qr, mt, pt, rs;
while ( query.next() ) {
aux_hour = query.value(0).toInt();
@ -1293,8 +1292,6 @@ void DbQuery::getRelationalCountsDay(std::tuple<bool, std::vector<std::tuple<lon
QString log_field_1 = this->LogFields_to_DbFields.value( log_field_1_ ),
log_field_2 = this->LogFields_to_DbFields.value( log_field_2_ );
int hour, aux_hour, minute, aux_minute, count;
// 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("'","''") )
@ -1381,6 +1378,7 @@ void DbQuery::getRelationalCountsDay(std::tuple<bool, std::vector<std::tuple<lon
} else {
try {
// get query data
int hour, aux_hour, minute, aux_minute, count;
int gap = 20;
hour = -1;
minute = count = 0;
@ -1557,8 +1555,7 @@ void DbQuery::getRelationalCountsPeriod(std::tuple<bool, std::vector<std::tuple<
QString log_field_1 = this->LogFields_to_DbFields.value( log_field_1_ ),
log_field_2 = this->LogFields_to_DbFields.value( log_field_2_ );
int n_days = 0,
n_months = this->getMonthsCount( from_year, from_month, to_year, to_month );
int n_months = this->getMonthsCount( from_year, from_month, to_year, to_month );
QDateTime time;
int year = from_year,

View File

@ -108,7 +108,6 @@ bool IOutils::renameAsCopy( const std::string& path ) noexcept(true)
const std::vector<std::string> IOutils::readLines(const std::string& path, const int n_lines, const bool random, const bool strip_lines )
{
// read rhe first N lines only
bool result = true;
std::ifstream file;
std::string line;
std::vector<std::string> lines, aux_lines;
@ -175,7 +174,6 @@ const std::vector<std::string> IOutils::readLines(const std::string& path, const
// failed reading
lines.clear();
aux_lines.clear();
result = false;
if ( file.is_open() ) {
file.close();
}
@ -183,7 +181,6 @@ const std::vector<std::string> IOutils::readLines(const std::string& path, const
} catch (...) {
lines.clear();
aux_lines.clear();
result = false;
if ( file.is_open() ) {
file.close();
}
@ -192,6 +189,7 @@ const std::vector<std::string> IOutils::readLines(const std::string& path, const
if ( file.is_open() ) {
file.close();
}
aux_lines.clear();
return lines;
}
@ -203,7 +201,7 @@ const std::string IOutils::readFile( const std::string& path )
std::string content = std::string();
try {
constexpr std::size_t read_size = std::size_t(4096);
std::ifstream file(path);
file = std::ifstream(path);
if ( file.is_open() == false ) {
throw std::ios_base::failure( "file is not open" );
}
@ -219,13 +217,11 @@ const std::string IOutils::readFile( const std::string& path )
std::istreambuf_iterator<char>() );
} catch (const std::ios_base::failure& err) {
// failed reading
content = "";
if ( file.is_open() == true ) {
file.close();
}
throw err;
} catch (...) {
content = "";
if ( file.is_open() == true ) {
file.close();
}

View File

@ -27,7 +27,7 @@ void RichText::enrichLogs( QString &rich_content, const std::string& content, co
QString rich_line="", class_name="";
std::string sep, fld, fld_str, aux_sep1, aux_sep2, aux_fld_str;
bool missing=false;
int start, stop=0, i, aux_start1, aux_start2, aux_stop,
int start, stop, i, aux_start1, aux_start2, aux_stop,
line_size, sep_size,
n_sep = logs_format.separators.size()-1;
std::vector<std::string> lines;

View File

@ -138,7 +138,7 @@ std::string StringOps::lstrip( const std::string& str, const std::string& chars
std::string StringOps::rstrip( const std::string& str, const std::string& chars )
{
bool found = true;
bool found;
int i = str.size() - 1;
while ( i >= 0 ) {
found = false;
@ -233,8 +233,8 @@ void StringOps::split( std::vector<std::string>& list, const std::string& target
void StringOps::splitrip( std::vector<std::string>& list, const std::string& target_str, const std::string& separator, const std::string& strip )
{
std::vector<std::string> aux;
const std::string str = StringOps::strip( target_str, strip );
StringOps::split( aux, str, separator );
const std::string str_ = StringOps::strip( target_str, strip );
StringOps::split( aux, str_, separator );
for ( const std::string& str : aux ) {
if ( str.size() == 0 ) {
continue;