Skip to content

Commit

Permalink
Merge pull request #464 from imperialCHEPI/combined_input_module
Browse files Browse the repository at this point in the history
Move config-related code into combined `HeathGPS.Input` module
  • Loading branch information
alexdewar authored Jul 1, 2024
2 parents b31cde0 + 9e19e45 commit 46677a6
Show file tree
Hide file tree
Showing 44 changed files with 240 additions and 252 deletions.
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ add_compile_definitions(TBB_USE_ASSERT)

# Include sub-projects.
add_subdirectory(HealthGPS.Core)
add_subdirectory(HealthGPS.Datastore)
add_subdirectory(HealthGPS.Input)
add_subdirectory(HealthGPS)
add_subdirectory(HealthGPS.Console)

Expand All @@ -23,7 +23,7 @@ endif()

# PUBLIC needed to make heathers and library available to other project
target_include_directories(HealthGPS.Core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(HealthGPS.Datastore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(HealthGPS.Input PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(HealthGPS PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

# External include directory
Expand Down
22 changes: 3 additions & 19 deletions src/HealthGPS.Console/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
find_package(fmt CONFIG REQUIRED)
find_package(cxxopts CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
find_package(TBB CONFIG REQUIRED)
find_path(RAPIDCSV_INCLUDE_DIRS "rapidcsv.h")
find_package(Threads REQUIRED)
Expand All @@ -10,27 +9,14 @@ add_library(
HealthGPS.LibConsole STATIC
"command_options.cpp"
"command_options.h"
"configuration.cpp"
"configuration.h"
"configuration_parsing.cpp"
"configuration_parsing.h"
"configuration_parsing_helpers.h"
"csvparser.cpp"
"csvparser.h"
"event_monitor.cpp"
"event_monitor.h"
"model_parser.cpp"
"model_parser.h"
"program.cpp"
"result_file_writer.cpp"
"result_file_writer.h"
"jsonparser.cpp"
"jsonparser.h"
"model_info.h"
"resource.h"
"result_writer.h"
"riskmodel.h"
"poco.h")
"result_writer.h")

# Under Windows, we also include a resource file to the build
if(WIN32)
Expand All @@ -45,24 +31,22 @@ target_include_directories(HealthGPS.LibConsole PUBLIC ${CMAKE_CURRENT_BINARY_DI
target_link_libraries(
HealthGPS.Console
PRIVATE HealthGPS.Core
HealthGPS.Datastore
HealthGPS.Input
HealthGPS.LibConsole
HealthGPS
fmt::fmt
Threads::Threads
cxxopts::cxxopts
nlohmann_json::nlohmann_json
TBB::tbb)

target_link_libraries(
HealthGPS.LibConsole
PRIVATE HealthGPS.Core
HealthGPS.Datastore
HealthGPS.Input
HealthGPS
fmt::fmt
Threads::Threads
cxxopts::cxxopts
nlohmann_json::nlohmann_json
TBB::tbb)

install(TARGETS HealthGPS.Console DESTINATION "")
Expand Down
4 changes: 2 additions & 2 deletions src/HealthGPS.Console/command_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <iostream>

namespace host {
namespace hgps {

cxxopts::Options create_options() {
cxxopts::Options options("HealthGPS.Console", "Health-GPS microsimulation for policy options.");
Expand Down Expand Up @@ -95,4 +95,4 @@ CommandOptions parse_arguments(cxxopts::Options &options, int &argc, char *argv[

return cmd;
}
} // namespace host
} // namespace hgps
4 changes: 2 additions & 2 deletions src/HealthGPS.Console/command_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <filesystem>

namespace host {
namespace hgps {
/// @brief Defines the Command Line Interface (CLI) arguments options
struct CommandOptions {
/// @brief Indicates whether the argument parsing succeed
Expand Down Expand Up @@ -40,4 +40,4 @@ cxxopts::Options create_options();
/// @return User command-line options
CommandOptions parse_arguments(cxxopts::Options &options, int &argc, char *argv[]);

} // namespace host
} // namespace hgps
4 changes: 2 additions & 2 deletions src/HealthGPS.Console/event_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <fmt/color.h>
#include <fmt/core.h>

namespace host {
namespace hgps {
EventMonitor::EventMonitor(hgps::EventAggregator &event_bus, ResultWriter &result_writer)
: result_writer_{result_writer}, tg_{tg_context_} {
handlers_.emplace_back(event_bus.subscribe(hgps::EventType::runner, [this](auto &&PH1) {
Expand Down Expand Up @@ -97,4 +97,4 @@ void EventMonitor::result_dispatch_thread() {

fmt::print(fg(fmt::color::gray), "Result event thread exited.\n");
}
} // namespace host
} // namespace hgps
10 changes: 5 additions & 5 deletions src/HealthGPS.Console/event_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

#include "result_writer.h"

namespace host {
namespace hgps {
/// @brief Defined the event monitor class used for processing Health-GPS event messages
///
/// All notification messages are written to the terminal, while the results messages are
/// queued to be processed by the host::ResultWriter instance provide at construction.
/// queued to be processed by the hgps::ResultWriter instance provide at construction.
class EventMonitor final : public hgps::EventMessageVisitor {
public:
EventMonitor() = delete;

/// @brief Initialises a new instance of the host::EventMonitor class.
/// @brief Initialises a new instance of the hgps::EventMonitor class.
/// @param event_bus The message bus instance to monitor
/// @param result_writer The results message writer instance
EventMonitor(hgps::EventAggregator &event_bus, ResultWriter &result_writer);

/// @brief Destroys a host::EventMonitor instance
/// @brief Destroys a hgps::EventMonitor instance
~EventMonitor() noexcept;

/// @brief Stops the monitor, no new messages are processed after stop
Expand All @@ -48,4 +48,4 @@ class EventMonitor final : public hgps::EventMessageVisitor {
void info_dispatch_thread();
void result_dispatch_thread();
};
} // namespace host
} // namespace hgps
4 changes: 2 additions & 2 deletions src/HealthGPS.Console/model_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <fmt/format.h>
#include <string>

namespace host {
namespace hgps {
/// @brief Simulation experiment run-time information for reproducibility.
struct ExperimentInfo {
/// @brief The model name
Expand All @@ -28,4 +28,4 @@ struct ExperimentInfo {
seed);
}
};
} // namespace host
} // namespace hgps
36 changes: 26 additions & 10 deletions src/HealthGPS.Console/program.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "configuration.h"
#include "csvparser.h"
#include "model_parser.h"

#include "HealthGPS.Core/thread_util.h"
#include "HealthGPS.Datastore/api.h"
#include "HealthGPS.Input/api.h"
#include "HealthGPS.Input/configuration.h"
#include "HealthGPS.Input/csvparser.h"
#include "HealthGPS.Input/model_parser.h"
#include "HealthGPS/api.h"
#include "HealthGPS/event_bus.h"
#include "command_options.h"
#include "event_monitor.h"
#include "model_info.h"
#include "result_file_writer.h"

#include <fmt/chrono.h>
#include <fmt/color.h>
Expand All @@ -15,6 +17,7 @@
#include <cstdlib>
#include <oneapi/tbb/global_control.h>

namespace {
/// @brief Get a string representation of current system time
/// @return The system time as string
std::string get_time_now_str() {
Expand All @@ -31,6 +34,18 @@ void print_app_title() {
tbb::global_control::active_value(tbb::global_control::max_allowed_parallelism));
}

hgps::ResultFileWriter create_results_file_logger(const hgps::input::Configuration &config,
const hgps::ModelInput &input) {
return {create_output_file_name(config.output, config.job_id),
hgps::ExperimentInfo{.model = config.app_name,
.version = config.app_version,
.intervention = config.active_intervention
? config.active_intervention->identifier
: "",
.job_id = config.job_id,
.seed = input.seed().value_or(0u)}};
}

/// @brief Prints application exit message
/// @param exit_code The application exit code
/// @return The respective exit code
Expand All @@ -40,14 +55,15 @@ int exit_application(int exit_code) {
fmt::print(" {}.\n\n", get_time_now_str());
return exit_code;
}
} // anonymous namespace

/// @brief Health-GPS host application entry point
/// @param argc The number of command arguments
/// @param argv The list of arguments provided
/// @return The application exit code
int main(int argc, char *argv[]) { // NOLINT(bugprone-exception-escape)
using namespace hgps;
using namespace host;
using namespace hgps::input;

// Set thread limit from OMP_THREAD_LIMIT, if set in environment.
char *env_threads = std::getenv("OMP_THREAD_LIMIT");
Expand All @@ -73,7 +89,7 @@ int main(int argc, char *argv[]) { // NOLINT(bugprone-exception-escape)
// Parse inputs configuration file, *.json.
Configuration config;
try {
config = get_configuration(cmd_args);
config = get_configuration(cmd_args.config_file, cmd_args.job_id, cmd_args.verbose);
} catch (const std::exception &ex) {
fmt::print(fg(fmt::color::red), "\n\nInvalid configuration - {}.\n", ex.what());
return exit_application(EXIT_FAILURE);
Expand All @@ -97,7 +113,7 @@ int main(int argc, char *argv[]) { // NOLINT(bugprone-exception-escape)
try {
#endif
// Create back-end data store, cached data repository wrapper
auto data_api = data::DataManager(cmd_args.data_path_or_url, config.verbosity);
auto data_api = input::DataManager(cmd_args.data_path_or_url, config.verbosity);
auto data_repository = hgps::CachedRepository{data_api};

// Register the input risk factors model definitions
Expand Down Expand Up @@ -178,8 +194,8 @@ int main(int argc, char *argv[]) { // NOLINT(bugprone-exception-escape)

// NOLINTBEGIN(modernize-concat-nested-namespaces)
/// @brief Top-level namespace for Health-GPS Console host application
namespace host {
namespace hgps {
/// @brief Internal details namespace for private data types and functions
namespace detail {}
} // namespace host
} // namespace hgps
// NOLINTEND(modernize-concat-nested-namespaces)
4 changes: 2 additions & 2 deletions src/HealthGPS.Console/result_file_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <sstream>
#include <utility>

namespace host {
namespace hgps {
ResultFileWriter::ResultFileWriter(const std::filesystem::path &file_name, ExperimentInfo info)
: info_{std::move(info)} {
stream_.open(file_name, std::ofstream::out | std::ofstream::app);
Expand Down Expand Up @@ -180,4 +180,4 @@ void ResultFileWriter::write_csv_channels(const hgps::ResultEventMessage &messag
fss.clear();
}
}
} // namespace host
} // namespace hgps
8 changes: 4 additions & 4 deletions src/HealthGPS.Console/result_file_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "model_info.h"
#include "result_writer.h"

namespace host {
namespace hgps {
/// @brief Defines the results message file stream writer class
///
/// The message content are written to a JSON (JavaScript Object Notation) file,
Expand All @@ -17,7 +17,7 @@ namespace host {
class ResultFileWriter final : public ResultWriter {
public:
ResultFileWriter() = delete;
/// @brief Initialises an instance of the host::ResultFileWriter class.
/// @brief Initialises an instance of the hgps::ResultFileWriter class.
/// @param file_name The JSON output file full name
/// @param info The associated experiment information
ResultFileWriter(const std::filesystem::path &file_name, ExperimentInfo info);
Expand All @@ -27,7 +27,7 @@ class ResultFileWriter final : public ResultWriter {
ResultFileWriter(ResultFileWriter &&other) noexcept;
ResultFileWriter &operator=(ResultFileWriter &&other) noexcept;

/// @brief Destroys a host::ResultFileWriter instance
/// @brief Destroys a hgps::ResultFileWriter instance
~ResultFileWriter();
void write(const hgps::ResultEventMessage &message) override;

Expand All @@ -45,4 +45,4 @@ class ResultFileWriter final : public ResultWriter {
void write_csv_channels(const hgps::ResultEventMessage &message);
void write_csv_header(const hgps::ResultEventMessage &message);
};
} // namespace host
} // namespace hgps
6 changes: 3 additions & 3 deletions src/HealthGPS.Console/result_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

#include "HealthGPS/result_message.h"

namespace host {
namespace hgps {
/// @brief Defines the Health-GPS results message writer interface
class ResultWriter {
public:
/// @brief Destroys a host::ResultWriter instance
/// @brief Destroys a hgps::ResultWriter instance
virtual ~ResultWriter() = default;

/// @brief Writes the hgps::ResultEventMessage contents to a stream
/// @param message The message instance to process
virtual void write(const hgps::ResultEventMessage &message) = 0;
};
} // namespace host
} // namespace hgps
24 changes: 0 additions & 24 deletions src/HealthGPS.Datastore/CMakeLists.txt

This file was deleted.

37 changes: 37 additions & 0 deletions src/HealthGPS.Input/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
find_package(fmt CONFIG REQUIRED)
find_package(jsoncons CONFIG REQUIRED)
find_package(libzippp CONFIG REQUIRED)
find_package(unofficial-curlpp CONFIG REQUIRED)

add_library(HealthGPS.Input STATIC "")
target_compile_features(HealthGPS.Input PUBLIC cxx_std_${CMAKE_CXX_STANDARD})

target_sources(
HealthGPS.Input
PRIVATE "api.h"
"configuration.cpp"
"configuration.h"
"configuration_parsing.cpp"
"configuration_parsing.h"
"configuration_parsing_helpers.h"
"csvparser.cpp"
"csvparser.h"
"datamanager.cpp"
"datamanager.h"
"download_file.cpp"
"download_file.h"
"jsonparser.cpp"
"jsonparser.h"
"model_parser.cpp"
"model_parser.h"
"poco.h"
"riskmodel.h"
"schema.cpp"
"schema.h"
"zip_file.cpp"
"zip_file.h")

target_link_libraries(HealthGPS.Input PRIVATE HealthGPS HealthGPS.Core fmt::fmt jsoncons
libzippp::libzippp unofficial::curlpp::curlpp)

set(ROOT_NAMESPACE hgps::input)
File renamed without changes.
Loading

0 comments on commit 46677a6

Please sign in to comment.