#78 - Add FileTS

This commit is contained in:
alexandra 2022-11-21 13:42:15 +04:00
parent 3b7812a075
commit 4de0e37902
2 changed files with 136 additions and 104 deletions

View File

@ -30,6 +30,7 @@ import ru.ulstu.extractor.heuristic.model.ResourceUnit;
import ru.ulstu.extractor.heuristic.service.StructuralUnitService;
import ru.ulstu.extractor.model.Commit;
import javax.swing.text.html.parser.Entity;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
@ -39,10 +40,7 @@ import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -280,80 +278,22 @@ public class GitRepositoryService {
private List<FileChange> parseOutputDiff(String output, org.eclipse.jgit.lib.Repository repository, RevCommit commit) {
List<FileChange> changes = new ArrayList<>();
String[] strings = output.split("\n");
FileChange fileChange = new FileChange();
boolean isAdded = false;
boolean isRemoved = false;
boolean isFileChanged = false;
int stringsLength = strings.length - 1;
for (int i = 0; i < strings.length; i++) {
while (i < stringsLength) {
Optional<String> maybeFileName = getFileName(strings[i]);
if (maybeFileName.isPresent()) {
fileChange = new FileChange();
fileChange.setFile(maybeFileName.get());
Future<Boolean> futureEntity = executorService.submit(() -> structuralUnitService.containsEntity(getContent(repository, commit, maybeFileName.get())));
Future<Boolean> futureBL = executorService.submit(() -> structuralUnitService.containsBusinessLogic(getContent(repository, commit, maybeFileName.get())));
fileChange.setAdded(isAdded);
fileChange.setRemoved(isRemoved);
try {
fileChange.setContainsBusinessLogic(futureBL.get());
fileChange.setContainsEntity(futureEntity.get());
} catch (Exception ex) {
LOG.warn(ex.getMessage());
}
/// вытащить другие изменения из коммита
changes.add(fileChange);
}
LineChange lineChange = new LineChange();
if (strings[i].startsWith("-")) {
while ((i < stringsLength) && strings[i].startsWith("-")) {
if (strings[i].startsWith("---")) {
i++;
} else {
if (lineChange.getLineFrom() == null) {
lineChange.setLineFrom(strings[i]);
i++;
} else {
lineChange.setLineFrom(lineChange.getLineFrom() + "\n" + strings[i]);
i++;
}
isRemoved = true;
}
}
lineChange.setRemoved(true);
}
if (strings[i].startsWith("+")) {
while ((i < stringsLength) && strings[i].startsWith("+")) {
if (strings[i].startsWith("+++")) {
i++;
} else {
if (lineChange.getLineTo() == null) {
lineChange.setLineTo(strings[i]);
i++;
} else {
lineChange.setLineTo(lineChange.getLineTo() + "\n" + strings[i]);
i++;
}
isAdded = true;
}
}
lineChange.setAdded(true);
}
if ((lineChange.getLineTo() != null) || (lineChange.getLineFrom() != null)
|| ((lineChange.getLineTo() != null) && (lineChange.getLineFrom() != null))) {
fileChange.getLineChanges().add(lineChange);
} else {
i++;
}
Map<String, List<String>> filesContent = getFilesContent(strings);
System.out.println(filesContent);
for(Map.Entry<String, List<String>> fileSterings: filesContent.entrySet()) {
FileChange fileChange = new FileChange();
fileChange.setFile(fileSterings.getKey());
Future<Boolean> futureEntity = executorService.submit(() -> structuralUnitService.containsEntity(getContent(repository, commit, fileSterings.getKey())));
Future<Boolean> futureBL = executorService.submit(() -> structuralUnitService.containsBusinessLogic(getContent(repository, commit, fileSterings.getKey())));
try {
fileChange.setContainsBusinessLogic(futureBL.get());
fileChange.setContainsEntity(futureEntity.get());
} catch (Exception ex) {
LOG.warn(ex.getMessage());
}
fileChange = getChange(fileChange, fileSterings.getValue());
changes.add(fileChange);
}
if ((isAdded) && (isRemoved)) {
isAdded = false;
isRemoved = false;
isFileChanged = true;
}
fileChange.setAdded(isAdded);
fileChange.setRemoved(isRemoved);
return changes;
}
@ -382,6 +322,101 @@ public class GitRepositoryService {
return Optional.empty();
}
private Map<String, List<String>> getFilesContent(String[] commitStrings) {
int i = 0;
Map<String, List<String>> result = new HashMap<>();
while (i < commitStrings.length) {
Optional<String> maybeFileName = getFileName(commitStrings[i]);
if (maybeFileName.isEmpty()) {
i++;
} else {
i = skipLinesForNextFile(commitStrings, i);
result.put(maybeFileName.get(), getFileContent(commitStrings, i));
i += getFileContent(commitStrings, i).size() - 1;
}
}
return result;
}
private List<String> getFileContent(String[] commitsString, int i) {
List<String> result = new ArrayList<>();
while (i < commitsString.length && getFileName(commitsString[i]).isEmpty()) {
result.add(commitsString[i]);
i++;
}
return result;
}
private int skipLinesForNextFile(String[] commitStrings, int index) {
boolean isFilePrefixSkipped = false;
while (!isFilePrefixSkipped && (index < commitStrings.length)) {
if ((commitStrings[index].startsWith("diff --git a/"))
|| (commitStrings[index].startsWith("delete"))
|| (commitStrings[index].startsWith("new"))
|| (commitStrings[index].startsWith("index"))
|| (commitStrings[index].startsWith("@@"))
|| (commitStrings[index].startsWith("---"))
|| (commitStrings[index].startsWith("+++"))) {
index++;
} else {
isFilePrefixSkipped = true;
}
}
return index;
}
private FileChange getChange(FileChange fileChange, List<String> fileContent){
int addedLine = 0;
int removedLine = 0;
StringBuilder builder = new StringBuilder();
boolean isRemoved = false;
boolean isAdded = false;
for (String line : fileContent){
LineChange lineChange = new LineChange();
if (line.startsWith("-")){
isRemoved = true;
if (isAdded) {
isAdded = false;
lineChange = setAdded(lineChange, builder);
builder.setLength(0);
}
builder.append(line).append("\n");
removedLine++;
} else if(line.startsWith("+")){
isAdded = true;
if (isRemoved) {
isRemoved = false;
lineChange = setRemoved(lineChange, builder);
builder.setLength(0);
}
builder.append(line).append("\n");
addedLine++;
} else {
if (isRemoved) {
lineChange = setRemoved(lineChange, builder);
builder.setLength(0);
} else if (isAdded){
lineChange = setAdded(lineChange, builder);
builder.setLength(0);
}
}
}
fileChange.setRemoved(removedLine == fileContent.size() - 1);
fileChange.setAdded(addedLine == fileContent.size() - 1);
return fileChange;
}
private LineChange setRemoved(LineChange lineChange, StringBuilder builder){
lineChange.setLineFrom(builder.toString());
lineChange.setRemoved(true);
return lineChange;
}
private LineChange setAdded(LineChange lineChange, StringBuilder builder){
lineChange.setLineTo(builder.toString());
lineChange.setAdded(true);
return lineChange;
}
public Page<GitRepository> findAll(Pageable pageable) {
return gitRepositoryRepository.findAll(pageable);
}

View File

@ -7,6 +7,7 @@ import ru.ulstu.extractor.gitrepository.service.GitRepositoryService;
import ru.ulstu.extractor.model.Commit;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService;
import java.util.ArrayList;
@ -26,18 +27,30 @@ public class FileTS extends AbstractTimeSeriesCreator {
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
// findSum(repositoryId,branchName);
return new ArrayList<>();
// return Collections.singletonList(
// new TimeSeries(
// String.format("",
// gitRepositoryService.findById(repositoryId).getName(),
// branchName,
// getTimeSeriesType().getDescription()),
// getTimeSeriesType(),
// )
// )
// )
List<TimeSeries> timeSeriesResult = new ArrayList<>();
List<Commit> commits = new ArrayList<>(commitService.findByRepositoryIdAndName(repositoryId, branchName));
double value = 0;
TimeSeries timeSeries = new TimeSeries(
String.format("%s %s %s",
gitRepositoryService.findById(repositoryId).getName(),
branchName,
getTimeSeriesType().getDescription()),
getTimeSeriesType());
for (Commit commit : commits) {
for (FileChange fileChange : commit.getFileChanges()) {
if ((fileChange.getAdded() != null) && (fileChange.getAdded())) {
value += 1;
}
if ((fileChange.getRemoved() != null) && (fileChange.getRemoved())) {
value -= 1;
}
}
timeSeries.getValues().add(new TimeSeriesValue(commit.getDate(), value));
}
if (!timeSeries.getValues().isEmpty()) {
timeSeriesResult.add(timeSeries);
}
return timeSeriesResult;
}
@Override
@ -49,20 +62,4 @@ public class FileTS extends AbstractTimeSeriesCreator {
public TimeSeriesType getTimeSeriesType() {
return TimeSeriesType.FILES;
}
public void findSum(Integer repositoryId, String branchName) {
List<Commit> commits = new ArrayList<>(commitService.findByRepositoryIdAndName(repositoryId, branchName));
int value = 0;
for (Commit commit : commits) {
for (FileChange fileChange : commit.getFileChanges()) {
if ((fileChange.getAdded() != null) && (fileChange.getAdded())) {
value = +1;
}
if ((fileChange.getRemoved() != null) && (fileChange.getRemoved())) {
value = -1;
}
}
System.out.println(value);
}
}
}