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
9 changed files with 170 additions and 21 deletions
Showing only changes of commit 3de664abb1 - Show all commits

View File

@ -0,0 +1,61 @@
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 = "%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";
public GithubApi(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]);
}
@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))
.length();
}
}

View File

@ -0,0 +1,61 @@
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 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";
public GitlabApi(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]);
}
@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))
.length();
}
}

View File

@ -58,7 +58,7 @@ public class HttpService {
return jsonArray; return jsonArray;
} }
} catch (Exception e) { } catch (Exception e) {
return new JSONArray("[{response:\"empty\"}]"); throw new RuntimeException(e);
} }
} }
} }

View File

@ -1,8 +1,8 @@
package ru.ulstu.extractor.ts.creator.scheduled; package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch; 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.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue; import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService; import ru.ulstu.extractor.ts.service.TimeSeriesService;
@ -10,11 +10,12 @@ import ru.ulstu.extractor.ts.service.TimeSeriesService;
@Component @Component
public class AuthorTS extends ScheduledTimeSeriesCreator { public class AuthorTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService; private final TimeSeriesService timeSeriesService;
private final GitApi gitApi; private final ApplicationContext applicationContext;
public AuthorTS(TimeSeriesService timeSeriesService, GitApi gitApi) { public AuthorTS(TimeSeriesService timeSeriesService,
ApplicationContext applicationContext) {
this.timeSeriesService = timeSeriesService; this.timeSeriesService = timeSeriesService;
this.gitApi = gitApi; this.applicationContext = applicationContext;
} }
@Override @Override
@ -24,7 +25,8 @@ public class AuthorTS extends ScheduledTimeSeriesCreator {
@Override @Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) { public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(gitApi.getAuthorsCount(branch)); return new TimeSeriesValue(applicationContext.getBean(getGitApiServiceClass(branch))
.getAuthorsCount(branch));
} }
@Override @Override

View File

@ -1,8 +1,8 @@
package ru.ulstu.extractor.ts.creator.scheduled; package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch; 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.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue; import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService; import ru.ulstu.extractor.ts.service.TimeSeriesService;
@ -10,12 +10,12 @@ import ru.ulstu.extractor.ts.service.TimeSeriesService;
@Component @Component
public class BranchTS extends ScheduledTimeSeriesCreator { public class BranchTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService; private final TimeSeriesService timeSeriesService;
private final GitApi gitApi; private final ApplicationContext applicationContext;
public BranchTS(TimeSeriesService timeSeriesService, public BranchTS(TimeSeriesService timeSeriesService,
GitApi gitApi) { ApplicationContext applicationContext) {
this.timeSeriesService = timeSeriesService; this.timeSeriesService = timeSeriesService;
this.gitApi = gitApi; this.applicationContext = applicationContext;
} }
@Override @Override
@ -30,6 +30,7 @@ public class BranchTS extends ScheduledTimeSeriesCreator {
@Override @Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) { public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(gitApi.getBranchesCount(branch)); return new TimeSeriesValue(applicationContext.getBean(getGitApiServiceClass(branch))
.getBranchesCount(branch));
} }
} }

View File

@ -1,8 +1,8 @@
package ru.ulstu.extractor.ts.creator.scheduled; package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch; 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.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue; import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService; import ru.ulstu.extractor.ts.service.TimeSeriesService;
@ -10,17 +10,18 @@ import ru.ulstu.extractor.ts.service.TimeSeriesService;
@Component @Component
public class IssuesTS extends ScheduledTimeSeriesCreator { public class IssuesTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService; private final TimeSeriesService timeSeriesService;
private final GitApi gitApi; private final ApplicationContext applicationContext;
public IssuesTS(TimeSeriesService timeSeriesService, public IssuesTS(TimeSeriesService timeSeriesService,
GitApi gitApi) { ApplicationContext applicationContext) {
this.timeSeriesService = timeSeriesService; this.timeSeriesService = timeSeriesService;
this.gitApi = gitApi; this.applicationContext = applicationContext;
} }
@Override @Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) { public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(gitApi.getOpenIssuesCount(branch)); return new TimeSeriesValue(applicationContext.getBean(getGitApiServiceClass(branch))
.getOpenIssuesCount(branch));
} }
@Override @Override

View File

@ -2,13 +2,24 @@ package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.extractor.branch.model.Branch; 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.creator.AbstractTimeSeriesCreator;
import ru.ulstu.extractor.ts.model.TimeSeries; import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesValue; import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.util.StringUtils;
import java.util.Collections; import java.util.Collections;
import java.util.Map;
public abstract class ScheduledTimeSeriesCreator extends AbstractTimeSeriesCreator { 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 @Transactional
public void addTimeSeriesValue(Branch branch) { public void addTimeSeriesValue(Branch branch) {
TimeSeries timeSeries = getTimeSeriesService().findByBranchAndTypeOrCreate(branch, getTimeSeriesType()); TimeSeries timeSeries = getTimeSeriesService().findByBranchAndTypeOrCreate(branch, getTimeSeriesType());
@ -17,4 +28,8 @@ public abstract class ScheduledTimeSeriesCreator extends AbstractTimeSeriesCreat
} }
public abstract TimeSeriesValue getNewTimeSeriesValue(Branch branch); public abstract TimeSeriesValue getNewTimeSeriesValue(Branch branch);
public Class<? extends GitApi> getGitApiServiceClass(Branch branch) {
return gitApiServiceHolder.get(StringUtils.getServerDomain(branch.getGitRepository().getUrl()));
}
} }

View File

@ -1,8 +1,8 @@
package ru.ulstu.extractor.ts.creator.scheduled; package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch; 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.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue; import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService; import ru.ulstu.extractor.ts.service.TimeSeriesService;
@ -10,11 +10,12 @@ import ru.ulstu.extractor.ts.service.TimeSeriesService;
@Component @Component
public class StarTS extends ScheduledTimeSeriesCreator { public class StarTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService; private final TimeSeriesService timeSeriesService;
private final GitApi gitApi; private final ApplicationContext applicationContext;
public StarTS(TimeSeriesService timeSeriesService, GitApi gitApi) { public StarTS(TimeSeriesService timeSeriesService,
ApplicationContext applicationContext) {
this.timeSeriesService = timeSeriesService; this.timeSeriesService = timeSeriesService;
this.gitApi = gitApi; this.applicationContext = applicationContext;
} }
@Override @Override
@ -29,6 +30,7 @@ public class StarTS extends ScheduledTimeSeriesCreator {
@Override @Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) { public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(gitApi.getStarsCount(branch)); return new TimeSeriesValue(applicationContext.getBean(getGitApiServiceClass(branch))
.getStarsCount(branch));
} }
} }

View File

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