|
| 1 | +/* |
| 2 | + * LSST Data Management System |
| 3 | + * |
| 4 | + * This product includes software developed by the |
| 5 | + * LSST Project (http://www.lsst.org/). |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the LSST License Statement and |
| 18 | + * the GNU General Public License along with this program. If not, |
| 19 | + * see <http://www.lsstcorp.org/LegalNotices/>. |
| 20 | + */ |
| 21 | + |
| 22 | +#include "cconfig/DataManagementEvent.h" |
| 23 | + |
| 24 | +// System headers |
| 25 | +#include <stdexcept> |
| 26 | + |
| 27 | +using namespace std; |
| 28 | +using json = nlohmann::json; |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +string parseDatabase(json const& jsonObj, string const& context) { |
| 33 | + if (!jsonObj.contains("database")) { |
| 34 | + throw invalid_argument(context + ": Missing required field 'database'"); |
| 35 | + } |
| 36 | + return jsonObj.at("database").get<string>(); |
| 37 | +} |
| 38 | + |
| 39 | +string parseTable(json const& jsonObj, string const& context) { |
| 40 | + if (!jsonObj.contains("table")) { |
| 41 | + throw invalid_argument(context + ": Missing required field 'table'"); |
| 42 | + } |
| 43 | + return jsonObj.at("table").get<string>(); |
| 44 | +} |
| 45 | + |
| 46 | +} // namespace |
| 47 | + |
| 48 | +namespace lsst::qserv::cconfig { |
| 49 | + |
| 50 | +string DataManagementEvent::type2str(Type type) { |
| 51 | + switch (type) { |
| 52 | + case Type::NONE: |
| 53 | + return "NONE"; |
| 54 | + case Type::CHUNK_MAP_REBUILT: |
| 55 | + return "CHUNK_MAP_REBUILT"; |
| 56 | + case Type::DATABASE_PUBLISHED: |
| 57 | + return "DATABASE_PUBLISHED"; |
| 58 | + case Type::DATABASE_DELETED: |
| 59 | + return "DATABASE_DELETED"; |
| 60 | + case Type::TABLE_DELETED: |
| 61 | + return "TABLE_DELETED"; |
| 62 | + } |
| 63 | + throw invalid_argument("Unknown DataManagementEvent::Type: " + std::to_string(static_cast<int>(type))); |
| 64 | +} |
| 65 | + |
| 66 | +DataManagementEvent::Type DataManagementEvent::str2type(string const& str) { |
| 67 | + string const context = "DataManagementEvent::" + string(__func__); |
| 68 | + if (str == "NONE") return Type::NONE; |
| 69 | + if (str == "CHUNK_MAP_REBUILT") return Type::CHUNK_MAP_REBUILT; |
| 70 | + if (str == "DATABASE_PUBLISHED") return Type::DATABASE_PUBLISHED; |
| 71 | + if (str == "DATABASE_DELETED") return Type::DATABASE_DELETED; |
| 72 | + if (str == "TABLE_DELETED") return Type::TABLE_DELETED; |
| 73 | + throw invalid_argument(context + ": unknown DataManagementEvent::Type: '" + str + "'"); |
| 74 | +} |
| 75 | + |
| 76 | +DataManagementEvent DataManagementEvent::fromJson(json const& jsonObj) { |
| 77 | + string const context = "DataManagementEvent::" + string(__func__); |
| 78 | + DataManagementEvent event; |
| 79 | + if (!jsonObj.is_object()) throw invalid_argument(context + ": JSON object expected"); |
| 80 | + if (!jsonObj.contains("timestamp")) |
| 81 | + throw invalid_argument(context + ": Missing required field 'timestamp'"); |
| 82 | + event.timestamp = |
| 83 | + chrono::system_clock::time_point(chrono::milliseconds(jsonObj.at("timestamp").get<long long>())); |
| 84 | + if (!jsonObj.contains("type")) throw invalid_argument(context + ": Missing required field 'type'"); |
| 85 | + event.type = DataManagementEvent::str2type(jsonObj.at("type").get<string>()); |
| 86 | + switch (event.type) { |
| 87 | + case Type::NONE: |
| 88 | + break; |
| 89 | + case Type::CHUNK_MAP_REBUILT: |
| 90 | + break; |
| 91 | + case Type::DATABASE_PUBLISHED: |
| 92 | + event.database = ::parseDatabase(jsonObj, context); |
| 93 | + break; |
| 94 | + case Type::DATABASE_DELETED: |
| 95 | + event.database = ::parseDatabase(jsonObj, context); |
| 96 | + break; |
| 97 | + case Type::TABLE_DELETED: |
| 98 | + event.database = ::parseDatabase(jsonObj, context); |
| 99 | + event.table = ::parseTable(jsonObj, context); |
| 100 | + break; |
| 101 | + default: |
| 102 | + throw invalid_argument(context + ": Unknown DataManagementEvent::Type"); |
| 103 | + } |
| 104 | + return event; |
| 105 | +} |
| 106 | + |
| 107 | +json DataManagementEvent::toJson() const { |
| 108 | + return {{"timestamp", chrono::duration_cast<chrono::milliseconds>(timestamp.time_since_epoch()).count()}, |
| 109 | + {"type", DataManagementEvent::type2str(type)}, |
| 110 | + {"database", database}, |
| 111 | + {"table", table}}; |
| 112 | +} |
| 113 | + |
| 114 | +} // namespace lsst::qserv::cconfig |
0 commit comments