Improvements and updates

Code improvements.
Replaced plain data types with Result's typedefs.
This commit is contained in:
Valentino Orlandi 2023-01-22 20:58:38 +01:00
parent 1e8d4a9bbc
commit d3e261cbb5
Signed by: elB4RTO
GPG Key ID: 1719E976DB2D4E71
4 changed files with 79 additions and 84 deletions

View File

@ -174,10 +174,13 @@ const QString Crapview::parseTextualFilter( const QString& filter_str )
void Crapview::refreshDates()
{
std::tuple<bool, std::unordered_map<int, std::unordered_map<int, std::unordered_map<int, std::vector<int>>>>> result;
Result<stats_dates_t> result;
this->dbQuery.refreshDates( result );
if ( std::get<0>(result) ) {
this->dates = std::get<1>(result);
if ( result ) {
this->dates.clear();
// std::unordered_map<int, std::unordered_map<int, std::unordered_map<int, std::vector<int>>>>
// { web_server_id : { year : { month : [ days ] } } }
this->dates = std::move( result.getData() );
}
}
void Crapview::clearDates()
@ -302,15 +305,16 @@ void Crapview::updateWarn( QTableWidget* table , const QString& web_server )
void Crapview::drawWarn( QTableWidget* table, QtCharts::QChartView* chart, const QChart::ChartTheme& theme, const std::unordered_map<std::string, QFont>& fonts, const QString& web_server, const QString& year, const QString& month, const QString& day, const QString& hour )
{
std::tuple<bool, std::vector<std::vector<std::vector<std::vector<QString>>>>> result;
Result<stats_warn_items_t> result;
this->dbQuery.getWarnCounts(
result,
web_server,
year, month, day, hour );
if ( std::get<0>(result) ) {
// get data
// { hour : { 10th_minutes : count } }
std::vector<std::vector<std::vector<std::vector<QString>>>> &items = std::get<1>(result);
if ( result ) {
// std::vector<std::vector<std::vector<std::vector<QString>>>>
// day -> [ hours[ 10th_minutes[ lines[ log_data ] ] ] ]
// hour -> [ 10th_minutes[ minute[ lines[ log_data ] ] ] ]
auto& items = result.getData();
// bars
std::vector<std::vector<QBarSet*>> sets;
@ -470,8 +474,6 @@ void Crapview::drawWarn( QTableWidget* table, QtCharts::QChartView* chart, const
// apply the chart to the view
chart->setChart( b_chart );
chart->setRenderHint( QPainter::Antialiasing );
items.clear();
}
}
@ -479,16 +481,16 @@ void Crapview::drawWarn( QTableWidget* table, QtCharts::QChartView* chart, const
void Crapview::drawSpeed( QTableWidget* table, QtCharts::QChartView* chart, const QChart::ChartTheme& theme, const std::unordered_map<std::string, QFont>& fonts, const QString& web_server, const QString& year, const QString& month, const QString& day, const QString& protocol, const QString& method, const QString& uri, const QString& query, const QString& response )
{
std::tuple<bool, std::vector<std::tuple<long long, std::vector<QString>>>> result;
Result<stats_speed_items_t> result;
this->dbQuery.getSpeedData(
result,
web_server,
year, month, day,
protocol, method, uri, query, response );
if ( std::get<0>(result) ) {
// get data
// { hour : { 10th_minutes : count } }
std::vector<std::tuple<long long, std::vector<QString>>> &items = std::get<1>(result);
if ( result ) {
// std::vector<std::tuple<long long, std::vector<QString>>>
// [ ( epoch_msec, [ log_data ] ) ]
auto& items = result.getData();
// draw the speed chart
QLineSeries *line = new QLineSeries();
@ -619,8 +621,6 @@ void Crapview::drawSpeed( QTableWidget* table, QtCharts::QChartView* chart, cons
// add the chart to the view
chart->setChart( l_chart );
chart->setRenderHint(QPainter::Antialiasing);
items.clear();
}
}
@ -628,15 +628,16 @@ void Crapview::drawSpeed( QTableWidget* table, QtCharts::QChartView* chart, cons
void Crapview::drawCount( QTableWidget* table, QtCharts::QChartView* chart, const QChart::ChartTheme& theme, const std::unordered_map<std::string, QFont>& fonts, const QString& web_server, const QString& year, const QString& month, const QString& day, const QString& field )
{
std::tuple<bool, std::vector<std::tuple<QString, int>>> result;
Result<stats_count_items_t> result;
this->dbQuery.getItemsCount(
result,
web_server,
year, month, day,
field );
if ( std::get<0>(result) ) {
// get data
std::vector<std::tuple<QString, int>> &aux_items = std::get<1>(result);
if ( result ) {
// std::vector<std::tuple<QString, int>>
// [ ( log_item, count ) ]
auto& items = result.getData();
// make the pie
QPieSeries *pie = new QPieSeries();
@ -644,9 +645,9 @@ void Crapview::drawCount( QTableWidget* table, QtCharts::QChartView* chart, cons
const int max_items=15;
int aux, count, oth_count=0;
QString item;
for ( int i=0; i<aux_items.size(); i++ ) {
item = std::get<0>( aux_items.at(i) );
count = std::get<1>( aux_items.at(i) );
for ( int i=0; i<items.size(); i++ ) {
item = std::get<0>( items.at(i) );
count = std::get<1>( items.at(i) );
if ( i >= max_items ) {
oth_count += count;
} else {
@ -655,10 +656,9 @@ void Crapview::drawCount( QTableWidget* table, QtCharts::QChartView* chart, cons
aux = table->rowCount();
table->insertRow( aux );
table->setItem( aux, 0, new QTableWidgetItem( QString::fromStdString( std::to_string(count) )));
table->setItem( aux, 1, new QTableWidgetItem( std::get<0>( aux_items.at(i) ) ));
table->setItem( aux, 1, new QTableWidgetItem( std::get<0>( items.at(i) ) ));
}
table->verticalHeader()->setVisible( false );
aux_items.clear();
if ( oth_count > 0 ) {
pie->append( TR::tr( "Others" ), oth_count );
@ -683,17 +683,17 @@ void Crapview::drawCount( QTableWidget* table, QtCharts::QChartView* chart, cons
void Crapview::drawDay( QtCharts::QChartView* chart, const QChart::ChartTheme& theme, const std::unordered_map<std::string, QFont>& fonts, 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 )
{
std::tuple<bool, std::unordered_map<int, std::unordered_map<int, int>>> result;
Result<stats_day_items_t> result;
this->dbQuery.getDaytimeCounts(
result,
web_server,
from_year, from_month, from_day,
to_year, to_month, to_day,
field, filter );
if ( std::get<0>(result) ) {
// get data
if ( result ) {
// std::unordered_map<int, std::unordered_map<int, int>>
// { hour : { 10th_minutes : count } }
std::unordered_map<int, std::unordered_map<int, int>> &items = std::get<1>(result);
auto& items = result.getData();
// draw the chart
QString date;
@ -799,8 +799,6 @@ void Crapview::drawDay( QtCharts::QChartView* chart, const QChart::ChartTheme& t
// apply the chart to the view
chart->setChart( b_chart );
chart->setRenderHint( QPainter::Antialiasing );
items.clear();
}
}
@ -809,7 +807,7 @@ void Crapview::drawDay( QtCharts::QChartView* chart, const QChart::ChartTheme& t
void Crapview::drawRelat( QtCharts::QChartView* chart, const QChart::ChartTheme& theme, const std::unordered_map<std::string, QFont>& fonts, 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 )
{
bool period = true;
std::tuple<bool, std::vector<std::tuple<long long, int>>> result;
Result<stats_relat_items_t> result;
if ( from_year == to_year
&& from_month == to_month
&& from_day == to_day ) {
@ -830,10 +828,10 @@ void Crapview::drawRelat( QtCharts::QChartView* chart, const QChart::ChartTheme&
field_2, filter_2 );
}
if ( std::get<0>(result) ) {
// get data
// { hour : { 10th_minutes : count } }
std::vector<std::tuple<long long, int>> &items = std::get<1>(result);
if ( result ) {
// std::vector<std::tuple<long long, int>>
// [ ( epoch_ms, count ) ]
auto& items = result.getData();
// draw the relational chart
QLineSeries *line = new QLineSeries();
@ -924,8 +922,6 @@ void Crapview::drawRelat( QtCharts::QChartView* chart, const QChart::ChartTheme&
// add the chart to the view
chart->setChart( a_chart );
chart->setRenderHint(QPainter::Antialiasing);
items.clear();
}
}
@ -1085,10 +1081,6 @@ const bool Crapview::calcGlobals( std::vector<std::tuple<QString,QString>>& recu
work_list.push_back( QString("%1.%2 %3").arg(f).arg(d).arg(sfx) );
}
}
recurs.clear();
traf_day.clear(); traf_hour.clear();
perf_time.clear(); perf_sent.clear(); perf_receiv.clear();
}
return result;

View File

@ -40,21 +40,21 @@ public:
//! Parses a filter for a database field with boolean type
/*!
\param field_str The given filter
\return The resulting filter to apply at the query
\return The resulting filter to apply to the query
*/
const QString parseBooleanFilter( const QString& filter_str );
//! Parses a filter for a log field with integer type
/*!
\param field_str The given filter
\return The resulting filter to apply at the query
\return The resulting filter to apply to the query
*/
const QString parseNumericFilter( const QString& filter_str );
//! Parses a filter for a log field with text type
/*!
\param field_str The given filter
\return The resulting filter to apply at the query
\return The resulting filter to apply to the query
*/
const QString parseTextualFilter( const QString& filter_str );
@ -286,8 +286,9 @@ private:
DbQuery dbQuery;
// collection of available dates
// { web_server_id : { year : { month_str : [ days ] } } }
std::unordered_map<int, std::unordered_map<int, std::unordered_map<int, std::vector<int>>>> dates;
// db_dates_t = std::unordered_map<int, std::unordered_map<int, std::unordered_map<int, std::vector<int>>>>
// { web_server_id : { year : { month : [ days ] } } }
stats_dates_t dates;
// collection of available fields, for tabs which needs them
// { tab : [ fields ] }
@ -342,7 +343,7 @@ private:
// 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( 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},

View File

@ -148,11 +148,12 @@ const QString DbQuery::getDbField( const QString& tr_fld )
// get a fresh map of available dates
void DbQuery::refreshDates(std::tuple<bool, std::unordered_map<int, std::unordered_map<int, std::unordered_map<int, std::vector<int>>>>> &result)
void DbQuery::refreshDates( Result<stats_dates_t>& result )
{
bool successful = true;
std::unordered_map<int, std::unordered_map<int, std::unordered_map<int, std::vector<int>>>> dates = {
{11, {}}, {12, {}}, {13, {}} };
stats_dates_t dates = { // std::unordered_map<int, std::unordered_map<int, std::unordered_map<int, std::vector<int>>>>
{11, {}}, {12, {}}, {13, {}}
};
QSqlDatabase db;
if ( QSqlDatabase::contains("qt_sql_default_connection") ) {
@ -290,7 +291,7 @@ void DbQuery::refreshDates(std::tuple<bool, std::unordered_map<int, std::unorder
if ( db.isOpen() ) {
db.close();
}
result = std::make_tuple( successful, dates );
result = Result( successful, dates );
}
@ -348,10 +349,10 @@ void DbQuery::updateWarnings( const QString& web_server, const std::vector<std::
// get daytime values for the warnings
void DbQuery::getWarnCounts( std::tuple<bool, std::vector<std::vector<std::vector<std::vector<QString>>>>> &result, const QString& web_server, const QString& year_, const QString& month_, const QString& day_, const QString& hour_ )
void DbQuery::getWarnCounts( Result<stats_warn_items_t>& result, const QString& web_server, const QString& year_, const QString& month_, const QString& day_, const QString& hour_ )
{
bool successful = true;
std::vector<std::vector<std::vector<std::vector<QString>>>> items;
stats_warn_items_t items; // std::vector<std::vector<std::vector<std::vector<QString>>>>
QSqlDatabase db;
if ( QSqlDatabase::contains("qt_sql_default_connection") ) {
@ -487,15 +488,15 @@ void DbQuery::getWarnCounts( std::tuple<bool, std::vector<std::vector<std::vecto
if ( db.isOpen() ) {
db.close();
}
result = std::make_tuple( successful, items );
result = Result( successful, items );
}
// get day-time values for the time-taken field
void DbQuery::getSpeedData(std::tuple<bool, std::vector<std::tuple<long long, std::vector<QString>>>>& result, const QString& web_server, const QString& year_, const QString& month_, const QString& day_, const QString& protocol_f, const QString& method_f, const QString& uri_f, const QString& query_f, const QString& response_f )
void DbQuery::getSpeedData( Result<stats_speed_items_t>& result, const QString& web_server, const QString& year_, const QString& month_, const QString& day_, const QString& protocol_f, const QString& method_f, const QString& uri_f, const QString& query_f, const QString& response_f )
{
bool successful = true;
std::vector<std::tuple<long long, std::vector<QString>>> data;
stats_speed_items_t data; // std::vector<std::tuple<long long, std::vector<QString>>>
QSqlDatabase db;
if ( QSqlDatabase::contains("qt_sql_default_connection") ) {
@ -764,12 +765,12 @@ void DbQuery::getSpeedData(std::tuple<bool, std::vector<std::tuple<long long, st
prev_second = second;
second = aux_second;
}
tt = query.value(3).toString();
ur = query.value(4).toString();
qr = query.value(5).toString();
mt = query.value(6).toString();
pt = query.value(7).toString();
rs = query.value(8).toString();
tt = query.value(3).toString(); // time taken
ur = query.value(4).toString(); // uri
qr = query.value(5).toString(); // query
mt = query.value(6).toString(); // method
pt = query.value(7).toString(); // protocol
rs = query.value(8).toString(); // response
}
// last one, append the prev
h=hour; m=minute; s=second-1;
@ -841,17 +842,17 @@ void DbQuery::getSpeedData(std::tuple<bool, std::vector<std::tuple<long long, st
if ( db.isOpen() ) {
db.close();
}
result = std::make_tuple( successful, data );
result = Result( successful, data );
}
// get, group and count identical items of a specific field in a date
void DbQuery::getItemsCount( std::tuple<bool, std::vector<std::tuple<QString, int>>>& result, const QString& web_server, const QString& year, const QString& month, const QString& day, const QString& log_field )
void DbQuery::getItemsCount( Result<stats_count_items_t>& result, const QString& web_server, const QString& year, const QString& month, const QString& day, const QString& log_field )
{
bool successful = true;
QHash<QString, int> aux_items;
std::vector<std::tuple<QString, int>> items;
stats_count_items_t items; // std::vector<std::tuple<QString, int>>
QSqlDatabase db;
if ( QSqlDatabase::contains("qt_sql_default_connection") ) {
@ -943,16 +944,16 @@ void DbQuery::getItemsCount( std::tuple<bool, std::vector<std::tuple<QString, in
if ( db.isOpen() ) {
db.close();
}
result = std::make_tuple( successful, items );
result = Result( successful, items );
}
// get and count items with a 10 minutes gap for every hour of the day
void DbQuery::getDaytimeCounts( std::tuple<bool, std::unordered_map<int, std::unordered_map<int, int>>>& result, 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& log_field_, const QString& field_filter )
void DbQuery::getDaytimeCounts( Result<stats_day_items_t>& result, 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& log_field_, const QString& field_filter )
{
bool successful = true;
std::unordered_map<int, std::unordered_map<int, int>> data = {
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}}},
{2, {{0,0},{10,0},{20,0},{30,0},{40,0},{50,0}}}, {3, {{0,0},{10,0},{20,0},{30,0},{40,0},{50,0}}},
{4, {{0,0},{10,0},{20,0},{30,0},{40,0},{50,0}}}, {5, {{0,0},{10,0},{20,0},{30,0},{40,0},{50,0}}},
@ -1199,16 +1200,16 @@ void DbQuery::getDaytimeCounts( std::tuple<bool, std::unordered_map<int, std::un
if ( db.isOpen() ) {
db.close();
}
result = std::make_tuple( successful, data );
result = Result( successful, data );
}
// get and count how many times a specific item value brought to another
void DbQuery::getRelationalCountsDay(std::tuple<bool, std::vector<std::tuple<long long, int>>> &result, const QString& web_server, const QString& year_, const QString& month_, const QString& day_, const QString& log_field_1_, const QString& field_filter_1, const QString& log_field_2_, const QString& field_filter_2 )
void DbQuery::getRelationalCountsDay( Result<stats_relat_items_t>& result, const QString& web_server, const QString& year_, const QString& month_, const QString& day_, const QString& log_field_1_, const QString& field_filter_1, const QString& log_field_2_, const QString& field_filter_2 )
{
bool successful = true;
std::vector<std::tuple<long long, int>> data;
stats_relat_items_t data; // std::vector<std::tuple<long long, int>>
QSqlDatabase db;
if ( QSqlDatabase::contains("qt_sql_default_connection") ) {
@ -1448,15 +1449,15 @@ void DbQuery::getRelationalCountsDay(std::tuple<bool, std::vector<std::tuple<lon
if ( db.isOpen() ) {
db.close();
}
result = std::make_tuple( successful, data );
result = Result( successful, data );
}
void DbQuery::getRelationalCountsPeriod(std::tuple<bool, std::vector<std::tuple<long long, int>>> &result, 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& log_field_1_, const QString& field_filter_1, const QString& log_field_2_, const QString& field_filter_2 )
void DbQuery::getRelationalCountsPeriod( Result<stats_relat_items_t>& result, 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& log_field_1_, const QString& field_filter_1, const QString& log_field_2_, const QString& field_filter_2 )
{
bool successful = true;
std::vector<std::tuple<long long, int>> data;
stats_relat_items_t data; // std::vector<std::tuple<long long, int>>
QSqlDatabase db;
if ( QSqlDatabase::contains("qt_sql_default_connection") ) {
@ -1839,7 +1840,7 @@ void DbQuery::getRelationalCountsPeriod(std::tuple<bool, std::vector<std::tuple<
if ( db.isOpen() ) {
db.close();
}
result = std::make_tuple( successful, data );
result = Result( successful, data );
}

View File

@ -2,6 +2,7 @@
#define QUERY_H
#include "modules/shared.h"
#include "utilities/result.h"
#include <QString>
@ -70,7 +71,7 @@ public:
/*!
\param result Tuple which will hold the result of the operation and the data
*/
void refreshDates( std::tuple<bool, std::unordered_map<int, std::unordered_map<int, std::unordered_map<int, std::vector<int>>>>>& result );
void refreshDates( Result<stats_dates_t>& result );
//! Updates the database applying the changes made in the Warnings statistics table
@ -92,7 +93,7 @@ public:
\param hour_ The hour
*/
void getWarnCounts(
std::tuple<bool, std::vector<std::vector<std::vector<std::vector<QString>>>>>& result,
Result<stats_warn_items_t>& result,
const QString& web_server,
const QString& year_,
const QString& month_,
@ -114,7 +115,7 @@ public:
\param response_f The filter for the Response field
*/
void getSpeedData(
std::tuple<bool, std::vector<std::tuple<long long, std::vector<QString>>>>& result,
Result<stats_speed_items_t>& result,
const QString& web_server,
const QString& year_,
const QString& month_,
@ -136,7 +137,7 @@ public:
\param log_field The log field
*/
void getItemsCount(
std::tuple<bool, std::vector<std::tuple<QString, int>>>& result,
Result<stats_count_items_t>& result,
const QString& web_server,
const QString& year,
const QString& month,
@ -158,7 +159,7 @@ public:
\param field_filter The filter to apply
*/
void getDaytimeCounts(
std::tuple<bool, std::unordered_map<int, std::unordered_map<int, int>>>& result,
Result<stats_day_items_t>& result,
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_,
@ -180,7 +181,7 @@ public:
\see getRelationalCountsPeriod()
*/
void getRelationalCountsDay(
std::tuple<bool, std::vector<std::tuple<long long, int>>>& result,
Result<stats_relat_items_t>& result,
const QString& web_server,
const QString& year_, const QString& month_, const QString& day_,
const QString& log_field_1_, const QString& field_filter_1,
@ -204,7 +205,7 @@ public:
\see getRelationalCountsDay()
*/
void getRelationalCountsPeriod(
std::tuple<bool, std::vector<std::tuple<long long, int>>>& result,
Result<stats_relat_items_t>& result,
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_,