add visit analysis.
All checks were successful
Deploy / PullDocker (push) Successful in 27s
Deploy / Build (push) Successful in 55s

This commit is contained in:
amass 2024-07-30 22:17:55 +08:00
parent a57fcd6693
commit 9c47e0f2c5
3 changed files with 68 additions and 1 deletions

View File

@ -119,6 +119,36 @@ Application::Application(const std::string &path)
session.reply(
ServiceLogic::make_200<boost::beast::http::string_body>(request, "notify successed.\n", "text/html"));
});
m_router->insert("/api/v1/visit_analysis", [this](HttpSession &session, const Request &request,
const boost::urls::matches &matches) {
using namespace boost::beast;
auto rootJson = boost::json::parse(request.body());
auto &root = rootJson.as_object();
std::string url;
std::string visitorUuid;
if (root.contains("url")) {
url = root["url"].as_string();
}
if (root.contains("visitor_uuid")) {
visitorUuid = root["visitor_uuid"].as_string();
}
auto database = Amass::Singleton<Database>::instance();
database->updateTodayVisitCount(url, visitorUuid);
auto data = database->visitAnalysisData(std::string(url));
boost::json::object reply;
reply["page_view_count"] = data.pageViewCount;
reply["unique_visitor_count"] = data.uniqueVisitorCount;
http::response<boost::beast::http::string_body> s{boost::beast::http::status::ok, request.version()};
s.set(http::field::server, BOOST_BEAST_VERSION_STRING);
s.set(http::field::content_type, "application/json;charset=UTF-8");
s.keep_alive(request.keep_alive());
s.body() = boost::json::serialize(reply);
s.prepare_payload();
session.reply(std::move(s));
});
// clang-format on
m_ioContext = Amass::Singleton<IoContext>::instance<Amass::Construct>(getThreads());
@ -138,6 +168,7 @@ const Application::RequestHandler *Application::find(boost::urls::segments_encod
boost::urls::matches_base &matches) const noexcept {
const Application::RequestHandler *ret = nullptr;
try {
LOG(info) << path << "cgdfc";
ret = m_router->find(path, matches);
} catch (const std::exception &e) {
boost::stacktrace::stacktrace trace = boost::stacktrace::stacktrace::from_current_exception();

View File

@ -216,6 +216,35 @@ void Database::updateVisitCount() {
}
}
VisitAnalysis Database::visitAnalysisData(const std::string &url) {
VisitAnalysis ret;
std::string query1 = "SELECT page_view_count, unique_visitor_count FROM visit_count WHERE url = '" + url + "';";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(m_sqlite3, query1.c_str(), -1, &stmt, nullptr) == SQLITE_OK) {
if (sqlite3_step(stmt) == SQLITE_ROW) {
ret.pageViewCount = sqlite3_column_int(stmt, 0);
ret.uniqueVisitorCount = sqlite3_column_int(stmt, 1);
}
sqlite3_finalize(stmt);
} else {
LOG(error) << "Failed to execute query: " << sqlite3_errmsg(m_sqlite3);
return ret;
}
std::string query2 =
"SELECT SUM(page_view_count), COUNT(DISTINCT visitor_uuid) FROM today_visit_count WHERE url = '" + url + "';";
if (sqlite3_prepare_v2(m_sqlite3, query2.c_str(), -1, &stmt, nullptr) == SQLITE_OK) {
if (sqlite3_step(stmt) == SQLITE_ROW) {
ret.pageViewCount += sqlite3_column_int(stmt, 0);
ret.uniqueVisitorCount += sqlite3_column_int(stmt, 1);
}
sqlite3_finalize(stmt);
} else {
LOG(error) << "Failed to execute query: " << sqlite3_errmsg(m_sqlite3);
}
return ret;
}
bool Database::addHomeBoxItem(const std::string &name, const std::string &location, int cost) {
bool ret = true;
std::ostringstream oss;

View File

@ -6,6 +6,13 @@
#include "Task.h"
#include <string>
class VisitAnalysis {
public:
std::string url;
int pageViewCount = 0;
int uniqueVisitorCount = 0;
};
struct sqlite3;
class Database {
@ -23,7 +30,7 @@ public:
void updateTodayVisitCount(const std::string &url, const std::string &visitorUuid);
void clearTodayVisitRecord();
void updateVisitCount();
VisitAnalysis visitAnalysisData(const std::string &url);
HomeBox::Items homeBoxItems();
bool addHomeBoxItem(const std::string &name, const std::string &location, int cost);