First-letter directory structure for RCGs
@@ -30,7 +30,10 @@ import java.io.FileWriter;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
+import java.util.Optional;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
@@ -159,7 +162,7 @@ public class FastenKafkaPlugin implements FastenServerPlugin {
String payload = null;
if (result.isPresent()) {
if (plugin instanceof CallGraphGeneratorPlugin) {
- payload = writeToFile(result.get());
+ payload = writeToFile(input, result.get());
} else {
payload = result.get();
}
@@ -200,21 +203,27 @@ public class FastenKafkaPlugin implements FastenServerPlugin {
* Writes {@link RevisionCallGraph} to JSON file and return JSON object containing
* a link to to written file.
*
+ * @param input message that triggered computation of the call graph
* @param callgraph String of JSON representation of {@link RevisionCallGraph}
* @return Path to a newly written JSON file
*/
- private String writeToFile(String callgraph) {
+ private String writeToFile(String input, String callgraph) {
RevisionCallGraph graph = new RevisionCallGraph(new JSONObject(callgraph));
+ var coordinate = findCoordinate(new JSONObject(input));
+ if (coordinate.isPresent()) {
+ var groupId = coordinate.get().get("groupId");
+ var artifactId = coordinate.get().get("artifactId");
+ var firstLetter = artifactId.substring(0, 1);
try {
File directory = new File(this.writeDirectory
- + "/" + graph.product + "/" + graph.version);
+ + "/" + graph.forge + "/" + firstLetter + "/" + artifactId);
if (!directory.exists()) {
directory.mkdirs();
}
File file = new File(directory.getAbsolutePath()
- + "/" + plugin.getClass().getSimpleName() + "_callgraph.json");
+ + "/" + artifactId + "(" + groupId + ")-v" + graph.version + ".json");
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(graph.toJSON().toString());
@@ -225,11 +234,37 @@ public class FastenKafkaPlugin implements FastenServerPlugin {
return link.toString();
} catch (IOException e) {
- e.printStackTrace();
+ logger.error("Failed to write call graph for {} to a file", graph.product);
+ }
}
return callgraph;
}
+ /**
+ * Recursively finds Maven coordinate from the input JSON.
+ *
+ * @param input input of the latest stdout message
+ * @return map containing groupId and artifactId
+ */
+ private Optional<Map<String, String>> findCoordinate(JSONObject input) {
+ if (input.get("groupId") != null && input.get("artifactId") != null) {
+ Map<String, String> map = new HashMap<>();
+
+ map.put("groupId", input.get("groupId").toString());
+ map.put("artifactId", input.get("artifactId").toString());
+
+ return Optional.of(map);
+ }
+
+ var previousInput = input.getJSONObject("input");
+
+ if (previousInput != null) {
+ return findCoordinate(previousInput);
+ } else {
+ return Optional.empty();
+ }
+ }
+
/**
* Create a message that will be send to STDOUT of a given plugin.
*
