Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories(basePackages = {"io.pakland.mdas.githubstats.domain.repository", "io.pakland.mdas.githubstats.infrastructure.postgres.repository"})
public class GithubStatsApplication {
public static void main(String[] args) {
SpringApplication.run(GithubStatsApplication.class, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.pakland.mdas.githubstats.application;

import io.pakland.mdas.githubstats.domain.repository.MetricRepository;
import io.pakland.mdas.githubstats.infrastructure.postgres.model.PostgresOptionRequest;
import io.pakland.mdas.githubstats.infrastructure.shell.model.ShellRequest;
import org.springframework.stereotype.Service;

@Service
public class CheckMetricsFromUserByRange {
private final MetricRepository metricRepository;

protected CheckMetricsFromUserByRange(MetricRepository metricRepository) {
this.metricRepository = metricRepository;
}

public Boolean execute(PostgresOptionRequest req) {
return metricRepository.countAggregatesFromOption(req).longValueExact() > 0;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.pakland.mdas.githubstats.application;

import io.pakland.mdas.githubstats.domain.Metric;
import io.pakland.mdas.githubstats.domain.repository.MetricRepository;
import io.pakland.mdas.githubstats.infrastructure.postgres.model.PostgresOptionRequest;
import io.pakland.mdas.githubstats.infrastructure.shell.model.ShellRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GetMetricsFromUserByRange {

private final MetricRepository metricRepository;

public GetMetricsFromUserByRange(MetricRepository metricRepository) {
this.metricRepository = metricRepository;
}

public List<Metric> execute(PostgresOptionRequest request) {
return metricRepository.getAggregatesFromOption(request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.pakland.mdas.githubstats.application;

import io.pakland.mdas.githubstats.domain.Metric;
import io.pakland.mdas.githubstats.domain.repository.MetricRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class SaveMetrics {

private final MetricRepository metricRepository;

public SaveMetrics(MetricRepository metricRepository) {
this.metricRepository = metricRepository;
}

@Transactional
public void execute(List<Metric> metrics) {
List<Metric> persistedMetrics = new ArrayList<>();

metrics.forEach(metric-> {
Optional<Metric> alreadyStoredMetric = metricRepository.getMetricUsingUniqueConstraint(metric);
alreadyStoredMetric.ifPresent(persistedMetrics::add);
});

metricRepository.deleteAll(persistedMetrics);
metricRepository.saveAll(metrics);
}
}
4 changes: 2 additions & 2 deletions src/main/java/io/pakland/mdas/githubstats/domain/Metric.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class Metric {
private Integer linesRemoved;

@Column(
name = "to",
name = "\"to\"",
columnDefinition = "date",
nullable = false
)
Expand All @@ -70,7 +70,7 @@ public class Metric {
private YearMonth to;

@Column(
name = "from",
name = "\"from\"",
columnDefinition = "date",
nullable = false
)
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/io/pakland/mdas/githubstats/domain/OptionType.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package io.pakland.mdas.githubstats.domain;

public enum OptionType {
ORGANIZATION,
TEAM,
USER
ORGANIZATION("organization"),
TEAM("team_slug"),
USER("user_name");

private final String options;
OptionType(String options) {
this.options = options;
}
public String getOption() {return options;}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import org.springframework.stereotype.Repository;

@Repository
public interface MetricRepository extends JpaRepository<Metric, Integer> {
}
public interface MetricRepository extends JpaRepository<Metric, Integer>, MetricRepositoryCustom {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.pakland.mdas.githubstats.domain.repository;

import io.pakland.mdas.githubstats.domain.Metric;
import io.pakland.mdas.githubstats.infrastructure.postgres.model.PostgresOptionRequest;
import io.pakland.mdas.githubstats.infrastructure.shell.model.ShellRequest;

import java.math.BigInteger;
import java.util.List;
import java.util.Optional;

public interface MetricRepositoryCustom {
Optional<Metric> getMetricUsingUniqueConstraint(Metric metric);

BigInteger countAggregatesFromOption(PostgresOptionRequest req);

List<Metric> getAggregatesFromOption(PostgresOptionRequest req);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.pakland.mdas.githubstats.domain.repository.impl;

import io.pakland.mdas.githubstats.domain.Metric;
import io.pakland.mdas.githubstats.domain.repository.MetricRepositoryCustom;
import io.pakland.mdas.githubstats.infrastructure.postgres.model.PostgresOptionRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.math.BigInteger;
import java.util.List;
import java.util.Optional;

@Transactional(readOnly=true)
public class MetricRepositoryCustomImpl implements MetricRepositoryCustom {

@PersistenceContext
EntityManager entityManager;

@Override
@SuppressWarnings("unchecked")
public Optional<Metric> getMetricUsingUniqueConstraint(Metric metric) {
return (Optional<Metric>) entityManager.createNativeQuery("""
SELECT *
FROM metric
WHERE organization = :organization
AND team_slug = :team_slug
AND user_name = :user_name
AND "from" = to_date(:from, 'YYYY-MM')
AND "to" = to_date(:to, 'YYYY-MM')
""", Metric.class)
.setParameter("organization", metric.getOrganization() )
.setParameter("team_slug", metric.getTeamSlug() )
.setParameter("user_name", metric.getUserName() )
.setParameter("from", metric.getFrom().toString() )
.setParameter("to", metric.getTo().toString() )
.getResultStream().findFirst();
}

@Override
public BigInteger countAggregatesFromOption(PostgresOptionRequest req) {

return (BigInteger) entityManager.createNativeQuery(String.format("""
SELECT COUNT(*)
FROM metric
WHERE %s = :name
AND "from" = to_date(:from, 'YYYY-MM')
AND "to" = to_date(:to, 'YYYY-MM')
""",req.getType().getOption()))
.setParameter("name", req.getName() )
.setParameter("from", req.getFrom().toString() )
.setParameter("to", req.getTo().toString() )
.getSingleResult();
}

@Override
@SuppressWarnings("unchecked")
public List<Metric> getAggregatesFromOption(PostgresOptionRequest req) {

return (List<Metric>) entityManager.createNativeQuery(String.format("""
SELECT *
FROM metric
WHERE %s = :name
AND "from" = to_date(:from, 'YYYY-MM')
AND "to" = to_date(:to, 'YYYY-MM')
""", req.getType().getOption()), Metric.class)
.setParameter("name", req.getName() )
.setParameter("from", req.getFrom().toString() )
.setParameter("to", req.getTo().toString() )
.getResultList();
}

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package io.pakland.mdas.githubstats.infrastructure.controller;

import io.pakland.mdas.githubstats.infrastructure.github.controller.GitHubMiddleware;
import io.pakland.mdas.githubstats.infrastructure.postgres.PostgresMiddleware;
import io.pakland.mdas.githubstats.infrastructure.postgres.controller.PostgresMiddleware;
import io.pakland.mdas.githubstats.infrastructure.shell.model.ShellRequest;
import org.springframework.stereotype.Controller;

@Controller
public class MainController {

private final Middleware middleware;

public MainController(ShellRequest request) {
middleware = Middleware.link(
new PostgresMiddleware(request),
new GitHubMiddleware(request)
);
public MainController(PostgresMiddleware postgresMiddleware, GitHubMiddleware gitHubMiddleware) {
middleware = Middleware.link( postgresMiddleware, gitHubMiddleware);
}

public String execute() {
return middleware.execute();
public void execute(ShellRequest request) {
middleware.execute(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@

public abstract class Middleware {

protected final ShellRequest request;

private Middleware next;

protected Middleware(ShellRequest request) {
this.request = request;
}

public static Middleware link(Middleware first, Middleware... chain) {
Middleware head = first;
for (Middleware nextInChain: chain) {
Expand All @@ -21,12 +15,13 @@ public static Middleware link(Middleware first, Middleware... chain) {
return first;
}

public abstract String execute();
public abstract String execute(ShellRequest request);

protected void checkNext(ShellRequest request) {

protected String checkNext() {
if (next == null) {
return "unknow middleware";
return;
}
return next.execute();
next.execute(request);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@NoArgsConstructor
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;

@Controller
public class GitHubController {

private final SaveMetrics saveMetrics;

private GitHubOptionRequest userOptionRequest;
private OrganizationExternalRepository organizationRepository;
private TeamExternalRepository teamRepository;
Expand All @@ -26,9 +29,13 @@ public class GitHubController {
private ReviewExternalRepository reviewRepository;
private CommentExternalRepository commentRepository;

public GitHubController(GitHubOptionRequest userOptionRequest) {
public GitHubController(SaveMetrics saveMetrics) {
this.saveMetrics = saveMetrics;
}

private void githubInit(GitHubOptionRequest userOptionRequest) {
WebClientConfiguration webClientConfiguration = new WebClientConfiguration(
"https://api.github.com", userOptionRequest.getApiKey());
"https://api.github.com", userOptionRequest.getApiKey());

this.userOptionRequest = userOptionRequest;
this.organizationRepository = new OrganizationGitHubRepository(webClientConfiguration);
Expand All @@ -50,7 +57,9 @@ private void logIfNotSilenced(String text) {
}
}

public void execute() {
public void execute(GitHubOptionRequest userOptionRequest) {
githubInit(userOptionRequest);

logIfNotSilenced("- Fetching organizations");

try {
Expand All @@ -69,6 +78,9 @@ public void execute() {
this.fetchTeamsFromOrganization(organization));
});

logIfNotSilenced("- Saving data to database...");
saveMetrics.execute(resultMetrics);

logIfNotSilenced("- Preparing CSV...");
// Finally export the results
new ExportMetricsToFile(new GitHubMetricCsvExporter())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@
import io.pakland.mdas.githubstats.infrastructure.controller.Middleware;
import io.pakland.mdas.githubstats.infrastructure.github.model.GitHubOptionRequest;
import io.pakland.mdas.githubstats.infrastructure.shell.model.ShellRequest;
import org.springframework.stereotype.Controller;

@Controller
public class GitHubMiddleware extends Middleware {

public GitHubMiddleware(ShellRequest request) {
super(request);
private final GitHubController gitHubController;

public GitHubMiddleware(GitHubController gitHubController) {
this.gitHubController = gitHubController;
}

@Override
public String execute() {
GitHubController gitHubController = new GitHubController(
GitHubOptionRequest.builder()
.name(super.request.getName())
.apiKey(super.request.getApiKey())
.type(super.request.getEntityType())
.from(super.request.transformYearMonthToDate(request.getDateFrom()))
.to(super.request.transformYearMonthToDate(request.getDateTo()))
.filePath(super.request.getFilePath())
.silenced(super.request.isSilence())
.build());
gitHubController.execute();
public String execute(ShellRequest request) {
gitHubController.execute(GitHubOptionRequest.builder()
.name(request.getName())
.apiKey(request.getApiKey())
.type(request.getOptionType())
.filePath(request.getFilePath())
.silenced(request.isSilence())
.from(request.transformYearMonthToDate(request.getDateFrom()))
.to(request.transformYearMonthToDate(request.getDateTo()))
.build());

return String.format("- Generated results in %s", super.request.getFilePath());
return String.format("- Generated results in %s", request.getFilePath());
}
}
Loading