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