diff options
| author | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-10-15 23:24:32 +0100 |
|---|---|---|
| committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-10-15 23:24:32 +0100 |
| commit | b9538c693b34a6382ea9f3b985bb922b9c17f12e (patch) | |
| tree | b0658689e0c500cdc2db44efac0c23f54a5f1771 /src/ingestor.cpp | |
| parent | 0845347d76868142af5d23bac1e22cfb55415e0a (diff) | |
| download | webstat-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.
Diffstat (limited to 'src/ingestor.cpp')
| -rw-r--r-- | src/ingestor.cpp | 37 |
1 files changed, 23 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 |
