Implemented threading for dynamic performances

Craplog now runs as thread, performance instants are shown meanwhile.
Added functions for printable formatting of the perfs.
This commit is contained in:
Valentino Orlandi 2022-06-30 20:47:05 +02:00
parent 6bedf98781
commit 9fac1187e0
Signed by: elB4RTO
GPG Key ID: 1719E976DB2D4E71
2 changed files with 209 additions and 55 deletions

View File

@ -4,7 +4,10 @@
#include "modules/dialogs.h"
#include "qtimer.h"
#include <iostream>
#include <chrono>
MainWindow::MainWindow(QWidget *parent)
@ -118,7 +121,7 @@ MainWindow::MainWindow(QWidget *parent)
// get a fresh list of LogFiles
this->ui->listLogFiles->header()->resizeSection(0,200);
this->ui->listLogFiles->header()->resizeSection(1,100);
this->on_buttonRefreshList_clicked();
this->on_button_LogFiles_RefreshList_clicked();
}
@ -128,6 +131,97 @@ MainWindow::~MainWindow()
}
/////////////////////
//// GENERAL USE ////
/////////////////////
// printable size with suffix and limited decimals
QString MainWindow::printableSize( int bytes )
{
std::string size_str, size_sfx=" B";
float size = (float)bytes;
if (size > 1024) {
size /= 1024;
size_sfx = " KiB";
if (size > 1024) {
size /= 1024;
size_sfx = " MiB";
}
}
// cut decimals depending on how big the floor is
size_str = std::to_string( size );
int cut_index = size_str.find('.')+1;
if ( cut_index == 0 ) {
cut_index = size_str.find(',')+1;
}
int n_decimals = 3;
if ( size >= 100 ) {
n_decimals = 2;
if ( size >= 1000 ) {
n_decimals = 1;
if ( size >= 10000 ) {
n_decimals = 0;
cut_index --;
}
}
}
if ( cut_index >= 1 ) {
cut_index += n_decimals;
if ( cut_index > size_str.size()-1 ) {
cut_index = size_str.size()-1;
}
}
return QString::fromStdString( size_str.substr(0, cut_index ) + size_sfx );
}
// printable speed with suffix and limited decimals
QString MainWindow::printableSpeed( int bytes, int secs )
{
std::string speed_str, speed_sfx=" B/s";
float size = (float)bytes;
if (size > 1024) {
size /= 1024;
speed_sfx = " KiB/s";
if (size > 1024) {
size /= 1024;
speed_sfx = " MiB/s";
}
}
// cut decimals depending on how big the floor is
speed_str = std::to_string( size / secs );
int cut_index = speed_str.find('.')+1;
if ( cut_index == 0 ) {
cut_index = speed_str.find(',')+1;
}
int n_decimals = 3;
if ( size >= 100 ) {
n_decimals = 2;
if ( size >= 1000 ) {
n_decimals = 1;
if ( size >= 10000 ) {
n_decimals = 0;
cut_index --;
}
}
}
if ( cut_index >= 1 ) {
cut_index += n_decimals;
if ( cut_index > speed_str.size()-1 ) {
cut_index = speed_str.size()-1;
}
}
return QString::fromStdString( speed_str.substr(0, cut_index ) + speed_sfx );
}
QString MainWindow::printableTime( int secs )
{
int mins = secs / 60;
secs = secs - (mins*60);
std::string mins_str = (mins<10) ? "0"+std::to_string(mins) : std::to_string(mins);
std::string secs_str = (secs<10) ? "0"+std::to_string(secs) : std::to_string(secs);
return QString::fromStdString( mins_str +":"+ secs_str );
}
//////////////
//// LOGS ////
//////////////
@ -142,7 +236,7 @@ void MainWindow::on_button_LogFiles_Apache_clicked()
this->ui->button_LogFiles_Iis->setFlat( true );
// load the list
this->craplog.setCurrentWSID( 11 );
this->on_buttonRefreshList_clicked();
this->on_button_LogFiles_RefreshList_clicked();
}
}
// switch to nginx web server
@ -156,7 +250,7 @@ void MainWindow::on_button_LogFiles_Nginx_clicked()
this->ui->button_LogFiles_Iis->setFlat( true );
// load the list
this->craplog.setCurrentWSID( 12 );
this->on_buttonRefreshList_clicked();
this->on_button_LogFiles_RefreshList_clicked();
}
}
// switch to iis web server
@ -170,13 +264,13 @@ void MainWindow::on_button_LogFiles_Iis_clicked()
this->ui->button_LogFiles_Nginx->setFlat( true );
// load the list
this->craplog.setCurrentWSID( 13 );
this->on_buttonRefreshList_clicked();
this->on_button_LogFiles_RefreshList_clicked();
}
}
// refresh the log files list
void MainWindow::on_buttonRefreshList_clicked()
void MainWindow::on_button_LogFiles_RefreshList_clicked()
{
std::string col;
// clear the current tree
@ -191,41 +285,12 @@ void MainWindow::on_buttonRefreshList_clicked()
// already used
if ( this->display_used_files == false ) {
// do not display
delete item; // possible memory leak
continue;
}
item->setForeground( 0, this->COLORS["red"] );
}
item->setText( 0, log_file.name );
// prepare the size of the file
float size = (float)log_file.size / 1024;
std::string sfx = " KiB";
if (size > 1024) {
size /= 1024;
sfx = " MiB";
}
// cut decimals depending on how big the floor is
std::string size_str = std::to_string( size );
int cut_index = size_str.find('.')+1;
if ( cut_index == 0 ) {
cut_index = size_str.find(',')+1;
}
int n_decimals = 3;
if ( size >= 100 ) {
n_decimals = 2;
if ( size >= 1000 ) {
n_decimals = 1;
if ( size >= 10000 ) {
n_decimals = 0;
cut_index --;
}
}
}
if ( cut_index >= 1 ) {
cut_index += n_decimals;
if ( cut_index > size_str.size()-1 ) {
cut_index = size_str.size()-1;
}
}
// apply text and color to the size text
col = "grey";
if ( log_file.size > this->craplog.getWarningSize() ) {
@ -236,7 +301,7 @@ void MainWindow::on_buttonRefreshList_clicked()
}
col = "orange";
}
item->setText( 1, QString::fromStdString( size_str.substr(0,cut_index) + sfx ) );
item->setText( 1, this->printableSize( log_file.size ) );
item->setForeground( 1, this->COLORS[ col ] );
item->setFont( 1, this->FONTS["main_italic"] );
// append the item (on top, forced)
@ -260,11 +325,14 @@ void MainWindow::on_checkBox_LogFiles_CheckAll_stateChanged(int arg1)
if ( this->ui->checkBox_LogFiles_CheckAll->checkState() == Qt::CheckState::Checked ) {
// check all
new_state = Qt::CheckState::Checked;
this->ui->button_MakeStats_Start->setEnabled(true);
} else if ( this->ui->checkBox_LogFiles_CheckAll->checkState() == Qt::CheckState::Unchecked ) {
// un-check all
new_state = Qt::CheckState::Unchecked;
this->ui->button_MakeStats_Start->setEnabled(false);
} else {
// do nothing
this->ui->button_MakeStats_Start->setEnabled(true);
return;
}
QTreeWidgetItemIterator i(this->ui->listLogFiles);
@ -275,7 +343,7 @@ void MainWindow::on_checkBox_LogFiles_CheckAll_stateChanged(int arg1)
}
void MainWindow::on_buttonViewFile_clicked()
void MainWindow::on_button_LogFiles_ViewFile_clicked()
{
if ( this->ui->listLogFiles->selectedItems().size() > 0 ) {
// display the selected item
@ -301,7 +369,7 @@ void MainWindow::on_buttonViewFile_clicked()
void MainWindow::on_listLogFiles_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
this->on_buttonViewFile_clicked();
this->on_button_LogFiles_ViewFile_clicked();
}
@ -326,9 +394,12 @@ void MainWindow::on_listLogFiles_itemChanged(QTreeWidgetItem *item, int column)
}
bool MainWindow::runCraplog()
void MainWindow::on_button_MakeStats_Start_clicked()
{
// feed craplog with the checked values
// take actions on Craplog's start
this->craplogStarted();
// feed craplog with the checked files
bool proceed = true;
QTreeWidgetItemIterator i(this->ui->listLogFiles);
while ( *i ) {
@ -337,18 +408,78 @@ bool MainWindow::runCraplog()
if ( this->craplog.setLogFileSelected( (*i)->text(0) ) == false ) {
// this shouldn't be, but...
if ( Dialogs::choiceSelectedFileNotFound( this, (*i)->text(0) ) == false ) {
proceed = false;
break;
}
}
}
++i;
}
if ( proceed == false ) {
return proceed;
if ( proceed == true ) {
this->craplog_timer = new QTimer(this);
connect(this->craplog_timer, SIGNAL(timeout()), this, SLOT(update_MakeStats_labels()));
this->craplog_timer->start(250);
this->craplog_timer_start = std::chrono::high_resolution_clock::now();
this->craplog.startWorking();
this->craplog_thread = std::thread( &Craplog::run, &this->craplog );
} else {
this->craplogFinished();
}
return proceed;
}
void MainWindow::update_MakeStats_labels()
{
// craplog is running as thread, update the values meanwhile
int size, secs;
// update values
// size and lines
size = this->craplog.getParsedSize();
this->ui->label_MakeStats_Size->setText( this->printableSize( size ) );
this->ui->label_MakeStats_Lines->setText( QString::fromStdString(std::to_string(this->craplog.getParsedLines())) );
// time and speed
this->craplog_timer_lapse = std::chrono::system_clock::now();
this->craplog_timer_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
this->craplog_timer_start - this->craplog_timer_lapse
);
secs = this->craplog_timer_elapsed.count() / -1000000000;
this->ui->label_MakeStats_Time->setText( this->printableTime( secs ));
this->ui->label_MakeStats_Speed->setText( this->printableSpeed( size, secs ));
if ( this->craplog.isWorking() == false ) {
this->craplog_timer->stop();
this->craplog_thread.join();
this->craplogFinished();
}
}
void MainWindow::craplogStarted()
{
// disable the LogFiles section
this->ui->LogBoxFiles->setEnabled(false);
// disable the start button
this->ui->button_MakeStats_Start->setEnabled(false);
// enable all labels (needed only the first time)
this->ui->icon_MakeStats_Size->setEnabled(false);
this->ui->label_MakeStats_Size->setEnabled(true);
this->ui->icon_MakeStats_Lines->setEnabled(false);
this->ui->label_MakeStats_Lines->setEnabled(true);
this->ui->icon_MakeStats_Time->setEnabled(false);
this->ui->label_MakeStats_Time->setEnabled(true);
this->ui->icon_MakeStats_Speed->setEnabled(false);
this->ui->label_MakeStats_Speed->setEnabled(true);
}
void MainWindow::craplogFinished()
{
// refresh the logs list
this->on_button_LogFiles_RefreshList_clicked();
// enable the LogFiles section
this->ui->LogBoxFiles->setEnabled(true);
// enable all labels (needed only the first time)
this->ui->icon_MakeStats_Size->setEnabled(true);
this->ui->icon_MakeStats_Lines->setEnabled(true);
this->ui->icon_MakeStats_Time->setEnabled(true);
this->ui->icon_MakeStats_Speed->setEnabled(true);
}

View File

@ -7,6 +7,8 @@
#include "qtreewidget.h"
#include <thread>
#include "utilities.h"
#include "modules.h"
#include "tools/craplog/craplog.h"
@ -23,12 +25,14 @@ public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
//void operator()( int a );
private slots:
void on_buttonViewFile_clicked();
void on_button_LogFiles_ViewFile_clicked();
void on_checkBox_LogFiles_CheckAll_stateChanged(int arg1);
void on_buttonRefreshList_clicked();
void on_button_LogFiles_RefreshList_clicked();
void on_listLogFiles_itemDoubleClicked(QTreeWidgetItem *item, int column);
@ -40,11 +44,23 @@ private slots:
void on_button_LogFiles_Iis_clicked();
void on_button_MakeStats_Start_clicked();
// custom
void update_MakeStats_labels();
private:
Ui::MainWindow *ui;
Craplog craplog;
bool runCraplog();
std::thread craplog_thread;
QTimer *craplog_timer;
std::chrono::system_clock::time_point
craplog_timer_start,
craplog_timer_lapse;
std::chrono::system_clock::duration craplog_timer_elapsed;
void craplogStarted();
void craplogFinished();
// quantoty of informational dialogs to display
int dialogs_Level = 1; // 0: essential, 1: usefull, 2: explanatory
@ -57,19 +73,26 @@ private:
font_size_small = 10;
// base font families, to build fonts from
QString main_font_family;
QString script_font_family;
QString main_font_family,
script_font_family;
/////////////////////
//// GENERAL USE ////
// get a printable size, from Bytes to the best fit
QString printableSize( int bytes ),
printableSpeed( int bytes, int secs ),
printableTime( int seconds );
//////////////
//// LOGS ////
// web servers related
bool loading_LogsList = false;
std::unordered_map<int, bool> allowed_web_servers;
void disableAllButtons_LogFiles_WS();
void enableAllButtons_LogFiles_WS();
void disableAllButtons_LogFiles_WS(),
enableAllButtons_LogFiles_WS();
// logs list related
bool display_used_files = true;
bool display_warnsize_files = true;
bool display_used_files = true,
display_warnsize_files = true;
// text browser related
TextBrowser TB;