LCOV - code coverage report
Current view: top level - model - AdminClientEndpoint.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 82.7 % 52 43
Test Date: 2026-03-07 03:59:50 Functions: 80.0 % 5 4

            Line data    Source code
       1              : /*
       2              :  ___________________________________________
       3              : |    _     ___                        _     |
       4              : |   | |   |__ \                      | |    |
       5              : |   | |__    ) |__ _  __ _  ___ _ __ | |_   |
       6              : |   | '_ \  / // _` |/ _` |/ _ \ '_ \| __|  |  HTTP/2 AGENT FOR MOCK TESTING
       7              : |   | | | |/ /| (_| | (_| |  __/ | | | |_   |  Version 0.0.z
       8              : |   |_| |_|____\__,_|\__, |\___|_| |_|\__|  |  https://github.com/testillano/h2agent
       9              : |                     __/ |                 |
      10              : |                    |___/                  |
      11              : |___________________________________________|
      12              : 
      13              : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
      14              : SPDX-License-Identifier: MIT
      15              : Copyright (c) 2021 Eduardo Ramos
      16              : 
      17              : Permission is hereby  granted, free of charge, to any  person obtaining a copy
      18              : of this software and associated  documentation files (the "Software"), to deal
      19              : in the Software  without restriction, including without  limitation the rights
      20              : to  use, copy,  modify, merge,  publish, distribute,  sublicense, and/or  sell
      21              : copies  of  the Software,  and  to  permit persons  to  whom  the Software  is
      22              : furnished to do so, subject to the following conditions:
      23              : 
      24              : The above copyright notice and this permission notice shall be included in all
      25              : copies or substantial portions of the Software.
      26              : 
      27              : THE SOFTWARE  IS PROVIDED "AS  IS", WITHOUT WARRANTY  OF ANY KIND,  EXPRESS OR
      28              : IMPLIED,  INCLUDING BUT  NOT  LIMITED TO  THE  WARRANTIES OF  MERCHANTABILITY,
      29              : FITNESS FOR  A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT  SHALL THE
      30              : AUTHORS  OR COPYRIGHT  HOLDERS  BE  LIABLE FOR  ANY  CLAIM,  DAMAGES OR  OTHER
      31              : LIABILITY, WHETHER IN AN ACTION OF  CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      32              : OUT OF OR IN CONNECTION WITH THE SOFTWARE  OR THE USE OR OTHER DEALINGS IN THE
      33              : SOFTWARE.
      34              : */
      35              : 
      36              : #include <sstream>
      37              : #include <string>
      38              : #include <algorithm>
      39              : 
      40              : #include <nlohmann/json.hpp>
      41              : 
      42              : #include <ert/tracing/Logger.hpp>
      43              : 
      44              : #include <AdminClientEndpoint.hpp>
      45              : 
      46              : 
      47              : #include <functions.hpp>
      48              : 
      49              : 
      50              : namespace h2agent
      51              : {
      52              : namespace model
      53              : {
      54              : 
      55            7 : void AdminClientEndpoint::connect(bool fromScratch) {
      56              : 
      57            7 :     if (fromScratch) {
      58            1 :         client_.reset();
      59              :     }
      60            7 :     if (!client_) {
      61              : 
      62            7 :         client_ = std::make_shared<h2agent::http2::MyTrafficHttp2Client>("h2agent_traffic_client", host_, std::to_string(port_), secure_, nullptr);
      63              :         try {
      64            7 :             client_->enableMetrics(metrics_, response_delay_seconds_histogram_bucket_boundaries_, message_size_bytes_histogram_bucket_boundaries_, application_name_ + "_" + h2agent::model::fixMetricsName(key_)/*source label*/);
      65            7 :             client_->enableMyMetrics(metrics_, application_name_);
      66              :         }
      67            0 :         catch(std::exception &e)
      68              :         {
      69            0 :             client_->enableMetrics(nullptr); // force no metrics again
      70            0 :             client_->enableMyMetrics(nullptr);
      71            0 :             std::string msg = ert::tracing::Logger::asString("Cannot enable metrics for client endpoint '%s': %s", key_.c_str(), e.what());
      72            0 :             ert::tracing::Logger::error(msg, ERT_FILE_LOCATION);
      73            0 :         }
      74              :     }
      75            7 : }
      76              : 
      77           19 : void AdminClientEndpoint::setMetricsData(ert::metrics::Metrics *metrics, const ert::metrics::bucket_boundaries_t &responseDelaySecondsHistogramBucketBoundaries,
      78              :         const ert::metrics::bucket_boundaries_t &messageSizeBytesHistogramBucketBoundaries, const std::string &applicationName) {
      79              : 
      80           19 :     metrics_ = metrics;
      81           19 :     response_delay_seconds_histogram_bucket_boundaries_ = responseDelaySecondsHistogramBucketBoundaries;
      82           19 :     message_size_bytes_histogram_bucket_boundaries_ = messageSizeBytesHistogramBucketBoundaries;
      83           19 :     application_name_ = applicationName;
      84           19 : }
      85              : 
      86           26 : bool AdminClientEndpoint::load(const nlohmann::json &j) {
      87              : 
      88              :     // Store whole document (useful for GET operation)
      89           26 :     json_ = j;
      90              : 
      91              :     // Mandatory
      92           26 :     auto it = j.find("id");
      93           26 :     key_ = *it;
      94              : 
      95           26 :     it = j.find("host");
      96           26 :     host_ = *it;
      97              : 
      98           26 :     it = j.find("port");
      99           26 :     port_ = *it;
     100              : 
     101              :     // Optionals
     102           26 :     it = j.find("secure");
     103           26 :     secure_ = false;
     104           26 :     if (it != j.end() && it->is_boolean()) {
     105           13 :         secure_ = *it;
     106              :     }
     107           26 :     it = j.find("permit");
     108           26 :     permit_ = true;
     109           26 :     if (it != j.end() && it->is_boolean()) {
     110            5 :         permit_ = *it;
     111              :     }
     112              : 
     113              :     // Validations not in schema:
     114           26 :     if (key_.empty()) {
     115            3 :         ert::tracing::Logger::error("Invalid client endpoint identifier: provide a non-empty string", ERT_FILE_LOCATION);
     116            3 :         return false;
     117              :     }
     118              : 
     119           23 :     if (host_.empty()) {
     120            1 :         ert::tracing::Logger::error("Invalid client endpoint host: provide a non-empty string", ERT_FILE_LOCATION);
     121            1 :         return false;
     122              :     }
     123              : 
     124           22 :     return true;
     125              : }
     126              : 
     127            6 : nlohmann::json AdminClientEndpoint::asJson() const
     128              : {
     129            6 :     if (!client_) return json_;
     130              : 
     131            2 :     nlohmann::json result = json_;
     132            2 :     result["status"] = client_->getConnectionStatus();
     133              : 
     134            2 :     return result;
     135            2 : }
     136              : 
     137            0 : std::uint64_t AdminClientEndpoint::getGeneralUniqueClientSequence() const
     138              : {
     139            0 :     if (!client_) return 1;
     140              : 
     141            0 :     return client_->getGeneralUniqueClientSequence();
     142              : }
     143              : 
     144              : }
     145              : }
     146              : 
        

Generated by: LCOV version 2.0-1