summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2025-09-24 21:28:56 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2025-09-24 21:33:13 +0100
commit897546d596d8d7213cff60146123bb8f97d4d1cc (patch)
treeaa3ec6a8db09d6eaf915805986e57d691c7d11f6
parent71803b97f1c9e31f2027da48bb742353f9c43e62 (diff)
downloadwebstat-897546d596d8d7213cff60146123bb8f97d4d1cc.tar.bz2
webstat-897546d596d8d7213cff60146123bb8f97d4d1cc.tar.xz
webstat-897546d596d8d7213cff60146123bb8f97d4d1cc.zip
Create settings structure
Holds all the settings and their defaults for use in program_options and tests. Disables missing-field-initializers in tests because its over sensitive to structures with defaults where you only provide some values specifically.
-rw-r--r--src/ingestor.cpp15
-rw-r--r--src/ingestor.hpp13
-rw-r--r--src/settings.hpp9
-rw-r--r--src/webstat_logger_main.cpp16
-rw-r--r--test/Jamfile.jam1
-rw-r--r--test/perf-ingest.cpp7
-rw-r--r--test/test-ingest.cpp8
7 files changed, 49 insertions, 20 deletions
diff --git a/src/ingestor.cpp b/src/ingestor.cpp
index db5f317..db11799 100644
--- a/src/ingestor.cpp
+++ b/src/ingestor.cpp
@@ -58,8 +58,17 @@ namespace WebStat {
}
}
- Ingestor::Ingestor(const utsname & host, DB::ConnectionPoolPtr dbpl) :
- dbpool {std::move(dbpl)}, hostnameId {crc32(host.nodename)}, curl {curl_multi_init()}
+ Ingestor::Ingestor(const utsname & host, IngestorSettings settings) :
+ Ingestor {host,
+ std::make_shared<DB::ConnectionPool>(
+ settings.dbMax, settings.dbKeep, settings.dbType, settings.dbConnStr),
+ std::move(settings)}
+ {
+ }
+
+ Ingestor::Ingestor(const utsname & host, DB::ConnectionPoolPtr dbpl, IngestorSettings settings) :
+ settings {std::move(settings)}, dbpool {std::move(dbpl)}, hostnameId {crc32(host.nodename)},
+ curl {curl_multi_init()}
{
auto dbconn = dbpool->get();
auto ins = dbconn->modify(SQL::HOST_UPSERT, SQL::HOST_UPSERT_OPTS);
@@ -202,7 +211,7 @@ namespace WebStat {
if (insert->execute() > 0) {
switch (type) {
case EntityType::UserAgent: {
- auto curlOp = curlGetUserAgentDetail(entityId, value, userAgentAPI.c_str());
+ auto curlOp = curlGetUserAgentDetail(entityId, value, settings.userAgentAPI.c_str());
auto added = curlOperations.emplace(curlOp->hnd.get(), std::move(curlOp));
curl_multi_add_handle(curl.get(), added.first->first);
break;
diff --git a/src/ingestor.hpp b/src/ingestor.hpp
index 879526d..719b65b 100644
--- a/src/ingestor.hpp
+++ b/src/ingestor.hpp
@@ -2,6 +2,7 @@
#include "curlOp.hpp"
#include "logTypes.hpp"
+#include "settings.hpp"
#include <c++11Helpers.h>
#include <connectionPool.h>
#include <connection_fwd.h>
@@ -12,9 +13,17 @@
#include <sys/utsname.h>
namespace WebStat {
+ struct IngestorSettings : Settings {
+ std::string dbConnStr = "dbname=webstat user=webstat";
+ std::string userAgentAPI = "https://useragentstring.com";
+ unsigned int dbMax = 4;
+ unsigned int dbKeep = 2;
+ };
+
class Ingestor {
public:
- Ingestor(const utsname &, DB::ConnectionPoolPtr);
+ Ingestor(const utsname &, IngestorSettings);
+ Ingestor(const utsname &, DB::ConnectionPoolPtr, IngestorSettings);
virtual ~Ingestor() = default;
SPECIAL_MEMBERS_DELETE(Ingestor);
@@ -32,7 +41,7 @@ namespace WebStat {
template<typename... T> void storeLogLine(DB::Connection *, const std::tuple<T...> &) const;
- std::string userAgentAPI = "https://useragentstring.com";
+ IngestorSettings settings;
protected:
DB::ConnectionPoolPtr dbpool;
diff --git a/src/settings.hpp b/src/settings.hpp
new file mode 100644
index 0000000..8ac5c0d
--- /dev/null
+++ b/src/settings.hpp
@@ -0,0 +1,9 @@
+#pragma once
+
+#include <string>
+
+namespace WebStat {
+ struct Settings {
+ std::string dbType = "postgresql";
+ };
+}
diff --git a/src/webstat_logger_main.cpp b/src/webstat_logger_main.cpp
index d30da80..eb8a30d 100644
--- a/src/webstat_logger_main.cpp
+++ b/src/webstat_logger_main.cpp
@@ -24,22 +24,19 @@ main(int argc, char ** argv)
namespace po = boost::program_options;
po::options_description opts("WebStat logger");
- std::string dbType;
- std::string dbConnStr;
- unsigned int dbMax = 4;
- unsigned int dbKeep = 2;
+ WebStat::IngestorSettings settings;
// clang-format off
opts.add_options()
("help,h", "Show this help message")
("config,c", po::value<std::string>(), "Read config from this config file")
- ("db.type", po::value(&dbType)->default_value("postgresql"),
+ ("db.type", po::value(&settings.dbType)->default_value(settings.dbType),
"Database connection type")
- ("db.wr.connstr,D", po::value(&dbConnStr)->default_value("dbname=webstat user=webstat"),
+ ("db.wr.connstr,D", po::value(&settings.dbConnStr)->default_value(settings.dbConnStr),
"Database connection string (read/write)")
- ("db.wr.max", po::value(&dbMax)->default_value(4),
+ ("db.wr.max", po::value(&settings.dbMax)->default_value(settings.dbMax),
"Maximum number of concurrent write/read write DB connections")
- ("db.wr.keep", po::value(&dbKeep)->default_value(2),
+ ("db.wr.keep", po::value(&settings.dbKeep)->default_value(settings.dbKeep),
"Number of write/read write DB connections to keep open")
;
// clang-format on
@@ -56,9 +53,8 @@ main(int argc, char ** argv)
}
po::notify(optVars);
- auto pool = std::make_shared<DB::ConnectionPool>(dbMax, dbKeep, std::move(dbType), std::move(dbConnStr));
try {
- WebStat::Ingestor {getHostDetail(), pool}.ingestLog(stdin);
+ WebStat::Ingestor {getHostDetail(), std::move(settings)}.ingestLog(stdin);
return EXIT_SUCCESS;
}
catch (const std::exception & excp) {
diff --git a/test/Jamfile.jam b/test/Jamfile.jam
index 217feec..493c5bc 100644
--- a/test/Jamfile.jam
+++ b/test/Jamfile.jam
@@ -12,6 +12,7 @@ project WebStat-Testing : requirements
<define>TEST=\"$(test)\"
<define>FIXTURES=\"$(fixtures)\"
<library>$(src)//webstat
+ <toolset>gcc,<variant>debug:<cflags>-Wno-missing-field-initializers
;
lib test-util :
diff --git a/test/perf-ingest.cpp b/test/perf-ingest.cpp
index d05de17..3251107 100644
--- a/test/perf-ingest.cpp
+++ b/test/perf-ingest.cpp
@@ -21,8 +21,11 @@ namespace {
void
doIngestFile(benchmark::State & state)
{
- WebStat::Ingestor ingestor {
- WebStat::getTestUtsName("perf-hostname"), std::make_shared<WebStat::MockDBPool>("webstat")};
+ WebStat::Ingestor ingestor {WebStat::getTestUtsName("perf-hostname"),
+ std::make_shared<WebStat::MockDBPool>("webstat"),
+ {
+ .userAgentAPI = {},
+ }};
for (auto loop : state) {
WebStat::FilePtr logFile {fopen(TMP_LOG.c_str(), "r")};
ingestor.ingestLog(logFile.get());
diff --git a/test/test-ingest.cpp b/test/test-ingest.cpp
index 722763a..83bac48 100644
--- a/test/test-ingest.cpp
+++ b/test/test-ingest.cpp
@@ -195,9 +195,11 @@ BOOST_DATA_TEST_CASE(ExtractFields,
class TestIngestor : public WebStat::Ingestor {
public:
TestIngestor() :
- WebStat::Ingestor {WebStat::getTestUtsName("test-hostname"), std::make_shared<MockDBPool>("webstat")}
+ WebStat::Ingestor {WebStat::getTestUtsName("test-hostname"), std::make_shared<MockDBPool>("webstat"),
+ {
+ .userAgentAPI = FIXTURE_URL_BASE + "/userAgent.json",
+ }}
{
- userAgentAPI = FIXTURE_URL_BASE + "/userAgent.json";
}
};
@@ -233,7 +235,7 @@ BOOST_AUTO_TEST_CASE(FetchMockUserAgentDetail)
{
const auto uaDetailReq = WebStat::curlGetUserAgentDetail(0,
R"(Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36)",
- userAgentAPI.c_str());
+ settings.userAgentAPI.c_str());
BOOST_REQUIRE(uaDetailReq);
BOOST_REQUIRE_EQUAL(CURLE_OK, curl_easy_perform(uaDetailReq->hnd.get()));