Code improvements and updates

Improved MainWindow
This commit is contained in:
Valentino Orlandi 2024-02-03 17:51:46 +01:00
parent ae92479fca
commit a1d0419969
Signed by: elB4RTO
GPG key ID: 1719E976DB2D4E71
2 changed files with 116 additions and 115 deletions

View file

@ -36,7 +36,6 @@
#include <QThread> #include <QThread>
#include <QTimer> #include <QTimer>
#include <QTreeWidget> #include <QTreeWidget>
#include <QSqlDatabase>
#include <QFontDatabase> #include <QFontDatabase>
#include <QChartView> #include <QChartView>
@ -291,8 +290,6 @@ void MainWindow::closeEvent( QCloseEvent *event )
if ( this->db_do_backup && this->db_edited ) { if ( this->db_do_backup && this->db_edited ) {
this->backupDatabase(); this->backupDatabase();
} }
QSqlDatabase::removeDatabase( DatabasesNames::data );
QSqlDatabase::removeDatabase( DatabasesNames::hashes );
// save splitters sizes => this->ui->splitter_StatsCount->sizes(); // save splitters sizes => this->ui->splitter_StatsCount->sizes();
} }
@ -1947,7 +1944,7 @@ void MainWindow::makeInitialChecks()
std::error_code err; std::error_code err;
QString err_msg; QString err_msg;
// check that the sqlite plugin is available // check that the sqlite plugin is available
if ( ! QSqlDatabase::drivers().contains("QSQLITE") ) { if ( ! this->dbHandler.checkDriver() ) {
// checks failed, abort // checks failed, abort
DialogSec::errSqlDriverNotFound( "QSQLITE" ); DialogSec::errSqlDriverNotFound( "QSQLITE" );
ok &= false; ok &= false;
@ -2035,8 +2032,6 @@ void MainWindow::makeInitialChecks()
} }
if ( ok ) { if ( ok ) {
QSqlDatabase::addDatabase( "QSQLITE", DatabasesNames::data );
QSqlDatabase::addDatabase( "QSQLITE", DatabasesNames::hashes );
// statistics' database // statistics' database
if ( ! CheckSec::checkCollectionDatabase( this->db_data_path + "/collection.db" ) ) { if ( ! CheckSec::checkCollectionDatabase( this->db_data_path + "/collection.db" ) ) {
// checks failed, abort // checks failed, abort
@ -2050,10 +2045,7 @@ void MainWindow::makeInitialChecks()
ok &= false; ok &= false;
} else { } else {
this->craplog.setHashesDatabasePath( this->db_hashes_path ); this->craplog.setHashesDatabasePath( this->db_hashes_path );
if ( ! this->craplog.hashOps.loadUsedHashesLists( this->db_hashes_path + "/hashes.db" ) ) { this->craplog.hashOps.loadUsedHashesLists( this->db_hashes_path + "/hashes.db" );
// failed to load the list, abort
ok &= false;
}
} }
} }
} }
@ -2106,39 +2098,31 @@ void MainWindow::makeInitialChecks()
bool MainWindow::checkDataDB() bool MainWindow::checkDataDB()
{ {
bool ok{ false };
if ( ! this->initiating ) { // avoid recursions if ( ! this->initiating ) { // avoid recursions
// check the db return false;
const std::string path{ this->db_data_path + "/collection.db" };
ok = IOutils::checkFile( path, true );
if ( ! ok ) {
// database file not found, make a new one
ok = CheckSec::checkCollectionDatabase( path );
// update ui stuff
if ( ! ok ) {
// checks failed
this->crapview.clearDates();
this->ui->box_StatsWarn_Year->clear();
this->ui->box_StatsSpeed_Year->clear();
this->ui->box_StatsCount_Year->clear();
this->ui->box_StatsDay_FromYear->clear();
if ( this->ui->checkBox_StatsDay_Period->isChecked() ) {
this->ui->box_StatsDay_ToYear->clear();
}
this->ui->box_StatsRelat_FromYear->clear();
this->ui->box_StatsRelat_ToYear->clear();
}
}
if ( ok && !this->db_ok ) {
this->db_ok = ok;
this->initiating |= true;
this->refreshStatsDates();
this->initiating &= false;
} else {
this->db_ok = ok;
}
} }
return ok;
const std::string path{ this->db_data_path + "/collection.db" };
if ( ! CheckSec::checkCollectionDatabase( path ) ) {
// db invalid and failed to create a new one or user refused to do so
this->crapview.clearDates();
this->ui->box_StatsWarn_Year->clear();
this->ui->box_StatsSpeed_Year->clear();
this->ui->box_StatsCount_Year->clear();
this->ui->box_StatsDay_FromYear->clear();
if ( this->ui->checkBox_StatsDay_Period->isChecked() ) {
this->ui->box_StatsDay_ToYear->clear();
}
this->ui->box_StatsRelat_FromYear->clear();
this->ui->box_StatsRelat_ToYear->clear();
this->db_ok &= false;
} else if ( ! this->db_ok ) {
// db was invalid but is now fine and ready to use
this->db_ok |= true;
}
return this->db_ok;
} }
@ -2609,8 +2593,13 @@ void MainWindow::setDbWorkingState( const bool working )
bool MainWindow::dbUsable() bool MainWindow::dbUsable()
{ {
if ( !this->db_working ) { if ( ! this->db_working ) {
return this->checkDataDB(); if ( this->db_ok ) {
const std::string path{ this->db_data_path + "/collection.db" };
return IOutils::checkFile( path, true );
} else {
this->checkDataDB(); // db is invalid, attempt to renew
}
} }
return false; return false;
} }
@ -2652,6 +2641,9 @@ void MainWindow::checkMakeStats_Makable()
} }
++i; ++i;
} }
if ( ! state ) {
this->ui->checkBox_LogFiles_CheckAll->setTristate( false );
}
} }
} }
this->ui->button_MakeStats_Start->setEnabled( state ); this->ui->button_MakeStats_Start->setEnabled( state );
@ -2951,61 +2943,55 @@ void MainWindow::on_listLogFiles_itemChanged(QTreeWidgetItem *item, int column)
void MainWindow::on_button_MakeStats_Start_clicked() void MainWindow::on_button_MakeStats_Start_clicked()
{ {
if ( this->dbUsable() ) { if ( !( this->dbUsable() && this->checkDataDB() ) ) {
bool proceed{ true }; return;
// check that the format has been set and is consistent }
proceed = craplog.checkCurrentLogsFormat();
if ( proceed ) { // check that the format has been set and is consistent
// take actions on Craplog's start if ( ! craplog.checkCurrentLogsFormat() ) {
this->craplogStarted(); return;
}
// feed craplog with the checked files // take actions on Craplog's start
QTreeWidgetItemIterator i{ this->ui->listLogFiles }; this->craplogStarted();
while ( *i ) {
if ( (*i)->checkState(0) == Qt::CheckState::Checked ) { // feed craplog with the checked files
// tell Craplog to set this file as selected QTreeWidgetItemIterator i{ this->ui->listLogFiles };
if ( ! this->craplog.setLogFileSelected( (*i)->text(0) ) ) { while ( *i ) {
// failed to retrieve the file. this shouldn't be, but... if ( (*i)->checkState(0) == Qt::CheckState::Checked ) {
const int choice{ DialogSec::choiceSelectedFileNotFound( (*i)->text(0) ) }; // tell Craplog to set this file as selected
if ( choice == 0 ) { if ( ! this->craplog.setLogFileSelected( (*i)->text(0) ) ) {
// choosed to abort all // failed to retrieve the file. this shouldn't be, but...
proceed &= false; const int choice{ DialogSec::choiceSelectedFileNotFound( (*i)->text(0) ) };
break; if ( choice == 0 ) {
} else if ( choice == 1 ) { // choosed to abort all
// choosed to discard the file and continue this->craplogFinished();
; return;
} else { } else if ( choice == 1 ) {
// shouldn't be here // choosed to discard the file and continue
throw GenericException( "Unexpeced value returned: "+std::to_string(choice) ); ;
} } else {
} // shouldn't be here
throw GenericException( "Unexpeced value returned: "+std::to_string(choice) );
} }
++i;
}
if ( proceed ) {
// check files to be used before to start
proceed = this->craplog.checkStuff();
} else {
this->craplogFinished();
}
if ( proceed ) {
// periodically update perfs
this->waiter_timer.reset( new QTimer(this) );
this->waiter_timer->setInterval(250);
this->waiter_timer->setTimerType( Qt::PreciseTimer );
connect( this->waiter_timer.get(), &QTimer::timeout,
this, &MainWindow::updatePerfsLabels );
// start processing
this->waiter_timer_start = std::chrono::system_clock::now();
this->waiter_timer->start();
emit runCraplog();
} else {
this->craplogFinished();
} }
} }
++i;
}
if ( this->craplog.checkStuff() ) {
// periodically update perfs
this->waiter_timer.reset( new QTimer(this) );
this->waiter_timer->setInterval(250);
this->waiter_timer->setTimerType( Qt::PreciseTimer );
connect( this->waiter_timer.get(), &QTimer::timeout,
this, &MainWindow::updatePerfsLabels );
// start processing
this->waiter_timer_start = std::chrono::system_clock::now();
this->waiter_timer->start();
emit runCraplog();
} else {
this->craplogFinished();
} }
} }
@ -3226,7 +3212,7 @@ void MainWindow::on_box_StatsWarn_Hour_currentIndexChanged(int index)
void MainWindow::on_button_StatsWarn_Draw_clicked() void MainWindow::on_button_StatsWarn_Draw_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->setDbWorkingState( true ); this->setDbWorkingState( true );
this->crapview_timer.reset( new QTimer(this) ); this->crapview_timer.reset( new QTimer(this) );
this->crapview_timer->setSingleShot( true ); this->crapview_timer->setSingleShot( true );
@ -3411,7 +3397,7 @@ void MainWindow::on_inLine_StatsSpeed_Response_textChanged(const QString& arg1)
void MainWindow::on_button_StatsSpeed_Draw_clicked() void MainWindow::on_button_StatsSpeed_Draw_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->setDbWorkingState( true ); this->setDbWorkingState( true );
this->crapview_timer.reset( new QTimer(this) ); this->crapview_timer.reset( new QTimer(this) );
this->crapview_timer->setSingleShot( true ); this->crapview_timer->setSingleShot( true );
@ -3576,7 +3562,7 @@ void MainWindow::makeStatsCount()
void MainWindow::on_button_StatsCount_Protocol_clicked() void MainWindow::on_button_StatsCount_Protocol_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->resetStatsCountButtons(); this->resetStatsCountButtons();
this->ui->button_StatsCount_Protocol->setFlat( false ); this->ui->button_StatsCount_Protocol->setFlat( false );
this->count_fld = this->ui->button_StatsCount_Protocol->text(); this->count_fld = this->ui->button_StatsCount_Protocol->text();
@ -3586,7 +3572,7 @@ void MainWindow::on_button_StatsCount_Protocol_clicked()
void MainWindow::on_button_StatsCount_Method_clicked() void MainWindow::on_button_StatsCount_Method_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->resetStatsCountButtons(); this->resetStatsCountButtons();
this->count_fld = this->ui->button_StatsCount_Method->text(); this->count_fld = this->ui->button_StatsCount_Method->text();
this->ui->button_StatsCount_Method->setFlat( false ); this->ui->button_StatsCount_Method->setFlat( false );
@ -3596,7 +3582,7 @@ void MainWindow::on_button_StatsCount_Method_clicked()
void MainWindow::on_button_StatsCount_Uri_clicked() void MainWindow::on_button_StatsCount_Uri_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->resetStatsCountButtons(); this->resetStatsCountButtons();
this->count_fld = this->ui->button_StatsCount_Uri->text(); this->count_fld = this->ui->button_StatsCount_Uri->text();
this->ui->button_StatsCount_Uri->setFlat( false ); this->ui->button_StatsCount_Uri->setFlat( false );
@ -3606,7 +3592,7 @@ void MainWindow::on_button_StatsCount_Uri_clicked()
void MainWindow::on_button_StatsCount_Query_clicked() void MainWindow::on_button_StatsCount_Query_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->resetStatsCountButtons(); this->resetStatsCountButtons();
this->count_fld = this->ui->button_StatsCount_Query->text(); this->count_fld = this->ui->button_StatsCount_Query->text();
this->ui->button_StatsCount_Query->setFlat( false ); this->ui->button_StatsCount_Query->setFlat( false );
@ -3616,7 +3602,7 @@ void MainWindow::on_button_StatsCount_Query_clicked()
void MainWindow::on_button_StatsCount_Response_clicked() void MainWindow::on_button_StatsCount_Response_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->resetStatsCountButtons(); this->resetStatsCountButtons();
this->count_fld = this->ui->button_StatsCount_Response->text(); this->count_fld = this->ui->button_StatsCount_Response->text();
this->ui->button_StatsCount_Response->setFlat( false ); this->ui->button_StatsCount_Response->setFlat( false );
@ -3626,7 +3612,7 @@ void MainWindow::on_button_StatsCount_Response_clicked()
void MainWindow::on_button_StatsCount_Referrer_clicked() void MainWindow::on_button_StatsCount_Referrer_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->resetStatsCountButtons(); this->resetStatsCountButtons();
this->count_fld = this->ui->button_StatsCount_Referrer->text(); this->count_fld = this->ui->button_StatsCount_Referrer->text();
this->ui->button_StatsCount_Referrer->setFlat( false ); this->ui->button_StatsCount_Referrer->setFlat( false );
@ -3636,7 +3622,7 @@ void MainWindow::on_button_StatsCount_Referrer_clicked()
void MainWindow::on_button_StatsCount_Cookie_clicked() void MainWindow::on_button_StatsCount_Cookie_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->resetStatsCountButtons(); this->resetStatsCountButtons();
this->count_fld = this->ui->button_StatsCount_Cookie->text(); this->count_fld = this->ui->button_StatsCount_Cookie->text();
this->ui->button_StatsCount_Cookie->setFlat( false ); this->ui->button_StatsCount_Cookie->setFlat( false );
@ -3646,7 +3632,7 @@ void MainWindow::on_button_StatsCount_Cookie_clicked()
void MainWindow::on_button_StatsCount_UserAgent_clicked() void MainWindow::on_button_StatsCount_UserAgent_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->resetStatsCountButtons(); this->resetStatsCountButtons();
this->count_fld = this->ui->button_StatsCount_UserAgent->text(); this->count_fld = this->ui->button_StatsCount_UserAgent->text();
this->ui->button_StatsCount_UserAgent->setFlat( false ); this->ui->button_StatsCount_UserAgent->setFlat( false );
@ -3656,7 +3642,7 @@ void MainWindow::on_button_StatsCount_UserAgent_clicked()
void MainWindow::on_button_StatsCount_Client_clicked() void MainWindow::on_button_StatsCount_Client_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->resetStatsCountButtons(); this->resetStatsCountButtons();
this->count_fld = this->ui->button_StatsCount_Client->text(); this->count_fld = this->ui->button_StatsCount_Client->text();
this->ui->button_StatsCount_Client->setFlat( false ); this->ui->button_StatsCount_Client->setFlat( false );
@ -3941,7 +3927,7 @@ void MainWindow::on_inLine_StatsDay_Filter_textChanged(const QString& arg1)
void MainWindow::on_button_StatsDay_Draw_clicked() void MainWindow::on_button_StatsDay_Draw_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->setDbWorkingState( true ); this->setDbWorkingState( true );
this->crapview_timer.reset( new QTimer(this) ); this->crapview_timer.reset( new QTimer(this) );
this->crapview_timer->setSingleShot( true ); this->crapview_timer->setSingleShot( true );
@ -4227,7 +4213,7 @@ void MainWindow::on_inLine_StatsRelat_Filter_2_textChanged(const QString &arg1)
void MainWindow::on_button_StatsRelat_Draw_clicked() void MainWindow::on_button_StatsRelat_Draw_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->setDbWorkingState( true ); this->setDbWorkingState( true );
this->crapview_timer.reset( new QTimer(this) ); this->crapview_timer.reset( new QTimer(this) );
this->crapview_timer->setSingleShot( true ); this->crapview_timer->setSingleShot( true );
@ -4380,7 +4366,7 @@ void MainWindow::makeStatsGlob()
void MainWindow::on_button_StatsGlob_Apache_clicked() void MainWindow::on_button_StatsGlob_Apache_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->glob_ws = "apache"; this->glob_ws = "apache";
this->makeStatsGlob(); this->makeStatsGlob();
} }
@ -4389,7 +4375,7 @@ void MainWindow::on_button_StatsGlob_Apache_clicked()
void MainWindow::on_button_StatsGlob_Nginx_clicked() void MainWindow::on_button_StatsGlob_Nginx_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->glob_ws = "nginx"; this->glob_ws = "nginx";
this->makeStatsGlob(); this->makeStatsGlob();
} }
@ -4398,7 +4384,7 @@ void MainWindow::on_button_StatsGlob_Nginx_clicked()
void MainWindow::on_button_StatsGlob_Iis_clicked() void MainWindow::on_button_StatsGlob_Iis_clicked()
{ {
if ( this->dbUsable() ) { if ( this->dbUsable() && this->checkDataDB() ) {
this->glob_ws = "iis"; this->glob_ws = "iis";
this->makeStatsGlob(); this->makeStatsGlob();
} }

View file

@ -8,9 +8,12 @@
#include "modules/tb.h" #include "modules/tb.h"
#include "modules/database/database.h"
#include "modules/craplog/craplog.h" #include "modules/craplog/craplog.h"
#include "modules/crapview/crapview.h" #include "modules/crapview/crapview.h"
class Craphelp; class Craphelp;
class Crapup; class Crapup;
class Crapinfo; class Crapinfo;
@ -728,23 +731,23 @@ private:
bool initiating{ true }; bool initiating{ true };
bool db_ok{ true };
//! Makes the initial integrity checks //! Makes the initial integrity checks
void makeInitialChecks(); void makeInitialChecks();
//! Checks the integrity of the logs data collection database
bool checkDataDB();
/////////////////// ///////////////////
//// DATABASES //// //// DATABASES ////
/////////////////// ///////////////////
DatabaseHandler dbHandler;
// true if a process edited the database and so a backup is required
bool db_edited{ false }; bool db_edited{ false };
// user-defined configuration: whether to make backups or not
bool db_do_backup{ true }; bool db_do_backup{ true };
// user-defined configuration: maximum number of backups to keep
unsigned db_backups_number{ 3 }; unsigned db_backups_number{ 3 };
//! Backs-up the logs data collection database //! Backs-up the logs data collection database
@ -753,14 +756,26 @@ private:
std::string db_data_path; std::string db_data_path;
std::string db_hashes_path; std::string db_hashes_path;
// actions when working on a db // true when a process is working on a db
bool db_working{ false }; bool db_working{ false };
//! Called when a member begins/ends performing operations on the database //! Called when a member begins/ends performing operations on the database
void setDbWorkingState( const bool working ); void setDbWorkingState( const bool working );
//! Weak check on the logs data collection database
/*!
Returns true if the database is free (no other process is running
that may access it for read/write operations) and it exists
\see checkDataDB
*/
bool dbUsable(); bool dbUsable();
// false if the logs data collection database is invalid
bool db_ok{ true };
//! Checks the integrity of the logs data collection database
bool checkDataDB();
////////////////// //////////////////
//// CRAPTABS //// //// CRAPTABS ////