mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-14 17:54:42 +00:00
Move brewery reverse geocoding from C# seeding to C++ pipeline (#249)
This commit is contained in:
@@ -144,6 +144,8 @@ FetchContent_MakeAvailable(cpp-httplib)
|
||||
# 5. Executable & Sources
|
||||
add_executable(${PROJECT_NAME}
|
||||
includes/services/enrichment/mock_enrichment.h
|
||||
includes/services/address/address_service.h
|
||||
includes/services/address/mock_address_service.h
|
||||
includes/services/curated_data/mock_curated_data_service.h
|
||||
includes/json_handling/pretty_print.h)
|
||||
|
||||
@@ -225,6 +227,11 @@ target_sources(${PROJECT_NAME} PRIVATE
|
||||
src/services/enrichment/wikipedia/get_summary.cc
|
||||
)
|
||||
|
||||
# --- services: address ---
|
||||
target_sources(${PROJECT_NAME} PRIVATE
|
||||
src/services/address/nominatim_address_service.cc
|
||||
)
|
||||
|
||||
# --- services: xeger ---
|
||||
target_sources(${PROJECT_NAME} PRIVATE
|
||||
src/services/xeger/xeger.cc
|
||||
|
||||
@@ -13,6 +13,11 @@
|
||||
// --- concurrency ---
|
||||
#include "concurrency/bounded_channel.h"
|
||||
|
||||
// --- services: address ---
|
||||
#include "services/address/address_service.h"
|
||||
#include "services/address/mock_address_service.h"
|
||||
#include "services/address/nominatim_address_service.h"
|
||||
|
||||
// --- data_generation ---
|
||||
#include "data_generation/data_generator.h"
|
||||
#include "data_generation/generation_json_validation.h"
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
#include "data_generation/data_generator.h"
|
||||
#include "data_model/generated_models.h"
|
||||
#include "services/address/address_service.h"
|
||||
#include "services/curated_data/curated_data_service.h"
|
||||
#include "services/database/export_service.h"
|
||||
#include "services/enrichment/enrichment_service.h"
|
||||
@@ -34,6 +35,8 @@ class BiergartenPipelineOrchestrator {
|
||||
* @param exporter Database backend for persisting generated records.
|
||||
* @param curated_data_service Loads curated location, persona, and name
|
||||
* data used to seed generation.
|
||||
* @param address_service Resolves a street address for generated
|
||||
* brewery coordinates.
|
||||
* @param application_options CLI configuration and paths.
|
||||
*/
|
||||
BiergartenPipelineOrchestrator(
|
||||
@@ -42,6 +45,7 @@ class BiergartenPipelineOrchestrator {
|
||||
std::unique_ptr<DataGenerator> generator,
|
||||
std::unique_ptr<IExportService> exporter,
|
||||
std::unique_ptr<ICuratedDataService> curated_data_service,
|
||||
std::unique_ptr<IAddressService> address_service,
|
||||
const ApplicationOptions& application_options);
|
||||
|
||||
/**
|
||||
@@ -78,6 +82,7 @@ class BiergartenPipelineOrchestrator {
|
||||
*/
|
||||
std::unique_ptr<IExportService> exporter_;
|
||||
std::unique_ptr<ICuratedDataService> curated_data_service_;
|
||||
std::unique_ptr<IAddressService> address_service_;
|
||||
|
||||
ApplicationOptions application_options_;
|
||||
|
||||
|
||||
@@ -95,6 +95,18 @@ struct BreweryAddress {
|
||||
City city;
|
||||
double longitude{};
|
||||
double latitude{};
|
||||
|
||||
/**
|
||||
* @brief Street address line (e.g. house number + road), resolved once
|
||||
* via reverse geocoding at generation time.
|
||||
*/
|
||||
std::string address_line1{};
|
||||
|
||||
/**
|
||||
* @brief Postal code, resolved once via reverse geocoding at generation
|
||||
* time.
|
||||
*/
|
||||
std::string postal_code{};
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
40
tooling/pipeline/includes/services/address/address_service.h
Normal file
40
tooling/pipeline/includes/services/address/address_service.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef BIERGARTEN_PIPELINE_INCLUDES_SERVICES_ADDRESS_ADDRESS_SERVICE_H_
|
||||
#define BIERGARTEN_PIPELINE_INCLUDES_SERVICES_ADDRESS_ADDRESS_SERVICE_H_
|
||||
|
||||
/**
|
||||
* @file services/address/address_service.h
|
||||
* @brief Abstraction for resolving a street address from coordinates.
|
||||
*/
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @brief Street-level address resolved from a coordinate pair.
|
||||
*/
|
||||
struct AddressLookupResult {
|
||||
std::string address_line1;
|
||||
std::string postal_code;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Interface for services that reverse-geocode coordinates into a
|
||||
* street address.
|
||||
*/
|
||||
class IAddressService {
|
||||
public:
|
||||
virtual ~IAddressService() = default;
|
||||
|
||||
/**
|
||||
* @brief Reverse-geocodes a coordinate pair into a street address.
|
||||
*
|
||||
* @param longitude Longitude of the point to resolve.
|
||||
* @param latitude Latitude of the point to resolve.
|
||||
* @return The resolved address, or std::nullopt if it could not be
|
||||
* resolved.
|
||||
*/
|
||||
virtual std::optional<AddressLookupResult> ReverseGeocode(
|
||||
double longitude, double latitude) = 0;
|
||||
};
|
||||
|
||||
#endif // BIERGARTEN_PIPELINE_INCLUDES_SERVICES_ADDRESS_ADDRESS_SERVICE_H_
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef BIERGARTEN_PIPELINE_INCLUDES_SERVICES_ADDRESS_MOCK_ADDRESS_SERVICE_H_
|
||||
#define BIERGARTEN_PIPELINE_INCLUDES_SERVICES_ADDRESS_MOCK_ADDRESS_SERVICE_H_
|
||||
|
||||
/**
|
||||
* @file services/address/mock_address_service.h
|
||||
* @brief Deterministic IAddressService used when network geocoding is
|
||||
* disabled.
|
||||
*/
|
||||
|
||||
#include <optional>
|
||||
|
||||
#include "services/address/address_service.h"
|
||||
|
||||
/**
|
||||
* @brief Address service that returns a fixed placeholder address for any
|
||||
* coordinate pair, without making network calls.
|
||||
*/
|
||||
class MockAddressService final : public IAddressService {
|
||||
public:
|
||||
std::optional<AddressLookupResult> ReverseGeocode(
|
||||
double /*longitude*/, double /*latitude*/) override {
|
||||
return AddressLookupResult{.address_line1 = "123 Mock Street",
|
||||
.postal_code = "00000"};
|
||||
}
|
||||
};
|
||||
|
||||
#endif // BIERGARTEN_PIPELINE_INCLUDES_SERVICES_ADDRESS_MOCK_ADDRESS_SERVICE_H_
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef BIERGARTEN_PIPELINE_INCLUDES_SERVICES_ADDRESS_NOMINATIM_ADDRESS_SERVICE_H_
|
||||
#define BIERGARTEN_PIPELINE_INCLUDES_SERVICES_ADDRESS_NOMINATIM_ADDRESS_SERVICE_H_
|
||||
|
||||
/**
|
||||
* @file services/address/nominatim_address_service.h
|
||||
* @brief Reverse geocoding backed by the public Nominatim API.
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include "services/address/address_service.h"
|
||||
#include "services/logging/logger.h"
|
||||
#include "web_client/web_client.h"
|
||||
|
||||
/**
|
||||
* @brief Reverse-geocodes coordinates into street addresses via the public
|
||||
* Nominatim API (https://nominatim.openstreetmap.org).
|
||||
*
|
||||
* Required for API usage:
|
||||
* - single identifying User-Agent and at most one request per
|
||||
* second (https://operations.osmfoundation.org/policies/nominatim/).
|
||||
*/
|
||||
class NominatimAddressService final : public IAddressService {
|
||||
public:
|
||||
explicit NominatimAddressService(std::unique_ptr<WebClient> client,
|
||||
std::shared_ptr<ILogger> logger);
|
||||
|
||||
std::optional<AddressLookupResult> ReverseGeocode(
|
||||
double longitude, double latitude) override;
|
||||
|
||||
private:
|
||||
std::unique_ptr<WebClient> client_;
|
||||
std::shared_ptr<ILogger> logger_;
|
||||
};
|
||||
|
||||
#endif // BIERGARTEN_PIPELINE_INCLUDES_SERVICES_ADDRESS_NOMINATIM_ADDRESS_SERVICE_H_
|
||||
@@ -56,6 +56,8 @@ CREATE TABLE IF NOT EXISTS brewery_addresses (
|
||||
city_id INTEGER NOT NULL,
|
||||
longitude REAL NOT NULL,
|
||||
latitude REAL NOT NULL,
|
||||
address_line1 TEXT NOT NULL,
|
||||
postal_code TEXT NOT NULL,
|
||||
FOREIGN KEY(brewery_id) REFERENCES breweries(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(city_id) REFERENCES cities(id) ON DELETE CASCADE
|
||||
);
|
||||
@@ -131,8 +133,10 @@ INSERT INTO brewery_addresses (
|
||||
brewery_id,
|
||||
city_id,
|
||||
longitude,
|
||||
latitude
|
||||
) VALUES (?, ?, ?, ?);
|
||||
latitude,
|
||||
address_line1,
|
||||
postal_code
|
||||
) VALUES (?, ?, ?, ?, ?, ?);
|
||||
)sql";
|
||||
|
||||
inline constexpr std::string_view kInsertUserSql = R"sql(
|
||||
@@ -184,6 +188,8 @@ enum BreweryAddressBindIndex {
|
||||
kBreweryAddressCityIdBindIndex,
|
||||
kBreweryAddressLongitudeBindIndex,
|
||||
kBreweryAddressLatitudeBindIndex,
|
||||
kBreweryAddressLine1BindIndex,
|
||||
kBreweryAddressPostalCodeBindIndex,
|
||||
};
|
||||
|
||||
enum UserBindIndex {
|
||||
|
||||
@@ -13,10 +13,12 @@ BiergartenPipelineOrchestrator::BiergartenPipelineOrchestrator(
|
||||
std::unique_ptr<DataGenerator> generator,
|
||||
std::unique_ptr<IExportService> exporter,
|
||||
std::unique_ptr<ICuratedDataService> curated_data_service,
|
||||
std::unique_ptr<IAddressService> address_service,
|
||||
const ApplicationOptions& application_options)
|
||||
: logger_(std::move(logger)),
|
||||
context_service_(std::move(context_service)),
|
||||
generator_(std::move(generator)),
|
||||
exporter_(std::move(exporter)),
|
||||
curated_data_service_(std::move(curated_data_service)),
|
||||
address_service_(std::move(address_service)),
|
||||
application_options_(application_options) {}
|
||||
@@ -7,14 +7,21 @@
|
||||
#include <format>
|
||||
#include <optional>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "biergarten_pipeline_orchestrator.h"
|
||||
#include "biergarten_pipeline_orchestrator/coords.h"
|
||||
#include "services/address/address_service.h"
|
||||
#include "services/logging/logger.h"
|
||||
|
||||
using biergarten_pipeline_orchestrator_internal::Coords;
|
||||
using biergarten_pipeline_orchestrator_internal::GetRandomCoordsWithinRange;
|
||||
|
||||
namespace {
|
||||
constexpr std::string_view kPlaceholderAddressLine1 = "Address unavailable";
|
||||
} // namespace
|
||||
|
||||
void BiergartenPipelineOrchestrator::GenerateBreweries(
|
||||
std::span<const EnrichedCity> cities) {
|
||||
logger_->Log({.level = LogLevel::Info,
|
||||
@@ -39,10 +46,32 @@ void BiergartenPipelineOrchestrator::GenerateBreweries(
|
||||
const Coords brewery_coords = GetRandomCoordsWithinRange(
|
||||
city_centre, kMaxDistanceFromCentreKm, rng);
|
||||
|
||||
const std::optional<AddressLookupResult> geocoded =
|
||||
address_service_->ReverseGeocode(brewery_coords.longitude,
|
||||
brewery_coords.latitude);
|
||||
|
||||
if (!geocoded.has_value()) {
|
||||
logger_->Log(
|
||||
{.level = LogLevel::Warn,
|
||||
.phase = PipelinePhase::BreweryAndBeerGeneration,
|
||||
.message = std::format(
|
||||
"[Pipeline] Could not reverse geocode brewery in '{}' "
|
||||
"({}, {}); using a placeholder address.",
|
||||
enriched_city.location.city, brewery_coords.latitude,
|
||||
brewery_coords.longitude)});
|
||||
}
|
||||
|
||||
return BreweryRecord{
|
||||
.address = BreweryAddress{.city = enriched_city.location,
|
||||
.longitude = brewery_coords.longitude,
|
||||
.latitude = brewery_coords.latitude},
|
||||
.address =
|
||||
BreweryAddress{
|
||||
.city = enriched_city.location,
|
||||
.longitude = brewery_coords.longitude,
|
||||
.latitude = brewery_coords.latitude,
|
||||
.address_line1 = geocoded.has_value()
|
||||
? geocoded->address_line1
|
||||
: std::string(kPlaceholderAddressLine1),
|
||||
.postal_code =
|
||||
geocoded.has_value() ? geocoded->postal_code : ""},
|
||||
.brewery = brewery};
|
||||
} catch (const std::exception& e) {
|
||||
++skipped_count;
|
||||
|
||||
@@ -166,6 +166,26 @@ int main(const int argc, char** argv) {
|
||||
inj.template create<std::unique_ptr<WebClient>>(),
|
||||
log_producer);
|
||||
}),
|
||||
di::bind<IAddressService>().to(
|
||||
[options, &log_producer](
|
||||
const auto& inj) -> std::unique_ptr<IAddressService> {
|
||||
if (options.generator.mode == GeneratorMode::kMock) {
|
||||
log_producer->Log({.level = LogLevel::Info,
|
||||
.phase = PipelinePhase::Startup,
|
||||
.message = "Address service: mock"});
|
||||
|
||||
return std::make_unique<MockAddressService>();
|
||||
}
|
||||
|
||||
log_producer->Log(
|
||||
{.level = LogLevel::Info,
|
||||
.phase = PipelinePhase::Startup,
|
||||
.message = "Address service: Nominatim"});
|
||||
|
||||
return std::make_unique<NominatimAddressService>(
|
||||
inj.template create<std::unique_ptr<WebClient>>(),
|
||||
log_producer);
|
||||
}),
|
||||
di::bind<DataGenerator>().to([&options, &model_path, &sampling,
|
||||
&prompt_directory,
|
||||
&log_producer](const auto& inj)
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* @file services/address/nominatim_address_service.cc
|
||||
* @brief NominatimAddressService implementation.
|
||||
*/
|
||||
|
||||
#include "services/address/nominatim_address_service.h"
|
||||
|
||||
#include <boost/json.hpp>
|
||||
#include <chrono>
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
NominatimAddressService::NominatimAddressService(
|
||||
std::unique_ptr<WebClient> client, std::shared_ptr<ILogger> logger)
|
||||
: client_(std::move(client)), logger_(std::move(logger)) {}
|
||||
|
||||
std::optional<AddressLookupResult> NominatimAddressService::ReverseGeocode(
|
||||
double longitude, double latitude) {
|
||||
if (!client_) {
|
||||
if (logger_) {
|
||||
logger_->Log({.level = LogLevel::Warn,
|
||||
.phase = PipelinePhase::Enrichment,
|
||||
.message = "Nominatim client is nullptr."});
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const std::string url = std::format(
|
||||
"https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat={}&"
|
||||
"lon={}&zoom=18&addressdetails=1",
|
||||
latitude, longitude);
|
||||
|
||||
std::string body;
|
||||
try {
|
||||
body = client_->Get(url);
|
||||
} catch (const std::runtime_error& e) {
|
||||
if (logger_) {
|
||||
logger_->Log({.level = LogLevel::Warn,
|
||||
.phase = PipelinePhase::Enrichment,
|
||||
.message = std::format(
|
||||
"NominatimAddressService: request failed for "
|
||||
"({}, {}): {}",
|
||||
latitude, longitude, e.what())});
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
{
|
||||
using namespace std::literals::chrono_literals;
|
||||
std::this_thread::sleep_for(1s);
|
||||
}
|
||||
|
||||
system::error_code ec;
|
||||
const json::value doc = json::parse(body, ec);
|
||||
|
||||
if (ec) {
|
||||
if (logger_) {
|
||||
logger_->Log({.level = LogLevel::Warn,
|
||||
.phase = PipelinePhase::Enrichment,
|
||||
.message = std::format(
|
||||
"NominatimAddressService: JSON parse error for "
|
||||
"({}, {}): {}",
|
||||
latitude, longitude, ec.message())});
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const json::object* obj = doc.if_object();
|
||||
if ((obj == nullptr) || obj->contains("error")) {
|
||||
if (logger_) {
|
||||
logger_->Log(
|
||||
{.level = LogLevel::Warn,
|
||||
.phase = PipelinePhase::Enrichment,
|
||||
.message = std::format("NominatimAddressService: no address "
|
||||
"found for ({}, {})",
|
||||
latitude, longitude)});
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const json::value* address_ptr = obj->if_contains("address");
|
||||
if ((address_ptr == nullptr) || !address_ptr->is_object()) {
|
||||
if (logger_) {
|
||||
logger_->Log(
|
||||
{.level = LogLevel::Warn,
|
||||
.phase = PipelinePhase::Enrichment,
|
||||
.message = std::format("NominatimAddressService: missing "
|
||||
"'address' for ({}, {})",
|
||||
latitude, longitude)});
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const json::object& address = address_ptr->get_object();
|
||||
|
||||
const auto extract_string = [&address](std::string_view key) -> std::string {
|
||||
const json::value* value_ptr = address.if_contains(key);
|
||||
if ((value_ptr == nullptr) || !value_ptr->is_string()) {
|
||||
return {};
|
||||
}
|
||||
return std::string(value_ptr->get_string());
|
||||
};
|
||||
|
||||
const std::string house_number = extract_string("house_number");
|
||||
const std::string road = extract_string("road");
|
||||
const std::string postal_code = extract_string("postcode");
|
||||
|
||||
std::string address_line1 = road;
|
||||
if (!house_number.empty()) {
|
||||
address_line1 = address_line1.empty() ? house_number
|
||||
: house_number + " " + road;
|
||||
}
|
||||
|
||||
if (address_line1.empty()) {
|
||||
if (logger_) {
|
||||
logger_->Log(
|
||||
{.level = LogLevel::Warn,
|
||||
.phase = PipelinePhase::Enrichment,
|
||||
.message = std::format("NominatimAddressService: no usable "
|
||||
"street address for ({}, {})",
|
||||
latitude, longitude)});
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return AddressLookupResult{.address_line1 = address_line1,
|
||||
.postal_code = postal_code};
|
||||
}
|
||||
@@ -184,6 +184,20 @@ uint64_t SqliteExportService::ProcessRecord(const BreweryRecord& brewery) {
|
||||
sqlite_export_service_internal::kBreweryAddressLatitudeBindIndex,
|
||||
.value = brewery.address.latitude,
|
||||
.action = "Failed to bind SQLite brewery address latitude"});
|
||||
sqlite_export_service_internal::Bind(
|
||||
insert_brewery_address_stmt_,
|
||||
sqlite_export_service_internal::BoundParam<std::string_view>{
|
||||
.index =
|
||||
sqlite_export_service_internal::kBreweryAddressLine1BindIndex,
|
||||
.value = brewery.address.address_line1,
|
||||
.action = "Failed to bind SQLite brewery address line 1"});
|
||||
sqlite_export_service_internal::Bind(
|
||||
insert_brewery_address_stmt_,
|
||||
sqlite_export_service_internal::BoundParam<std::string_view>{
|
||||
.index = sqlite_export_service_internal::
|
||||
kBreweryAddressPostalCodeBindIndex,
|
||||
.value = brewery.address.postal_code,
|
||||
.action = "Failed to bind SQLite brewery address postal code"});
|
||||
|
||||
sqlite_export_service_internal::StepStatement(
|
||||
db_handle_, insert_brewery_address_stmt_,
|
||||
|
||||
Reference in New Issue
Block a user