Skip to content

Commit

Permalink
fix for getLatestKernels (#49)
Browse files Browse the repository at this point in the history
* fixed getLatestKernels

* removed log
  • Loading branch information
Kelvinrr authored Jan 7, 2025
1 parent d3de2a8 commit 5fcdab7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion SpiceQL/src/inventoryimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ namespace SpiceQL {
m_required_kernels.load(lsk_json);

for (auto &[mission, kernels] : json_kernels.items()) {
fmt::print("mission: {}\n", mission);
SPDLOG_TRACE("MISSION: {}", mission);

json sclk_json = getLatestKernels(config[mission].getRecursive("sclk"));
SPDLOG_TRACE("{} SCLKs: {}", mission, sclk_json.dump(4));
Expand Down
22 changes: 20 additions & 2 deletions SpiceQL/src/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ namespace SpiceQL {
}
}

// Comparator function for kernel paths
bool fileNameComp(string a, string b) {
string fna = static_cast<fs::path>(a).filename();
string fnb = static_cast<fs::path>(b).filename();
int comp = fna.compare(fnb);
SPDLOG_TRACE("Comparing {} and {}: {}", fna, fnb, comp);
return comp < 0;
}

vector<string> getLatestKernel(vector<string> kernels) {
if(kernels.empty()) {
Expand All @@ -88,17 +96,25 @@ namespace SpiceQL {
}
bool foundList = false;
for (int i = 0; i < files.size(); i++) {

const fs::path &firstVecElem = files[i][0];
string fileName = firstVecElem.filename();
string kernelName = k.filename();
SPDLOG_TRACE("filename: {}", fileName);
SPDLOG_TRACE("kernel name: {}", kernelName);

int findRes = fileName.find_first_of("0123456789");
if (findRes != string::npos) {
fileName = fileName.erase(findRes);
}

findRes = kernelName.find_first_of("0123456789");
if (findRes != string::npos) {
kernelName = kernelName.erase(findRes);
}
SPDLOG_TRACE("Truncated filename: {}", fileName);
SPDLOG_TRACE("Truncated kernel name: {}", kernelName);

if (fileName == kernelName) {
files[i].push_back(k);
foundList = true;
Expand All @@ -112,19 +128,21 @@ namespace SpiceQL {

vector<string> outKernels = {};
for (auto kernelList : files) {
outKernels.push_back(*(max_element(kernelList.begin(), kernelList.end())));
outKernels.push_back(*(max_element(kernelList.begin(), kernelList.end(), fileNameComp)));
}

return outKernels;
}


json getLatestKernels(json kernels) {

SPDLOG_TRACE("Looking for kernels to get Latest: {}", kernels.dump(2));
vector<json::json_pointer> kptrs = findKeyInJson(kernels, "kernels", true);
vector<vector<string>> lastest;

for (json::json_pointer &ptr : kptrs) {
SPDLOG_TRACE("Getting Latest Kernels from: {}", ptr.to_string());
SPDLOG_TRACE("JSON: {}", kernels[ptr]);
vector<vector<string>> kvect = json2DArrayTo2DVector(kernels[ptr]);
vector<vector<string>> newLatest;

Expand Down
21 changes: 21 additions & 0 deletions SpiceQL/tests/QueryTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ TEST(QueryTests, UnitTestGetLatestKernel) {
EXPECT_EQ(getLatestKernel(kernels)[0], "test/iak.0004.ti");
}


TEST(QueryTests, UnitTestGetLatestKernelDuplicateFileNames) {
vector<string> kernels = {
"/base/kernels/spk/mar080.bsp",
"/base/kernels/spk/mar097.bsp",
"/mro/kernels/spk/mar063.bsp",
"/odyssey/kernels/spk/mar063.bsp",
"/tgo/kernels/spk/mar085.bsp",
"/tgo/kernels/spk/mar097.bsp",
"/tgo/kernels/tspk/mar085.bsp",
"/tgo/kernels/tspk/mar097.bsp",
"/viking1/kernels/spk/mar033.bsp",
"/viking2/kernels/spk/mar033.bsp",
"/base/kernels/spk/mar097.bsp"
};

// which one doesn't matter, as long as we get mar097.bsp
EXPECT_EQ(static_cast<fs::path>(getLatestKernel(kernels)[0]).filename(), "mar097.bsp");
}


TEST(QueryTests, getKernelStringValue){
unique_ptr<Kernel> k(new Kernel("data/msgr_mdis_v010.ti"));
// INS-236810_CCD_CENTER = ( 511.5, 511.5 )
Expand Down

0 comments on commit 5fcdab7

Please sign in to comment.