Skip to content

Commit d645f8c

Browse files
committed
Provide list of detectors for given run
1 parent 1c3ac89 commit d645f8c

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

examples/8-KafkaToHttpServer.cxx

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <boost/beast/version.hpp>
66
#include <boost/asio.hpp>
77
#include <boost/program_options.hpp>
8+
#include <boost/algorithm/string.hpp>
89
#include <boost/algorithm/string/join.hpp>
910
#include <chrono>
1011
#include <iostream>
@@ -112,7 +113,7 @@ void httpServer(tcp::acceptor& acceptor, tcp::socket& socket) {
112113
response.set(http::field::content_type, "application/json");
113114
beast::ostream(response.body()) << "{}\n";
114115
});
115-
connection->addCallback("SHOW+TAG+VALUES+FROM",
116+
connection->addCallback("SHOW+TAG+VALUES+FROM+runs",
116117
[](http::request<http::dynamic_body>& /*request*/, http::response<http::dynamic_body>& response) {
117118
std::string jsonPrefix = R"({"results": [{"statement_id": 0, "series": [{"name": "env_active", "columns": ["key", "value"], "values": [)";
118119
std::string jsonSuffix = R"(]}]}]})";
@@ -127,6 +128,34 @@ void httpServer(tcp::acceptor& acceptor, tcp::socket& socket) {
127128
}
128129
beast::ostream(response.body()) << jsonPrefix << envsJson << jsonSuffix << '\n';
129130
});
131+
connection->addCallback("SHOW+TAG+VALUES+FROM+detectors",
132+
[](http::request<http::dynamic_body>& request, http::response<http::dynamic_body>& response) {
133+
std::string jsonPrefix = R"({"results": [{"statement_id": 0, "series": [{"name": "detectors", "columns": ["key", "value"], "values": [)";
134+
std::string jsonSuffix = R"(]}]}]})";
135+
std::string runString = std::string(request.target().substr(request.target().find("WHERE+run+%3D+") + 14));
136+
response.set(http::field::content_type, "application/json");
137+
uint32_t run;
138+
try {
139+
run = static_cast<uint32_t>(std::stoul(runString));
140+
} catch(...) {
141+
beast::ostream(response.body()) << "{}\r\n";
142+
return;
143+
}
144+
const std::lock_guard<std::mutex> lock(gEnvAccess);
145+
std::string detectorsJson;
146+
for (int i = 0; i < gActiveEnvs.activeruns_size(); i++) {
147+
if (run != gActiveEnvs.activeruns(i).runnumber()) {
148+
continue;
149+
}
150+
for (int j = 0; j < gActiveEnvs.activeruns(i).detectors_size(); j++) {
151+
detectorsJson += "[\"detector\", \"" + boost::algorithm::to_lower_copy(gActiveEnvs.activeruns(i).detectors(j)) + "\"],";
152+
}
153+
if (!detectorsJson.empty()) {
154+
detectorsJson.pop_back();
155+
}
156+
}
157+
beast::ostream(response.body()) << jsonPrefix << detectorsJson << jsonSuffix << '\n';
158+
});
130159
connection->addCallback("active_runs+WHERE+run",
131160
[](http::request<http::dynamic_body>& request, http::response<http::dynamic_body>& response) {
132161
std::string jsonPrefix = R"({"results":[{"statement_id":0,"series":[{"name":"env","columns":["time","Env ID","Run number","Detectors","State"],"values":[)";
@@ -137,7 +166,6 @@ void httpServer(tcp::acceptor& acceptor, tcp::socket& socket) {
137166
try {
138167
run = static_cast<uint32_t>(std::stoul(runString));
139168
} catch(...) {
140-
response.set(http::field::content_type, "application/json");
141169
beast::ostream(response.body()) << "{}\r\n";
142170
return;
143171
}

0 commit comments

Comments
 (0)