LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-analytics/contrail-query-engine - utils.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 53 69 76.8 %
Date: 2026-11-09 02:10:49 Functions: 5 6 83.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include <string>
       7             : #include <sstream>
       8             : #include <base/time_util.h>
       9             : #include <base/string_util.h>
      10             : 
      11             : #include "json_parse.h"
      12             : #include "utils.h"
      13             : #include "analytics/viz_types.h"
      14             : 
      15             : /*
      16             :  * time could be in now +/-10m/h/s format.Parse that and return th
      17             :  * UTC corresponding to the parsed val
      18             :  * only formats supported are of the format ""\now+1d\"", ""\now-10h\"", etc..
      19             :  */
      20        3982 : bool parse_time(const std::string& relative_time, uint64_t *usec_time)
      21             : {
      22        3982 :     uint64_t offset_usec = 0;
      23        3982 :     std::string temp;
      24        3982 :     if (!relative_time.compare("\"now\"")) {
      25          71 :         *usec_time = UTCTimestampUsec();
      26          71 :         return true;
      27        3911 :     } else if (!(relative_time.substr(1,3)).compare("now")) {
      28             :         //Find the offset to be shifted
      29          79 :         int found = 0;
      30             :         //Extract any number after now
      31          79 :         if ((found = relative_time.find_last_of("h")) > 0) {
      32          21 :             if (!stringToInteger(relative_time.substr(5,relative_time.length()-7), offset_usec)) {
      33           2 :                 return false;
      34             :             }
      35          19 :             offset_usec = offset_usec*3600*1000000;
      36          58 :         } else if ((found = relative_time.find_last_of("m")) > 0) {
      37          55 :             if (!stringToInteger(relative_time.substr(5,relative_time.length()-7), offset_usec)) {
      38           2 :                 return false;
      39             :             }
      40          53 :             offset_usec = offset_usec*60*1000000;
      41           3 :         } else if ((found = relative_time.find_last_of("s")) > 0) {
      42           1 :             if (!stringToInteger(relative_time.substr(5,relative_time.length()-7), offset_usec)) {
      43           0 :                 return false;
      44             :             }
      45           1 :             offset_usec = offset_usec*1000000;
      46           2 :         } else if ((found = relative_time.find_last_of("d")) > 0) {
      47           1 :             if (!stringToInteger(relative_time.substr(5,relative_time.length()-7), offset_usec)) {
      48           0 :                 return false;
      49             :             }
      50           1 :             offset_usec = offset_usec*24*3600*1000000;
      51             :         } else {
      52           1 :             return false;
      53             :         }
      54             : 
      55             :         //If now+ return UTC + offset else UTC - offset 
      56          74 :         if (!(relative_time.substr(1,4)).compare("now+")) {
      57           2 :             *usec_time = UTCTimestampUsec() + offset_usec;
      58           2 :             return true;
      59          72 :         } else if (!(relative_time.substr(1,4)).compare("now-")) {
      60          72 :             *usec_time = UTCTimestampUsec() - offset_usec;
      61          72 :             return true;
      62             :         } else {
      63           0 :             return false;
      64             :         }
      65             :     } else {
      66             :         //To handle old version of input where integer is parsed
      67        3835 :         if (!stringToInteger(relative_time, offset_usec)) {
      68           2 :             return false;
      69             :         }
      70        3835 :         *usec_time = offset_usec;
      71        3835 :         return true;
      72             :     }
      73        3987 : }
      74             : 
      75        1093 : GenDb::Op::type get_gendb_op_from_op(int op) {
      76        1093 :     switch(op) {
      77         988 :         case EQUAL:
      78         988 :             return GenDb::Op::EQ;
      79           0 :         case LEQ:
      80           0 :             return GenDb::Op::LE;
      81           0 :         case GEQ:
      82           0 :             return GenDb::Op::GE;
      83         105 :         case PREFIX:
      84         105 :             return GenDb::Op::LIKE;
      85           0 :         default:
      86           0 :             return GenDb::Op::INVALID;
      87             :     }
      88             : }
      89             : 
      90       77725 : std::string MsgTableIndexToColumn(const int index) {
      91      155331 :     return (g_viz_constants._VIZD_TABLE_SCHEMA.find(
      92             :                     g_viz_constants.COLLECTOR_GLOBAL_TABLE))\
      93      155273 :                     ->second.columns[index].name;
      94             : }
      95             : 
      96       52836 : GenDb::ColIndexType::type MsgTableIndexToIndexType(const int index) {
      97       52836 :     return (g_viz_constants._VIZD_TABLE_SCHEMA.find(
      98             :                     g_viz_constants.COLLECTOR_GLOBAL_TABLE))\
      99       52819 :                     ->second.columns[index].index_type;
     100             : }
     101             : 
     102           0 : std::string MsgTableQueryColumnToColumn(const std::string query_column) {
     103           0 :     table_schema schema = g_viz_constants._VIZD_TABLE_SCHEMA.find(
     104           0 :                                 g_viz_constants.COLLECTOR_GLOBAL_TABLE)->second;
     105             :     std::map<std::string, std::string>::const_iterator it = 
     106           0 :                 schema.query_column_to_column.find(query_column);
     107           0 :     assert(it != schema.query_column_to_column.end());
     108           0 :     return it->second;
     109           0 : }
     110             : 
     111       77612 : std::string MsgTableColumnToQueryColumn(const std::string columnN) {
     112       77612 :     table_schema schema = g_viz_constants._VIZD_TABLE_SCHEMA.find(
     113       77522 :                                 g_viz_constants.COLLECTOR_GLOBAL_TABLE)->second;
     114             :     std::map<std::string, std::string>::const_iterator it = 
     115       77439 :                 schema.column_to_query_column.find(columnN);
     116       77499 :     assert(it != schema.column_to_query_column.end());
     117      155213 :     return it->second;
     118       77416 : }

Generated by: LCOV version 1.14