Line data Source code
1 : /* 2 : * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include "stat_walker.h" 6 : #include "viz_message.h" 7 : 8 : using std::string; 9 : using std::vector; 10 : using std::make_pair; 11 : 12 : void 13 0 : StatWalker::Push(const std::string& name, 14 : const TagMap& tags, 15 : const DbHandler::AttribMap& attribs) { 16 : 17 0 : string prename(""); 18 0 : for (vector<StatNode>::const_iterator ni = nodes_.begin(); 19 0 : ni != nodes_.end(); ni++) { 20 0 : const StatNode& ancestor = *ni; 21 0 : if (!prename.empty()) { 22 0 : prename.append("."); 23 : } 24 0 : prename.append(ancestor.name); 25 : } 26 : 27 0 : StatNode sn; 28 0 : sn.name = name; 29 : 30 0 : if (!prename.empty()) { 31 0 : prename.append("."); 32 : } 33 0 : prename.append(sn.name); 34 : 35 : // For both tag names and attribute names, we need to convert 36 : // from local name to fully-qualified name 37 0 : for (TagMap::const_iterator ti = tags.begin(); 38 0 : ti != tags.end(); ti++) { 39 0 : std::string tname; 40 : 41 0 : VIZD_ASSERT(ti->first.find('.') == string::npos); 42 0 : tname = prename + "." + ti->first; 43 : 44 : // For prefixes, the tag prefix name is already fully-qualified 45 0 : sn.tags.insert(make_pair(tname, ti->second)); 46 0 : } 47 0 : for (DbHandler::AttribMap::const_iterator ai = attribs.begin(); 48 0 : ai != attribs.end(); ai++) { 49 0 : std::string aname; 50 : 51 0 : VIZD_ASSERT(ai->first.find('.') == string::npos); 52 0 : aname = prename + "." + ai->first; 53 0 : sn.attribs.insert(make_pair(aname, ai->second)); 54 0 : } 55 0 : nodes_.push_back(sn); 56 0 : } 57 : 58 : void 59 0 : StatWalker::FillTag(DbHandler::TagMap *attribs_tag, 60 : const std::pair<const std::string, TagVal>* ti) { 61 0 : DbHandler::Var pval, sval; 62 0 : std::string pname, sname; 63 0 : if (ti->second.prefix.second.type == DbHandler::INVALID) { 64 : // There is no prefix supplied 65 0 : pname = ti->first; 66 0 : pval = ti->second.val; 67 : } else { 68 : // Use the prefix 69 0 : pname = ti->second.prefix.first; 70 0 : pval = ti->second.prefix.second; 71 0 : sname = ti->first; 72 0 : sval = ti->second.val; 73 : } 74 : 75 0 : DbHandler::TagMap::iterator di = attribs_tag->find(pname); 76 0 : if (di == attribs_tag->end()) { 77 : // This 1st level tag is absent 78 0 : DbHandler::AttribMap amap; 79 0 : if (sval.type != DbHandler::INVALID) { 80 0 : amap.insert(make_pair(sname, sval)); 81 : } 82 0 : attribs_tag->insert(make_pair(pname, make_pair(pval, amap))); 83 0 : } else { 84 : // The 1st level tag is already present 85 : // Add the 2nd level tag if needed 86 0 : DbHandler::AttribMap &amap = di->second.second; 87 0 : if (sval.type != DbHandler::INVALID) { 88 0 : if (amap.find(sname) == amap.end()) { 89 0 : amap.insert(make_pair(sname, sval)); 90 : } 91 : } 92 : } 93 0 : } 94 : 95 : 96 : void 97 0 : StatWalker::Pop(void) { 98 0 : VIZD_ASSERT(!nodes_.empty()); 99 0 : DbHandler::AttribMap attribs = nodes_.back().attribs; 100 0 : DbHandler::TagMap attribs_tag; 101 : 102 0 : for (TagMap::const_iterator ti = top_tags_.begin(); 103 0 : ti != top_tags_.end(); ti++) { 104 0 : FillTag(&attribs_tag, &(*ti)); 105 : } 106 : 107 0 : string prename(""); 108 0 : for (vector<StatNode>::const_iterator ni = nodes_.begin(); 109 0 : ni != nodes_.end(); ni++) { 110 : 111 0 : const StatNode& ancestor = *ni; 112 0 : if (!prename.empty()) { 113 0 : prename.append("."); 114 : } 115 0 : prename.append(ancestor.name); 116 : 117 0 : for (TagMap::const_iterator ti = ancestor.tags.begin(); 118 0 : ti != ancestor.tags.end(); ti++) { 119 0 : FillTag(&attribs_tag, &(*ti)); 120 : } 121 : } 122 : 123 : // Take the final tags and also insert them as attribs 124 : // We may get duplicates ; the last value read will get used 125 0 : for (DbHandler::TagMap::const_iterator fi = attribs_tag.begin(); 126 0 : fi != attribs_tag.end(); fi++) { 127 0 : attribs.insert(make_pair(fi->first, fi->second.first)); 128 0 : attribs.insert(fi->second.second.begin(), fi->second.second.end()); 129 : } 130 0 : fn_(timestamp_, stat_name_, prename, attribs_tag, attribs); 131 0 : nodes_.pop_back(); 132 0 : } 133 : 134 0 : StatWalker::~StatWalker() { 135 0 : VIZD_ASSERT(nodes_.empty()); 136 0 : }