Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::interfaces::stats_snapshot Struct Reference

Point-in-time snapshot of component statistics. More...

#include <stats_snapshot.h>

Collaboration diagram for kcenon::common::interfaces::stats_snapshot:
Collaboration graph

Public Member Functions

auto to_json () const -> std::string
 Serialize snapshot to JSON string.
 

Public Attributes

std::string component_name
 Component identifier.
 
std::chrono::system_clock::time_point timestamp
 Snapshot capture time.
 
std::unordered_map< std::string, stats_valuevalues
 Metric key-value pairs.
 

Detailed Description

Point-in-time snapshot of component statistics.

Immutable value type that captures statistics with metadata. Designed for serialization, logging, and transmission to monitoring systems.

Usage Example:

stats_snapshot snapshot{
.component_name = "http_client",
.timestamp = std::chrono::system_clock::now(),
.values = {
{"request_count", 1500},
{"error_rate", 0.02},
{"is_healthy", true}
}
};
// Serialize to JSON for logging
std::cout << snapshot.to_json() << std::endl;
Point-in-time snapshot of component statistics.
auto to_json() const -> std::string
Serialize snapshot to JSON string.

Definition at line 52 of file stats_snapshot.h.

Member Function Documentation

◆ to_json()

auto kcenon::common::interfaces::stats_snapshot::to_json ( ) const -> std::string
inlinenodiscard

Serialize snapshot to JSON string.

Format: { "component": "component_name", "timestamp": "2025-01-30T12:34:56Z", "metrics": { "metric1": value1, "metric2": value2 } }

Returns
JSON string representation

Definition at line 72 of file stats_snapshot.h.

73 {
74 std::ostringstream json;
75 json << "{\n";
76 json << " \"component\": \"" << component_name << "\",\n";
77
78 // Format timestamp as ISO 8601
79 const auto time_t = std::chrono::system_clock::to_time_t(timestamp);
80 std::tm tm{};
81#ifdef _WIN32
82 localtime_s(&tm, &time_t);
83#else
84 localtime_r(&time_t, &tm);
85#endif
86 json << " \"timestamp\": \""
87 << std::put_time(&tm, "%Y-%m-%dT%H:%M:%S")
88 << "Z\",\n";
89
90 // Serialize metrics
91 json << " \"metrics\": {\n";
92
93 bool first = true;
94 for (const auto& [key, value] : values) {
95 if (!first) {
96 json << ",\n";
97 }
98 first = false;
99
100 json << " \"" << key << "\": ";
101
102 // Visit variant and format value
103 std::visit([&json](const auto& v) {
104 using T = std::decay_t<decltype(v)>;
105 if constexpr (std::is_same_v<T, std::int64_t>) {
106 json << v;
107 } else if constexpr (std::is_same_v<T, double>) {
108 json << std::fixed << std::setprecision(6) << v;
109 } else if constexpr (std::is_same_v<T, std::string>) {
110 json << "\"" << v << "\"";
111 } else if constexpr (std::is_same_v<T, bool>) {
112 json << (v ? "true" : "false");
113 }
114 }, value);
115 }
116
117 json << "\n }\n";
118 json << "}";
119
120 return json.str();
121 }
std::string component_name
Component identifier.
std::unordered_map< std::string, stats_value > values
Metric key-value pairs.
std::chrono::system_clock::time_point timestamp
Snapshot capture time.

References component_name, timestamp, and values.

Member Data Documentation

◆ component_name

std::string kcenon::common::interfaces::stats_snapshot::component_name

Component identifier.

Definition at line 53 of file stats_snapshot.h.

Referenced by to_json().

◆ timestamp

std::chrono::system_clock::time_point kcenon::common::interfaces::stats_snapshot::timestamp

Snapshot capture time.

Definition at line 54 of file stats_snapshot.h.

Referenced by to_json().

◆ values

std::unordered_map<std::string, stats_value> kcenon::common::interfaces::stats_snapshot::values

Metric key-value pairs.

Definition at line 55 of file stats_snapshot.h.

Referenced by to_json().


The documentation for this struct was generated from the following file: