more cleanup and make mc configurable
This commit is contained in:
parent
e8e9126faa
commit
2c80406795
@ -182,7 +182,7 @@ int main(int argc, char** argv) {
|
|||||||
Contact3Registry cr;
|
Contact3Registry cr;
|
||||||
RegistryMessageModel rmm{cr};
|
RegistryMessageModel rmm{cr};
|
||||||
MessageTimeSort mts{rmm};
|
MessageTimeSort mts{rmm};
|
||||||
MessageCleanser mc{cr, rmm};
|
MessageCleanser mc{cr, rmm, conf};
|
||||||
MessageCommandDispatcher mcd{cr, rmm, conf};
|
MessageCommandDispatcher mcd{cr, rmm, conf};
|
||||||
|
|
||||||
{ // setup basic commands for bot
|
{ // setup basic commands for bot
|
||||||
|
@ -2,13 +2,20 @@
|
|||||||
|
|
||||||
#include <solanaceae/contact/components.hpp>
|
#include <solanaceae/contact/components.hpp>
|
||||||
#include <solanaceae/message3/components.hpp>
|
#include <solanaceae/message3/components.hpp>
|
||||||
|
#include <solanaceae/util/utils.hpp>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
MessageCleanser::MessageCleanser(Contact3Registry& cr, RegistryMessageModel& rmm) : _cr(cr), _rmm(rmm) {
|
MessageCleanser::MessageCleanser(
|
||||||
// config
|
Contact3Registry& cr,
|
||||||
// has MessageCleanser::old_age
|
RegistryMessageModel& rmm,
|
||||||
|
ConfigModelI& conf
|
||||||
|
) : _cr(cr), _rmm(rmm), _conf(conf) {
|
||||||
|
if (!_conf.has_int("MessageCleanser", "old_age_minutes")) {
|
||||||
|
_conf.set("MessageCleanser", "old_age_minutes", int64_t(_old_age_default));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MessageCleanser::~MessageCleanser(void) {
|
MessageCleanser::~MessageCleanser(void) {
|
||||||
@ -23,17 +30,27 @@ float MessageCleanser::iterate(float time_delta) {
|
|||||||
|
|
||||||
uint64_t now_ts = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
uint64_t now_ts = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
||||||
|
|
||||||
uint64_t deleted_count{0};
|
|
||||||
|
|
||||||
// TODO: iterate rmm directly
|
// TODO: iterate rmm directly
|
||||||
//_rmm.get();
|
//_rmm.get();
|
||||||
// workaround by iterating contacts
|
// workaround by iterating contacts
|
||||||
for (const auto& c : _cr.view<Contact::Components::TagBig>()) {
|
for (const auto& c : _cr.view<Contact::Components::TagBig>()) {
|
||||||
if (auto* reg = _rmm.get(c); reg != nullptr) {
|
if (auto* reg = _rmm.get(c); reg != nullptr) {
|
||||||
|
float old_age {0.f};
|
||||||
|
{ // old age from conf
|
||||||
|
// TODO: find some way to extract this (maybe map into contact reg?)
|
||||||
|
if (const auto* id_comp = _cr.try_get<Contact::Components::ID>(c); id_comp != nullptr) {
|
||||||
|
const auto id_hex = bin2hex(id_comp->data);
|
||||||
|
old_age = _conf.get_int("MessageCleanser", "old_age_minutes", id_hex).value_or(_old_age_default);
|
||||||
|
} else {
|
||||||
|
old_age = _conf.get_int("MessageCleanser", "old_age_minutes").value_or(_old_age_default);
|
||||||
|
}
|
||||||
|
old_age *= 60.f; // to sec
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<Message3> to_remove;
|
std::vector<Message3> to_remove;
|
||||||
// assuming all messages have Timestamp comp
|
// assuming all messages have Timestamp comp
|
||||||
reg->view<Message::Components::Timestamp>().each([this, now_ts, &to_remove](const Message3 ent, const Message::Components::Timestamp& ts) {
|
reg->view<Message::Components::Timestamp>().each([this, now_ts, old_age, &to_remove](const Message3 ent, const Message::Components::Timestamp& ts) {
|
||||||
if (now_ts >= ts.ts + static_cast<uint64_t>(_old_age*1000.f)) {
|
if (now_ts >= ts.ts + static_cast<uint64_t>(old_age*1000.f)) {
|
||||||
to_remove.push_back(ent);
|
to_remove.push_back(ent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -44,12 +61,11 @@ float MessageCleanser::iterate(float time_delta) {
|
|||||||
_rmm.throwEventDestroy(*reg, c);
|
_rmm.throwEventDestroy(*reg, c);
|
||||||
reg->destroy(c);
|
reg->destroy(c);
|
||||||
}
|
}
|
||||||
deleted_count += to_remove.size();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (deleted_count > 0) {
|
if (!to_remove.empty()) {
|
||||||
std::cout << "MC: cleaned up " << deleted_count << "\n";
|
std::cout << "MC: cleaned up " << to_remove.size() << ", age >= " << old_age << "sec\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,17 +1,24 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <solanaceae/message3/registry_message_model.hpp>
|
#include <solanaceae/message3/registry_message_model.hpp>
|
||||||
|
#include <solanaceae/util/config_model.hpp>
|
||||||
|
|
||||||
class MessageCleanser {
|
class MessageCleanser {
|
||||||
Contact3Registry& _cr;
|
Contact3Registry& _cr;
|
||||||
RegistryMessageModel& _rmm;
|
RegistryMessageModel& _rmm;
|
||||||
|
ConfigModelI& _conf;
|
||||||
|
|
||||||
|
static constexpr int64_t _old_age_default{150}; // minutes
|
||||||
|
|
||||||
float _old_age{150.f*60.f}; // max 150min
|
|
||||||
float _interval{3.f*60.f}; // every 3min
|
float _interval{3.f*60.f}; // every 3min
|
||||||
float _timer{0.f};
|
float _timer{0.f};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MessageCleanser(Contact3Registry& cr, RegistryMessageModel& rmm);
|
MessageCleanser(
|
||||||
|
Contact3Registry& cr,
|
||||||
|
RegistryMessageModel& rmm,
|
||||||
|
ConfigModelI& conf
|
||||||
|
);
|
||||||
~MessageCleanser(void);
|
~MessageCleanser(void);
|
||||||
|
|
||||||
float iterate(float time_delta);
|
float iterate(float time_delta);
|
||||||
|
Loading…
Reference in New Issue
Block a user