Skip to content

Commit 24a58fe

Browse files
committed
Builds successfully
1 parent be5ca08 commit 24a58fe

File tree

7 files changed

+862
-125
lines changed

7 files changed

+862
-125
lines changed

CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,6 @@ target_link_libraries(thales PRIVATE thales_lib)
117117
# Tests
118118
add_subdirectory(tests)
119119

120-
# Examples
121-
add_executable(black_scholes_example examples/black_scholes_example.cpp)
122-
target_link_libraries(black_scholes_example PRIVATE thales_lib)
123-
124-
# Memory issues example for Address Sanitizer testing
125-
add_executable(memory_issues_example examples/memory_issues_example.cpp)
126-
target_link_libraries(memory_issues_example PRIVATE thales_lib)
127-
128120
# Install targets
129121
install(TARGETS thales thales_lib
130122
RUNTIME DESTINATION bin

examples/black_scholes_example.cpp

Lines changed: 0 additions & 106 deletions
This file was deleted.

include/thales/utils/db_logger.h

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
#ifndef THALES_UTILS_DB_LOGGER_H
2+
#define THALES_UTILS_DB_LOGGER_H
3+
4+
#include <memory>
5+
#include <mutex>
6+
#include <string>
7+
#include <queue>
8+
#include <thread>
9+
#include <atomic>
10+
#include <condition_variable>
11+
12+
namespace thales {
13+
namespace utils {
14+
15+
/**
16+
* @class DbLogger
17+
* @brief Database logger class for the trading bot.
18+
*
19+
* This class provides thread-safe logging functionality to a PostgreSQL database.
20+
* It implements a singleton pattern and uses asynchronous logging with a queue
21+
* to minimize impact on trading performance.
22+
*/
23+
class DbLogger {
24+
public:
25+
/**
26+
* @brief Get the singleton instance of the database logger
27+
* @return Reference to the database logger instance
28+
*/
29+
static DbLogger& getInstance();
30+
31+
/**
32+
* @brief Initialize the database logger
33+
* @param dbHost Database host
34+
* @param dbPort Database port
35+
* @param dbName Database name
36+
* @param dbUser Database user
37+
* @param dbPassword Database password
38+
* @param maxQueueSize Maximum size of the log queue
39+
* @param batchSize Number of logs to insert in a batch
40+
* @return true if initialization was successful, false otherwise
41+
*/
42+
static bool initialize(
43+
const std::string& dbHost = "localhost",
44+
int dbPort = 5432,
45+
const std::string& dbName = "thales",
46+
const std::string& dbUser = "thales_user",
47+
const std::string& dbPassword = "",
48+
size_t maxQueueSize = 10000,
49+
size_t batchSize = 100
50+
);
51+
52+
/**
53+
* @brief Log a trade execution to the database
54+
* @param strategyName Name of the strategy that generated the trade
55+
* @param symbol Trading symbol
56+
* @param orderId Order ID
57+
* @param executionId Execution ID
58+
* @param side Trade side (BUY or SELL)
59+
* @param quantity Trade quantity
60+
* @param price Trade price
61+
* @param commission Trade commission
62+
* @param totalValue Total value of the trade
63+
* @param executionTime Time of execution
64+
* @param accountId Account ID
65+
* @param exchange Exchange name
66+
* @param orderType Order type
67+
* @param isOption Whether the trade is for an option
68+
* @param optionData JSON string with option-specific data
69+
* @param additionalData JSON string with additional data
70+
* @return true if the log was queued successfully, false otherwise
71+
*/
72+
bool log_trade_execution(
73+
const std::string& strategy_name,
74+
const std::string& symbol,
75+
const std::string& order_id,
76+
const std::string& execution_id,
77+
const std::string& side,
78+
double quantity,
79+
double price,
80+
double commission,
81+
double total_value,
82+
const std::string& execution_time,
83+
const std::string& account_id,
84+
const std::string& exchange,
85+
const std::string& order_type,
86+
bool is_option = false,
87+
const std::string& option_data = "{}",
88+
const std::string& additional_data = "{}"
89+
);
90+
91+
/**
92+
* @brief Shutdown the database logger
93+
*
94+
* This method stops the worker thread and flushes any remaining logs.
95+
*/
96+
void shutdown();
97+
98+
/**
99+
* @brief Check if the database logger is connected
100+
* @return true if connected, false otherwise
101+
*/
102+
bool is_connected() const;
103+
104+
/**
105+
* @brief Get the number of logs in the queue
106+
* @return Number of logs in the queue
107+
*/
108+
size_t get_queue_size() const;
109+
110+
/**
111+
* @brief Get the number of failed log insertions
112+
* @return Number of failed log insertions
113+
*/
114+
size_t get_failed_count() const;
115+
116+
private:
117+
// Private constructor for singleton pattern
118+
DbLogger();
119+
120+
// Destructor
121+
~DbLogger();
122+
123+
// Prevent copying and assignment
124+
DbLogger(const DbLogger&) = delete;
125+
DbLogger& operator=(const DbLogger&) = delete;
126+
127+
// Structure to hold a trade execution log
128+
struct TradeExecutionLog {
129+
std::string strategy_name;
130+
std::string symbol;
131+
std::string order_id;
132+
std::string execution_id;
133+
std::string side;
134+
double quantity;
135+
double price;
136+
double commission;
137+
double total_value;
138+
std::string execution_time;
139+
std::string account_id;
140+
std::string exchange;
141+
std::string order_type;
142+
bool is_option;
143+
std::string option_data;
144+
std::string additional_data;
145+
std::string timestamp;
146+
};
147+
148+
// Connect to the database
149+
bool connect();
150+
151+
// Disconnect from the database
152+
void disconnect();
153+
154+
// Get the connection string
155+
std::string get_connection_string() const;
156+
157+
// Create the necessary tables if they don't exist
158+
bool create_tables_if_not_exist();
159+
160+
// Worker thread function
161+
void worker_thread();
162+
163+
// Process logs in the queue
164+
void process_logs();
165+
166+
// Insert a batch of logs
167+
bool insert_log_batch(const std::vector<TradeExecutionLog>& logs);
168+
169+
// Get the current timestamp
170+
static std::string get_current_timestamp();
171+
172+
// Instance variables
173+
std::mutex mutex_;
174+
std::string db_host_;
175+
int db_port_;
176+
std::string db_name_;
177+
std::string db_user_;
178+
std::string db_password_;
179+
size_t max_queue_size_;
180+
size_t batch_size_;
181+
182+
// Connection status
183+
std::atomic<bool> connected_;
184+
185+
// Queue for logs
186+
std::queue<TradeExecutionLog> log_queue_;
187+
std::mutex queue_mutex_;
188+
std::condition_variable queue_condition_;
189+
190+
// Worker thread
191+
std::thread worker_thread_;
192+
std::atomic<bool> running_;
193+
194+
// Statistics
195+
std::atomic<size_t> failed_count_;
196+
197+
// Singleton instance
198+
static std::unique_ptr<DbLogger> instance_;
199+
};
200+
201+
} // namespace utils
202+
} // namespace thales
203+
204+
#endif // THALES_UTILS_DB_LOGGER_H

include/thales/utils/logger.h

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,49 @@ class Logger {
8383
*/
8484
void error(const std::string& message);
8585

86-
/**
87-
* @brief Log a message with FATAL level
88-
* @param message The message to log
89-
*/
90-
void fatal(const std::string& message);
86+
/**
87+
* @brief Log a message with FATAL level
88+
* @param message The message to log
89+
*/
90+
void fatal(const std::string& message);
91+
92+
/**
93+
* @brief Log a trade execution
94+
* @param strategyName Name of the strategy that generated the trade
95+
* @param symbol Trading symbol
96+
* @param orderId Order ID
97+
* @param executionId Execution ID
98+
* @param side Trade side (BUY or SELL)
99+
* @param quantity Trade quantity
100+
* @param price Trade price
101+
* @param commission Trade commission
102+
* @param totalValue Total value of the trade
103+
* @param executionTime Time of execution
104+
* @param accountId Account ID
105+
* @param exchange Exchange name
106+
* @param orderType Order type
107+
* @param isOption Whether the trade is for an option
108+
* @param optionData JSON string with option-specific data
109+
* @param additionalData JSON string with additional data
110+
*/
111+
void logTradeExecution(
112+
const std::string& strategy_name,
113+
const std::string& symbol,
114+
const std::string& order_id,
115+
const std::string& execution_id,
116+
const std::string& side,
117+
double quantity,
118+
double price,
119+
double commission,
120+
double total_value,
121+
const std::string& execution_time,
122+
const std::string& account_id,
123+
const std::string& exchange,
124+
const std::string& order_type,
125+
bool is_option = false,
126+
const std::string& option_data = "{}",
127+
const std::string& additional_data = "{}"
128+
);
91129

92130
/**
93131
* @brief Set the minimum log level for console output

0 commit comments

Comments
 (0)