Skip to content

Commit

Permalink
Added feature to save all IRs and Deltas
Browse files Browse the repository at this point in the history
  • Loading branch information
samnperry committed Sep 19, 2024
1 parent b177df3 commit f9f9d97
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ public void processDelta(List<DiffEntry> diffEntries) {
systemChange.getChanges().add(new Delta(oldPath, newPath, changeType, data));
}

String filePath = "./output/Delta_" + commitOld.substring(0, 4) + "_" + commitNew.substring(0, 4) + ".json";

// Output the system changes
JsonReadWriteUtils.writeToJSON("./output/Delta.json", systemChange);
JsonReadWriteUtils.writeToJSON(filePath, systemChange);

// Report
LoggerManager.info(() -> "Delta changes extracted between " + commitOld + " -> " + commitNew);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public class DetectionService {
private static final int METRICS = 24;
private static final int ARCHRULES = 4;

private static final String OLD_IR_PATH = "./output/OldIR.json";
private static final String DELTA_PATH = "./output/Delta.json";
private static final String NEW_IR_PATH = "./output/NewIR.json";
// private static final String OLD_IR_PATH = "./output/OldIR_";
private static final String DELTA_PATH = "./output/Delta_";
// private static final String NEW_IR_PATH = "./output/NewIR_";

private final String configPath;
private final Config config;
Expand Down Expand Up @@ -80,7 +80,8 @@ public void runDetection() {

// Generate the initial IR
irExtractionService = new IRExtractionService(configPath, Optional.of(commits.get(0).toString().split(" ")[1]));
irExtractionService.generateIR("OldIR.json");
String firstCommit = commits.get(0).getName().substring(0, 4);
irExtractionService.generateIR("IR_" + firstCommit + ".json");

// Setup sheet and headers
sheet = workbook.createSheet(config.getSystemName());
Expand Down Expand Up @@ -112,22 +113,25 @@ public void runDetection() {
commitIdCell.setCellValue(commitIdOld.substring(0, 7));

// Read in the old system
MicroserviceSystem oldSystem = JsonReadWriteUtils.readFromJSON(OLD_IR_PATH, MicroserviceSystem.class);
String oldSysPath = "./output/IR_" + commitIdOld.substring(0, 4) +".json";
MicroserviceSystem oldSystem = JsonReadWriteUtils.readFromJSON(oldSysPath, MicroserviceSystem.class);

// Extract changes from one commit to the other
if(i < commits.size() - 1) {
String commitIdNew = commits.get(i + 1).toString().split(" ")[1];
String newSysPath = "./output/IR_" + commitIdNew.substring(0, 4) +".json";
String deltaPath = DELTA_PATH + commitIdOld.substring(0, 4) + "_" + commitIdNew.substring(0, 4) + ".json";

deltaExtractionService = new DeltaExtractionService(configPath, OLD_IR_PATH, commitIdOld, commitIdNew);
deltaExtractionService = new DeltaExtractionService(configPath, oldSysPath, commitIdOld, commitIdNew);
deltaExtractionService.generateDelta();

// Merge Delta changes to old IR to create new IR representing new commit changes
MergeService mergeService = new MergeService(OLD_IR_PATH, DELTA_PATH, configPath);
mergeService.generateMergeIR();
MergeService mergeService = new MergeService(oldSysPath, deltaPath, configPath);
mergeService.generateMergeIR(commitIdNew.substring(0, 4));

// Read in the new system and system change
newSystem = JsonReadWriteUtils.readFromJSON(NEW_IR_PATH, MicroserviceSystem.class);
systemChange = JsonReadWriteUtils.readFromJSON(DELTA_PATH, SystemChange.class);
newSystem = JsonReadWriteUtils.readFromJSON(newSysPath, MicroserviceSystem.class);
systemChange = JsonReadWriteUtils.readFromJSON(deltaPath, SystemChange.class);
}

// Init all the lists/maps
Expand All @@ -138,7 +142,7 @@ public void runDetection() {
// We can detect/update if there are >= 1 microservices
if (Objects.nonNull(oldSystem.getMicroservices()) && !oldSystem.getMicroservices().isEmpty()) {
detectAntipatterns(oldSystem, antipatterns);
detectMetrics(oldSystem, metrics);
detectMetrics(oldSystem, metrics, commitIdOld.substring(0, 4));

updateAntiPatterns(currIndex, antipatterns);
updateMetrics(currIndex, metrics);
Expand All @@ -155,12 +159,12 @@ public void runDetection() {
}

// After completing this iteration, we can replace oldIR with newIR
try {
Files.move(Paths.get(NEW_IR_PATH), Paths.get(OLD_IR_PATH), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
// try {
// Files.move(Paths.get(NEW_IR_PATH), Paths.get(OLD_IR_PATH), StandardCopyOption.REPLACE_EXISTING);
// } catch (IOException e) {
// e.printStackTrace();
// System.exit(1);
// }
}

// At the end we write the workbook to file
Expand Down Expand Up @@ -223,7 +227,7 @@ private void detectAntipatterns(MicroserviceSystem microserviceSystem, Map<Strin

}

private void detectMetrics(MicroserviceSystem microserviceSystem, Map<String, Double> metrics) {
private void detectMetrics(MicroserviceSystem microserviceSystem, Map<String, Double> metrics, String commitID) {

ServiceDependencyGraph sdg = new ServiceDependencyGraph(microserviceSystem);
DegreeCoupling dc = new DegreeCoupling(sdg);
Expand All @@ -245,7 +249,7 @@ private void detectMetrics(MicroserviceSystem microserviceSystem, Map<String, Do
ConnectedComponentsModularity mod = new ConnectedComponentsModularity(sdg);
metrics.put("SCCmodularity", mod.getModularity());

MetricResultCalculation cohesionMetrics = RunCohesionMetrics.calculateCohesionMetrics(OLD_IR_PATH);
MetricResultCalculation cohesionMetrics = RunCohesionMetrics.calculateCohesionMetrics("./output/IR_" + commitID + ".json");
metrics.put("maxSIDC", cohesionMetrics.getMax("ServiceInterfaceDataCohesion"));
metrics.put("avgSIDC", cohesionMetrics.getAverage("ServiceInterfaceDataCohesion"));
metrics.put("stdSIDC", cohesionMetrics.getStdDev("ServiceInterfaceDataCohesion"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class ExcelOutputRunner {


public static void main(String[] args) throws IOException {
String configPath = args[0];
String configPath = "./valid_configs/java-microservice.json";
DetectionService detectionService = new DetectionService(configPath);
detectionService.runDetection();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public static void main(String[] args) throws IOException {

MergeService mergeService = new MergeService(args[0], args[1], args[2]);

mergeService.generateMergeIR();
//mergeService.generateMergeIR();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ public MergeService(
/**
* This method generates the new IR from the old IR + Delta file
*/
public void generateMergeIR() {
public void generateMergeIR(String newCommitID) {

// If no changes are present we will write back out same IR
if (Objects.isNull(systemChange.getChanges())) {
LoggerManager.debug(() -> "No changes found at " + systemChange.getOldCommit() + " -> " + systemChange.getNewCommit());
JsonReadWriteUtils.writeToJSON("./output/NewIR.json", microserviceSystem);
String filePath = "./output/IR_" + newCommitID + ".json";
JsonReadWriteUtils.writeToJSON(filePath, microserviceSystem);
return;
}

Expand All @@ -69,7 +70,8 @@ public void generateMergeIR() {
microserviceSystem.setCommitID(systemChange.getNewCommit());

LoggerManager.info(() -> "Merged to new IR at " + systemChange.getNewCommit());
JsonReadWriteUtils.writeToJSON("./output/NewIR.json", microserviceSystem);
String filePath = "./output/IR_" + newCommitID + ".json";
JsonReadWriteUtils.writeToJSON(filePath, microserviceSystem);
}


Expand Down
2 changes: 1 addition & 1 deletion src/test/java/integration/IRComparisonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void testComparison() {

// Merge Delta changes to old IR to create new IR representing new commit changes
MergeService mergeService = new MergeService(OLD_IR_PATH, DELTA_PATH, TEST_CONFIG_PATH);
mergeService.generateMergeIR();
mergeService.generateMergeIR(commitIdNew);

if(i < list.size() - 2) {
try {
Expand Down

0 comments on commit f9f9d97

Please sign in to comment.