Program Listing for File webis_writer.cpp

Return to documentation for file (monitoring/webis_writer.cpp)

#include "webis_writer.hpp"
#include "webdaq/webdaq.hpp"

void WebISWriter::write_message(const nlohmann::json& message) {
    try {
        std::string name = message["hostname"].get<std::string>();

        const std::string partition {"FelixPhase2_IS_Partition"};
        const std::string server {"FelixPhase2_IS_Server"};
        const std::string type_prefix {"FELIX"};

        for (const auto& device : message["devices"]) {
            // Select the correct schema
            std::string app = device["app"].get<std::string>();

            const auto type_postfix = std::invoke([&app]() {
                if (app == "tohost") {
                    return "tohost";
                } else if (app == "fromhost"){
                    return "fromhost";
                } else {
                    throw monitoring_log::webis_issue("\"app\" is neither Tohost nor Fromhost");
                }
            });

            name = std::format("{}-{}", name, type_postfix);
            const auto type = std::format("{}-{}", type_prefix, type_postfix);

            // Send data to WebIS server
            const bool success = webdaq::is::put(partition, server, name, type, message);
            if (not success) {
                ers::error(monitoring_log::webis_issue("Failed to send data to WebIS server"));
            }
        }
    } catch (const nlohmann::json::exception& json_err) {
        ers::error(monitoring_log::webis_issue(std::format("JSON error: {}", json_err.what())));
    } catch (const monitoring_log::webis_issue& monitoring_err) {
        ers::error(monitoring_err);
    } catch (const std::exception& general_err) {
        ers::error(monitoring_log::webis_issue(std::format("Error: {}", general_err.what())));
    }
}