| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "na64calib/indices/yaml-index.hh" | ||
| 2 | #include "na64util/str-fmt.hh" | ||
| 3 | |||
| 4 | #include <yaml-cpp/yaml.h> | ||
| 5 | |||
| 6 | namespace na64dp { | ||
| 7 | namespace calib { | ||
| 8 | |||
| 9 | void | ||
| 10 | ✗ | YAMLIndex::add_records( const YAML::Node & entries | |
| 11 | , const std::unordered_map<std::string, Dispatcher::CIDataID> & aliases | ||
| 12 | , const std::unordered_map<std::string, std::shared_ptr<iLoader>> & loadersByName | ||
| 13 | ) { | ||
| 14 | ✗ | auto & L = log4cpp::Category::getInstance("calibrations.yamlDoc"); | |
| 15 | ✗ | if( ! entries.IsSequence() ) { | |
| 16 | ✗ | NA64DP_RUNTIME_ERROR( "YAML entity provided to YAMLIndex ctr" | |
| 17 | " is not a sequence of objects." ); | ||
| 18 | } | ||
| 19 | ✗ | size_t nRecord = 0; | |
| 20 | ✗ | for( YAML::const_iterator it = entries.begin() | |
| 21 | ✗ | ; entries.end() != it | |
| 22 | ✗ | ; ++it, ++nRecord ) { | |
| 23 | ✗ | if( ! it->IsMap() ) { | |
| 24 | ✗ | NA64DP_RUNTIME_ERROR( "YAML node provided as data record #%zu" | |
| 25 | " is not a map (object).", nRecord ); | ||
| 26 | } | ||
| 27 | ✗ | if( ! (*it)["type"] ) { | |
| 28 | ✗ | NA64DP_RUNTIME_ERROR( "YAML node provided as data record #%zu" | |
| 29 | " has no \"type\" key.", nRecord ); | ||
| 30 | } | ||
| 31 | // Get type | ||
| 32 | ✗ | const std::string aliasTypeName = (*it)["type"].as<std::string>(); | |
| 33 | ✗ | auto aliasIt = aliases.find(aliasTypeName); | |
| 34 | ✗ | if( aliases.end() == aliasIt ) { | |
| 35 | ✗ | L.warn("YAML calibration index has record #%zu defined for" | |
| 36 | " an unknown aliased type \"%s\". Record omitted." | ||
| 37 | , nRecord, aliasTypeName.c_str() ); | ||
| 38 | ✗ | continue; | |
| 39 | } | ||
| 40 | ✗ | const Dispatcher::CIDataID typeID(aliasIt->second); | |
| 41 | // Get loader | ||
| 42 | ✗ | std::shared_ptr<iLoader> loaderPtr; | |
| 43 | ✗ | if( (*it)["loader"] ) { | |
| 44 | ✗ | auto loaderIt = loadersByName.find((*it)["loader"].as<std::string>()); | |
| 45 | ✗ | if(loadersByName.end() == loaderIt) { | |
| 46 | // Not a warning -- dynamic loader lookup will be involved, | ||
| 47 | // probably resulting insignificant performance drop. | ||
| 48 | ✗ | L.debug("YAML calibration index has record #%zu defined for" | |
| 49 | " an aliased type \"%s\" refers to unknown loader \"%s\"." | ||
| 50 | , nRecord | ||
| 51 | , aliasTypeName.c_str() | ||
| 52 | ✗ | , (*it)["loader"].as<std::string>().c_str() | |
| 53 | ); | ||
| 54 | } else { | ||
| 55 | ✗ | loaderPtr = loaderIt->second; | |
| 56 | } | ||
| 57 | } | ||
| 58 | // Require that validity is set either by runs or by date and time | ||
| 59 | ✗ | if(((bool) (*it)["validForRuns"]) == ((bool) (*it)["validForDatetime"])) { | |
| 60 | ✗ | NA64DP_RUNTIME_ERROR( "Validity range" | |
| 61 | " for the record #%zu (type \"%s\") is not properly set." | ||
| 62 | " Record have one of \"validForRuns\" / \"validForDatetime\"" | ||
| 63 | " attributes.", nRecord, aliasTypeName.c_str() ); | ||
| 64 | } | ||
| 65 | // Add to the validity map | ||
| 66 | ✗ | if( (*it)["validForRuns"] ) { | |
| 67 | ✗ | const YAML::Node & runsValidity = (*it)["validForRuns"]; | |
| 68 | |||
| 69 | ✗ | if(!(runsValidity.IsSequence() && 2 == runsValidity.size()) ) { | |
| 70 | ✗ | NA64DP_RUNTIME_ERROR( "Validity range (\"validForRuns\")" | |
| 71 | " for the record #%zu is not a list of length 2." | ||
| 72 | , nRecord ); | ||
| 73 | } | ||
| 74 | |||
| 75 | // copy YAML, remove the `validFromRun` and `loader` fields | ||
| 76 | ✗ | YAML::Node nodeCpy(*it); | |
| 77 | ✗ | nodeCpy.remove("loader"); | |
| 78 | ✗ | nodeCpy.remove("validForRuns"); | |
| 79 | |||
| 80 | ✗ | EventID from( (!runsValidity[0].IsNull()) | |
| 81 | ✗ | ? runsValidity[0].as<na64sw_runNo_t>() | |
| 82 | : 1 | ||
| 83 | ✗ | , 0, 0 ) | |
| 84 | ✗ | , to((runsValidity[1] && !runsValidity[1].IsNull()) | |
| 85 | ✗ | ? runsValidity[1].as<na64sw_runNo_t>() | |
| 86 | : 0 // (unset) | ||
| 87 | ✗ | ) | |
| 88 | ; | ||
| 89 | |||
| 90 | ✗ | L << log4cpp::Priority::DEBUG | |
| 91 | ✗ | << "Adding entry of " << aliasTypeName | |
| 92 | ✗ | << " with run number validity " | |
| 93 | ✗ | << from.to_str() | |
| 94 | ✗ | << " - " | |
| 95 | ✗ | << to.to_str() | |
| 96 | ✗ | << ") from YAML index."; | |
| 97 | |||
| 98 | RangeOverrideRunIndex<EventID, YAML::Node> | ||
| 99 | ✗ | ::add_entry( from | |
| 100 | , aliasTypeName | ||
| 101 | ✗ | , RangeOverrideRunIndex<EventID, YAML::Node>::UpdateRecipe( | |
| 102 | typeID | ||
| 103 | , to | ||
| 104 | , loaderPtr | ||
| 105 | , nodeCpy | ||
| 106 | ) | ||
| 107 | ); | ||
| 108 | ✗ | } else if( (*it)["validForDatetime"] ) { | |
| 109 | ✗ | const YAML::Node & runsValidity = (*it)["validForDatetime"]; | |
| 110 | ✗ | if( !(runsValidity.IsSequence() && 2 == runsValidity.size()) ) { | |
| 111 | ✗ | NA64DP_RUNTIME_ERROR( "Validity range (\"validForDatetime\")" | |
| 112 | " for the record #%zu is not a list of length 2." | ||
| 113 | , nRecord ); | ||
| 114 | } | ||
| 115 | |||
| 116 | ✗ | std::pair<time_t, uint32_t> from{ util::parse_timedate_strexpr( | |
| 117 | ✗ | (*it)["validForDatetime"][0].as<std::string>().c_str()), 0 }; | |
| 118 | ✗ | if( !from.first ) { | |
| 119 | ✗ | NA64DP_RUNTIME_ERROR( "Failed to parse validity range" | |
| 120 | " `from'-token (first element of \"validForDatetime\"" | ||
| 121 | " attribute) \"%s\" for the record #%zu." | ||
| 122 | , (*it)["validForDatetime"][0].as<std::string>().c_str() | ||
| 123 | , nRecord | ||
| 124 | ); | ||
| 125 | } | ||
| 126 | ✗ | std::pair<time_t, uint32_t> to{0, 0}; | |
| 127 | ✗ | if((*it)["validForDatetime"][1]) { | |
| 128 | ✗ | to.first = util::parse_timedate_strexpr( | |
| 129 | ✗ | (*it)["validForDatetime"][1].as<std::string>().c_str()); | |
| 130 | ✗ | if( !from.first ) { | |
| 131 | ✗ | NA64DP_RUNTIME_ERROR( "Failed to parse validity range" | |
| 132 | " to-token (first element of \"validForDatetime\"" | ||
| 133 | " attribute) \"%s\" for the record #%zu." | ||
| 134 | , (*it)["validForDatetime"][1].as<std::string>().c_str() | ||
| 135 | , nRecord ); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | // copy YAML, remove the `validFromRun` and `loader` fields | ||
| 140 | ✗ | YAML::Node nodeCpy(*it); | |
| 141 | ✗ | nodeCpy.remove("loader"); | |
| 142 | ✗ | nodeCpy.remove("validForDatetime"); | |
| 143 | |||
| 144 | ✗ | L << log4cpp::Priority::DEBUG | |
| 145 | ✗ | << "Adding entry of " << aliasTypeName | |
| 146 | ✗ | << " with datetime validity " | |
| 147 | ✗ | << util::str_format_timedate(from.first) | |
| 148 | ✗ | << " (" | |
| 149 | ✗ | << aux::key_to_str_for_yaml( from ) | |
| 150 | ✗ | << ") - " | |
| 151 | ✗ | << util::str_format_timedate(to.first) | |
| 152 | ✗ | << " (" | |
| 153 | ✗ | << aux::key_to_str_for_yaml( to ) | |
| 154 | ✗ | << ") from YAML index."; | |
| 155 | RangeOverrideRunIndex<std::pair<time_t, uint32_t>, YAML::Node> | ||
| 156 | ✗ | ::add_entry( from | |
| 157 | , aliasTypeName | ||
| 158 | ✗ | , RangeOverrideRunIndex<std::pair<time_t, uint32_t>, YAML::Node>::UpdateRecipe( | |
| 159 | typeID | ||
| 160 | , to, loaderPtr, nodeCpy ) | ||
| 161 | ); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | void | ||
| 167 | ✗ | YAMLIndex::append_updates( EventID oldEventID, EventID newEventID | |
| 168 | , const std::pair<time_t, uint32_t> & oldDatetime | ||
| 169 | , const std::pair<time_t, uint32_t> & newDateTime | ||
| 170 | , Updates & upds ) { | ||
| 171 | ✗ | RangeOverrideRunIndex<EventID, YAML::Node>::append_updates( | |
| 172 | oldEventID, newEventID, oldDatetime, newDateTime, upds ); | ||
| 173 | ✗ | RangeOverrideRunIndex<std::pair<time_t, uint32_t>, YAML::Node>::append_updates( | |
| 174 | oldEventID, newEventID, oldDatetime, newDateTime, upds ); | ||
| 175 | } | ||
| 176 | |||
| 177 | void | ||
| 178 | ✗ | YAMLIndex::to_yaml(YAML::Node & outNode) const { | |
| 179 | char bf[64]; | ||
| 180 | ✗ | YAML::Node byEvent, byTime; | |
| 181 | ✗ | RangeOverrideRunIndex<EventID, YAML::Node>::to_yaml(byEvent); | |
| 182 | ✗ | RangeOverrideRunIndex<std::pair<time_t, uint32_t>, YAML::Node>::to_yaml(byTime); | |
| 183 | ✗ | outNode["byEvent"] = byEvent; | |
| 184 | ✗ | outNode["byTime"] = byTime; | |
| 185 | ✗ | outNode["_type"] = "YAMLIndex"; | |
| 186 | ✗ | snprintf(bf, sizeof(bf), "%p", this); | |
| 187 | ✗ | outNode["_ptr"] = bf; | |
| 188 | } | ||
| 189 | |||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 |