89-time-series-by-cron #90

Merged
romanov73 merged 5 commits from 89-time-series-by-cron into master 2023-02-20 10:16:33 +04:00
7 changed files with 56 additions and 94 deletions
Showing only changes of commit 78016741cf - Show all commits

View File

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

View File

@ -9,9 +9,7 @@ public interface GitApi {
Integer getOpenIssuesCount(Branch branch); Integer getOpenIssuesCount(Branch branch);
String getFormattedUrl(String gitRepositoryUrl, String template); String getFormattedUrl(Branch branch, String template);
Integer getAuthorsCompletedIssues(Branch branch); Integer getAuthorsCompletedIssues(Branch branch);
Integer getAuthorsCount(Branch branch);
} }

View File

@ -10,8 +10,7 @@ public class GitAtheneApi implements GitApi {
private static final String BRANCHES_COUNT_URL = "%s/api/v1/repos/%s/%s/branches"; 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 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 OPEN_ISSUES_URL = "%s/api/v1/repos/%s/%s/issues?state=open";
private static final String AUTHOR_COMPLETED_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";
private static final String AUTHORS_COUNT_URL = "%s/api/v1/repos/%s/%s/branches";
public GitAtheneApi(HttpService httpService) { public GitAtheneApi(HttpService httpService) {
this.httpService = httpService; this.httpService = httpService;
@ -20,14 +19,14 @@ public class GitAtheneApi implements GitApi {
@Override @Override
public Integer getBranchesCount(Branch branch) { public Integer getBranchesCount(Branch branch) {
return httpService return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), BRANCHES_COUNT_URL)) .get(getFormattedUrl(branch, BRANCHES_COUNT_URL))
.length(); .length();
} }
@Override @Override
public Integer getStarsCount(Branch branch) { public Integer getStarsCount(Branch branch) {
return httpService return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), STARS_COUNT_URL)) .get(getFormattedUrl(branch, STARS_COUNT_URL))
.getJSONObject(0) .getJSONObject(0)
.getInt("stars_count"); .getInt("stars_count");
} }
@ -35,27 +34,20 @@ public class GitAtheneApi implements GitApi {
@Override @Override
public Integer getOpenIssuesCount(Branch branch) { public Integer getOpenIssuesCount(Branch branch) {
return httpService return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), OPEN_ISSUES_URL)) .get(getFormattedUrl(branch, OPEN_ISSUES_URL))
.length(); .length();
} }
@Override @Override
public String getFormattedUrl(String gitRepositoryUrl, String template) { public String getFormattedUrl(Branch branch, String template) {
String[] urlParts = gitRepositoryUrl.split("/"); String[] urlParts = branch.getGitRepository().getUrl().split("/");
return String.format(template, urlParts[0] + "/" + urlParts[1] + "/" + urlParts[2], urlParts[3], urlParts[4]); return String.format(template, urlParts[0] + "/" + urlParts[1] + "/" + urlParts[2], urlParts[3], urlParts[4]);
} }
@Override @Override
public Integer getAuthorsCompletedIssues(Branch branch) { public Integer getAuthorsCompletedIssues(Branch branch) {
return httpService return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), AUTHOR_COMPLETED_ISSUES_URL)) .get(getFormattedUrl(branch, COMPLETED_ISSUES_URL))
.length();
}
@Override
public Integer getAuthorsCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), AUTHORS_COUNT_URL))
.length(); .length();
} }
} }

View File

@ -7,11 +7,10 @@ import ru.ulstu.extractor.http.HttpService;
@Service @Service
public class GithubApi implements GitApi { public class GithubApi implements GitApi {
private final HttpService httpService; private final HttpService httpService;
private static final String BRANCHES_COUNT_URL = "%s/api/v1/repos/%s/%s/branches"; private static final String BRANCHES_COUNT_URL = "https://api.github.com/repos/%s/%s/branches";
private static final String STARS_COUNT_URL = "%s/api/v1/repos/%s/%s"; private static final String STARS_COUNT_URL = "https://api.github.com/repos/%s/%s";
private static final String OPEN_ISSUES_URL = "%s/api/v1/repos/%s/%s/issues?state=open"; 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 = "%s/api/v1/repos/%s/%s/issues?state=open"; private static final String AUTHOR_COMPLETED_ISSUES_URL = "https://api.github.com/repos/%s/%s/issues?state=open";
private static final String AUTHORS_COUNT_URL = "%s/api/v1/repos/%s/%s/branches";
public GithubApi(HttpService httpService) { public GithubApi(HttpService httpService) {
this.httpService = httpService; this.httpService = httpService;
@ -20,42 +19,35 @@ public class GithubApi implements GitApi {
@Override @Override
public Integer getBranchesCount(Branch branch) { public Integer getBranchesCount(Branch branch) {
return httpService return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), BRANCHES_COUNT_URL)) .get(getFormattedUrl(branch, BRANCHES_COUNT_URL))
.length(); .length();
} }
@Override @Override
public Integer getStarsCount(Branch branch) { public Integer getStarsCount(Branch branch) {
return httpService return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), STARS_COUNT_URL)) .get(getFormattedUrl(branch, STARS_COUNT_URL))
.getJSONObject(0) .getJSONObject(0)
.getInt("stars_count"); .getInt("watchers");
} }
@Override @Override
public Integer getOpenIssuesCount(Branch branch) { public Integer getOpenIssuesCount(Branch branch) {
return httpService return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), OPEN_ISSUES_URL)) .get(getFormattedUrl(branch, OPEN_ISSUES_URL))
.length(); .length();
} }
@Override @Override
public String getFormattedUrl(String gitRepositoryUrl, String template) { public String getFormattedUrl(Branch branch, String template) {
String[] urlParts = gitRepositoryUrl.split("/"); String[] urlParts = branch.getGitRepository().getUrl().split("/");
return String.format(template, urlParts[0] + "/" + urlParts[1] + "/" + urlParts[2], urlParts[3], urlParts[4]); return String.format(template, urlParts[3], urlParts[4]);
} }
@Override @Override
public Integer getAuthorsCompletedIssues(Branch branch) { public Integer getAuthorsCompletedIssues(Branch branch) {
return httpService return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), AUTHOR_COMPLETED_ISSUES_URL)) .get(getFormattedUrl(branch, AUTHOR_COMPLETED_ISSUES_URL))
.length();
}
@Override
public Integer getAuthorsCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), AUTHORS_COUNT_URL))
.length(); .length();
} }
} }

View File

@ -1,5 +1,6 @@
package ru.ulstu.extractor.gitrepository.service; package ru.ulstu.extractor.gitrepository.service;
import org.json.JSONArray;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.extractor.branch.model.Branch; import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.http.HttpService; import ru.ulstu.extractor.http.HttpService;
@ -7,11 +8,11 @@ import ru.ulstu.extractor.http.HttpService;
@Service @Service
public class GitlabApi implements GitApi { public class GitlabApi implements GitApi {
private final HttpService httpService; private final HttpService httpService;
private static final String BRANCHES_COUNT_URL = "%s/api/v1/repos/%s/%s/branches"; private static final String BRANCHES_COUNT_URL = "%s/api/v4/projects/%s/repository/branches";
private static final String STARS_COUNT_URL = "%s/api/v1/repos/%s/%s"; private final static String PROJECT_ID_URL = "https://gitlab.com/api/v4/users/%s/projects?search=%s";
private static final String OPEN_ISSUES_URL = "%s/api/v1/repos/%s/%s/issues?state=open"; private static final String PROJECT_INFO_URL = "%s/api/v4/projects/%s";
private static final String AUTHOR_COMPLETED_ISSUES_URL = "%s/api/v1/repos/%s/%s/issues?state=open"; private static final String OPEN_ISSUES_URL = "%s/api/v4/projects/%s/issues?state=opened";
private static final String AUTHORS_COUNT_URL = "%s/api/v1/repos/%s/%s/branches"; private static final String COMPLETED_ISSUES_URL = "%s/api/v4/projects/%s/issues?state=closed";
public GitlabApi(HttpService httpService) { public GitlabApi(HttpService httpService) {
this.httpService = httpService; this.httpService = httpService;
@ -20,42 +21,51 @@ public class GitlabApi implements GitApi {
@Override @Override
public Integer getBranchesCount(Branch branch) { public Integer getBranchesCount(Branch branch) {
return httpService return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), BRANCHES_COUNT_URL)) .get(getFormattedUrl(branch, BRANCHES_COUNT_URL))
.length(); .length();
} }
@Override @Override
public Integer getStarsCount(Branch branch) { public Integer getStarsCount(Branch branch) {
return httpService return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), STARS_COUNT_URL)) .get(getFormattedUrl(branch, PROJECT_INFO_URL))
.getJSONObject(0) .getJSONObject(0)
.getInt("stars_count"); .getInt("star_count");
} }
@Override @Override
public Integer getOpenIssuesCount(Branch branch) { public Integer getOpenIssuesCount(Branch branch) {
return httpService return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), OPEN_ISSUES_URL)) .get(getFormattedUrl(branch, OPEN_ISSUES_URL))
.length(); .length();
} }
@Override @Override
public String getFormattedUrl(String gitRepositoryUrl, String template) { public String getFormattedUrl(Branch branch, String template) {
String[] urlParts = gitRepositoryUrl.split("/"); String[] urlParts = branch.getGitRepository().getUrl().split("/");
return String.format(template, urlParts[0] + "/" + urlParts[1] + "/" + urlParts[2], urlParts[3], urlParts[4]); 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 @Override
public Integer getAuthorsCompletedIssues(Branch branch) { public Integer getAuthorsCompletedIssues(Branch branch) {
return httpService return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), AUTHOR_COMPLETED_ISSUES_URL)) .get(getFormattedUrl(branch, COMPLETED_ISSUES_URL))
.length(); .length();
} }
@Override private String getProjectId(Branch branch) {
public Integer getAuthorsCount(Branch branch) { JSONArray projects = httpService.get(getFormattedUrlForProjectId(branch, PROJECT_ID_URL));
return httpService for (int i = 0; i < projects.length(); i++) {
.get(getFormattedUrl(branch.getGitRepository().getUrl(), AUTHORS_COUNT_URL)) if (projects.getJSONObject(i).get("name").equals(branch.getGitRepository().getName())) {
.length(); return String.valueOf(projects.getJSONObject(i).getInt("id"));
}
}
throw new RuntimeException("Id проекта не найден: " + branch.getGitRepository().getName());
} }
} }

View File

@ -9,6 +9,7 @@ import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import java.time.Duration;
import java.util.Optional; import java.util.Optional;
@Service @Service
@ -49,6 +50,7 @@ public class HttpService {
.accept(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)
.retrieve() .retrieve()
.bodyToMono(String.class) .bodyToMono(String.class)
.timeout(Duration.ofMinutes(1))
.toFuture().get(); .toFuture().get();
if (response.startsWith("[")) { if (response.startsWith("[")) {
return new JSONArray(response); return new JSONArray(response);

View File

@ -1,36 +0,0 @@
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 AuthorTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final ApplicationContext applicationContext;
public AuthorTS(TimeSeriesService timeSeriesService,
ApplicationContext applicationContext) {
this.timeSeriesService = timeSeriesService;
this.applicationContext = applicationContext;
}
@Override
public TimeSeriesType getTimeSeriesType() {
return TimeSeriesType.AUTHORS;
}
@Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(applicationContext.getBean(getGitApiServiceClass(branch))
.getAuthorsCount(branch));
}
@Override
public TimeSeriesService getTimeSeriesService() {
return timeSeriesService;
}
}