26#include <unordered_map>
34using config_map = std::unordered_map<std::string, std::string>;
39using stats_map = std::unordered_map<std::string, double>;
52 std::unordered_map<std::string, std::string>
tags;
59 std::unordered_map<std::string, std::string> tgs = {})
63 ,
timestamp(std::chrono::system_clock::now())
64 ,
tags(std::move(tgs)) {}
103template <
typename Derived>
124 if (
auto it = config.find(
"enabled"); it != config.end()) {
125 enabled_ = (it->second ==
"true" || it->second ==
"1");
128 if (
auto it = config.find(
"ae_title"); it != config.end()) {
132 init_time_ = std::chrono::steady_clock::now();
135 return derived().do_initialize(config);
148 auto metrics =
derived().do_collect();
163 return Derived::collector_name;
171 return derived().do_get_metric_types();
182 return derived().is_available();
194 stats[
"enabled"] =
enabled_ ? 1.0 : 0.0;
199 if (
init_time_.time_since_epoch().count() > 0) {
200 const auto uptime = std::chrono::duration_cast<std::chrono::seconds>(
201 std::chrono::steady_clock::now() -
init_time_);
202 stats[
"uptime_seconds"] =
static_cast<double>(uptime.count());
206 derived().do_add_statistics(stats);
255 const std::string&
name,
257 const std::string& type,
258 const std::unordered_map<std::string, std::string>& extra_tags = {})
const {
260 std::unordered_map<std::string, std::string> tags = extra_tags;
261 tags[
"collector"] = Derived::collector_name;
266 return dicom_metric(
name, value, type, std::move(tags));
284 Derived&
derived() {
return static_cast<Derived&
>(*this); }
285 const Derived&
derived()
const {
return static_cast<const Derived&
>(*this); }
CRTP base class for DICOM metric collectors.
std::chrono::steady_clock::time_point init_time_
std::size_t get_collection_errors() const
Get error count.
dicom_collector_base & operator=(const dicom_collector_base &)=delete
bool initialize(const config_map &config)
Initialize the collector with configuration.
std::vector< std::string > get_metric_types() const
Get supported metric types.
dicom_collector_base(dicom_collector_base &&)=delete
std::vector< dicom_metric > collect()
Collect metrics from the data source.
const Derived & derived() const
std::atomic< std::size_t > collection_errors_
dicom_collector_base()=default
std::string get_ae_title() const
Get the AE title.
bool is_healthy() const
Check if the collector is healthy.
void set_ae_title(std::string ae_title)
Set the AE title.
std::size_t get_collection_count() const
Get collection count.
std::chrono::system_clock::time_point last_collection_time_
Derived & derived()
Get reference to derived class (CRTP helper)
virtual ~dicom_collector_base()=default
dicom_collector_base & operator=(dicom_collector_base &&)=delete
std::atomic< std::size_t > collection_count_
dicom_collector_base(const dicom_collector_base &)=delete
stats_map get_statistics() const
Get collector statistics.
std::string get_name() const
Get the name of this collector.
dicom_metric create_base_metric(const std::string &name, double value, const std::string &type, const std::unordered_map< std::string, std::string > &extra_tags={}) const
Create a metric with common tags.
bool is_enabled() const
Check if collector is enabled.
std::unordered_map< std::string, double > stats_map
Type alias for statistics map.
std::unordered_map< std::string, std::string > config_map
Type alias for configuration map.
Standard metric structure for DICOM data.
std::unordered_map< std::string, std::string > tags
dicom_metric(std::string n, double v, std::string t, std::unordered_map< std::string, std::string > tgs={})
std::chrono::system_clock::time_point timestamp