Skip to content

Commit

Permalink
added better progress and resuming from given last path
Browse files Browse the repository at this point in the history
  • Loading branch information
Cornul11 committed Feb 26, 2024
1 parent 560709c commit a7cbe1a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void run() {
String evaluationDirectory = options.getEvaluationDirectory();
if (evaluationDirectory != null) {
JarEvaluator jarEvaluator = new JarEvaluator(signatureDao, evaluationDirectory);
Map<String, List<JarEvaluator.InferredLibrary>> inferredLibrariesMap = jarEvaluator.inferLibrariesFromJars();
Map<String, List<JarEvaluator.InferredLibrary>> inferredLibrariesMap = jarEvaluator.inferLibrariesFromJars(options.getLastPath());
jarEvaluator.evaluate(inferredLibrariesMap);
} else {
System.out.println("Evaluation directory is required for EVALUATION_MODE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public void evaluate(Map<String, List<JarEvaluator.InferredLibrary>> inferredLib
}
}

public Map<String, List<JarEvaluator.InferredLibrary>> inferLibrariesFromJars() {
public Map<String, List<JarEvaluator.InferredLibrary>> inferLibrariesFromJars(String resumePath) {
boolean resumeProcessing = (resumePath == null || resumePath.isEmpty());
Map<String, List<JarEvaluator.InferredLibrary>> inferredLibrariesMap = new HashMap<>();


try {
Map<String, List<InferredLibrary>> loadedLibraries = loadInferredLibraries();
if (!loadedLibraries.isEmpty()) {
Expand All @@ -51,16 +55,26 @@ public Map<String, List<JarEvaluator.InferredLibrary>> inferLibrariesFromJars()
logger.warn("Failed to load inferred libraries from file. Re-inferring them", e);
}

Map<String, List<JarEvaluator.InferredLibrary>> inferredLibrariesMap = new HashMap<>();

File[] projectFolders = getProjectFolders();
if (projectFolders == null) return inferredLibrariesMap;

int total = projectFolders.length;
logger.info("Inferring libraries from {} projects", total);

int current = 0;
for (File projectFolder : projectFolders) {
String projectName = projectFolder.getName();
String jarPath = Paths.get(projectFolder.getAbsolutePath(), "target", projectName + "-1.0-SNAPSHOT.jar").toString();
Path jarFilePath = Path.of(jarPath);

if (!resumeProcessing) {
if (jarPath.equals(resumePath)) {
resumeProcessing = true;
} else {
continue;
}
}

Path jarFilePath = Path.of(jarPath);
if (!jarFilePath.toFile().exists()) {
logger.info("Skipping " + jarPath + " because it does not exist");
continue;
Expand All @@ -76,15 +90,21 @@ public Map<String, List<JarEvaluator.InferredLibrary>> inferLibrariesFromJars()
.map(libraryCandidate -> new JarEvaluator.InferredLibrary(libraryCandidate, jarPath))
.collect(Collectors.toList());


inferredLibrariesMap.put(jarPath, candidateLibraries);
}

try {
storeInferredLibraries(inferredLibrariesMap);
} catch (IOException e) {
logger.error("Failed to store inferred libraries to file", e);
try {
storeInferredLibraries(candidateLibraries);
} catch (IOException e) {
logger.error("Failed to store inferred libraries to file", e);
}

current++;
double percentageCompleted = ((double) current / total) * 100;
logger.info("Processed {} out of {} ({}% completed)",
current, total,
String.format("%.2f", percentageCompleted));
}

return inferredLibrariesMap;
}

Expand Down Expand Up @@ -146,14 +166,14 @@ private List<JarEvaluator.InferredLibrary> filterLibrariesByThreshold(List<JarEv
return filteredList;
}

private void storeInferredLibraries(Map<String, List<JarEvaluator.InferredLibrary>> inferredLibrariesMap) throws IOException {
private void storeInferredLibraries(List<JarEvaluator.InferredLibrary> inferredLibraries) throws IOException {
Path filePath = Paths.get(evaluationDirectory, "evaluation", "inferredLibraries.json");

if (!filePath.getParent().toFile().exists()) {
filePath.getParent().toFile().mkdirs();
}

objectMapper.writerWithDefaultPrettyPrinter().writeValue(filePath.toFile(), inferredLibrariesMap);
objectMapper.writerWithDefaultPrettyPrinter().writeValue(filePath.toFile(), inferredLibraries);
logger.info("Stored inferred libraries to {}", filePath);
}

Expand Down

0 comments on commit a7cbe1a

Please sign in to comment.