44// /
55
66#include " ../src/Transports/KafkaConsumer.h"
7+ #include " ../src/Backends/InfluxDB.h"
78#include " ../src/Transports/HTTP.h"
8- #include " ../src/Transports/WebSocket.h"
99
1010#include < iostream>
1111#include < memory>
1212#include < thread>
13- #include < boost/algorithm/string/join.hpp>
1413#include < boost/program_options.hpp>
14+ #include < boost/algorithm/string/join.hpp>
15+ #include < boost/algorithm/string.hpp>
1516
1617#include " envs.pb.h"
1718
@@ -22,21 +23,46 @@ int main(int argc, char* argv[])
2223 boost::program_options::options_description desc (" Program options" );
2324 desc.add_options ()
2425 (" kafka-host" , boost::program_options::value<std::string>()->required (), " Kafka broker hostname" )
25- (" kafka-topic" , boost::program_options::value<std::string>()->required (), " Kafka topic" );
26+ (" kafka-topics" , boost::program_options::value<std::vector<std::string>>()->multitoken ()->required (), " Kafka topics" )
27+ (" influxdb-host" , boost::program_options::value<std::string>()->required (), " InfluxDB hostname" )
28+ (" influxdb-token" , boost::program_options::value<std::string>()->required (), " InfluxDB token" )
29+ (" influxdb-org" , boost::program_options::value<std::string>()->default_value (" cern" ), " InfluxDB organisation" )
30+ (" influxdb-bucket" , boost::program_options::value<std::string>()->default_value (" aliecs" ), " InfluxDB bucket" );
2631 boost::program_options::variables_map vm;
2732 boost::program_options::store (boost::program_options::parse_command_line (argc, argv, desc), vm);
2833 boost::program_options::notify (vm);
2934
30- auto kafkaConsumer = std::make_unique<transports::KafkaConsumer>(vm[" kafka-host" ].as <std::string>(), 9092 , vm[" kafka-topic" ].as <std::string>());
35+ std::vector<std::string> topics = vm[" kafka-topics" ].as <std::vector<std::string>>();
36+ auto kafkaConsumer = std::make_unique<transports::KafkaConsumer>(vm[" kafka-host" ].as <std::string>(), 9092 , topics);
37+ auto httpTransport = std::make_unique<transports::HTTP>(
38+ " http://" + vm[" influxdb-host" ].as <std::string>() + " :8086/api/v2/write?" +
39+ " org=" + vm[" influxdb-org" ].as <std::string>() + " &" +
40+ " bucket=" + vm[" influxdb-bucket" ].as <std::string>()
41+ );
42+ httpTransport->addHeader (" Authorization: Token " + vm[" influxdb-token" ].as <std::string>());
43+ auto influxdbBackend = std::make_unique<backends::InfluxDB>(std::move (httpTransport));
3144 for (;;) {
3245 auto changes = kafkaConsumer->receive ();
3346 if (!changes.empty ()) {
3447 for (auto & change : changes) {
3548 aliceo2::envs::NewStateNotification stateChange;
3649 stateChange.ParseFromString (change);
37- std::cout << " EnvID: " << stateChange.envinfo ().environmentid () << std::endl
38- << " State: " << stateChange.envinfo ().state () << std::endl
39- << " First detector: " << stateChange.envinfo ().detectors ().Get (0 ) << std::endl;
50+ if (stateChange.envinfo ().state ().empty ()) {
51+ continue ;
52+ }
53+ std::cout << stateChange.envinfo ().environmentid () << " => " << stateChange.envinfo ().state ()
54+ << " (" << stateChange.envinfo ().runnumber () << " )" << std::endl;
55+ auto metric = Metric{" env_info" }.addValue (stateChange.envinfo ().state (), " state" );
56+ auto detectorsProto = stateChange.envinfo ().detectors ();
57+ std::vector<std::string> detectors (detectorsProto.begin (), detectorsProto.end ());
58+ if (detectors.size () > 0 ) {
59+ metric.addValue (boost::algorithm::join (detectors, " " ), " detectors" );
60+ }
61+ int run = stateChange.envinfo ().runnumber ();
62+ if (run > 1 ) {
63+ metric.addValue (run, " run" );
64+ }
65+ influxdbBackend->sendWithId (metric, stateChange.envinfo ().environmentid ());
4066 }
4167 }
4268 std::this_thread::sleep_for (std::chrono::milliseconds (1000 ));
0 commit comments