139 lines
4.3 KiB
C++
139 lines
4.3 KiB
C++
#include "lib/orderman/actors/SODPosition.h"
|
|
#include "lib/config/config.h"
|
|
#include "lib/config/protos/orders_fills_cleanup.pb.h"
|
|
#include "lib/api/service_helpers/AdminServiceUtils.h"
|
|
#include "lib/config/protos/toggles.pb.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/utils/PBUtils.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
#include <google/protobuf/timestamp.pb.h>
|
|
#include <sstream>
|
|
#include <sys/time.h>
|
|
|
|
namespace bts {
|
|
namespace orderman {
|
|
|
|
SODPosition::SODPosition()
|
|
:
|
|
SODPosition(sod_time()) {
|
|
}
|
|
|
|
SODPosition::SODPosition(timeval sod_time)
|
|
:
|
|
_sod_time(sod_time){
|
|
LOG(persist, Info, "SODPosition::cons sod_time:{}", sod_time.tv_sec);
|
|
}
|
|
|
|
// returns the timeval for today's hh,mm,ss (in local machine terms)
|
|
// (timeval is the microseconds from epoch)
|
|
timeval SODPosition::to_timeval(uint16_t hh, uint16_t mm, uint16_t ss) {
|
|
// function does the conversion the following way:
|
|
// 1. gets the now secs from epoch using time
|
|
// 2. converts it into local time (so we have now local time)
|
|
// 3. overrides the hh, mm, ss passed
|
|
// 4. converts it back into secs from epoch using mktime
|
|
bts_assert(hh < 24);
|
|
bts_assert(mm < 60);
|
|
bts_assert(ss < 60);
|
|
time_t secs;
|
|
time(&secs); // seconds since epoch (utc) (independent of machine timezone)
|
|
struct tm *ptm_local = localtime(&secs); // in local timezone
|
|
ptm_local->tm_hour = hh;
|
|
ptm_local->tm_min = mm;
|
|
ptm_local->tm_sec = ss;
|
|
secs = mktime(ptm_local); // converts local time to UTC epoch time
|
|
timeval rv;
|
|
rv.tv_sec = secs;
|
|
rv.tv_usec = 0;
|
|
return rv;
|
|
}
|
|
|
|
timeval SODPosition::subtract_days(const timeval &tv, uint32_t n_days) {
|
|
uint32_t secs_in_day = 86400 * n_days;
|
|
timeval rv = tv;
|
|
rv.tv_sec = (rv.tv_sec > secs_in_day) ? rv.tv_sec - secs_in_day : 0;
|
|
return rv;
|
|
}
|
|
|
|
timeval SODPosition::sod_time() {
|
|
const auto &sod_time = config::get<config::SODTime>();
|
|
timeval sod = to_timeval(sod_time.sod_hh(), sod_time.sod_mm(), 0);
|
|
timeval now;
|
|
gettimeofday(&now, NULL);
|
|
// if the server started before the sod for today
|
|
// we go back to yesterday
|
|
if (sod.tv_sec >= now.tv_sec)
|
|
sod = subtract_days(sod);
|
|
return sod;
|
|
}
|
|
|
|
timeval SODPosition::get_orders_start_time() {
|
|
const auto &sod_time = config::get<config::SODTime>();
|
|
timeval sod = to_timeval(sod_time.sod_hh(), sod_time.sod_mm(), 0);
|
|
const auto &orders_cleanup = config::get<config::OrdersSoftCleanupCutoff>();
|
|
timeval now;
|
|
gettimeofday(&now, NULL);
|
|
// if the server started before the sod for today
|
|
// we go back to yesterday
|
|
if (sod.tv_sec >= now.tv_sec)
|
|
sod = subtract_days(sod);
|
|
sod = subtract_days(sod, orders_cleanup.extra_days());
|
|
return sod;
|
|
}
|
|
|
|
timeval SODPosition::get_order_cutoff_time(
|
|
const std::map<std::string, std::string> &order_map) {
|
|
// returns true is t1 >= t2
|
|
auto compare_ts = [&](google::protobuf::Timestamp t1,
|
|
google::protobuf::Timestamp t2) -> bool {
|
|
if (t1.seconds() > t2.seconds())
|
|
return true;
|
|
else if (t1.seconds() < t2.seconds())
|
|
return false;
|
|
else
|
|
return t1.nanos() >= t2.nanos();
|
|
};
|
|
google::protobuf::Timestamp max_ts;
|
|
max_ts.set_seconds(0);
|
|
max_ts.set_nanos(0);
|
|
|
|
for (auto const &kvp : order_map) {
|
|
OrderRecord trec;
|
|
if (utils::pb::from_string<api::orderman::OrderRecord>(kvp.second,
|
|
&trec)) {
|
|
const auto &ts = trec.state().created();
|
|
if (compare_ts(ts, max_ts))
|
|
(&max_ts)->CopyFrom(ts);
|
|
} else
|
|
LOG(OrderMan, Error,
|
|
"Could not recover order {} from storage. "
|
|
"This should be investigated",
|
|
kvp.first);
|
|
}
|
|
timeval rv;
|
|
rv.tv_sec = max_ts.seconds();
|
|
rv.tv_usec = static_cast<double>(max_ts.nanos()) / 1000;
|
|
const auto &orders_cleanup = config::get<config::OrdersSoftCleanupCutoff>();
|
|
return subtract_days(rv, orders_cleanup.extra_days());
|
|
}
|
|
// returns now in UTC
|
|
timeval SODPosition::now(){
|
|
timeval tv;
|
|
gettimeofday(&tv, NULL);
|
|
return tv;
|
|
}
|
|
|
|
google::protobuf::Timestamp SODPosition::to_timestamp (timeval tv){
|
|
google::protobuf::Timestamp rv;
|
|
rv.set_seconds(tv.tv_sec);
|
|
rv.set_nanos(tv.tv_usec*1000);
|
|
return rv;
|
|
}
|
|
|
|
bool SODPosition::is_sod(const google::protobuf::Timestamp& fill_time) const{
|
|
return fill_time.seconds() < _sod_time.tv_sec;
|
|
}
|
|
|
|
} // namespace orderman
|
|
} // namespace bts
|