Add examples

This commit is contained in:
altffour 2019-12-20 16:22:20 +03:00
parent ed00feb8fa
commit 035a756bba
No known key found for this signature in database
GPG Key ID: 2E7B9E061AF66FC3
11 changed files with 197 additions and 7 deletions

View File

@ -73,6 +73,19 @@ if (WIN32)
target_link_libraries(contractNotifier Qt5::Core Qt5::Widgets Boost::serialization ${CMAKE_THREAD_LIBS_INIT} CURL::libcurl)
endif(WIN32)
# examples
add_executable(dataBenchmark "examples/dataBenchmark.cpp")
target_link_libraries(dataBenchmark Qt5::Core Qt5::Widgets Boost::serialization ${CMAKE_THREAD_LIBS_INIT} ${CURL_LIBRARIES})
if (WIN32)
target_link_libraries(dataBenchmark Qt5::Core Qt5::Widgets Boost::serialization ${CMAKE_THREAD_LIBS_INIT} CURL::libcurl)
endif(WIN32)
add_executable(searchBenchmark "examples/searchBenchmark.cpp")
target_link_libraries(searchBenchmark Qt5::Core Qt5::Widgets Boost::serialization ${CMAKE_THREAD_LIBS_INIT} ${CURL_LIBRARIES})
if (WIN32)
target_link_libraries(searchBenchmark Qt5::Core Qt5::Widgets Boost::serialization ${CMAKE_THREAD_LIBS_INIT} CURL::libcurl)
endif(WIN32)
# res files:
add_custom_command(
TARGET contractNotifier POST_BUILD
@ -84,4 +97,3 @@ add_custom_command(
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/vendor/notifu.exe
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/notifu.exe)

4
benchmarks.txt Normal file
View File

@ -0,0 +1,4 @@
Search Benchmark Results:
Size of DB: 1,000,000
Last Contract search took: 0.834289
Average random Contract search took: 0.890232

View File

@ -0,0 +1,49 @@
#include <iostream>
#include <cstdio>
#include <ctime>
#include "lib/notify.h"
#include "lib/db.h"
#include "lib/export.h"
#include "lib/import.h"
#define DB_CNT 1000000
int main() {
std::clock_t start;
double duration;
DB db;
Category category;
category._name = "All";
category._desc = "Default Category";
db._categories.push_back(category);
std::cout << "Making " << DB_CNT << " contracts" << std::endl;
start = std::clock();
for (int i = 0; i < DB_CNT; i++) {
Contract contract;
contract._name = std::to_string(i);
contract._desc = "This is an average description.";
contract._expiry = "01/01/2000";
contract._notify_from_months = 3;
contract._notify_from_days = 15;
db._categories[0]._contracts.push_back(contract);
}
duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
std::cout << "Made " << DB_CNT << " contracts in " << duration << "s" << std::endl;
std::cout << "Size of " << DB_CNT << " contracts: " << sizeof(db) << " bytes" << std::endl;
std::cout << "Saving " << DB_CNT << " contracts." << std::endl;
start = std::clock();
export_db_as_db(db, "db.db");
duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
std::cout << "Saved " << DB_CNT << " contracts in " << duration << "s" << std::endl;
std::cout << "Reading " << DB_CNT << " contracts." << std::endl;
start = std::clock();
import_db_as_db(db, "db.db");
duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
std::cout << "Read " << DB_CNT << " contracts in " << duration << "s" << std::endl;
return 0;
}

View File

@ -0,0 +1,62 @@
#include <iostream>
#include <cstdio>
#include <ctime>
#include "lib/notify.h"
#include "lib/db.h"
#include "lib/export.h"
#include "lib/import.h"
#define DB_CNT 1000000
#define RND_CNT 1000
int main() {
std::clock_t start;
double duration;
DB db;
Category category;
category._name = "All";
category._desc = "Default Category";
db._categories.push_back(category);
std::cout << "Making " << DB_CNT << " contracts" << std::endl;
start = std::clock();
for (int i = 0; i < DB_CNT; i++) {
Contract contract;
contract._name = std::to_string(i);
contract._desc = "This is an average description.";
contract._expiry = "01/01/2000";
contract._notify_from_months = 3;
contract._notify_from_days = 15;
db._categories[0]._contracts.push_back(contract);
}
duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
std::cout << "Made " << DB_CNT << " contracts in " << duration << "s" << std::endl;
std::cout << "Size of " << DB_CNT << " contracts: " << sizeof(db) << " bytes" << std::endl;
std::cout << "Making list of " << DB_CNT << " contracts." << std::endl;
start = std::clock();
db_makeList(db);
duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
std::cout << "Made list of " << DB_CNT << " contracts in " << duration << std::endl;
std::cout << "Searching for last contract." << std::endl;
start = std::clock();
db_search_sort(db, std::to_string(DB_CNT-1));
duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
std::cout << "Searching for last contract too: " << duration << std::endl;
double total_duration;
for (int i = 0; i < RND_CNT; i++) {
int random = rand()%((DB_CNT-1)-0 + 1) + 0;
std::cout << "Searching for random contract number: " << random << std::endl;
start = std::clock();
db_search_sort(db, std::to_string(random));
duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
std::cout << "Searching for random contract took: " << duration << std::endl;
total_duration += duration;
}
std::cout << "Average search time: " << total_duration / RND_CNT << std::endl;
return 0;
}

View File

@ -14,6 +14,21 @@ auto addCategoryWindow::on_closeBtn_clicked() -> void {
}
auto addCategoryWindow::on_addBtn_clicked() -> void {
// Check if name is empty.
if (this->nameBox->text().toStdString() == "") {
QMessageBox msgBox;
msgBox.setText("Name of contract can not be empty!");
msgBox.exec();
return;
}
// Check if name is already there.
if (!db_isNameAvaliable(*_db, this->nameBox->text().toStdString())) {
QMessageBox msgBox;
msgBox.setText("Name is already taken!");
msgBox.exec();
return;
}
// Make category
Category category;
category._name = this->nameBox->text().toUtf8().constData();

View File

@ -16,15 +16,30 @@ addContractWindow::~addContractWindow() {
auto addContractWindow::on_closeBtn_clicked() -> void {
this->close();
}
auto addContractWindow::on_addBtn_clicked() -> void {
// Check if name is empty.
if (this->nameBox->text().toStdString() == "") {
QMessageBox msgBox;
msgBox.setText("Name of contract can not be empty!");
msgBox.exec();
return;
}
// Check if name is already there.
if (!db_isNameAvaliable(*_db, this->nameBox->text().toStdString())) {
QMessageBox msgBox;
msgBox.setText("Name is already taken!");
msgBox.exec();
return;
}
// Get category.
for (int i = 0; i < _db->_categories.size(); i++) {
Category* category = &_db->_categories[i];
if (category->_name == this->categoryBox->currentText().toUtf8().constData()) {
// Make contract.
Contract contract;
contract._name = this->nameBox->text().toUtf8().constData();
contract._desc = this->descBox->text().toUtf8().constData();
contract._name = this->nameBox->text().toStdString();
contract._desc = this->descBox->text().toStdString();
contract._notify_from_months = this->notifyMonthsVal->value();
contract._notify_from_days = this->notifyDaysVal->value();
contract_setExpiry(contract, this->expiryDate->date());

View File

@ -2,6 +2,7 @@
#define ADDCNTRCT_WIN_H
#include <QMainWindow>
#include <QMessageBox>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>

View File

@ -29,6 +29,23 @@ struct DB {
}
};
static auto db_isNameAvaliable(const DB &db, const std::string &str) -> bool {
// Iterate over deactivated category.
if (str == "Expired") return false;
for (auto c : db._deactivatedCategory._contracts)
if (c._name == str) return false;
// Iterate over categories names.
for (auto c : db._categories)
if (c._name == str) return false;
// Iterate over contracts names.
for (auto cat : db._categories)
for (auto cont : cat._contracts)
if (cont._name == str) return false;
return true;
}
static auto db_search_getCorrectness(const std::string &str, const Contract &contract) -> int {
int val = 0;
if (contract._name.size() < str.size())

View File

@ -1,6 +1,5 @@
#include "mainwindow.h"
#include <iostream>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) {
setupUi(this);
@ -38,6 +37,7 @@ MainWindow::MainWindow(QWidget *parent)
_db._deactivatedCategory._name = "Expired";
_db._deactivatedCategory._desc = "Permanent Category";
this->updateDB();
}
this->listDB();
@ -97,6 +97,10 @@ auto MainWindow::on_searchBox_textChanged(const QString &text) -> void {
}
}
auto MainWindow::on_clearBtn_clicked() -> void {
this->searchBox->clear();
}
auto MainWindow::closeEvent(QCloseEvent *event) -> void {
if(closing) {
event->accept();
@ -106,6 +110,9 @@ auto MainWindow::closeEvent(QCloseEvent *event) -> void {
event->ignore();
}
}
auto MainWindow::on_actionExit_triggered() -> void {
this->close();
}
auto MainWindow::on_closeBtn_clicked() -> void {
hide();
@ -117,6 +124,11 @@ auto MainWindow::on_settingsBtn_clicked() -> void {
win->show();
}
auto MainWindow::on_actionSettings_triggered() -> void {
settingsWindow *win = new settingsWindow(&_db, this);
win->show();
}
auto MainWindow::on_infoBtn_clicked() -> void {
if (this->_selectedContract) {
infoWindow *win = new infoWindow(&_db, _selectedContract, _selectedCategoryName, this);

View File

@ -44,11 +44,14 @@ public slots:
void closeEvent(QCloseEvent *event) override;
void on_treeView_itemClicked(QTreeWidgetItem* _item, int column);
void on_searchBox_textChanged(const QString &text);
void on_clearBtn_clicked();
void on_actionExport_triggered();
void on_actionImport_triggered();
void on_actionAdd_Contract_triggered();
void on_actionAdd_Category_triggered();
void on_actionInfo_triggered();
void on_actionSettings_triggered();
void on_actionExit_triggered();
void listDB();
void updateDB();

View File

@ -14,7 +14,7 @@
<string>Contract Notifier</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QWidget" name="">
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>10</x>
@ -30,9 +30,9 @@
<widget class="QLineEdit" name="searchBox"/>
</item>
<item>
<widget class="QPushButton" name="searchBtn">
<widget class="QPushButton" name="clearBtn">
<property name="text">
<string>Search</string>
<string>Clear</string>
</property>
</widget>
</item>