#89 -- Fix scheduled time series

pull/90/head
Anton Romanov 1 year ago
parent 3de664abb1
commit 78016741cf

@ -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"));
}
}

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

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

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

@ -1,5 +1,6 @@
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;
@ -7,11 +8,11 @@ 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/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 AUTHOR_COMPLETED_ISSUES_URL = "%s/api/v1/repos/%s/%s/issues?state=open";
private static final String AUTHORS_COUNT_URL = "%s/api/v1/repos/%s/%s/branches";
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;
@ -20,42 +21,51 @@ public class GitlabApi implements GitApi {
@Override
public Integer getBranchesCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), BRANCHES_COUNT_URL))
.get(getFormattedUrl(branch, BRANCHES_COUNT_URL))
.length();
}
@Override
public Integer getStarsCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), STARS_COUNT_URL))
.get(getFormattedUrl(branch, PROJECT_INFO_URL))
.getJSONObject(0)
.getInt("stars_count");
.getInt("star_count");
}
@Override
public Integer getOpenIssuesCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), OPEN_ISSUES_URL))
.get(getFormattedUrl(branch, 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]);
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.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();
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());
}
}

@ -9,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
@ -49,6 +50,7 @@ public class HttpService {
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(String.class)
.timeout(Duration.ofMinutes(1))
.toFuture().get();
if (response.startsWith("[")) {
return new JSONArray(response);

@ -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;
}
}
Loading…
Cancel
Save