LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-analytics/contrail-collector - usrdef_counters.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 5 83 6.0 %
Date: 2026-11-09 02:10:49 Functions: 3 8 37.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : 
       7             : #include <sstream>
       8             : #include <boost/make_shared.hpp>
       9             : #include <analytics/analytics_types.h>
      10             : #include "usrdef_counters.h"
      11             : #include "http/client/vncapi.h"
      12             : #include "options.h"
      13             : 
      14           7 : UserDefinedCounters::UserDefinedCounters() {
      15           7 : }
      16             : 
      17          14 : UserDefinedCounters::~UserDefinedCounters() {
      18           7 :     config_.erase(config_.begin(), config_.end());
      19          14 : }
      20             : 
      21             : void
      22           0 : UserDefinedCounters::UDCHandler(const contrail_rapidjson::Document &jdoc, bool add_change)
      23             : {
      24           0 :     if (!add_change) {
      25           0 :         return;
      26             :     }
      27             : 
      28           0 :     if (jdoc.IsObject() 
      29           0 :                  && jdoc.HasMember("global_system_config")) {
      30           0 :         if (!jdoc["global_system_config"].HasMember(
      31             :                   "user_defined_log_statistics")) {
      32           0 :             Cfg_t::iterator cit=config_.begin();
      33           0 :             while (cit != config_.end()) {
      34           0 :                 Cfg_t::iterator dit = cit++;
      35           0 :                 if (!dit->second->IsRefreshed()) {
      36           0 :                     UserDefinedLogStatistic udc;
      37           0 :                     udc.set_name(dit->first);
      38           0 :                     udc.set_deleted(true);
      39           0 :                     UserDefinedLogStatisticUVE::Send(udc);
      40           0 :                     config_.erase(dit);
      41           0 :                 }
      42             :             }    
      43           0 :             return;
      44             :         }
      45             : 
      46           0 :         if (!jdoc["global_system_config"]
      47           0 :                  ["user_defined_log_statistics"].IsObject()) {
      48           0 :             return;
      49             :         }
      50             : 
      51           0 :         if (!jdoc["global_system_config"]
      52           0 :                  ["user_defined_log_statistics"].HasMember("statlist")) {
      53           0 :             return;
      54             :         }
      55             : 
      56             :         const contrail_rapidjson::Value& gsc = jdoc
      57           0 :                 ["global_system_config"]["user_defined_log_statistics"]["statlist"];
      58           0 :         if (!gsc.IsArray()) {
      59           0 :             return;
      60             :         }
      61             : 
      62           0 :         for (contrail_rapidjson::SizeType i = 0; i < gsc.Size(); i++) {
      63           0 :             if (!gsc[i].IsObject() || !gsc[i].HasMember("name") ||
      64           0 :                     !gsc[i].HasMember("pattern")) {
      65           0 :                 continue;
      66             :             }
      67           0 :             std::string name = gsc[i]["name"].GetString(),
      68           0 :                         patrn = gsc[i]["pattern"].GetString();
      69           0 :             AddConfig(name, patrn);
      70             :             std::cout << "\nname: " << name << "\npattern: "
      71           0 :                 << patrn << "\n";
      72           0 :         }
      73             : 
      74           0 :         Cfg_t::iterator cit=config_.begin();
      75           0 :         while (cit != config_.end()) {
      76           0 :             Cfg_t::iterator dit = cit++;
      77           0 :             if (!dit->second->IsRefreshed()) {
      78           0 :                 UserDefinedLogStatistic udc;
      79           0 :                 udc.set_name(dit->first);
      80           0 :                 udc.set_deleted(true);
      81           0 :                 UserDefinedLogStatisticUVE::Send(udc);
      82           0 :                 config_.erase(dit);
      83           0 :             }
      84             :         }
      85             :     }
      86             : 
      87           0 :     return;
      88             : }
      89             : 
      90             : void
      91           0 : UserDefinedCounters::MatchFilter(std::string text, LineParser::WordListType *w)
      92             : {
      93           0 :     for(Cfg_t::iterator it=config_.begin(); it != config_.end(); ++it) {
      94           0 :         if (LineParser::SearchPattern(it->second->regexp(), text)) {
      95           0 :             UserDefinedLogStatistic udc;
      96           0 :             udc.set_name(it->first);
      97           0 :             udc.set_rx_event(1);
      98           0 :             UserDefinedLogStatisticUVE::Send(udc);
      99           0 :             w->insert(it->first);
     100           0 :         }
     101             :     }
     102           0 : }
     103             : 
     104             : void
     105           0 : UserDefinedCounters::AddConfig(std::string name, std::string pattern)
     106             : {
     107           0 :     Cfg_t::iterator it=config_.find(name);
     108           0 :     if (it  != config_.end()) {
     109           0 :         if (pattern == it->second->pattern()) {
     110             :             // ignore
     111             :         } else {
     112           0 :             it->second->SetPattern(pattern);
     113             :         }
     114           0 :         it->second->Refresh();
     115             :     } else {
     116             :         boost::shared_ptr<UserDefinedCounterData> c(new UserDefinedCounterData(
     117           0 :                     name, pattern));
     118             :         //config_.insert(std::make_pair<std::string,
     119             :         //        boost::shared_ptr<UserDefinedCounterData> >(name, c));
     120           0 :          config_.insert(std::make_pair(name, c));
     121             : 
     122           0 :     }
     123           0 : }
     124             : 
     125             : bool
     126           0 : UserDefinedCounters::FindByName(std::string name) {
     127           0 :     Cfg_t::iterator it=config_.find(name);
     128           0 :     return it  != config_.end();
     129             : }
     130             : 
     131             : void 
     132           0 : UserDefinedCounters::GetUDCConfig(std::vector<LogStatisticConfigInfo> 
     133             :                                               *config_info) {
     134           0 :     for (Cfg_t::iterator it = config_.begin(); 
     135           0 :                  it != config_.end(); it++) {
     136           0 :         LogStatisticConfigInfo config;
     137           0 :         config.set_name(it->second->name());
     138           0 :         config.set_pattern(it->second->pattern());
     139           0 :         config_info->push_back(config);
     140           0 :     }
     141           0 : }

Generated by: LCOV version 1.14