diff --git a/core/stats/IStatObserver.hpp b/core/stats/IStatObserver.hpp new file mode 100644 index 00000000..a1211df9 --- /dev/null +++ b/core/stats/IStatObserver.hpp @@ -0,0 +1,11 @@ +#ifndef ISTATOBSERVER_HPP +#define ISTATOBSERVER_HPP + +class IStatObserver +{ +public: + virtual ~IStatObserver(){}; + // virtual void function_name(smth to transmit) = 0 +}; + +#endif \ No newline at end of file diff --git a/core/stats/IStatSubject.hpp b/core/stats/IStatSubject.hpp new file mode 100644 index 00000000..bad4e30d --- /dev/null +++ b/core/stats/IStatSubject.hpp @@ -0,0 +1,16 @@ +#ifndef ISTATSUBJECT_HPP +#define ISTATSUBJECT_HPP + +#include "IStatObserver.hpp" + +#include + +class IStatSubject +{ + + public: + virtual ~IStatSubject(){}; + virtual void attach(std::string observer_name, IStatObserver* observer) = 0; +}; + +#endif \ No newline at end of file diff --git a/core/stats/Stat.hpp b/core/stats/Stat.hpp new file mode 100644 index 00000000..a1b69836 --- /dev/null +++ b/core/stats/Stat.hpp @@ -0,0 +1,33 @@ +#ifndef STAT_HPP +#define STAT_HPP + +#include "IStatObserver.hpp" +#include "StatManager.hpp" + +#include +#include +#include +#include +#include + +class Stat : public IStatObserver +{ + private: + std::map stats_map; + + public: + template + Stat(const std::string name, const Iterable& list_entries) + { + for (const auto c : list_entries) { + stats_map.insert(std::make_pair(c, 0)); + } + + StatManager::get_instance().attach(name, this); + }; + + void increment(const std::string key) { stats_map.at(key) += 1; } + void add(const std::string key, uint64_t value) { stats_map.at(key) += value; } +}; + +#endif \ No newline at end of file diff --git a/core/stats/StatManager.cpp b/core/stats/StatManager.cpp new file mode 100644 index 00000000..d841ca99 --- /dev/null +++ b/core/stats/StatManager.cpp @@ -0,0 +1,18 @@ +#include "StatManager.hpp" + +#include + +using namespace std; + +void +StatManager::attach(string observer_name, IStatObserver* observer) +{ + observer_map_.insert(make_pair(move(observer_name), move(observer))); +} + +StatManager& +StatManager::get_instance() +{ + if (instance_ == nullptr) { instance_ = new StatManager; } + return *instance_; +} \ No newline at end of file diff --git a/core/stats/StatManager.hpp b/core/stats/StatManager.hpp new file mode 100644 index 00000000..8de3d472 --- /dev/null +++ b/core/stats/StatManager.hpp @@ -0,0 +1,32 @@ + +#ifndef STATMANAGER_HPP +#define STATMANAGER_HPP + +#include "IStatObserver.hpp" +#include "IStatSubject.hpp" + +#include +#include + +class StatManager : public IStatSubject +{ + private: + static StatManager* instance_; + std::map observer_map_; + + private: + StatManager() = default; + + public: + static StatManager& get_instance(); + StatManager(StatManager& instance) = delete; + void operator=(const StatManager&) = delete; + + virtual void attach(std::string observer_name, IStatObserver* observer) override; + + void dump(const std::string filename); +}; + +StatManager* StatManager::instance_ = nullptr; + +#endif \ No newline at end of file