more cleanup and make mc configurable

This commit is contained in:
Green Sky 2024-02-04 15:12:03 +01:00
parent e8e9126faa
commit 2c80406795
No known key found for this signature in database
3 changed files with 38 additions and 15 deletions

View File

@ -182,7 +182,7 @@ int main(int argc, char** argv) {
Contact3Registry cr;
RegistryMessageModel rmm{cr};
MessageTimeSort mts{rmm};
MessageCleanser mc{cr, rmm};
MessageCleanser mc{cr, rmm, conf};
MessageCommandDispatcher mcd{cr, rmm, conf};
{ // setup basic commands for bot

View File

@ -2,13 +2,20 @@
#include <solanaceae/contact/components.hpp>
#include <solanaceae/message3/components.hpp>
#include <solanaceae/util/utils.hpp>
#include <iostream>
#include <chrono>
#include <cstdint>
MessageCleanser::MessageCleanser(Contact3Registry& cr, RegistryMessageModel& rmm) : _cr(cr), _rmm(rmm) {
// config
// has MessageCleanser::old_age
MessageCleanser::MessageCleanser(
Contact3Registry& cr,
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) {
@ -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 deleted_count{0};
// TODO: iterate rmm directly
//_rmm.get();
// workaround by iterating contacts
for (const auto& c : _cr.view<Contact::Components::TagBig>()) {
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;
// 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) {
if (now_ts >= ts.ts + static_cast<uint64_t>(_old_age*1000.f)) {
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)) {
to_remove.push_back(ent);
}
});
@ -44,12 +61,11 @@ float MessageCleanser::iterate(float time_delta) {
_rmm.throwEventDestroy(*reg, c);
reg->destroy(c);
}
deleted_count += to_remove.size();
}
}
if (deleted_count > 0) {
std::cout << "MC: cleaned up " << deleted_count << "\n";
if (!to_remove.empty()) {
std::cout << "MC: cleaned up " << to_remove.size() << ", age >= " << old_age << "sec\n";
}
}
}
}

View File

@ -1,17 +1,24 @@
#pragma once
#include <solanaceae/message3/registry_message_model.hpp>
#include <solanaceae/util/config_model.hpp>
class MessageCleanser {
Contact3Registry& _cr;
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 _timer{0.f};
public:
MessageCleanser(Contact3Registry& cr, RegistryMessageModel& rmm);
MessageCleanser(
Contact3Registry& cr,
RegistryMessageModel& rmm,
ConfigModelI& conf
);
~MessageCleanser(void);
float iterate(float time_delta);