-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_health_status.cpp
More file actions
264 lines (234 loc) · 10.4 KB
/
print_health_status.cpp
File metadata and controls
264 lines (234 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/// @file
///
/// A command line executable which connects to a Horus project manager service and prints the
/// current health status of the system.
#include <chrono>
#include <cstddef>
#include <string>
#include <utility>
#include <vector>
#include "examples/helpers.h"
#include "horus/future/cancel.h"
#include "horus/rpc/endpoint.h"
#include "horus/rpc/services.h"
#include "horus/sdk.h"
#include "horus/sdk/errors.h"
#include "horus/sdk/health.h"
#include "horus/sdk/version.h"
#include "horus/strings/chrono.h"
#include "horus/strings/logging.h"
#include "horus/strings/stdio.h"
#include "horus/strings/str_cat.h"
#include "horus/strings/string_view.h"
#include "horus/strings/stringify.h"
#include "horus/types/span.h"
namespace {
/// Converts a `LicenseFeature` to its stringified representation.
horus::StringView LicenseFeatureToString(
const horus::sdk::LicenseStatus::LicenseInfo::LicenseFeature feature) {
switch (feature) {
case horus::sdk::LicenseStatus::LicenseInfo::LicenseFeature::kCan: {
return "CAN bus";
}
case horus::sdk::LicenseStatus::LicenseInfo::LicenseFeature::kVinAssociator: {
return "VIN associator";
}
case horus::sdk::LicenseStatus::LicenseInfo::LicenseFeature::kMultiRosbag: {
return "Multiple rosbags";
}
case horus::sdk::LicenseStatus::LicenseInfo::LicenseFeature::kDebuggingSupport: {
return "Debugging support";
}
case horus::sdk::LicenseStatus::LicenseInfo::LicenseFeature::kAtlas: {
return "Atlas";
}
case horus::sdk::LicenseStatus::LicenseInfo::LicenseFeature::kMacgyver: {
return "SR Analytics";
}
case horus::sdk::LicenseStatus::LicenseInfo::LicenseFeature::kUnknownWireValue:
default: {
return "UNKNOWN";
}
}
}
/// Stringifies a `LicenseStatus`.
void PrintLicenseStatus(const horus::sdk::LicenseStatus& license_status) {
horus::StringifyTo(horus::StdoutSink(), "- License level: ", license_status.LicenseLevel(), "\n");
horus::StringifyTo(horus::StdoutSink(), "- Reason: ", license_status.Reason(), "\n");
horus::StringifyTo(horus::StdoutSink(), "- Privileges:\n");
if (license_status.HasPrivilege(horus::sdk::LicenseStatus::Privilege::kLowest)) {
horus::StringifyTo(horus::StdoutSink(), " - Lowest privilege\n");
}
if (license_status.HasPrivilege(horus::sdk::LicenseStatus::Privilege::kSubscribe)) {
horus::StringifyTo(horus::StdoutSink(), " - Subscribe to services\n");
}
if (license_status.HasPrivilege(horus::sdk::LicenseStatus::Privilege::kLicenseActions)) {
horus::StringifyTo(horus::StdoutSink(), " - Perform license actions\n");
}
if (license_status.HasPrivilege(horus::sdk::LicenseStatus::Privilege::kRead)) {
horus::StringifyTo(horus::StdoutSink(), " - Read\n");
}
if (license_status.HasPrivilege(horus::sdk::LicenseStatus::Privilege::kWrite)) {
horus::StringifyTo(horus::StdoutSink(), " - Write\n");
}
if (!license_status.HasLicenseInfo()) {
return;
}
const horus::sdk::LicenseStatus::LicenseInfo& license_info{license_status.GetLicenseInfo()};
horus::StringifyTo(horus::StdoutSink(), "- License info:\n");
horus::StringifyTo(horus::StdoutSink(),
" - Expiration date: ", horus::Iso8601{license_info.ExpirationTimestamp()},
"\n");
horus::StringifyTo(horus::StdoutSink(), " - Number of lidars: ", license_info.NumberOfLidars(),
"\n");
if (!license_info.AllowedFeatures().empty()) {
horus::StringifyTo(horus::StdoutSink(), " - Allowed features:\n");
for (const horus::sdk::LicenseStatus::LicenseInfo::LicenseFeature feature :
license_info.AllowedFeatures()) {
horus::StringifyTo(horus::StdoutSink(), " - ", LicenseFeatureToString(feature), "\n");
}
} else {
horus::StringifyTo(horus::StdoutSink(), " - Allowed features: NONE!\n");
}
}
void PrintSensorHealth(const horus::sdk::SensorHealth& sensor_health_status) {
const horus::sdk::SensorInfo& sensor_info{sensor_health_status.Info()};
horus::StringifyTo(horus::StdoutSink(), sensor_info.LidarId(), ":");
if (sensor_health_status.IsNodeUnreachable()) {
const horus::StringView error{sensor_health_status.NodeUnreachableErrorMessage()};
horus::StringifyTo(horus::StdoutSink(), " Unreachable! ", error, "\n");
return;
}
horus::StringifyTo(horus::StdoutSink(), "\n- Frequency: ", sensor_info.MeasuredFrequencyHz(),
"Hz\n");
horus::StringifyTo(horus::StdoutSink(), "- Status:\n");
if (sensor_info.HasStatus(horus::sdk::SensorInfo::SensorStatus::kNoData)) {
horus::StringifyTo(horus::StdoutSink(), " - No data\n");
}
if (sensor_info.HasStatus(horus::sdk::SensorInfo::SensorStatus::kReceivingData)) {
horus::StringifyTo(horus::StdoutSink(), " - Receiving data\n");
}
if (sensor_info.HasStatus(horus::sdk::SensorInfo::SensorStatus::kLowFrequency)) {
horus::StringifyTo(horus::StdoutSink(), " - Low frequency\n");
}
if (sensor_info.HasStatus(horus::sdk::SensorInfo::SensorStatus::kHighFrequency)) {
horus::StringifyTo(horus::StdoutSink(), " - High frequency\n");
}
if (sensor_info.HasStatus(horus::sdk::SensorInfo::SensorStatus::kTilted)) {
horus::StringifyTo(horus::StdoutSink(), " - Titled\n");
}
if (sensor_info.HasStatus(horus::sdk::SensorInfo::SensorStatus::kObstructed)) {
horus::StringifyTo(horus::StdoutSink(), " - Obstructed\n");
}
if (sensor_info.HasStatus(horus::sdk::SensorInfo::SensorStatus::kPacketDrop)) {
horus::StringifyTo(horus::StdoutSink(), " - Packet drop\n");
}
if (sensor_info.HasStatus(horus::sdk::SensorInfo::SensorStatus::kAutoCorrectionModerate)) {
horus::StringifyTo(horus::StdoutSink(), " - Moderate auto-correction detected\n");
}
if (sensor_info.HasStatus(horus::sdk::SensorInfo::SensorStatus::kAutoCorrectionSevere)) {
horus::StringifyTo(horus::StdoutSink(), " - Severe auto-correction detected\n");
}
}
void PrintServiceStatuses(horus::Span<const horus::sdk::NodeHealth> service_health_statuses) {
// This vector will be progressively emptied while we call
// stringify_service_health_status().
std::vector<horus::sdk::NodeHealth> service_health_statuses_copy{};
for (const horus::sdk::NodeHealth& node : service_health_statuses) {
service_health_statuses_copy.push_back(node);
}
const auto stringify_service_health_status = [&service_health_statuses_copy](
const std::string& service_name,
const horus::sdk::NodeHealth::Service service) {
std::vector<horus::sdk::NodeHealth> service_nodes{};
for (horus::sdk::NodeHealth& node : service_health_statuses_copy) {
if (node.service != service) {
continue;
}
service_nodes.emplace_back(std::move(node));
}
if (service_nodes.empty()) {
horus::StringifyTo(horus::StdoutSink(), service_name, ": No node found!\n");
return;
}
horus::StringifyTo(horus::StdoutSink(), service_name, ":\n");
for (const auto& node_status : service_nodes) {
std::string str_status{};
switch (node_status.node_status) {
case horus::sdk::NodeHealth::Status::kAlive: {
str_status = "alive";
break;
}
case horus::sdk::NodeHealth::Status::kUnreachable: {
str_status = "Unreachable!";
break;
}
case horus::sdk::NodeHealth::Status::kUnknownWireValue:
case horus::sdk::NodeHealth::Status::kUnspecified:
default: {
str_status = "unknown";
}
}
horus::StringifyTo(horus::StdoutSink(),
horus::StrCat(" - ", node_status.node_id, ": ", str_status, "\n"));
}
horus::StringifyTo(horus::StdoutSink(), "\n");
};
stringify_service_health_status("Detection service", horus::sdk::NodeHealth::Service::kDetection);
stringify_service_health_status("Lidar runner service",
horus::sdk::NodeHealth::Service::kLidarRunner);
stringify_service_health_status("Notification service",
horus::sdk::NodeHealth::Service::kNotification);
stringify_service_health_status("Point aggregator service",
horus::sdk::NodeHealth::Service::kPointAggregator);
stringify_service_health_status("Preprocessing service",
horus::sdk::NodeHealth::Service::kPreprocessing);
stringify_service_health_status("Project manager service",
horus::sdk::NodeHealth::Service::kProjectManager);
}
} // namespace
int main(int argc, const char* argv[]) {
const horus::Span<const char*> args{argv, static_cast<std::size_t>(argc)};
horus::RpcServices::ServiceResolutionMap service_map;
if (!horus::ParseArgs(service_map, args)) {
return 1;
}
horus::Sdk sdk{service_map};
try {
horus::StringifyTo(horus::StdoutSink(), "Sending RPC request...\n");
const horus::sdk::Version version{sdk.GetVersion({}).Wait()};
horus::StringifyTo(horus::StdoutSink(), "Horus version: ", version.ToString(), "\n\n");
const horus::sdk::HealthStatus health_status{sdk.GetHealthStatus({}).Wait()};
horus::StringifyTo(horus::StdoutSink(), "---- License status ----\n");
PrintLicenseStatus(health_status.GetLicenseStatus());
horus::StringifyTo(horus::StdoutSink(), "\n");
if (!health_status.HasReadPrivilege()) {
horus::StringifyTo(
horus::StdoutSink(),
"Sensor and service statuses not received because license level does not grant the "
"Read "
"privilege.");
return 0;
}
horus::StringifyTo(horus::StdoutSink(), "---- Lidar statuses ----\n");
for (const horus::sdk::SensorHealth& sensor_health_status : health_status.SensorStatuses()) {
PrintSensorHealth(sensor_health_status);
horus::StringifyTo(horus::StdoutSink(), "\n");
}
horus::StringifyTo(horus::StdoutSink(), "---- Service statuses ----\n");
PrintServiceStatuses(health_status.ServiceStatuses());
} catch (const horus::RpcEndpointDisconnectedError&) {
horus::Log("Cannot connect to the ProjectManager.");
return 1;
} catch (const horus::CancellationError&) {
horus::Log("Request timeout.");
return 1;
} catch (const horus::sdk::RpcApplicationError& error) {
horus::Log("RPC returned an error: ", error.what());
return 1;
} catch (const horus::sdk::UnexpectedDataReceivedFromRpcError& error) {
horus::Log("Something is wrong with the SDK: ", error.what());
return 1;
}
return 0;
}