Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

DB Utility Tool #96

Merged
merged 12 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ endforeach()

# Boost
find_package(Boost 1.74.0 COMPONENTS REQUIRED
program_options
serialization
system
thread
Expand Down
16 changes: 9 additions & 7 deletions functions.cmake
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
# Create module library
function(make_lib module srcs link_libs inc_dirs compile_defs)
add_library(${module} INTERFACE ${srcs})
target_compile_definitions(${module} INTERFACE ${compile_defs})
target_link_libraries(${module} INTERFACE ${link_libs})
add_library(${module} ${srcs})
ament_target_dependencies(${module} PUBLIC ${ROS_DEPS})
target_compile_definitions(${module} PUBLIC ${compile_defs})
target_link_libraries(${module} PUBLIC ${link_libs})
target_include_directories(
${module} INTERFACE
${module} PUBLIC
${CMAKE_CURRENT_LIST_DIR}/inc
${CMAKE_SOURCE_DIR}/lib
${inc_dirs}
)
add_dependencies(${module} ${AUTOGEN_TARGETS})
set(${module}_inc_dir ${CMAKE_CURRENT_LIST_DIR}/inc CACHE INTERNAL "${module} header include directory")
endfunction()

# Create module ROS executable
function(make_exe module srcs link_libs inc_dirs ${compile_defs})
# Create project module ROS executable
function(make_exe module srcs link_libs inc_dirs compile_defs)
set(bin_module bin_${module})
add_executable(${bin_module} ${srcs})
target_compile_definitions(${bin_module} PUBLIC ${compile_defs})
ament_target_dependencies(${bin_module} PUBLIC ${ROS_DEPS})
target_link_libraries(${bin_module} PUBLIC ${link_libs})
target_link_libraries(${bin_module} PUBLIC ${link_libs} boost_program_options)
target_include_directories(
${bin_module} PUBLIC
${CMAKE_CURRENT_LIST_DIR}/inc
Expand Down
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_subdirectory(cmn_hdrs)
add_subdirectory(protofiles)
add_subdirectory(sailbot_db)

# add directories as needed
36 changes: 36 additions & 0 deletions lib/sailbot_db/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
set(module sailbot_db)

set(link_libs
${PROTOBUF_LINK_LIBS}
mongo::mongocxx_shared
mongo::bsoncxx_shared
)

set(inc_dirs
${PROTOBUF_INCLUDE_PATH}
${LIBMONGOCXX_INCLUDE_DIRS}
${LIBBSONCXX_INCLUDE_DIRS}
)

set(srcs
${CMAKE_CURRENT_LIST_DIR}/src/sailbot_db.cpp
${CMAKE_CURRENT_LIST_DIR}/src/util_db.cpp
)

# make sailbot_db library
make_lib(${module} "${srcs}" "${link_libs}" "${inc_dirs}" "${compile_defs}")

set(bin_srcs
${srcs}
${CMAKE_CURRENT_LIST_DIR}/src/main.cpp
)

# Make executable
make_exe(${module} "${bin_srcs}" "${link_libs}" "${inc_dirs}" "${compile_defs}")

# Create unit test
set(test_srcs
${srcs}
${CMAKE_CURRENT_LIST_DIR}/test/test_sailbot_db.cpp
)
make_unit_test(${module} "${test_srcs}" "${link_libs}" "${inc_dirs}" "${compile_defs}")
Original file line number Diff line number Diff line change
Expand Up @@ -10,88 +10,8 @@
#include "sensors.pb.h"
#include "waypoint.pb.h"

// BSON document formats (from: https://ubcsailbot.atlassian.net/wiki/spaces/prjt22/pages/1907589126/Database+Schemas):

// GPS
// {
// latitude: decimal,
// longitude: decimal,
// speed: decimal,
// heading: decimal,
// timestamp: <year - 2000>-<month>-<day> <hour>:<minute>:<second>
// }

// Global Path
// {
// waypoints: [
// {
// latitude: decimal,
// longitude: decimal
// }
// ],
// timestamp: <year - 2000>-<month>-<day> <hour>:<minute>:<second>
// }

// Local Path
// {
// waypoints: [
// {
// latitude: decimal,
// longitude: decimal
// }
// ],
// timestamp: <year - 2000>-<month>-<day> <hour>:<minute>:<second>
// }

// AIS Ships
// {
// ships: [
// {
// id: Number,
// latitude: decimal,
// longitude: decimal,
// cog: decimal,
// rot: decimal,
// sog: decimal,
// width: decimal,
// length: decimal
// }
// ],
// timestamp: <year - 2000>-<month>-<day> <hour>:<minute>:<second>
// }

// Generic Sensors
// {
// genericSensors: [
// {
// id: integer
// data: long
// }
// ],
// timestamp: <year - 2000>-<month>-<day> <hour>:<minute>:<second>
// }

// Wind Sensors
// {
// windSensors: [
// {
// speed: decimal,
// direction: number
// }
// ],
// timestamp: <year - 2000>-<month>-<day> <hour>:<minute>:<second>
// }

// Batteries
// {
// batteries: [
// {
// voltage: decimal,
// current: decimal
// }
// ],
// timestamp: <year - 2000>-<month>-<day> <hour>:<minute>:<second>
// }
// >>>>IMPORTANT<<<<<
// BSON document formats from: https://ubcsailbot.atlassian.net/wiki/spaces/prjt22/pages/1907589126/Database+Schemas:

const std::string COLLECTION_AIS_SHIPS = "ais_ships";
const std::string COLLECTION_BATTERIES = "batteries";
Expand Down Expand Up @@ -125,14 +45,15 @@ class SailbotDB
/**
* @brief overload stream operator
*/
friend std::ostream & operator<<(std::ostream & os, const RcvdMsgInfo & info)
{
os << "Latitude: " << info.lat_ << "\n"
<< "Longitude: " << info.lon_ << "\n"
<< "Accuracy (km): " << info.cep_ << "\n"
<< "Timestamp: " << info.timestamp_;
return os;
}
friend std::ostream & operator<<(std::ostream & os, const RcvdMsgInfo & info);

/**
* @brief Get a properly formatted timestamp string
*
* @param tm standard C/C++ time structure
* @return tm converted to a timestamp string
*/
static std::string mkTimestamp(const std::tm & tm);
};

/**
Expand All @@ -141,7 +62,7 @@ class SailbotDB
* @param db_name name of desired database
* @param mongodb_conn_str URL for mongodb database (ex. mongodb://localhost:27017)
*/
explicit SailbotDB(const std::string & db_name, const std::string & mongodb_conn_str);
SailbotDB(const std::string & db_name, const std::string & mongodb_conn_str);

/**
* @brief Format and print a document in the DB
Expand Down
114 changes: 114 additions & 0 deletions lib/sailbot_db/inc/util_db.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#pragma once

#include <random>
#include <span>

#include "sailbot_db.h"
#include "sensors.pb.h"
#include "utils/utils.h"

class UtilDB : public SailbotDB
{
public:
static constexpr int NUM_AIS_SHIPS = 15; // arbitrary number
static constexpr int NUM_GENERIC_SENSORS = 5; // arbitrary number
static constexpr int NUM_PATH_WAYPOINTS = 5; // arbitrary number

/**
* @brief Construct a UtilDB, which has debug utilities for SailbotDB
*
* @param db_name
* @param mongodb_conn_str
* @param rng
*/
UtilDB(const std::string & db_name, const std::string & mongodb_conn_str, std::shared_ptr<std::mt19937> rng);

/**
* @brief Delete all documents in all collections
*/
void cleanDB();

/**
* @brief Generate random data for all sensors
*
* @return Sensors object
*/
Polaris::Sensors genRandSensors();

/**
* @return timestamp for the current time
*/
static std::tm getTimestamp();

/**
* @brief Generate random sensors and Iridium msg info
*
* @param tm Timestamp returned by getTimestamp() (with any modifications made to it)
* @return std::pair<Sensors, SailbotDB::RcvdMsgInfo>
*/
std::pair<Polaris::Sensors, SailbotDB::RcvdMsgInfo> genRandData(const std::tm & tm);

/**
* @brief Query the database and check that the sensor and message are correct
*
* @param expected_sensors
* @param expected_msg_info
*/
bool verifyDBWrite(
std::span<Polaris::Sensors> expected_sensors, std::span<SailbotDB::RcvdMsgInfo> expected_msg_info);

/**
* @brief Dump and check all sensors and timestamps from the database
*
* @param tracker FailureTracker that gets if any unexpected results are dumped
* @param expected_num_docs Expected number of documents. tracker is updated if there's a mismatch
* @return std::pair{Vector of dumped Sensors, Vector of dumped timestamps}
*/
std::pair<std::vector<Polaris::Sensors>, std::vector<std::string>> dumpSensors(
utils::FailTracker & tracker, size_t expected_num_docs = 1);

private:
std::shared_ptr<std::mt19937> rng_; // random number generator

/**
* @brief generate random GPS data
*
* @param gps_data GPS data to modify
*/
void genRandGpsData(Polaris::Sensors::Gps & gps_data);

/**
* @brief generate random ais ships data
*
* @param ais_ship AIS ship data to modify
*/
void genRandAisData(Polaris::Sensors::Ais & ais_ship);

/**
* @brief generate random generic sensor data
*
* @param generic_sensor Generic sensor data to modify
*/
void genRandGenericSensorData(Polaris::Sensors::Generic & generic_sensor);

/**
* @brief generate random battery data
*
* @param battery battery data to modify
*/
void genRandBatteryData(Polaris::Sensors::Battery & battery);

/**
* @brief generate random wind sensors data
*
* @param wind_data Wind sensor data to modify
*/
void genRandWindData(Polaris::Sensors::Wind & wind_data);

/**
* @brief generate random path data
*
* @param path_data Path data to modify
*/
void genRandPathData(Polaris::Sensors::Path & path_data);
};
Loading