summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2025-10-15 23:24:32 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2025-10-15 23:24:32 +0100
commitb9538c693b34a6382ea9f3b985bb922b9c17f12e (patch)
treeb0658689e0c500cdc2db44efac0c23f54a5f1771
parent0845347d76868142af5d23bac1e22cfb55415e0a (diff)
downloadwebstat-b9538c693b34a6382ea9f3b985bb922b9c17f12e.tar.bz2
webstat-b9538c693b34a6382ea9f3b985bb922b9c17f12e.tar.xz
webstat-b9538c693b34a6382ea9f3b985bb922b9c17f12e.zip
Refactor handling of new entity insert
Replaces weird select with one thing with a function pointer stored in the type definition array.
-rw-r--r--src/ingestor.cpp37
-rw-r--r--src/ingestor.hpp1
2 files changed, 24 insertions, 14 deletions
diff --git a/src/ingestor.cpp b/src/ingestor.cpp
index cb758cd..1bf4c99 100644
--- a/src/ingestor.cpp
+++ b/src/ingestor.cpp
@@ -283,32 +283,41 @@ namespace WebStat {
Ingestor::NewEntityIds
Ingestor::storeEntities(DB::Connection * dbconn, const std::span<const std::optional<Entity>> values) const
{
- static constexpr std::array ENTITY_TYPE_VALUES {
- "host", "virtual_host", "path", "query_string", "referrer", "user_agent", "unparsable_line"};
+ static constexpr std::array<std::pair<std::string_view, void (Ingestor::*)(const Entity &) const>, 7>
+ ENTITY_TYPE_VALUES {{
+ {"host", nullptr},
+ {"virtual_host", nullptr},
+ {"path", nullptr},
+ {"query_string", nullptr},
+ {"referrer", nullptr},
+ {"user_agent", &Ingestor::onNewUserAgent},
+ {"unparsable_line", nullptr},
+ }};
auto insert = dbconn->modify(SQL::ENTITY_INSERT, SQL::ENTITY_INSERT_OPTS);
NewEntityIds ids;
std::ranges::transform(values | std::views::take_while(&std::optional<Entity>::has_value), ids.begin(),
[this, &insert](auto && entity) {
const auto & [entityId, type, value] = *entity;
- bindMany(insert, 0, entityId, ENTITY_TYPE_VALUES[std::to_underlying(type)], value);
- if (insert->execute() > 0) {
- switch (type) {
- case EntityType::UserAgent: {
- 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;
- }
- default:
- break;
- }
+ const auto & [typeName, onInsert] = ENTITY_TYPE_VALUES[std::to_underlying(type)];
+ bindMany(insert, 0, entityId, typeName, value);
+ if (insert->execute() > 0 && onInsert) {
+ std::invoke(onInsert, this, *entity);
}
return std::get<0>(*entity);
});
return ids;
}
+ void
+ Ingestor::onNewUserAgent(const Entity & entity) const
+ {
+ const auto & [entityId, type, value] = entity;
+ 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);
+ }
+
template<typename... T>
void
Ingestor::storeLogLine(DB::Connection * dbconn, const std::tuple<T...> & values) const
diff --git a/src/ingestor.hpp b/src/ingestor.hpp
index 8be7360..3e25938 100644
--- a/src/ingestor.hpp
+++ b/src/ingestor.hpp
@@ -71,6 +71,7 @@ namespace WebStat {
NewEntityIds storeEntities(DB::Connection *, std::span<const std::optional<Entity>>) const;
using NewEntities = std::array<std::optional<Entity>, MAX_NEW_ENTITIES>;
template<typename... T> NewEntities newEntities(const std::tuple<T...> &) const;
+ void onNewUserAgent(const Entity &) const;
void handleCurlOperations();
void jobIngestParkedLine(const std::filesystem::directory_iterator &);