From 3b7812a075919ab41d215f9a5dcd0356de85ed4e Mon Sep 17 00:00:00 2001 From: BarminaA Date: Sat, 5 Nov 2022 17:58:21 +0400 Subject: [PATCH] #76-create authorTS --- .../author/service/AuthorService.java | 5 +++ .../service/GitRepositoryService.java | 16 +++++++- .../ulstu/extractor/ts/creator/AuthorTS.java | 37 +++++++++++++++++- .../ulstu/extractor/ts/creator/EntityTS.java | 38 +++++++++++++++++- .../ru/ulstu/extractor/ts/creator/FileTS.java | 39 ++++++++++++++++++- .../ulstu/extractor/ts/model/TimeSeries.java | 5 +++ 6 files changed, 133 insertions(+), 7 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/author/service/AuthorService.java b/src/main/java/ru/ulstu/extractor/author/service/AuthorService.java index 659e17c..ce80869 100644 --- a/src/main/java/ru/ulstu/extractor/author/service/AuthorService.java +++ b/src/main/java/ru/ulstu/extractor/author/service/AuthorService.java @@ -11,6 +11,7 @@ import org.springframework.stereotype.Service; import ru.ulstu.extractor.author.model.Author; import ru.ulstu.extractor.author.repository.AuthorRepository; +import java.util.List; import java.util.Optional; @Service @@ -30,4 +31,8 @@ public class AuthorService { } return newAuthor.get(); } + + public List findAll() { + return authorRepository.findAll(); + } } diff --git a/src/main/java/ru/ulstu/extractor/gitrepository/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/gitrepository/service/GitRepositoryService.java index 78038ad..b08f1d9 100644 --- a/src/main/java/ru/ulstu/extractor/gitrepository/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/gitrepository/service/GitRepositoryService.java @@ -269,7 +269,7 @@ public class GitRepositoryService { try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { DiffFormatter diffFormatter = new DiffFormatter(out); diffFormatter.setRepository(localRepo); - diffFormatter.format(earlierCommit.getId(), laterCommit.getId()); + diffFormatter.format(laterCommit.getId(), earlierCommit.getId()); output = out.toString(); } catch (IOException e) { throw new RuntimeException("Error occurred during diff computation. Message: " + e.getMessage()); @@ -281,6 +281,9 @@ public class GitRepositoryService { List 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) { @@ -290,6 +293,8 @@ public class GitRepositoryService { fileChange.setFile(maybeFileName.get()); Future futureEntity = executorService.submit(() -> structuralUnitService.containsEntity(getContent(repository, commit, maybeFileName.get()))); Future 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()); @@ -312,6 +317,7 @@ public class GitRepositoryService { lineChange.setLineFrom(lineChange.getLineFrom() + "\n" + strings[i]); i++; } + isRemoved = true; } } lineChange.setRemoved(true); @@ -328,6 +334,7 @@ public class GitRepositoryService { lineChange.setLineTo(lineChange.getLineTo() + "\n" + strings[i]); i++; } + isAdded = true; } } lineChange.setAdded(true); @@ -340,6 +347,13 @@ public class GitRepositoryService { } } } + if ((isAdded) && (isRemoved)) { + isAdded = false; + isRemoved = false; + isFileChanged = true; + } + fileChange.setAdded(isAdded); + fileChange.setRemoved(isRemoved); return changes; } diff --git a/src/main/java/ru/ulstu/extractor/ts/creator/AuthorTS.java b/src/main/java/ru/ulstu/extractor/ts/creator/AuthorTS.java index ae71399..b1c41fb 100644 --- a/src/main/java/ru/ulstu/extractor/ts/creator/AuthorTS.java +++ b/src/main/java/ru/ulstu/extractor/ts/creator/AuthorTS.java @@ -1,8 +1,13 @@ package ru.ulstu.extractor.ts.creator; import org.springframework.stereotype.Component; +import ru.ulstu.extractor.author.model.Author; +import ru.ulstu.extractor.author.service.AuthorService; +import ru.ulstu.extractor.commit.service.CommitService; +import ru.ulstu.extractor.gitrepository.service.GitRepositoryService; 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; @@ -11,9 +16,17 @@ import java.util.List; @Component public class AuthorTS extends AbstractTimeSeriesCreator { private final TimeSeriesService timeSeriesService; + private final CommitService commitService; + private final AuthorService authorService; + private final GitRepositoryService gitRepositoryService; - public AuthorTS(TimeSeriesService timeSeriesService) { + public AuthorTS(TimeSeriesService timeSeriesService, + CommitService commitService, + AuthorService authorService, GitRepositoryService gitRepositoryService) { this.timeSeriesService = timeSeriesService; + this.commitService = commitService; + this.authorService = authorService; + this.gitRepositoryService = gitRepositoryService; } @Override @@ -23,7 +36,27 @@ public class AuthorTS extends AbstractTimeSeriesCreator { @Override public List getTimeSeries(Integer repositoryId, String branchName) { - return new ArrayList<>(); + List timeSeriesResult = new ArrayList<>(); + List authors = authorService.findAll(); + List commits = new ArrayList<>(commitService.findByRepositoryIdAndName(repositoryId, branchName)); + for (Author author : authors) { + TimeSeries timeSeries = new TimeSeries( + String.format("%s %s %s %s", + gitRepositoryService.findById(repositoryId).getName(), + branchName, + author.getName(), + getTimeSeriesType().getDescription()), + getTimeSeriesType()); + for (ru.ulstu.extractor.model.Commit commit : commits) { + if (commit.getAuthor().equals(author)) { + timeSeries.getValues().add(new TimeSeriesValue(commit.getDate(), 1.0)); + } + } + if (!timeSeries.getValues().isEmpty()) { + timeSeriesResult.add(timeSeries); + } + } + return timeSeriesResult; } @Override diff --git a/src/main/java/ru/ulstu/extractor/ts/creator/EntityTS.java b/src/main/java/ru/ulstu/extractor/ts/creator/EntityTS.java index c635556..946d9ac 100644 --- a/src/main/java/ru/ulstu/extractor/ts/creator/EntityTS.java +++ b/src/main/java/ru/ulstu/extractor/ts/creator/EntityTS.java @@ -1,24 +1,45 @@ package ru.ulstu.extractor.ts.creator; import org.springframework.stereotype.Component; +import ru.ulstu.extractor.commit.service.CommitService; +import ru.ulstu.extractor.gitrepository.model.FileChange; +import ru.ulstu.extractor.gitrepository.service.GitRepositoryService; 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; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; @Component public class EntityTS extends AbstractTimeSeriesCreator { private final TimeSeriesService timeSeriesService; + private final CommitService commitService; + private final GitRepositoryService gitRepositoryService; - public EntityTS(TimeSeriesService timeSeriesService) { + + public EntityTS(TimeSeriesService timeSeriesService, CommitService commitService, GitRepositoryService gitRepositoryService) { this.timeSeriesService = timeSeriesService; + this.commitService = commitService; + this.gitRepositoryService = gitRepositoryService; } @Override public List getTimeSeries(Integer repositoryId, String branchName) { - return new ArrayList(); + return Collections.singletonList( + new TimeSeries( + String.format("%s %s %s", + gitRepositoryService.findById(repositoryId).getName(), + branchName, + getTimeSeriesType().getDescription()), + getTimeSeriesType(), + commitService.findByRepositoryIdAndName(repositoryId, branchName) + .stream() + .map(c -> new TimeSeriesValue(c.getDate(), findEntity(repositoryId, branchName))) + .collect(Collectors.toList()))); } @Override @@ -30,4 +51,17 @@ public class EntityTS extends AbstractTimeSeriesCreator { public TimeSeriesType getTimeSeriesType() { return TimeSeriesType.ENTITIES; } + + public Double findEntity(Integer repositoryId, String branchName) { + List commits = new ArrayList<>(commitService.findByRepositoryIdAndName(repositoryId, branchName)); + double value = 0; + for (ru.ulstu.extractor.model.Commit commit : commits) { + for (FileChange fileChange : commit.getFileChanges()) { + if ((fileChange.getContainsEntity() != null) && (fileChange.getContainsEntity())) { + value = value + 1; + } + } + } + return value; + } } diff --git a/src/main/java/ru/ulstu/extractor/ts/creator/FileTS.java b/src/main/java/ru/ulstu/extractor/ts/creator/FileTS.java index 5797212..27e3453 100644 --- a/src/main/java/ru/ulstu/extractor/ts/creator/FileTS.java +++ b/src/main/java/ru/ulstu/extractor/ts/creator/FileTS.java @@ -1,6 +1,10 @@ package ru.ulstu.extractor.ts.creator; import org.springframework.stereotype.Component; +import ru.ulstu.extractor.commit.service.CommitService; +import ru.ulstu.extractor.gitrepository.model.FileChange; +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.service.TimeSeriesService; @@ -11,14 +15,29 @@ import java.util.List; @Component public class FileTS extends AbstractTimeSeriesCreator { private final TimeSeriesService timeSeriesService; + private final GitRepositoryService gitRepositoryService; + private final CommitService commitService; - public FileTS(TimeSeriesService timeSeriesService) { + public FileTS(TimeSeriesService timeSeriesService, GitRepositoryService gitRepositoryService, CommitService commitService) { this.timeSeriesService = timeSeriesService; + this.gitRepositoryService = gitRepositoryService; + this.commitService = commitService; } @Override public List getTimeSeries(Integer repositoryId, String branchName) { - return new ArrayList(); +// findSum(repositoryId,branchName); + return new ArrayList<>(); +// return Collections.singletonList( +// new TimeSeries( +// String.format("", +// gitRepositoryService.findById(repositoryId).getName(), +// branchName, +// getTimeSeriesType().getDescription()), +// getTimeSeriesType(), +// ) +// ) +// ) } @Override @@ -30,4 +49,20 @@ public class FileTS extends AbstractTimeSeriesCreator { public TimeSeriesType getTimeSeriesType() { return TimeSeriesType.FILES; } + + public void findSum(Integer repositoryId, String branchName) { + List 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); + } + } } diff --git a/src/main/java/ru/ulstu/extractor/ts/model/TimeSeries.java b/src/main/java/ru/ulstu/extractor/ts/model/TimeSeries.java index b6d6931..7a810da 100644 --- a/src/main/java/ru/ulstu/extractor/ts/model/TimeSeries.java +++ b/src/main/java/ru/ulstu/extractor/ts/model/TimeSeries.java @@ -32,6 +32,11 @@ public class TimeSeries extends BaseEntity { this.name = name; } + public TimeSeries(String name, TimeSeriesType timeSeriesType) { + this.name = name; + this.timeSeriesType = timeSeriesType; + } + public TimeSeries(String name, List values) { this.name = name; this.values = values;