|
|
@@ -1,15 +1,15 @@
|
|
|
package de.jplag.reporting.reportobject;
|
|
|
|
|
|
+import static de.jplag.reporting.jsonfactory.DirectoryCreator.createDirectory;
|
|
|
+import static de.jplag.reporting.reportobject.mapper.SubmissionNameToIdMapper.buildSubmissionNameToIdMap;
|
|
|
+
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.StandardCopyOption;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashSet;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.*;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
@@ -19,85 +19,66 @@ import de.jplag.JPlagComparison;
|
|
|
import de.jplag.JPlagResult;
|
|
|
import de.jplag.Language;
|
|
|
import de.jplag.Submission;
|
|
|
+import de.jplag.reporting.jsonfactory.ComparisonReportWriter;
|
|
|
import de.jplag.reporting.jsonfactory.FileWriter;
|
|
|
import de.jplag.reporting.reportobject.mapper.ClusteringResultMapper;
|
|
|
-import de.jplag.reporting.reportobject.mapper.ComparisonReportMapper;
|
|
|
import de.jplag.reporting.reportobject.mapper.MetricMapper;
|
|
|
import de.jplag.reporting.reportobject.model.Metric;
|
|
|
import de.jplag.reporting.reportobject.model.OverviewReport;
|
|
|
|
|
|
/**
|
|
|
- * Factory class, responsible for converting a JPlagResult object to Overview and Comparison DTO classes.
|
|
|
+ * Factory class, responsible for converting a JPlagResult object to Overview and Comparison DTO classes and writing it
|
|
|
+ * to the disk.
|
|
|
*/
|
|
|
public class ReportObjectFactory {
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ReportObjectFactory.class);
|
|
|
|
|
|
- private static final ClusteringResultMapper clusteringResultMapper = new ClusteringResultMapper();
|
|
|
- private static final MetricMapper metricMapper = new MetricMapper();
|
|
|
- private static final ComparisonReportMapper comparisonReportMapper = new ComparisonReportMapper();
|
|
|
private static final FileWriter fileWriter = new FileWriter();
|
|
|
public static final String OVERVIEW_FILE_NAME = "overview.json";
|
|
|
public static final String SUBMISSIONS_FOLDER = "submissions";
|
|
|
+ private Map<String, String> submissionNameToIdMap;
|
|
|
+ private Function<Submission, String> submissionToIdFunction;
|
|
|
+ private Map<String, Map<String, String>> submissionNameToNameToComparisonFileName;
|
|
|
|
|
|
/**
|
|
|
+ * Creates all necessary report viewer files and writes them to the disk.
|
|
|
* @param result The JPlagResult to be converted into a report.
|
|
|
* @param path The Path to save the report to
|
|
|
*/
|
|
|
- public static void createAndSaveReport(JPlagResult result, String path) {
|
|
|
- createDirectory(path);
|
|
|
- writeOverview(result, path);
|
|
|
- copySubmissionFilesToReport(path, result);
|
|
|
- comparisonReportMapper.writeComparisonReports(result, path);
|
|
|
- }
|
|
|
-
|
|
|
- private static File createDirectory(String path, String name) {
|
|
|
- File directory = new File(path.concat("/").concat(name));
|
|
|
- if (!directory.exists() && !directory.mkdirs()) {
|
|
|
- logger.error("Failed to create dir.");
|
|
|
- }
|
|
|
- return directory;
|
|
|
- }
|
|
|
+ public void createAndSaveReport(JPlagResult result, String path) {
|
|
|
|
|
|
- private static void createDirectory(String path) {
|
|
|
- createDirectory(path, "");
|
|
|
- }
|
|
|
+ buildSubmissionToIdMap(result);
|
|
|
+ try {
|
|
|
+ createDirectory(path);
|
|
|
+ copySubmissionFilesToReport(path, result);
|
|
|
|
|
|
- private static void writeOverview(JPlagResult result, String path) {
|
|
|
- List<JPlagComparison> comparisons = getComparisons(result);
|
|
|
- OverviewReport overviewReport = new OverviewReport();
|
|
|
+ writeComparisons(result, path);
|
|
|
+ writeOverview(result, path);
|
|
|
|
|
|
- // TODO: Consider to treat entries that were checked differently from old entries with prior work.
|
|
|
- List<String> folders = new ArrayList<>();
|
|
|
- folders.addAll(result.getOptions().getSubmissionDirectories());
|
|
|
- folders.addAll(result.getOptions().getOldSubmissionDirectories());
|
|
|
- overviewReport.setSubmissionFolderPath(folders);
|
|
|
-
|
|
|
- String baseCodePath = result.getOptions().hasBaseCode() ? result.getOptions().getBaseCodeSubmissionName().orElse("") : "";
|
|
|
- overviewReport.setBaseCodeFolderPath(baseCodePath);
|
|
|
-
|
|
|
- overviewReport.setLanguage(result.getOptions().getLanguage().getName());
|
|
|
- overviewReport.setFileExtensions(List.of(result.getOptions().getFileSuffixes()));
|
|
|
- overviewReport.setSubmissionIds(extractSubmissionNames(comparisons));
|
|
|
- overviewReport.setFailedSubmissionNames(List.of()); // No number of failed submissions
|
|
|
- overviewReport.setExcludedFiles(result.getOptions().getExcludedFiles());
|
|
|
- overviewReport.setMatchSensitivity(result.getOptions().getMinimumTokenMatch());
|
|
|
- overviewReport.setDateOfExecution(getDate());
|
|
|
- overviewReport.setExecutionTime(result.getDuration());
|
|
|
- overviewReport.setComparisonNames(getComparisonNames(comparisons));
|
|
|
- overviewReport.setMetrics(getMetrics(result));
|
|
|
- overviewReport.setClusters(clusteringResultMapper.map(result));
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("Could not create directory " + path + " for report viewer generation", e);
|
|
|
+ }
|
|
|
|
|
|
- fileWriter.saveAsJSON(overviewReport, path, OVERVIEW_FILE_NAME);
|
|
|
+ }
|
|
|
|
|
|
+ private void buildSubmissionToIdMap(JPlagResult result) {
|
|
|
+ submissionNameToIdMap = buildSubmissionNameToIdMap(result);
|
|
|
+ submissionToIdFunction = (Submission submission) -> submissionNameToIdMap.get(submission.getName());
|
|
|
}
|
|
|
|
|
|
- private static void copySubmissionFilesToReport(String path, JPlagResult result) {
|
|
|
+ private void copySubmissionFilesToReport(String path, JPlagResult result) {
|
|
|
List<JPlagComparison> comparisons = result.getComparisons(result.getOptions().getMaximumNumberOfComparisons());
|
|
|
var submissions = getSubmissions(comparisons);
|
|
|
- var submissionsPath = createDirectory(path, SUBMISSIONS_FOLDER);
|
|
|
+ var submissionsPath = createSubmissionsDirectory(path);
|
|
|
+ if (submissionsPath == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
Language language = result.getOptions().getLanguage();
|
|
|
for (var submission : submissions) {
|
|
|
- File directory = createDirectory(submissionsPath.getPath(), submission.getName());
|
|
|
+ File directory = createSubmissionDirectory(path, submissionsPath, submission);
|
|
|
+ if (directory == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
for (var file : submission.getFiles()) {
|
|
|
var fileToCopy = language.useViewFiles() ? new File(file.getPath() + language.viewFileSuffix()) : file;
|
|
|
try {
|
|
|
@@ -109,40 +90,61 @@ public class ReportObjectFactory {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static Set<Submission> getSubmissions(List<JPlagComparison> comparisons) {
|
|
|
- var submissions = comparisons.stream().map(JPlagComparison::getFirstSubmission).collect(Collectors.toSet());
|
|
|
- Set<Submission> secondSubmissions = comparisons.stream().map(JPlagComparison::getSecondSubmission).collect(Collectors.toSet());
|
|
|
- submissions.addAll(secondSubmissions);
|
|
|
- return submissions;
|
|
|
+ private File createSubmissionDirectory(String path, File submissionsPath, Submission submission) {
|
|
|
+ try {
|
|
|
+ return createDirectory(submissionsPath.getPath(), submissionToIdFunction.apply(submission));
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("Could not create directory " + path + " for report viewer generation", e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private static List<JPlagComparison> getComparisons(JPlagResult result) {
|
|
|
- int numberOfComparisons = result.getOptions().getMaximumNumberOfComparisons();
|
|
|
- return result.getComparisons(numberOfComparisons);
|
|
|
+ private File createSubmissionsDirectory(String path) {
|
|
|
+ try {
|
|
|
+ return createDirectory(path, SUBMISSIONS_FOLDER);
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("Could not create directory " + path + " for report viewer generation", e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Gets the names of all submissions.
|
|
|
- * @return A list containing all submission names.
|
|
|
- */
|
|
|
- private static List<String> extractSubmissionNames(List<JPlagComparison> comparisons) {
|
|
|
- HashSet<String> names = new HashSet<>();
|
|
|
- comparisons.forEach(comparison -> {
|
|
|
- names.add(comparison.getFirstSubmission().getName());
|
|
|
- names.add(comparison.getSecondSubmission().getName());
|
|
|
- });
|
|
|
- return new ArrayList<>(names);
|
|
|
+ private void writeComparisons(JPlagResult result, String path) {
|
|
|
+ ComparisonReportWriter comparisonReportWriter = new ComparisonReportWriter(submissionToIdFunction);
|
|
|
+ submissionNameToNameToComparisonFileName = comparisonReportWriter.writeComparisonReports(result, path);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Gets the names of all comparison.
|
|
|
- * @return A list containing all comparisons.
|
|
|
- */
|
|
|
- private static List<String> getComparisonNames(List<JPlagComparison> comparisons) {
|
|
|
- List<String> names = new ArrayList<>();
|
|
|
- comparisons.forEach(
|
|
|
- comparison -> names.add(String.join("-", comparison.getFirstSubmission().getName(), comparison.getSecondSubmission().getName())));
|
|
|
- return names;
|
|
|
+ private void writeOverview(JPlagResult result, String path) {
|
|
|
+
|
|
|
+ List<String> folders = new ArrayList<>();
|
|
|
+ folders.addAll(result.getOptions().getSubmissionDirectories());
|
|
|
+ folders.addAll(result.getOptions().getOldSubmissionDirectories());
|
|
|
+
|
|
|
+ String baseCodePath = result.getOptions().hasBaseCode() ? result.getOptions().getBaseCodeSubmissionName().orElse("") : "";
|
|
|
+ ClusteringResultMapper clusteringResultMapper = new ClusteringResultMapper(submissionToIdFunction);
|
|
|
+
|
|
|
+ OverviewReport overviewReport = new OverviewReport(folders, // submissionFolderPath
|
|
|
+ baseCodePath, // baseCodeFolderPath
|
|
|
+ result.getOptions().getLanguage().getName(), // language
|
|
|
+ List.of(result.getOptions().getFileSuffixes()), // fileExtensions
|
|
|
+ submissionNameToIdMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)), // submissionIds
|
|
|
+ submissionNameToNameToComparisonFileName, // result.getOptions().getMinimumTokenMatch(),
|
|
|
+ List.of(), // failedSubmissionNames
|
|
|
+ result.getOptions().getExcludedFiles(), // excludedFiles
|
|
|
+ result.getOptions().getMinimumTokenMatch(), // matchSensitivity
|
|
|
+ getDate(),// dateOfExecution
|
|
|
+ result.getDuration(), // executionTime
|
|
|
+ getMetrics(result),// metrics
|
|
|
+ clusteringResultMapper.map(result)); // clusters
|
|
|
+
|
|
|
+ fileWriter.saveAsJSON(overviewReport, path, OVERVIEW_FILE_NAME);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private Set<Submission> getSubmissions(List<JPlagComparison> comparisons) {
|
|
|
+ var submissions = comparisons.stream().map(JPlagComparison::getFirstSubmission).collect(Collectors.toSet());
|
|
|
+ Set<Submission> secondSubmissions = comparisons.stream().map(JPlagComparison::getSecondSubmission).collect(Collectors.toSet());
|
|
|
+ submissions.addAll(secondSubmissions);
|
|
|
+ return submissions;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -150,11 +152,12 @@ public class ReportObjectFactory {
|
|
|
* Metric.
|
|
|
* @return A list contains Metric DTOs.
|
|
|
*/
|
|
|
- private static List<Metric> getMetrics(JPlagResult result) {
|
|
|
+ private List<Metric> getMetrics(JPlagResult result) {
|
|
|
+ MetricMapper metricMapper = new MetricMapper(submissionToIdFunction);
|
|
|
return List.of(metricMapper.getAverageMetric(result), metricMapper.getMaxMetric(result));
|
|
|
}
|
|
|
|
|
|
- private static String getDate() {
|
|
|
+ private String getDate() {
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
|
|
|
Date date = new Date();
|
|
|
return dateFormat.format(date);
|