Merge pull request '89-time-series-by-cron' (#90) from 89-time-series-by-cron into master

Reviewed-on: #90
This commit is contained in:
romanov73 2023-02-20 10:16:32 +04:00
commit a3a032b70b
33 changed files with 554 additions and 502 deletions

View File

@ -26,7 +26,11 @@ public class GitRepository extends BaseEntity {
public String getName() {
int lastDelimiterIndex = url.lastIndexOf("/");
return (lastDelimiterIndex > 0 && lastDelimiterIndex < url.length())
? url.substring(lastDelimiterIndex + 1)
: url;
? removeDotGit(url.substring(lastDelimiterIndex + 1))
: removeDotGit(url);
}
private String removeDotGit(String prevName) {
return prevName.substring(0, prevName.lastIndexOf(".git"));
}
}

View File

@ -0,0 +1,15 @@
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(Branch branch, String template);
Integer getAuthorsCompletedIssues(Branch branch);
}

View File

@ -0,0 +1,53 @@
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";
private static final String COMPLETED_ISSUES_URL = "%s/api/v1/repos/%s/%s/issues?state=closed";
public GitAtheneApi(HttpService httpService) {
this.httpService = httpService;
}
@Override
public Integer getBranchesCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch, BRANCHES_COUNT_URL))
.length();
}
@Override
public Integer getStarsCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch, STARS_COUNT_URL))
.getJSONObject(0)
.getInt("stars_count");
}
@Override
public Integer getOpenIssuesCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch, OPEN_ISSUES_URL))
.length();
}
@Override
public String getFormattedUrl(Branch branch, String template) {
String[] urlParts = branch.getGitRepository().getUrl().split("/");
return String.format(template, urlParts[0] + "/" + urlParts[1] + "/" + urlParts[2], urlParts[3], urlParts[4]);
}
@Override
public Integer getAuthorsCompletedIssues(Branch branch) {
return httpService
.get(getFormattedUrl(branch, COMPLETED_ISSUES_URL))
.length();
}
}

View File

@ -0,0 +1,53 @@
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 GithubApi implements GitApi {
private final HttpService httpService;
private static final String BRANCHES_COUNT_URL = "https://api.github.com/repos/%s/%s/branches";
private static final String STARS_COUNT_URL = "https://api.github.com/repos/%s/%s";
private static final String OPEN_ISSUES_URL = "https://api.github.com/repos/%s/%s/issues?state=open";
private static final String AUTHOR_COMPLETED_ISSUES_URL = "https://api.github.com/repos/%s/%s/issues?state=open";
public GithubApi(HttpService httpService) {
this.httpService = httpService;
}
@Override
public Integer getBranchesCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch, BRANCHES_COUNT_URL))
.length();
}
@Override
public Integer getStarsCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch, STARS_COUNT_URL))
.getJSONObject(0)
.getInt("watchers");
}
@Override
public Integer getOpenIssuesCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch, OPEN_ISSUES_URL))
.length();
}
@Override
public String getFormattedUrl(Branch branch, String template) {
String[] urlParts = branch.getGitRepository().getUrl().split("/");
return String.format(template, urlParts[3], urlParts[4]);
}
@Override
public Integer getAuthorsCompletedIssues(Branch branch) {
return httpService
.get(getFormattedUrl(branch, AUTHOR_COMPLETED_ISSUES_URL))
.length();
}
}

View File

@ -0,0 +1,71 @@
package ru.ulstu.extractor.gitrepository.service;
import org.json.JSONArray;
import org.springframework.stereotype.Service;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.http.HttpService;
@Service
public class GitlabApi implements GitApi {
private final HttpService httpService;
private static final String BRANCHES_COUNT_URL = "%s/api/v4/projects/%s/repository/branches";
private final static String PROJECT_ID_URL = "https://gitlab.com/api/v4/users/%s/projects?search=%s";
private static final String PROJECT_INFO_URL = "%s/api/v4/projects/%s";
private static final String OPEN_ISSUES_URL = "%s/api/v4/projects/%s/issues?state=opened";
private static final String COMPLETED_ISSUES_URL = "%s/api/v4/projects/%s/issues?state=closed";
public GitlabApi(HttpService httpService) {
this.httpService = httpService;
}
@Override
public Integer getBranchesCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch, BRANCHES_COUNT_URL))
.length();
}
@Override
public Integer getStarsCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch, PROJECT_INFO_URL))
.getJSONObject(0)
.getInt("star_count");
}
@Override
public Integer getOpenIssuesCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch, OPEN_ISSUES_URL))
.length();
}
@Override
public String getFormattedUrl(Branch branch, String template) {
String[] urlParts = branch.getGitRepository().getUrl().split("/");
return String.format(template, urlParts[0] + "/" + urlParts[1] + "/" + urlParts[2], getProjectId(branch));
}
public String getFormattedUrlForProjectId(Branch branch, String template) {
String[] urlParts = branch.getGitRepository().getUrl().split("/");
// получаем корректное название репозитория, gitlab всегда добавляет .git в конец
return String.format(template, urlParts[3], branch.getGitRepository().getName());
}
@Override
public Integer getAuthorsCompletedIssues(Branch branch) {
return httpService
.get(getFormattedUrl(branch, COMPLETED_ISSUES_URL))
.length();
}
private String getProjectId(Branch branch) {
JSONArray projects = httpService.get(getFormattedUrlForProjectId(branch, PROJECT_ID_URL));
for (int i = 0; i < projects.length(); i++) {
if (projects.getJSONObject(i).get("name").equals(branch.getGitRepository().getName())) {
return String.valueOf(projects.getJSONObject(i).getInt("id"));
}
}
throw new RuntimeException("Id проекта не найден: " + branch.getGitRepository().getName());
}
}

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(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;
@ -8,6 +9,7 @@ import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import java.time.Duration;
import java.util.Optional;
@Service
@ -38,4 +40,27 @@ 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)
.timeout(Duration.ofMinutes(1))
.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) {
throw new RuntimeException(e);
}
}
}

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,75 +0,0 @@
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.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;
import ru.ulstu.extractor.ts.service.TimeSeriesService;
import java.util.ArrayList;
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;
private final BranchService branchService;
public AuthorTS(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;
}
@Override
public TimeSeriesType getTimeSeriesType() {
return TimeSeriesType.AUTHORS;
}
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, 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));
for (Author author : authors) {
TimeSeries timeSeries = new TimeSeries(
String.format("%s %s %s %s",
gitRepository.getName(),
branchName,
author.getName(),
getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(gitRepository, branchName),
getTimeSeriesType());
for (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
public TimeSeriesService getTimeSeriesService() {
return timeSeriesService;
}
}

View File

@ -1,54 +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 AuthorsCompletedIssueTS extends AbstractTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final BranchService branchService;
private final GitRepositoryService gitRepositoryService;
public AuthorsCompletedIssueTS(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);
//TODO: добавить постраничное чтение
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.AUTHOR_COMPLETED_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 AuthorsIssueTS extends AbstractTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final BranchService branchService;
private final GitRepositoryService gitRepositoryService;
public AuthorsIssueTS(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.AUTHOR_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 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,13 +1,12 @@
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;
import ru.ulstu.extractor.author.service.AuthorService;
import ru.ulstu.extractor.branch.model.Branch;
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,19 +38,20 @@ public class AuthorsCommitTS extends AbstractTimeSeriesCreator {
}
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
public List<TimeSeries> getTimeSeries(Branch branch) {
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(branch.getGitRepository().getId(),
branch.getName()));
for (Author author : authors) {
TimeSeries timeSeries = new TimeSeries(
String.format("%s %s %s %s",
gitRepository.getName(),
branchName,
branch.getGitRepository().getName(),
branch.getName(),
author.getName(),
getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(gitRepository, branchName),
branchService.findByRepositoryAndName(branch.getGitRepository(),
branch.getName()),
getTimeSeriesType());
for (Commit commit : commits) {
if (commit.getAuthor().equals(author)) {

View File

@ -1,9 +1,8 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.db;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
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;
@ -13,30 +12,25 @@ import java.util.Collections;
import java.util.List;
@Component
public class ClassTS extends AbstractTimeSeriesCreator {
public class ClassTS extends DBTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final BranchService branchService;
private final GitRepositoryService gitRepositoryService;
public ClassTS(TimeSeriesService timeSeriesService,
BranchService branchService,
GitRepositoryService gitRepositoryService) {
BranchService branchService) {
this.timeSeriesService = timeSeriesService;
this.branchService = branchService;
this.gitRepositoryService = gitRepositoryService;
}
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
GitRepository gitRepository = gitRepositoryService.findById(repositoryId);
public List<TimeSeries> getTimeSeries(Branch branch) {
return Collections.singletonList(
new TimeSeries(
String.format("%s %s %s",
gitRepository.getName(),
branchName,
branch.getGitRepository().getName(),
branch.getName(),
getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(gitRepository, branchName),
branchService.findByRepositoryAndName(branch.getGitRepository(), branch.getName()),
getTimeSeriesType(),
Dummy.getDefaultTimeSeries()));
}

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.model.Branch;
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,35 +14,33 @@ 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(Branch branch) {
//TODO: добавить постраничное чтение
return Collections.singletonList(
new TimeSeries(
String.format("%s %s %s",
gitRepository.getName(),
branchName,
branch.getGitRepository().getName(),
branch.getName(),
getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(gitRepository, branchName),
branchService.findByRepositoryAndName(branch.getGitRepository(),
branch.getName()),
getTimeSeriesType(),
commitService.findByRepositoryIdAndName(repositoryId, branchName)
commitService.findByRepositoryIdAndName(branch.getGitRepository().getId(),
branch.getName())
.stream()
.map(c -> new TimeSeriesValue(c.getDate(), 1.0))
.collect(Collectors.toList())));

View File

@ -0,0 +1,42 @@
package ru.ulstu.extractor.ts.creator.db;
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.Comparator;
import java.util.List;
import static ru.ulstu.extractor.ts.util.TimeSeriesDateMapper.mapTimeSeriesToInterval;
public abstract class DBTimeSeriesCreator extends AbstractTimeSeriesCreator {
/**
* Извлечь список точек временных рядов
*
* @param branch
* @return
*/
public abstract List<TimeSeries> getTimeSeries(Branch branch);
/**
* Сохранить извлеченные временные ряды
*
* @param branch
*/
public void addTimeSeries(Branch branch) {
// извлеченные временных рядов
List<TimeSeries> timeSeries = getTimeSeries(branch);
// сгруппированные по временным интервалам точки временных рядов
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,9 +1,8 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.db;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
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;
@ -13,30 +12,25 @@ import java.util.Collections;
import java.util.List;
@Component
public class DependenceTS extends AbstractTimeSeriesCreator {
public class DependenceTS extends DBTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final BranchService branchService;
private final GitRepositoryService gitRepositoryService;
public DependenceTS(TimeSeriesService timeSeriesService,
BranchService branchService,
GitRepositoryService gitRepositoryService) {
BranchService branchService) {
this.timeSeriesService = timeSeriesService;
this.branchService = branchService;
this.gitRepositoryService = gitRepositoryService;
}
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
GitRepository gitRepository = gitRepositoryService.findById(repositoryId);
public List<TimeSeries> getTimeSeries(Branch branch) {
return Collections.singletonList(
new TimeSeries(
String.format("%s %s %s",
gitRepository.getName(),
branchName,
branch.getGitRepository().getName(),
branch.getName(),
getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(gitRepository, branchName),
branchService.findByRepositoryAndName(branch.getGitRepository(), branch.getName()),
getTimeSeriesType(),
Dummy.getDefaultTimeSeries()));
}

View File

@ -1,12 +1,11 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.db;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
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.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;
@ -18,36 +17,32 @@ import java.util.List;
import java.util.stream.Collectors;
@Component
public class EntityTS extends AbstractTimeSeriesCreator {
public class EntityTS extends DBTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final CommitService commitService;
private final GitRepositoryService gitRepositoryService;
private final BranchService branchService;
public EntityTS(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(Branch branch) {
return Collections.singletonList(
new TimeSeries(
String.format("%s %s %s",
gitRepository.getName(),
branchName,
branch.getGitRepository().getName(),
branch.getName(),
getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(gitRepository, branchName),
branchService.findByRepositoryAndName(branch.getGitRepository(), branch.getName()),
getTimeSeriesType(),
commitService.findByRepositoryIdAndName(repositoryId, branchName)
commitService.findByRepositoryIdAndName(branch.getGitRepository().getId(), branch.getName())
.stream()
.map(c -> new TimeSeriesValue(c.getDate(), findEntity(repositoryId, branchName)))
.map(c -> new TimeSeriesValue(c.getDate(), findEntity(branch.getGitRepository().getId(), branch.getName())))
.collect(Collectors.toList())));
}

View File

@ -1,12 +1,11 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.db;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
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.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,34 +15,32 @@ 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(Branch branch) {
List<TimeSeries> timeSeriesResult = new ArrayList<>();
List<Commit> commits = new ArrayList<>(commitService.findByRepositoryIdAndName(repositoryId, branchName));
List<Commit> commits = new ArrayList<>(commitService.findByRepositoryIdAndName(branch.getGitRepository().getId(),
branch.getName()));
double value = 0;
TimeSeries timeSeries = new TimeSeries(
String.format("%s %s %s",
gitRepository.getName(),
branchName,
branch.getGitRepository().getName(),
branch.getName(),
getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(gitRepository, branchName),
branchService.findByRepositoryAndName(branch.getGitRepository(),
branch.getName()),
getTimeSeriesType());
for (Commit commit : commits) {
for (FileChange fileChange : commit.getFileChanges()) {

View File

@ -1,9 +1,8 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.db;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
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;
@ -13,30 +12,25 @@ import java.util.Collections;
import java.util.List;
@Component
public class InterfaceTS extends AbstractTimeSeriesCreator {
public class InterfaceTS extends DBTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final BranchService branchService;
private final GitRepositoryService gitRepositoryService;
public InterfaceTS(TimeSeriesService timeSeriesService,
BranchService branchService,
GitRepositoryService gitRepositoryService) {
BranchService branchService) {
this.timeSeriesService = timeSeriesService;
this.branchService = branchService;
this.gitRepositoryService = gitRepositoryService;
}
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
GitRepository gitRepository = gitRepositoryService.findById(repositoryId);
public List<TimeSeries> getTimeSeries(Branch branch) {
return Collections.singletonList(
new TimeSeries(
String.format("%s %s %s",
gitRepository.getName(),
branchName,
branch.getGitRepository().getName(),
branch.getName(),
getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(gitRepository, branchName),
branchService.findByRepositoryAndName(branch.getGitRepository(), branch.getName()),
getTimeSeriesType(),
Dummy.getDefaultTimeSeries()));
}

View File

@ -1,9 +1,8 @@
package ru.ulstu.extractor.ts.creator;
package ru.ulstu.extractor.ts.creator.db;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
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;
@ -13,30 +12,25 @@ import java.util.Collections;
import java.util.List;
@Component
public class ProcessTS extends AbstractTimeSeriesCreator {
public class ProcessTS extends DBTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final BranchService branchService;
private final GitRepositoryService gitRepositoryService;
public ProcessTS(TimeSeriesService timeSeriesService,
BranchService branchService,
GitRepositoryService gitRepositoryService) {
BranchService branchService) {
this.timeSeriesService = timeSeriesService;
this.branchService = branchService;
this.gitRepositoryService = gitRepositoryService;
}
@Override
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
GitRepository gitRepository = gitRepositoryService.findById(repositoryId);
public List<TimeSeries> getTimeSeries(Branch branch) {
return Collections.singletonList(
new TimeSeries(
String.format("%s %s %s",
gitRepository.getName(),
branchName,
branch.getGitRepository().getName(),
branch.getName(),
getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(gitRepository, branchName),
branchService.findByRepositoryAndName(branch.getGitRepository(), branch.getName()),
getTimeSeriesType(),
Dummy.getDefaultTimeSeries()));
}

View File

@ -0,0 +1,36 @@
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
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 ApplicationContext applicationContext;
public BranchTS(TimeSeriesService timeSeriesService,
ApplicationContext applicationContext) {
this.timeSeriesService = timeSeriesService;
this.applicationContext = applicationContext;
}
@Override
public TimeSeriesType getTimeSeriesType() {
return TimeSeriesType.BRANCHES;
}
@Override
public TimeSeriesService getTimeSeriesService() {
return timeSeriesService;
}
@Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(applicationContext.getBean(getGitApiServiceClass(branch))
.getBranchesCount(branch));
}
}

View File

@ -0,0 +1,36 @@
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
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 ApplicationContext applicationContext;
public IssuesTS(TimeSeriesService timeSeriesService,
ApplicationContext applicationContext) {
this.timeSeriesService = timeSeriesService;
this.applicationContext = applicationContext;
}
@Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(applicationContext.getBean(getGitApiServiceClass(branch))
.getOpenIssuesCount(branch));
}
@Override
public TimeSeriesService getTimeSeriesService() {
return timeSeriesService;
}
@Override
public TimeSeriesType getTimeSeriesType() {
return TimeSeriesType.ISSUES;
}
}

View File

@ -0,0 +1,35 @@
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.gitrepository.service.GitApi;
import ru.ulstu.extractor.gitrepository.service.GitAtheneApi;
import ru.ulstu.extractor.gitrepository.service.GithubApi;
import ru.ulstu.extractor.gitrepository.service.GitlabApi;
import ru.ulstu.extractor.ts.creator.AbstractTimeSeriesCreator;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.util.StringUtils;
import java.util.Collections;
import java.util.Map;
public abstract class ScheduledTimeSeriesCreator extends AbstractTimeSeriesCreator {
private final Map<String, Class<? extends GitApi>> gitApiServiceHolder = Map.of(
"git.athene.tech", GitAtheneApi.class,
"github.com", GithubApi.class,
"gitlab.com", GitlabApi.class);
@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);
public Class<? extends GitApi> getGitApiServiceClass(Branch branch) {
return gitApiServiceHolder.get(StringUtils.getServerDomain(branch.getGitRepository().getUrl()));
}
}

View File

@ -0,0 +1,36 @@
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
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 ApplicationContext applicationContext;
public StarTS(TimeSeriesService timeSeriesService,
ApplicationContext applicationContext) {
this.timeSeriesService = timeSeriesService;
this.applicationContext = applicationContext;
}
@Override
public TimeSeriesService getTimeSeriesService() {
return timeSeriesService;
}
@Override
public TimeSeriesType getTimeSeriesType() {
return TimeSeriesType.STARS;
}
@Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(applicationContext.getBean(getGitApiServiceClass(branch))
.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 0 * * * *", zone = "Europe/Samara")
public void addTimeSeriesPoints() {
log.debug("Старт добавления новых точек временного ряда");
List<Branch> branches = branchService.findAll();
branches.forEach(branch -> {
scheduledTimeSeriesCreators.forEach(creator -> creator.addTimeSeriesValue(branch));
});
log.debug("Завершение добавления новых точек временного ряда");
}
}

View File

@ -47,19 +47,19 @@ public class TimeSeriesService {
@Transactional
public TimeSeries save(String timeSeriesName, Branch branch, TimeSeriesType timeSeriesType, List<TimeSeriesValue> timeSeriesValues) {
LOG.debug("Start save {} time series with {} time series values ", timeSeriesName, timeSeriesValues.size());
LOG.debug("Начало сохранения временного ряда {} с {} значениями", timeSeriesName, timeSeriesValues.size());
final TimeSeries timeSeries = findOrCreate(timeSeriesName, branch, timeSeriesType);
List<TimeSeriesValue> timeSeriesValuesToRemove = timeSeries.getValues();
timeSeries.setValues(timeSeriesValues);
LOG.debug("Save time series {} ", timeSeries.getName());
LOG.debug("Сохранение временного ряда {} ", timeSeries.getName());
TimeSeries savedTimeSeries = timeSeriesRepository.save(timeSeries);
LOG.debug("Clear {} time series values ", timeSeriesValuesToRemove.size());
LOG.debug("Очистить {} точек временного ряда", timeSeriesValuesToRemove.size());
timeSeriesValueRepository.deleteAll(timeSeriesValuesToRemove);
sendToTimeSeriesService(savedTimeSeries);
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));
}
/**
* Сохранить список временных рядов
*
@ -91,7 +100,7 @@ public class TimeSeriesService {
new Thread(() -> {
try {
httpService.post(TIME_SERIES_SAVE_SERVICE_URL, new JSONObject(new JsonTimeSeries(timeSeries)));
LOG.debug("Успешно отправлен на сервис");
LOG.debug("{} успешно отправлен на сервис временных рядов", timeSeries.getName());
} catch (Exception ex) {
LOG.debug(ex.getMessage());
}
@ -104,7 +113,7 @@ public class TimeSeriesService {
public TimeSeries getById(Integer tsId) {
return timeSeriesRepository.findById(tsId)
.orElseThrow(() -> new RuntimeException("Time series not found by id " + tsId));
.orElseThrow(() -> new RuntimeException("Временной ряд не найден по идентификатору " + tsId));
}
public List<TimeSeriesType> getAllTimeSeriesTypes() {

View File

@ -9,6 +9,7 @@ import java.nio.file.FileSystems;
public class StringUtils {
public final static String EMPTY_STRING = "";
public final static String SERVER_DOMAIN_PATTERN = "%s";
private final static String PATH_SEPARATOR = FileSystems.getDefault().getSeparator();
public static String addPathSeparator(String path) {
@ -25,4 +26,9 @@ public class StringUtils {
public static boolean fileInSubdirectory(String filePath, String projectPath, String subDirectory) {
return filePath.startsWith(projectPath + PATH_SEPARATOR + subDirectory);
}
public static String getServerDomain(String url) {
String[] urlParts = url.split("/");
return urlParts[2];
}
}