From 5f3bf1067d4d63016c6a2418febf3db9fa1d63d1 Mon Sep 17 00:00:00 2001 From: luocai Date: Thu, 28 Nov 2024 19:34:24 +0800 Subject: [PATCH] add model. --- Server/SystemUsage.cpp | 2 +- UnitTest/main.cpp | 2 +- WebApplication/CMakeLists.txt | 1 + WebApplication/Hello.cpp | 4 ++ WebApplication/VisitorRecordTableModel.cpp | 19 +++++++ WebApplication/VisitorRecordTableModel.h | 12 +++++ WebApplication/VisitorRecordsPage.cpp | 62 +++++++++++++++++++++- WebApplication/VisitorRecordsPage.h | 12 +++-- resources/wt_config.xml | 51 ------------------ 9 files changed, 108 insertions(+), 57 deletions(-) create mode 100644 WebApplication/VisitorRecordTableModel.cpp create mode 100644 WebApplication/VisitorRecordTableModel.h diff --git a/Server/SystemUsage.cpp b/Server/SystemUsage.cpp index b2a7d64..a2bd5ef 100644 --- a/Server/SystemUsage.cpp +++ b/Server/SystemUsage.cpp @@ -50,7 +50,7 @@ void SystemUsage::start() { LOG(info) << "network speed: " << std::fixed << std::setprecision(2) << speed << "GB/h"; static system_clock::time_point lastNotify; auto now = system_clock::now(); - if ((speed >= 1.f) && (duration_cast(now - lastNotify) > minutes(10))) { // 一个小时1GB的流量 + if ((speed >= 1.5f) && (duration_cast(now - lastNotify) > minutes(10))) { // 一个小时1.5GB的流量 std::ostringstream oss; oss << "当前服务器流量存在异常, " << DateTime::toString(front.time) << " - " << DateTime::toString(back.time) << ": " << std::endl; diff --git a/UnitTest/main.cpp b/UnitTest/main.cpp index 7bf9f40..5b4d4a0 100644 --- a/UnitTest/main.cpp +++ b/UnitTest/main.cpp @@ -3,7 +3,7 @@ #include #include "Database/Session.h" -constexpr auto databasePath = "build/database.sqlite"; +constexpr auto databasePath = "build/unit_test_database.sqlite"; int main(int argc, char *argv[]) { if (std::filesystem::exists(databasePath)) { diff --git a/WebApplication/CMakeLists.txt b/WebApplication/CMakeLists.txt index cad285b..24d216b 100644 --- a/WebApplication/CMakeLists.txt +++ b/WebApplication/CMakeLists.txt @@ -4,6 +4,7 @@ add_library(WebApplication WebApplication.h WebApplication.cpp LoginPage.h LoginPage.cpp VisitorRecordsPage.h VisitorRecordsPage.cpp + VisitorRecordTableModel.h VisitorRecordTableModel.cpp Hello.h Hello.cpp Restful.h Restful.cpp Dialog.h Dialog.cpp diff --git a/WebApplication/Hello.cpp b/WebApplication/Hello.cpp index 642b30f..6cb1ee0 100644 --- a/WebApplication/Hello.cpp +++ b/WebApplication/Hello.cpp @@ -3,6 +3,7 @@ #include "Database/Session.h" #include "Dialog.h" #include "LoginPage.h" +#include "VisitorRecordsPage.h" #include "WebApplication.h" #include #include @@ -100,5 +101,8 @@ void Hello::handlePathChange(const std::string &path) { m_root->clear(); m_root->setStyleClass("WtCenterContainer"); m_root->addNew(m_session->users(), m_session->login()); + } else if (path.starts_with("/wt/visitor/analysis")) { + m_root->clear(); + m_root->addNew(*m_session); } } diff --git a/WebApplication/VisitorRecordTableModel.cpp b/WebApplication/VisitorRecordTableModel.cpp new file mode 100644 index 0000000..4622208 --- /dev/null +++ b/WebApplication/VisitorRecordTableModel.cpp @@ -0,0 +1,19 @@ +#include "VisitorRecordTableModel.h" +#include "BoostLog.h" + +int VisitorRecordTableModel::columnCount(const Wt::WModelIndex &parent) const { + return 5; +} + +int VisitorRecordTableModel::rowCount(const Wt::WModelIndex &parent) const { + return 50; +} + +Wt::cpp17::any VisitorRecordTableModel::data(const Wt::WModelIndex &index, Wt::ItemDataRole role) const { + LOG(info) << "VisitorRecordTableModel: " << role.value(); + if (role == Wt::ItemDataRole::Checked && index.column() == 0) { // Assuming first column for checkbox + return true; + } else { + return Wt::cpp17::any{}; + } +} diff --git a/WebApplication/VisitorRecordTableModel.h b/WebApplication/VisitorRecordTableModel.h new file mode 100644 index 0000000..65523f0 --- /dev/null +++ b/WebApplication/VisitorRecordTableModel.h @@ -0,0 +1,12 @@ +#ifndef __VISITORRECORDTABLEMODEL_H__ +#define __VISITORRECORDTABLEMODEL_H__ + +#include + +class VisitorRecordTableModel : public Wt::WAbstractTableModel { +public: + Wt::cpp17::any data(const Wt::WModelIndex &index, Wt::ItemDataRole role = Wt::ItemDataRole::Display) const final; + int columnCount(const Wt::WModelIndex &parent = Wt::WModelIndex()) const final; + int rowCount(const Wt::WModelIndex &parent = Wt::WModelIndex()) const final; +}; +#endif // __VISITORRECORDTABLEMODEL_H__ \ No newline at end of file diff --git a/WebApplication/VisitorRecordsPage.cpp b/WebApplication/VisitorRecordsPage.cpp index a0bed2c..11ebaf5 100644 --- a/WebApplication/VisitorRecordsPage.cpp +++ b/WebApplication/VisitorRecordsPage.cpp @@ -1 +1,61 @@ -#include "VisitorRecordsPage.h" \ No newline at end of file +#include "VisitorRecordsPage.h" +#include "BoostLog.h" +#include "Database/Session.h" +#include +#include +#include +#include +#include "VisitorRecordTableModel.h" + +class CheckBoxDelegate : public Wt::WAbstractItemDelegate { +public: + CheckBoxDelegate() : Wt::WAbstractItemDelegate() { + } + std::unique_ptr update(Wt::WWidget *widget, const Wt::WModelIndex &index, + Wt::WFlags flags) final { + auto checkBox = dynamic_cast(widget); + if (!checkBox) { + checkBox = new Wt::WCheckBox(); + } + LOG(info) << Wt::asString(index.data()); + // checkBox->setChecked(index.data().toBool()); + checkBox->setEnabled(true); + + return std::unique_ptr(checkBox); + } +}; + +VisitorRecordsPage::VisitorRecordsPage(Session &session) { + addNew("访客数据统计"); + + auto view = addNew(); + view->setColumnWidth(0, Wt::WLength::Auto); + view->setColumnWidth(1, Wt::WLength::Auto); + view->setHeaderHeight(30); // 设置表头的高度 + view->setWidth(Wt::WLength("99%")); + view->setColumnResizeEnabled(true); + view->setAlternatingRowColors(true); + view->setRowHeight(50); + // view->setSelectable(true); + view->setSelectionMode(Wt::SelectionMode::Extended); + // view->setEditTriggers(Wt::EditTrigger::None); + // view->setItemDelegateForColumn(0, std::make_shared()); + // view->setHeaderItemDelegate( std::make_shared()); + + // auto model = std::make_shared>>(); + // model->setQuery(session.find()); + // model->addColumn("id", "ID",Wt::ItemFlag::UserCheckable); + // model->addColumn("url", "URL"); + // model->addColumn("time", "Time"); + // model->addColumn("user_agent", "User Agent"); + auto model = std::make_shared(); + view->setModel(model); + + + + /* + * Configure column widths and matching table width + */ + const int WIDTH = 240; + for (int i = 0; i < view->model()->columnCount(); ++i) view->setColumnWidth(i, 120); +} diff --git a/WebApplication/VisitorRecordsPage.h b/WebApplication/VisitorRecordsPage.h index 0ced9b8..bba2596 100644 --- a/WebApplication/VisitorRecordsPage.h +++ b/WebApplication/VisitorRecordsPage.h @@ -1,9 +1,15 @@ #ifndef __VISITORRECORDSPAGE_H__ #define __VISITORRECORDSPAGE_H__ -class VisitorRecordsPage { - +#include + +class Session; + +// https://infima.dev/ + +class VisitorRecordsPage : public Wt::WContainerWidget { +public: + VisitorRecordsPage(Session &session); }; - #endif // __VISITORRECORDSPAGE_H__ \ No newline at end of file diff --git a/resources/wt_config.xml b/resources/wt_config.xml index e4b76be..587b469 100644 --- a/resources/wt_config.xml +++ b/resources/wt_config.xml @@ -134,57 +134,6 @@ 50 - - - - - - - /opt/Libraries/wt-4.11.1/var/run/wt - - - - - - - - 128 - -