Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
result_pattern_example.cpp File Reference

Example demonstrating Result<T> and VoidResult error handling patterns. More...

Include dependency graph for result_pattern_example.cpp:

Go to the source code of this file.

Functions

kcenon::common::Result< double > divide (double a, double b)
 
kcenon::common::VoidResult validate_range (double value, double min, double max)
 
kcenon::common::Result< std::string > process_metric (double value)
 
int main ()
 

Detailed Description

Example demonstrating Result<T> and VoidResult error handling patterns.

Definition in file result_pattern_example.cpp.

Function Documentation

◆ divide()

kcenon::common::Result< double > divide ( double a,
double b )
Examples
result_pattern_example.cpp.

Definition at line 25 of file result_pattern_example.cpp.

25 {
26 if (b == 0) {
27 return kcenon::common::Result<double>::err(error_info(monitoring_error_code::invalid_configuration, "Division by zero").to_common_error());
28 }
29 return kcenon::common::ok(a / b);
30}
Extended error information with context.

Referenced by main(), and process_metric().

Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 55 of file result_pattern_example.cpp.

55 {
56 std::cout << "=== Result Pattern Example ===" << std::endl << std::endl;
57
58 // Example 1: Successful operation
59 std::cout << "Example 1: Successful division" << std::endl;
60 auto result1 = divide(10.0, 2.0);
61 if (result1.is_ok()) {
62 std::cout << " Result: " << result1.value() << std::endl;
63 } else {
64 std::cout << " Error: " << result1.error().message << std::endl;
65 }
66 std::cout << std::endl;
67
68 // Example 2: Failed operation
69 std::cout << "Example 2: Division by zero" << std::endl;
70 auto result2 = divide(10.0, 0.0);
71 if (result2.is_ok()) {
72 std::cout << " Result: " << result2.value() << std::endl;
73 } else {
74 std::cout << " Error: " << result2.error().message << std::endl;
75 }
76 std::cout << std::endl;
77
78 // Example 3: Using value_or
79 std::cout << "Example 3: Using value_or with default" << std::endl;
80 auto result3 = divide(5.0, 0.0);
81 double value = result3.value_or(-1.0);
82 std::cout << " Value (with default): " << value << std::endl;
83 std::cout << std::endl;
84
85 // Example 4: kcenon::common::VoidResult usage
86 std::cout << "Example 4: Validation with kcenon::common::VoidResult" << std::endl;
87 auto validation1 = validate_range(50.0, 0.0, 100.0);
88 if (validation1.is_ok()) {
89 std::cout << " Validation passed" << std::endl;
90 } else {
91 std::cout << " Validation failed: " << validation1.error().message << std::endl;
92 }
93
94 auto validation2 = validate_range(150.0, 0.0, 100.0);
95 if (validation2.is_ok()) {
96 std::cout << " Validation passed" << std::endl;
97 } else {
98 std::cout << " Validation failed: " << validation2.error().message << std::endl;
99 }
100 std::cout << std::endl;
101
102 // Example 5: Monadic operations
103 std::cout << "Example 5: Chaining operations" << std::endl;
104 auto result4 = process_metric(4.0);
105 if (result4.is_ok()) {
106 std::cout << " " << result4.value() << std::endl;
107 } else {
108 std::cout << " Error: " << result4.error().message << std::endl;
109 }
110
111 auto result5 = process_metric(1.0);
112 if (result5.is_ok()) {
113 std::cout << " " << result5.value() << std::endl;
114 } else {
115 std::cout << " Error: " << result5.error().message << std::endl;
116 }
117 std::cout << std::endl;
118
119 // Example 6: Metrics snapshot
120 std::cout << "Example 6: Metrics snapshot" << std::endl;
121 metrics_snapshot snapshot;
122 snapshot.add_metric("cpu_usage", 65.5);
123 snapshot.add_metric("memory_usage", 4096.0);
124 snapshot.add_metric("disk_io", 150.25);
125
126 std::cout << " Metrics collected: " << snapshot.metrics.size() << std::endl;
127
128 if (auto cpu = snapshot.get_metric("cpu_usage")) {
129 std::cout << " CPU Usage: " << cpu.value() << "%" << std::endl;
130 }
131
132 if (auto mem = snapshot.get_metric("memory_usage")) {
133 std::cout << " Memory Usage: " << mem.value() << " MB" << std::endl;
134 }
135 std::cout << std::endl;
136
137 // Example 7: Configuration validation
138 std::cout << "Example 7: Configuration validation" << std::endl;
139 monitoring_config config;
140 config.history_size = 1000;
141 config.collection_interval = std::chrono::milliseconds(100);
142 config.buffer_size = 5000;
143
144 auto config_result = config.validate();
145 if (config_result.is_ok()) {
146 std::cout << " Configuration is valid" << std::endl;
147 std::cout << " - History size: " << config.history_size << std::endl;
148 std::cout << " - Collection interval: " << config.collection_interval.count() << "ms" << std::endl;
149 std::cout << " - Buffer size: " << config.buffer_size << std::endl;
150 } else {
151 std::cout << " Configuration error: " << config_result.error().message << std::endl;
152 }
153
154 return 0;
155}
@ cpu
CPU power domain (RAPL)
kcenon::common::Result< double > divide(double a, double b)
kcenon::common::VoidResult validate_range(double value, double min, double max)
kcenon::common::Result< std::string > process_metric(double value)
Complete snapshot of metrics at a point in time.
std::optional< double > get_metric(const std::string &name) const
Get a specific metric value.
std::vector< metric_value > metrics
void add_metric(const std::string &name, double value)
Add a metric to the snapshot.
Configuration for the monitoring system.
std::chrono::milliseconds collection_interval
common::VoidResult validate() const
Validate configuration parameters.

References kcenon::monitoring::metrics_snapshot::add_metric(), kcenon::monitoring::monitoring_config::buffer_size, kcenon::monitoring::monitoring_config::collection_interval, kcenon::monitoring::cpu, divide(), kcenon::monitoring::metrics_snapshot::get_metric(), kcenon::monitoring::monitoring_config::history_size, kcenon::monitoring::metrics_snapshot::metrics, process_metric(), kcenon::monitoring::monitoring_config::validate(), and validate_range().

Here is the call graph for this function:

◆ process_metric()

kcenon::common::Result< std::string > process_metric ( double value)
Examples
result_pattern_example.cpp.

Definition at line 43 of file result_pattern_example.cpp.

43 {
44 // Chain operations using map and and_then
45 return divide(100.0, value)
46 .map([](double x) { return x * 2; })
47 .and_then([](double x) {
48 if (x > 50) {
49 return kcenon::common::ok("High value: " + std::to_string(x));
50 }
51 return kcenon::common::ok("Normal value: " + std::to_string(x));
52 });
53}

References divide().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ validate_range()

kcenon::common::VoidResult validate_range ( double value,
double min,
double max )
Examples
result_pattern_example.cpp.

Definition at line 33 of file result_pattern_example.cpp.

33 {
34 if (value < min || value > max) {
35 error_info err(monitoring_error_code::invalid_configuration,
36 "Value out of range [" + std::to_string(min) + ", " + std::to_string(max) + "]");
37 return kcenon::common::VoidResult::err(err.to_common_error());
38 }
39 return kcenon::common::ok();
40}

References kcenon::monitoring::error_info::to_common_error().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function: