diff --git a/src/lotman.cpp b/src/lotman.cpp index 7a6d428..d8f4611 100644 --- a/src/lotman.cpp +++ b/src/lotman.cpp @@ -899,6 +899,10 @@ int lotman_get_policy_attributes(const char *policy_attributes_JSON_str, } return -1; } + if (pair.value() == "true") { + // In this case, we unpack the returned object + output_obj[pair.key()] = rp_json_bool.first["value"]; + } output_obj[pair.key()] = rp_json_bool.first; } } @@ -1570,8 +1574,9 @@ int lotman_get_lot_as_json(const char *lot_name, const bool recursive, char **ou // Add management policy attributes according to recursive flag std::array man_pol_keys = {"dedicated_GB", "opportunistic_GB", "max_num_objects", "creation_time", "deletion_time", "expiration_time"}; json internal_man_pol_obj; + json internal_man_pol_obj_restrictive; for (const auto &key : man_pol_keys) { - rp_json_str = lot.get_restricting_attribute(key, recursive); + rp_json_str = lot.get_restricting_attribute(key, false); if (!rp_json_str.second.empty()) { // There was an error if (err_msg) { std::string int_err = rp_json_str.second; @@ -1581,10 +1586,29 @@ int lotman_get_lot_as_json(const char *lot_name, const bool recursive, char **ou return -1; } - internal_man_pol_obj[key] = rp_json_str.first; + internal_man_pol_obj[key] = rp_json_str.first["value"]; + + if (recursive) { + rp_json_str = lot.get_restricting_attribute(key, true); + if (!rp_json_str.second.empty()) { // There was an error + if (err_msg) { + std::string int_err = rp_json_str.second; + std::string ext_err = "Failure on call to get_restricting_attribute: "; + *err_msg = strdup((ext_err + int_err).c_str()); + } + return -1; + } + + + internal_man_pol_obj_restrictive[key] = rp_json_str.first; + } } output_obj["management_policy_attrs"] = internal_man_pol_obj; + if (recursive) { + output_obj["restrictive_management_policy_attrs"] = internal_man_pol_obj_restrictive; + } + // Add usage according to recursive flag std::array usage_keys = {"dedicated_GB", "opportunistic_GB", "total_GB", "num_objects", "GB_being_written", "objects_being_written"}; json internal_usage_obj; diff --git a/src/lotman_internal.cpp b/src/lotman_internal.cpp index 7db293c..4058963 100644 --- a/src/lotman_internal.cpp +++ b/src/lotman_internal.cpp @@ -555,6 +555,8 @@ std::pair lotman::Lot::get_restricting_attribute(const std::s } internal_obj["lot_name"] = restricting_parent_name; internal_obj["value"] = std::stod(value[0]); + } else { + internal_obj["value"] = std::stod(value[0]); } return std::make_pair(internal_obj, ""); } @@ -696,7 +698,7 @@ std::pair lotman::Lot::get_lot_usage(const std::string key, c return std::make_pair(json(), ext_err + int_err); } query_output = rp_single.first; - output_obj["total"] = std::stod(query_output[0]); + output_obj["self_contrib"] = std::stod(query_output[0]); } } @@ -757,7 +759,7 @@ std::pair lotman::Lot::get_lot_usage(const std::string key, c return std::make_pair(json(), ext_err + int_err); } query_output = rp_single.first; - output_obj["total"] = std::stod(query_output[0]); + output_obj["self_contrib"] = std::stod(query_output[0]); } } @@ -776,6 +778,7 @@ std::pair lotman::Lot::get_lot_usage(const std::string key, c query_multi_out = rp_multi.first; output_obj["self_contrib"] = std::stod(query_multi_out[0][0]); output_obj["children_contrib"] = std::stod(query_multi_out[0][1]); + output_obj["total"] = std::stod(query_multi_out[0][0]) + std::stod(query_multi_out[0][1]); } else { std::string usage_GB_query = "SELECT self_GB FROM lot_usage WHERE lot_name = ?;"; @@ -787,7 +790,7 @@ std::pair lotman::Lot::get_lot_usage(const std::string key, c return std::make_pair(json(), ext_err + int_err); } query_output = rp_single.first; - output_obj["total"] = std::stod(query_output[0]); + output_obj["self_contrib"] = std::stod(query_output[0]); } } @@ -804,6 +807,7 @@ std::pair lotman::Lot::get_lot_usage(const std::string key, c query_multi_out = rp_multi.first; output_obj["self_contrib"] = std::stod(query_multi_out[0][0]); output_obj["children_contrib"] = std::stod(query_multi_out[0][1]); + output_obj["total"] = std::stod(query_multi_out[0][0]) + std::stod(query_multi_out[0][1]); } else { @@ -816,7 +820,7 @@ std::pair lotman::Lot::get_lot_usage(const std::string key, c return std::make_pair(json(), ext_err + int_err); } query_output = rp_single.first; - output_obj["total"] = std::stod(query_output[0]); + output_obj["self_contrib"] = std::stod(query_output[0]); } } @@ -833,6 +837,7 @@ std::pair lotman::Lot::get_lot_usage(const std::string key, c query_multi_out = rp_multi.first; output_obj["self_contrib"] = std::stod(query_multi_out[0][0]); output_obj["children_contrib"] = std::stod(query_multi_out[0][1]); + output_obj["total"] = std::stod(query_multi_out[0][0]) + std::stod(query_multi_out[0][1]); } else { @@ -845,7 +850,7 @@ std::pair lotman::Lot::get_lot_usage(const std::string key, c return std::make_pair(json(), ext_err + int_err); } query_output = rp_single.first; - output_obj["total"] = std::stod(query_output[0]); + output_obj["self_contrib"] = std::stod(query_output[0]); } } @@ -862,6 +867,7 @@ std::pair lotman::Lot::get_lot_usage(const std::string key, c query_multi_out = rp_multi.first; output_obj["self_contrib"] = std::stod(query_multi_out[0][0]); output_obj["children_contrib"] = std::stod(query_multi_out[0][1]); + output_obj["total"] = std::stod(query_multi_out[0][0]) + std::stod(query_multi_out[0][1]); } else { @@ -874,7 +880,7 @@ std::pair lotman::Lot::get_lot_usage(const std::string key, c return std::make_pair(json(), ext_err + int_err); } query_output = rp_single.first; - output_obj["total"] = std::stod(query_output[0]); + output_obj["self_contrib"] = std::stod(query_output[0]); } } diff --git a/test/main.cpp b/test/main.cpp index 4abac34..07a0230 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -297,7 +297,7 @@ namespace { json json_out = json::parse(output); ASSERT_TRUE(json_out["dedicated_GB"]["children_contrib"] == 6.5 && json_out["dedicated_GB"]["self_contrib"] == 3.5 && json_out["dedicated_GB"]["total"] == 10 && json_out["opportunistic_GB"]["children_contrib"] == 3.5 && json_out["opportunistic_GB"]["self_contrib"] == 0 && json_out["opportunistic_GB"]["total"] == 3.5 && - json_out["total_GB"]["children_contrib"] == 10.5 && json_out["total_GB"]["self_contrib"] == 3.5); + json_out["total_GB"]["children_contrib"] == 10.5 && json_out["total_GB"]["self_contrib"] == 3.5 && json_out["total_GB"]["total"] == 14); free(output); @@ -314,7 +314,7 @@ namespace { ASSERT_TRUE(rv == 0) << err_msg3; json lot1_json_out = json::parse(lot1_output); free(lot1_output); - ASSERT_TRUE(lot1_json_out["total_GB"]["total"] == 10.383 && lot1_json_out["num_objects"]["total"] == 40); + ASSERT_TRUE(lot1_json_out["total_GB"]["self_contrib"] == 10.383 && lot1_json_out["num_objects"]["self_contrib"] == 40); // Check output for lot4 const char *lot4_usage_query = "{\"lot_name\": \"lot4\", \"total_GB\":false, \"num_objects\":false}"; @@ -323,7 +323,7 @@ namespace { ASSERT_TRUE(rv == 0) << err_msg3; json lot4_json_out = json::parse(lot4_output); free(lot4_output); - ASSERT_TRUE(lot4_json_out["total_GB"]["total"] == 3.14 && lot4_json_out["num_objects"]["total"] == 6); + ASSERT_TRUE(lot4_json_out["total_GB"]["self_contrib"] == 3.14 && lot4_json_out["num_objects"]["self_contrib"] == 6); // Check output for default const char *default_usage_query = "{\"lot_name\": \"default\", \"total_GB\":false, \"num_objects\":false}"; @@ -332,7 +332,7 @@ namespace { ASSERT_TRUE(rv == 0) << err_msg; json default_json_out = json::parse(default_output); free(default_output); - ASSERT_TRUE(default_json_out["total_GB"]["total"] == 0.75 && default_json_out["num_objects"]["total"] == 0); + ASSERT_TRUE(default_json_out["total_GB"]["self_contrib"] == 0.75 && default_json_out["num_objects"]["self_contrib"] == 0); // Update by dir in delta mode -- This ends up updating lot4 deltaMode = true; @@ -346,7 +346,7 @@ namespace { ASSERT_TRUE(rv == 0) << err_msg3; lot4_json_out = json::parse(lot4_output); free(lot4_output); - ASSERT_TRUE(lot4_json_out["total_GB"]["total"] == 5.14 && lot4_json_out["num_objects"]["total"] == 3) << lot4_json_out.dump(); + ASSERT_TRUE(lot4_json_out["total_GB"]["self_contrib"] == 5.14 && lot4_json_out["num_objects"]["self_contrib"] == 3) << lot4_json_out.dump(); // Update by dir in delta mode, but attempt to make self_gb negative (should fail) const char *update3_JSON_str = "[{\"includes_subdirs\": false,\"num_obj\": 0,\"path\": \"/1/2/3/4\",\"size_GB\": -10,\"subdirs\": []}]"; @@ -613,14 +613,22 @@ namespace { ASSERT_FALSE(rv == 0); free(err_msg); + // Non-recursive test char *err_msg2; - rv = lotman_get_lot_as_json("lot3", true, &output, &err_msg); - ASSERT_TRUE(rv == 0); - + rv = lotman_get_lot_as_json("lot3", false, &output, &err_msg); + ASSERT_TRUE(rv == 0) << err_msg; json output_JSON = json::parse(output); free(output); - json expected_output = R"({"children":["lot4","lot5"],"lot_name":"lot3","management_policy_attrs":{"creation_time":{"lot_name":"lot3","value":123.0},"dedicated_GB":{"lot_name":"sep_node","value":3.0},"deletion_time":{"lot_name":"lot3","value":333.0},"expiration_time":{"lot_name":"lot3","value":222.0},"max_num_objects":{"lot_name":"sep_node","value":10.0},"opportunistic_GB":{"lot_name":"lot2","value":1.5}},"owners":["not owner1","owner1"],"parents":["lot1","lot2","sep_node"],"paths":[{"lot_name":"lot3","path":"/another/path","recursive":true},{"lot_name":"lot3","path":"/updated/path","recursive":false},{"lot_name":"lot3","path":"/foo/barr","recursive":true},{"lot_name":"lot4","path":"/1/2/3/4","recursive":true},{"lot_name":"lot4","path":"/345","recursive":true},{"lot_name":"lot5","path":"/456","recursive":false},{"lot_name":"lot5","path":"/567","recursive":true}],"usage":{"GB_being_written":{"children_contrib":3.4,"self_contrib":0.0},"dedicated_GB":{"children_contrib":8.64,"self_contrib":0.0,"total":8.64},"num_objects":{"children_contrib":10.0,"self_contrib":0.0},"objects_being_written":{"children_contrib":7.0,"self_contrib":0.0},"opportunistic_GB":{"children_contrib":0.0,"self_contrib":0.0,"total":0.0},"total_GB":{"children_contrib":8.64,"self_contrib":0.0}}})"_json; + json expected_output = R"({"children":["lot5"],"lot_name":"lot3","management_policy_attrs":{"creation_time":123.0,"dedicated_GB":10.111,"deletion_time":333.0,"expiration_time":222.0,"max_num_objects":50.0,"opportunistic_GB":6.6},"owners":"not owner1","parents":["lot2","sep_node"],"paths":[{"lot_name":"lot3","path":"/another/path","recursive":true},{"lot_name":"lot3","path":"/updated/path","recursive":false},{"lot_name":"lot3","path":"/foo/barr","recursive":true}],"usage":{"GB_being_written":{"self_contrib":0.0},"dedicated_GB":{"self_contrib":0.0},"num_objects":{"self_contrib":0.0},"objects_being_written":{"self_contrib":0.0},"opportunistic_GB":{"self_contrib":0.0},"total_GB":{"self_contrib":0.0}}})"_json; ASSERT_TRUE(output_JSON == expected_output) << output_JSON; + + char *output2; + rv = lotman_get_lot_as_json("lot3", true, &output2, &err_msg); + ASSERT_TRUE(rv == 0); + json output_JSON2 = json::parse(output2); + free(output2); + json expected_output2 = R"({"children":["lot4","lot5"],"lot_name":"lot3","management_policy_attrs":{"creation_time":123.0,"dedicated_GB":10.111,"deletion_time":333.0,"expiration_time":222.0,"max_num_objects":50.0,"opportunistic_GB":6.6},"owners":["not owner1","owner1"],"parents":["lot1","lot2","sep_node"],"paths":[{"lot_name":"lot3","path":"/another/path","recursive":true},{"lot_name":"lot3","path":"/updated/path","recursive":false},{"lot_name":"lot3","path":"/foo/barr","recursive":true},{"lot_name":"lot4","path":"/1/2/3/4","recursive":true},{"lot_name":"lot4","path":"/345","recursive":true},{"lot_name":"lot5","path":"/456","recursive":false},{"lot_name":"lot5","path":"/567","recursive":true}],"restrictive_management_policy_attrs":{"creation_time":{"lot_name":"lot3","value":123.0},"dedicated_GB":{"lot_name":"sep_node","value":3.0},"deletion_time":{"lot_name":"lot3","value":333.0},"expiration_time":{"lot_name":"lot3","value":222.0},"max_num_objects":{"lot_name":"sep_node","value":10.0},"opportunistic_GB":{"lot_name":"lot2","value":1.5}},"usage":{"GB_being_written":{"children_contrib":3.4,"self_contrib":0.0,"total":3.4},"dedicated_GB":{"children_contrib":8.64,"self_contrib":0.0,"total":8.64},"num_objects":{"children_contrib":10.0,"self_contrib":0.0,"total":10.0},"objects_being_written":{"children_contrib":7.0,"self_contrib":0.0,"total":7.0},"opportunistic_GB":{"children_contrib":0.0,"self_contrib":0.0,"total":0.0},"total_GB":{"children_contrib":8.64,"self_contrib":0.0,"total":8.64}}})"_json; + ASSERT_TRUE(output_JSON2 == expected_output2) << output_JSON2; } TEST_F(LotManTest, LotsFromDirTest) {