#89 -- Use git service api to create time series

This commit is contained in:
Anton Romanov 2023-02-15 00:55:06 +04:00
parent b8c6aaf9da
commit 0d43bf8ccd
29 changed files with 346 additions and 250 deletions

View File

@ -0,0 +1,13 @@
package ru.ulstu.extractor.gitrepository.service;
import ru.ulstu.extractor.branch.model.Branch;
public interface GitApi {
Integer getBranchesCount(Branch branch);
Integer getStarsCount(Branch branch);
Integer getOpenIssuesCount(Branch branch);
String getFormattedUrl(String gitRepositoryUrl, String template);
}

View File

@ -0,0 +1,45 @@
package ru.ulstu.extractor.gitrepository.service;
import org.springframework.stereotype.Service;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.http.HttpService;
@Service
public class GitAtheneApi implements GitApi {
private final HttpService httpService;
private static final String BRANCHES_COUNT_URL = "%s/api/v1/repos/%s/%s/branches";
private static final String STARS_COUNT_URL = "%s/api/v1/repos/%s/%s";
private static final String OPEN_ISSUES_URL = "%s/api/v1/repos/%s/%s/issues?state=open";
public GitAtheneApi(HttpService httpService) {
this.httpService = httpService;
}
@Override
public Integer getBranchesCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), BRANCHES_COUNT_URL))
.length();
}
@Override
public Integer getStarsCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), STARS_COUNT_URL))
.getJSONObject(0)
.getInt("stars_count");
}
@Override
public Integer getOpenIssuesCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), OPEN_ISSUES_URL))
.length();
}
@Override
public String getFormattedUrl(String gitRepositoryUrl, String template) {
String[] urlParts = gitRepositoryUrl.split("/");
return String.format(template, urlParts[0] + "/" + urlParts[1] + "/" + urlParts[2], urlParts[3], urlParts[4]);
}
}

View File

@ -18,7 +18,7 @@ import ru.ulstu.extractor.branch.service.BranchService;
import ru.ulstu.extractor.commit.model.Commit;
import ru.ulstu.extractor.gitrepository.model.GitRepository;
import ru.ulstu.extractor.gitrepository.repository.GitRepositoryRepository;
import ru.ulstu.extractor.ts.creator.AbstractTimeSeriesCreator;
import ru.ulstu.extractor.ts.creator.db.DBTimeSeriesCreator;
import java.io.IOException;
import java.util.Collections;
@ -33,13 +33,13 @@ public class IndexService {
private final GitRepositoryService gitRepositoryService;
private final GitRepositoryRepository gitRepositoryRepository;
private final BranchService branchService;
private final List<AbstractTimeSeriesCreator> timeSeriesCreators;
private final List<DBTimeSeriesCreator> timeSeriesCreators;
private final static ExecutorService EXECUTOR = Executors.newFixedThreadPool(10);
public IndexService(GitRepositoryService gitRepositoryService,
GitRepositoryRepository gitRepositoryRepository,
BranchService branchService,
List<AbstractTimeSeriesCreator> timeSeriesCreators) {
List<DBTimeSeriesCreator> timeSeriesCreators) {
this.gitRepositoryService = gitRepositoryService;
this.gitRepositoryRepository = gitRepositoryRepository;
this.branchService = branchService;
@ -78,9 +78,8 @@ public class IndexService {
commitsTo += COMMITS_PAGE_SIZE;
commits = gitRepositoryService.getCommits(gitRepository.getUrl(), branch.getName(), commitsFrom, commitsTo, false);
}
Integer repositoryId = gitRepository.getId();
final Branch branchForSave = branch;
timeSeriesCreators.forEach(tsCreator -> tsCreator.addTimeSeries(repositoryId, branchForSave));
timeSeriesCreators.forEach(tsCreator -> tsCreator.addTimeSeries(gitRepository, branchForSave));
branchService.updateStatus(branch, IndexingStatus.FINISHED);
LOG.debug("Complete indexing {} branch", branch.getName());
}

View File

@ -1,5 +1,6 @@
package ru.ulstu.extractor.http;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -38,4 +39,26 @@ public class HttpService {
log.debug("Service response: {}", response);
return response;
}
public JSONArray get(String url) {
log.debug("Service call: {}", url);
try {
String response = client
.get()
.uri(url)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(String.class)
.toFuture().get();
if (response.startsWith("[")) {
return new JSONArray(response);
} else {
JSONArray jsonArray = new JSONArray();
jsonArray.put(0, new JSONObject(response));
return jsonArray;
}
} catch (Exception e) {
return new JSONArray("[{response:\"empty\"}]");
}
}
}

View File

@ -1,49 +1,10 @@
package ru.ulstu.extractor.ts.creator;
import ru.ulstu.extractor.branch.model.Branch;
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.Comparator;
import java.util.List;
import static ru.ulstu.extractor.ts.util.TimeSeriesDateMapper.mapTimeSeriesToInterval;
public abstract class AbstractTimeSeriesCreator {
public abstract TimeSeriesType getTimeSeriesType();
/**
* Извлечь список точек временных рядов
*
* @param repositoryId
* @param branchName
* @return
*/
public abstract List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName);
public abstract TimeSeriesService getTimeSeriesService();
/**
* Сохранить извлеченные временные ряды
*
* @param repositoryId
* @param branch
*/
public void addTimeSeries(Integer repositoryId, Branch branch) {
// извлеченные временных рядов
List<TimeSeries> timeSeries = getTimeSeries(repositoryId, branch.getName());
// сгруппированные по временным интервалам точки временных рядов
timeSeries.forEach(ts -> ts.setValues(mapTimeSeriesToInterval(getTimeSeriesService().getTimeSeriesInterval(), ts.getValues())));
getTimeSeriesService().save(sortTimeSeries(timeSeries), branch);
}
private List<TimeSeries> sortTimeSeries(List<TimeSeries> timeSeries) {
for (TimeSeries ts : timeSeries) {
ts.getValues().sort(Comparator.comparing(TimeSeriesValue::getDate));
}
return timeSeries;
}
}

View File

@ -1,53 +0,0 @@
package ru.ulstu.extractor.ts.creator;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.service.BranchService;
import ru.ulstu.extractor.gitrepository.model.GitRepository;
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.service.TimeSeriesService;
import ru.ulstu.extractor.ts.util.Dummy;
import java.util.Collections;
import java.util.List;
@Component
public class BranchTS extends AbstractTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final BranchService branchService;
private final GitRepositoryService gitRepositoryService;
public BranchTS(TimeSeriesService timeSeriesService,
BranchService branchService,
GitRepositoryService gitRepositoryService) {
this.timeSeriesService = timeSeriesService;
this.branchService = branchService;
this.gitRepositoryService = gitRepositoryService;
}
@Override
public TimeSeriesType getTimeSeriesType() {
return TimeSeriesType.BRANCHES;
}
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
GitRepository gitRepository = gitRepositoryService.findById(repositoryId);
return Collections.singletonList(
new TimeSeries(
String.format("%s %s %s",
gitRepository.getName(),
branchName,
getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(gitRepository, branchName),
getTimeSeriesType(),
Dummy.getDefaultTimeSeries()));
}
@Override
public TimeSeriesService getTimeSeriesService() {
return timeSeriesService;
}
}

View File

@ -1,53 +0,0 @@
package ru.ulstu.extractor.ts.creator;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.service.BranchService;
import ru.ulstu.extractor.gitrepository.model.GitRepository;
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.service.TimeSeriesService;
import ru.ulstu.extractor.ts.util.Dummy;
import java.util.Collections;
import java.util.List;
@Component
public class IssuesTS extends AbstractTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final BranchService branchService;
private final GitRepositoryService gitRepositoryService;
public IssuesTS(TimeSeriesService timeSeriesService,
BranchService branchService,
GitRepositoryService gitRepositoryService) {
this.timeSeriesService = timeSeriesService;
this.branchService = branchService;
this.gitRepositoryService = gitRepositoryService;
}
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
GitRepository gitRepository = gitRepositoryService.findById(repositoryId);
return Collections.singletonList(
new TimeSeries(
String.format("%s %s %s",
gitRepository.getName(),
branchName,
getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(gitRepository, branchName),
getTimeSeriesType(),
Dummy.getDefaultTimeSeries()));
}
@Override
public TimeSeriesService getTimeSeriesService() {
return timeSeriesService;
}
@Override
public TimeSeriesType getTimeSeriesType() {
return TimeSeriesType.ISSUES;
}
}

View File

@ -1,53 +0,0 @@
package ru.ulstu.extractor.ts.creator;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.service.BranchService;
import ru.ulstu.extractor.gitrepository.model.GitRepository;
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.service.TimeSeriesService;
import ru.ulstu.extractor.ts.util.Dummy;
import java.util.Collections;
import java.util.List;
@Component
public class StarTS extends AbstractTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final BranchService branchService;
private final GitRepositoryService gitRepositoryService;
public StarTS(TimeSeriesService timeSeriesService,
BranchService branchService,
GitRepositoryService gitRepositoryService) {
this.timeSeriesService = timeSeriesService;
this.branchService = branchService;
this.gitRepositoryService = gitRepositoryService;
}
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
GitRepository gitRepository = gitRepositoryService.findById(repositoryId);
return Collections.singletonList(
new TimeSeries(
String.format("%s %s %s",
gitRepository.getName(),
branchName,
getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(gitRepository, branchName),
getTimeSeriesType(),
Dummy.getDefaultTimeSeries()));
}
@Override
public TimeSeriesService getTimeSeriesService() {
return timeSeriesService;
}
@Override
public TimeSeriesType getTimeSeriesType() {
return TimeSeriesType.STARS;
}
}

View File

@ -1,4 +1,4 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.db;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.author.model.Author;
@ -7,7 +7,6 @@ import ru.ulstu.extractor.branch.service.BranchService;
import ru.ulstu.extractor.commit.model.Commit;
import ru.ulstu.extractor.commit.service.CommitService;
import ru.ulstu.extractor.gitrepository.model.GitRepository;
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;
@ -17,22 +16,19 @@ import java.util.ArrayList;
import java.util.List;
@Component
public class AuthorsCommitTS extends AbstractTimeSeriesCreator {
public class AuthorsCommitTS extends DBTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final CommitService commitService;
private final AuthorService authorService;
private final GitRepositoryService gitRepositoryService;
private final BranchService branchService;
public AuthorsCommitTS(TimeSeriesService timeSeriesService,
CommitService commitService,
AuthorService authorService,
GitRepositoryService gitRepositoryService,
BranchService branchService) {
this.timeSeriesService = timeSeriesService;
this.commitService = commitService;
this.authorService = authorService;
this.gitRepositoryService = gitRepositoryService;
this.branchService = branchService;
}
@ -42,11 +38,10 @@ public class AuthorsCommitTS extends AbstractTimeSeriesCreator {
}
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
public List<TimeSeries> getTimeSeries(GitRepository gitRepository, String branchName) {
List<TimeSeries> timeSeriesResult = new ArrayList<>();
List<Author> authors = authorService.findAll();
GitRepository gitRepository = gitRepositoryService.findById(repositoryId);
List<Commit> commits = new ArrayList<>(commitService.findByRepositoryIdAndName(repositoryId, branchName));
List<Commit> commits = new ArrayList<>(commitService.findByRepositoryIdAndName(gitRepository.getId(), branchName));
for (Author author : authors) {
TimeSeries timeSeries = new TimeSeries(
String.format("%s %s %s %s",

View File

@ -1,10 +1,9 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.db;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.service.BranchService;
import ru.ulstu.extractor.commit.service.CommitService;
import ru.ulstu.extractor.gitrepository.model.GitRepository;
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;
@ -15,25 +14,21 @@ import java.util.List;
import java.util.stream.Collectors;
@Component
public class CommitsTS extends AbstractTimeSeriesCreator {
public class CommitsTS extends DBTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final CommitService commitService;
private final GitRepositoryService gitRepositoryService;
private final BranchService branchService;
public CommitsTS(TimeSeriesService timeSeriesService,
CommitService commitService,
GitRepositoryService gitRepositoryService,
BranchService branchService) {
this.timeSeriesService = timeSeriesService;
this.commitService = commitService;
this.gitRepositoryService = gitRepositoryService;
this.branchService = branchService;
}
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
GitRepository gitRepository = gitRepositoryService.findById(repositoryId);
public List<TimeSeries> getTimeSeries(GitRepository gitRepository, String branchName) {
//TODO: добавить постраничное чтение
return Collections.singletonList(
new TimeSeries(
@ -43,7 +38,7 @@ public class CommitsTS extends AbstractTimeSeriesCreator {
getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(gitRepository, branchName),
getTimeSeriesType(),
commitService.findByRepositoryIdAndName(repositoryId, branchName)
commitService.findByRepositoryIdAndName(gitRepository.getId(), branchName)
.stream()
.map(c -> new TimeSeriesValue(c.getDate(), 1.0))
.collect(Collectors.toList())));

View File

@ -0,0 +1,45 @@
package ru.ulstu.extractor.ts.creator.db;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.gitrepository.model.GitRepository;
import ru.ulstu.extractor.ts.creator.AbstractTimeSeriesCreator;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import java.util.Comparator;
import java.util.List;
import static ru.ulstu.extractor.ts.util.TimeSeriesDateMapper.mapTimeSeriesToInterval;
public abstract class DBTimeSeriesCreator extends AbstractTimeSeriesCreator {
/**
* Извлечь список точек временных рядов
*
* @param gitRepository
* @param branchName
* @return
*/
public abstract List<TimeSeries> getTimeSeries(GitRepository gitRepository, String branchName);
/**
* Сохранить извлеченные временные ряды
*
* @param gitRepository
* @param branch
*/
public void addTimeSeries(GitRepository gitRepository, Branch branch) {
// извлеченные временных рядов
List<TimeSeries> timeSeries = getTimeSeries(gitRepository, branch.getName());
// сгруппированные по временным интервалам точки временных рядов
timeSeries.forEach(ts -> ts.setValues(mapTimeSeriesToInterval(getTimeSeriesService().getTimeSeriesInterval(), ts.getValues())));
getTimeSeriesService().save(sortTimeSeries(timeSeries), branch);
}
private List<TimeSeries> sortTimeSeries(List<TimeSeries> timeSeries) {
for (TimeSeries ts : timeSeries) {
ts.getValues().sort(Comparator.comparing(TimeSeriesValue::getDate));
}
return timeSeries;
}
}

View File

@ -1,4 +1,4 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.db;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.service.BranchService;
@ -6,7 +6,6 @@ import ru.ulstu.extractor.commit.model.Commit;
import ru.ulstu.extractor.commit.service.CommitService;
import ru.ulstu.extractor.gitrepository.model.FileChange;
import ru.ulstu.extractor.gitrepository.model.GitRepository;
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;
@ -16,27 +15,23 @@ import java.util.ArrayList;
import java.util.List;
@Component
public class FileTS extends AbstractTimeSeriesCreator {
public class FileTS extends DBTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final GitRepositoryService gitRepositoryService;
private final CommitService commitService;
private final BranchService branchService;
public FileTS(TimeSeriesService timeSeriesService,
GitRepositoryService gitRepositoryService,
CommitService commitService,
BranchService branchService) {
this.timeSeriesService = timeSeriesService;
this.gitRepositoryService = gitRepositoryService;
this.commitService = commitService;
this.branchService = branchService;
}
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
GitRepository gitRepository = gitRepositoryService.findById(repositoryId);
public List<TimeSeries> getTimeSeries(GitRepository gitRepository, String branchName) {
List<TimeSeries> timeSeriesResult = new ArrayList<>();
List<Commit> commits = new ArrayList<>(commitService.findByRepositoryIdAndName(repositoryId, branchName));
List<Commit> commits = new ArrayList<>(commitService.findByRepositoryIdAndName(gitRepository.getId(), branchName));
double value = 0;
TimeSeries timeSeries = new TimeSeries(
String.format("%s %s %s",

View File

@ -1,4 +1,4 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.author.model.Author;
@ -8,6 +8,7 @@ import ru.ulstu.extractor.commit.model.Commit;
import ru.ulstu.extractor.commit.service.CommitService;
import ru.ulstu.extractor.gitrepository.model.GitRepository;
import ru.ulstu.extractor.gitrepository.service.GitRepositoryService;
import ru.ulstu.extractor.ts.creator.db.DBTimeSeriesCreator;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
@ -17,11 +18,10 @@ import java.util.ArrayList;
import java.util.List;
@Component
public class AuthorTS extends AbstractTimeSeriesCreator {
public class AuthorTS extends DBTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final CommitService commitService;
private final AuthorService authorService;
private final GitRepositoryService gitRepositoryService;
private final BranchService branchService;
public AuthorTS(TimeSeriesService timeSeriesService,
@ -32,7 +32,6 @@ public class AuthorTS extends AbstractTimeSeriesCreator {
this.timeSeriesService = timeSeriesService;
this.commitService = commitService;
this.authorService = authorService;
this.gitRepositoryService = gitRepositoryService;
this.branchService = branchService;
}
@ -42,11 +41,10 @@ public class AuthorTS extends AbstractTimeSeriesCreator {
}
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
public List<TimeSeries> getTimeSeries(GitRepository gitRepository, String branchName) {
List<TimeSeries> timeSeriesResult = new ArrayList<>();
List<Author> authors = authorService.findAll();
GitRepository gitRepository = gitRepositoryService.findById(repositoryId);
List<Commit> commits = new ArrayList<>(commitService.findByRepositoryIdAndName(repositoryId, branchName));
List<Commit> commits = new ArrayList<>(commitService.findByRepositoryIdAndName(gitRepository.getId(), branchName));
for (Author author : authors) {
TimeSeries timeSeries = new TimeSeries(
String.format("%s %s %s %s",

View File

@ -1,4 +1,4 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.service.BranchService;
@ -13,7 +13,7 @@ import java.util.Collections;
import java.util.List;
@Component
public class AuthorsCompletedIssueTS extends AbstractTimeSeriesCreator {
public class AuthorsCompletedIssueTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final BranchService branchService;
@ -28,8 +28,7 @@ public class AuthorsCompletedIssueTS extends AbstractTimeSeriesCreator {
}
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
GitRepository gitRepository = gitRepositoryService.findById(repositoryId);
public List<TimeSeries> getTimeSeries(GitRepository gitRepository, String branchName) {
//TODO: добавить постраничное чтение
return Collections.singletonList(
new TimeSeries(

View File

@ -1,4 +1,4 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.service.BranchService;
@ -13,7 +13,7 @@ import java.util.Collections;
import java.util.List;
@Component
public class AuthorsIssueTS extends AbstractTimeSeriesCreator {
public class AuthorsIssueTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final BranchService branchService;

View File

@ -0,0 +1,35 @@
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.gitrepository.service.GitApi;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService;
@Component
public class BranchTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final GitApi gitApi;
public BranchTS(TimeSeriesService timeSeriesService,
GitApi gitApi) {
this.timeSeriesService = timeSeriesService;
this.gitApi = gitApi;
}
@Override
public TimeSeriesType getTimeSeriesType() {
return TimeSeriesType.BRANCHES;
}
@Override
public TimeSeriesService getTimeSeriesService() {
return timeSeriesService;
}
@Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(gitApi.getBranchesCount(branch));
}
}

View File

@ -1,9 +1,10 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.service.BranchService;
import ru.ulstu.extractor.gitrepository.model.GitRepository;
import ru.ulstu.extractor.gitrepository.service.GitRepositoryService;
import ru.ulstu.extractor.ts.creator.AbstractTimeSeriesCreator;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.service.TimeSeriesService;

View File

@ -1,9 +1,10 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.service.BranchService;
import ru.ulstu.extractor.gitrepository.model.GitRepository;
import ru.ulstu.extractor.gitrepository.service.GitRepositoryService;
import ru.ulstu.extractor.ts.creator.AbstractTimeSeriesCreator;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.service.TimeSeriesService;

View File

@ -1,4 +1,4 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.service.BranchService;
@ -7,6 +7,7 @@ import ru.ulstu.extractor.commit.service.CommitService;
import ru.ulstu.extractor.gitrepository.model.FileChange;
import ru.ulstu.extractor.gitrepository.model.GitRepository;
import ru.ulstu.extractor.gitrepository.service.GitRepositoryService;
import ru.ulstu.extractor.ts.creator.AbstractTimeSeriesCreator;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;

View File

@ -1,9 +1,10 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.service.BranchService;
import ru.ulstu.extractor.gitrepository.model.GitRepository;
import ru.ulstu.extractor.gitrepository.service.GitRepositoryService;
import ru.ulstu.extractor.ts.creator.AbstractTimeSeriesCreator;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.service.TimeSeriesService;

View File

@ -0,0 +1,35 @@
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.gitrepository.service.GitApi;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService;
@Component
public class IssuesTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final GitApi gitApi;
public IssuesTS(TimeSeriesService timeSeriesService,
GitApi gitApi) {
this.timeSeriesService = timeSeriesService;
this.gitApi = gitApi;
}
@Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(gitApi.getOpenIssuesCount(branch));
}
@Override
public TimeSeriesService getTimeSeriesService() {
return timeSeriesService;
}
@Override
public TimeSeriesType getTimeSeriesType() {
return TimeSeriesType.ISSUES;
}
}

View File

@ -1,4 +1,4 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.service.BranchService;
@ -13,7 +13,7 @@ import java.util.Collections;
import java.util.List;
@Component
public class ProcessTS extends AbstractTimeSeriesCreator {
public class ProcessTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final BranchService branchService;

View File

@ -0,0 +1,20 @@
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.ts.creator.AbstractTimeSeriesCreator;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import java.util.Collections;
public abstract class ScheduledTimeSeriesCreator extends AbstractTimeSeriesCreator {
@Transactional
public void addTimeSeriesValue(Branch branch) {
TimeSeries timeSeries = getTimeSeriesService().findByBranchAndTypeOrCreate(branch, getTimeSeriesType());
timeSeries.getValues().add(getNewTimeSeriesValue(branch));
getTimeSeriesService().save(Collections.singletonList(timeSeries), branch);
}
public abstract TimeSeriesValue getNewTimeSeriesValue(Branch branch);
}

View File

@ -0,0 +1,34 @@
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.gitrepository.service.GitApi;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService;
@Component
public class StarTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final GitApi gitApi;
public StarTS(TimeSeriesService timeSeriesService, GitApi gitApi) {
this.timeSeriesService = timeSeriesService;
this.gitApi = gitApi;
}
@Override
public TimeSeriesService getTimeSeriesService() {
return timeSeriesService;
}
@Override
public TimeSeriesType getTimeSeriesType() {
return TimeSeriesType.STARS;
}
@Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(gitApi.getStarsCount(branch));
}
}

View File

@ -33,6 +33,13 @@ public class TimeSeries extends BaseEntity {
public TimeSeries() {
}
public TimeSeries(Branch branch, TimeSeriesType timeSeriesType) {
this(String.format("%s %s %s",
branch.getGitRepository().getName(),
branch.getName(),
timeSeriesType.getDescription()), branch, timeSeriesType);
}
public TimeSeries(String name, Branch branch, TimeSeriesType timeSeriesType) {
this(name, branch, timeSeriesType, new ArrayList<>());
}

View File

@ -18,6 +18,11 @@ public class TimeSeriesValue extends BaseEntity {
this.value = value;
}
public TimeSeriesValue(Integer value) {
this.date = new Date();
this.value = Double.valueOf(value);
}
public TimeSeriesValue(TimeSeries timeSeries, Date date, Double value) {
this.date = date;
this.value = value;

View File

@ -1,7 +1,9 @@
package ru.ulstu.extractor.ts.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import java.util.List;
import java.util.Optional;
@ -10,4 +12,6 @@ public interface TimeSeriesRepository extends JpaRepository<TimeSeries, Integer>
Optional<TimeSeries> findByName(String name);
List<TimeSeries> getTimeSeriesByBranchId(Integer branchId);
Optional<TimeSeries> getTimeSeriesByBranchAndTimeSeriesType(Branch branch, TimeSeriesType timeSeriesType);
}

View File

@ -0,0 +1,34 @@
package ru.ulstu.extractor.ts.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.branch.service.BranchService;
import ru.ulstu.extractor.ts.creator.scheduled.ScheduledTimeSeriesCreator;
import java.util.List;
@Service
public class ScheduledTimeSeriesService {
private final Logger log = LoggerFactory.getLogger(ScheduledTimeSeriesService.class);
private final List<ScheduledTimeSeriesCreator> scheduledTimeSeriesCreators;
private final BranchService branchService;
public ScheduledTimeSeriesService(List<ScheduledTimeSeriesCreator> scheduledTimeSeriesCreators,
BranchService branchService) {
this.scheduledTimeSeriesCreators = scheduledTimeSeriesCreators;
this.branchService = branchService;
}
@Scheduled(cron = "0 */5 * * * *", zone = "Europe/Samara")
public void addTimeSeriesPoints() {
log.debug("ConferenceScheduler.checkDeadlineBeforeWeek started");
List<Branch> branches = branchService.findAll();
branches.forEach(branch -> {
scheduledTimeSeriesCreators.forEach(creator -> creator.addTimeSeriesValue(branch));
});
log.debug("ConferenceScheduler.checkDeadlineBeforeWeek finished");
}
}

View File

@ -59,7 +59,7 @@ public class TimeSeriesService {
return savedTimeSeries;
}
public TimeSeries findOrCreate(String timeSeriesName, Branch branch, TimeSeriesType timeSeriesType) {
private TimeSeries findOrCreate(String timeSeriesName, Branch branch, TimeSeriesType timeSeriesType) {
Optional<TimeSeries> maybeTimeSeries = timeSeriesRepository.findByName(timeSeriesName);
if (maybeTimeSeries.isPresent()) {
LOG.debug("TimeSeries {} exists.", maybeTimeSeries.get().getName());
@ -68,6 +68,15 @@ public class TimeSeriesService {
return timeSeriesRepository.save(new TimeSeries(timeSeriesName, branch, timeSeriesType));
}
public TimeSeries findByBranchAndTypeOrCreate(Branch branch, TimeSeriesType timeSeriesType) {
Optional<TimeSeries> maybeTimeSeries = timeSeriesRepository.getTimeSeriesByBranchAndTimeSeriesType(branch, timeSeriesType);
if (maybeTimeSeries.isPresent()) {
LOG.debug("TimeSeries {} exists.", maybeTimeSeries.get().getName());
return maybeTimeSeries.get();
}
return timeSeriesRepository.save(new TimeSeries(branch, timeSeriesType));
}
/**
* Сохранить список временных рядов
*