Skip to content

Commit 56d5824

Browse files
✨ Add Stats ressources
1 parent 8cb36dc commit 56d5824

7 files changed

Lines changed: 79 additions & 24 deletions

File tree

src/main/java/zenika/oss/stats/mapper/StatsMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static List<StatsContribution> mapGithubStatisticsToStatsContribution(Str
1515
return statsContributions;
1616
}
1717

18-
private static StatsContribution mapGithubStatisticToStatsContribution(String githubMember, int year, CustomStatsContributionsUserByMonth customStatsContributionsUserByMonth) {
18+
public static StatsContribution mapGithubStatisticToStatsContribution(String githubMember, int year, CustomStatsContributionsUserByMonth customStatsContributionsUserByMonth) {
1919
StatsContribution statsContribution = new StatsContribution();
2020

2121
statsContribution.setYear(String.valueOf(year));

src/main/java/zenika/oss/stats/ressources/ContributionsRessources.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import jakarta.ws.rs.GET;
55
import jakarta.ws.rs.Path;
66
import jakarta.ws.rs.PathParam;
7+
import jakarta.ws.rs.QueryParam;
78
import jakarta.ws.rs.core.Response;
89
import zenika.oss.stats.exception.DatabaseException;
910
import zenika.oss.stats.services.FirestoreServices;
@@ -31,10 +32,8 @@ public Response getContributionsByMember(@PathParam("memberId") String memberId)
3132
}
3233

3334
@GET
34-
@Path("/contributions/month/{month}")
35-
public Response getContributionsForAMonth(@PathParam("month") int month) {
36-
37-
return Response.ok()
38-
.build();
35+
@Path("/")
36+
public Response getContributionsForAMonth(@QueryParam("year") int year, @QueryParam("month") String month) throws DatabaseException {
37+
return Response.ok(firestoreServices.getContributionsForAYearAndMonthOrderByMonth(year, month)).build();
3938
}
4039
}

src/main/java/zenika/oss/stats/ressources/workflow/WorkflowRessources.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import jakarta.ws.rs.Produces;
99
import jakarta.ws.rs.core.MediaType;
1010
import jakarta.ws.rs.core.Response;
11-
import zenika.oss.stats.beans.ZenikaMember;
1211
import zenika.oss.stats.beans.CustomStatsContributionsUserByMonth;
1312
import zenika.oss.stats.beans.ZenikaMember;
1413
import zenika.oss.stats.beans.gcp.StatsContribution;
@@ -84,11 +83,11 @@ public Response saveStatsForYear(@PathParam("year") int year) throws DatabaseExc
8483

8584
for (ZenikaMember zenikaMember : zMembers) {
8685
if (zenikaMember.getGitHubAccount() != null) {
87-
System.out.println("\uD83D\uDD0E Check information for " + zenikaMember.getGitHubAccount().getLogin());
86+
System.out.print("\uD83D\uDD0E Check information for " + zenikaMember.getGitHubAccount().getLogin());
8887
stats.addAll(gitHubServices.getContributionsForTheCurrentYear(zenikaMember.getGitHubAccount().getLogin(), year));
8988
List<StatsContribution> statsMap = StatsMapper.mapGithubStatisticsToStatsContribution(zenikaMember.getGitHubAccount().getLogin(), year, stats);
89+
System.out.println("... ✅");
9090

91-
System.out.println("\uD83D\uDCBD Save information for " + zenikaMember.getGitHubAccount().getLogin());
9291
if (!statsMap.isEmpty()) {
9392
for (StatsContribution stat : statsMap) {
9493
firestoreServices.saveStatsForAGitHubAccountForAYear(stat);

src/main/java/zenika/oss/stats/services/FirestoreServices.java

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class FirestoreServices {
2424

2525
/**
2626
* Create one member in the databse.
27+
*
2728
* @param zMember the member to create.
2829
*/
2930
public void createMember(ZenikaMember zMember) {
@@ -74,6 +75,7 @@ public void saveStatsForAGitHubAccountForAYear(StatsContribution statsContributi
7475

7576
/**
7677
* Delete all stats for the year in parameter.
78+
*
7779
* @param year : the year that we want to remove stats
7880
* @throws DatabaseException exception
7981
*/
@@ -93,6 +95,7 @@ public void deleteStatsForAllGitHubAccountForAYear(int year) throws DatabaseExce
9395

9496
/**
9597
* Create a project in the Firestore database.
98+
*
9699
* @param project the project to create.
97100
*/
98101
public void createProject(GitHubProject project) {
@@ -101,6 +104,7 @@ public void createProject(GitHubProject project) {
101104

102105
/**
103106
* Retrieve all projects from the Firestore database.
107+
*
104108
* @return a list of all projects.
105109
*/
106110
public List<GitHubProject> getAllProjects() throws DatabaseException {
@@ -115,6 +119,7 @@ public List<GitHubProject> getAllProjects() throws DatabaseException {
115119

116120
/**
117121
* Remove all projects from the Firestore database.
122+
*
118123
* @throws DatabaseException exception
119124
*/
120125
public void deleteAllProjects() throws DatabaseException {
@@ -133,10 +138,11 @@ public void deleteAllMembers() throws DatabaseException {
133138

134139
/**
135140
* Create a document in the Firestore database.
136-
* @param document the document to create.
141+
*
142+
* @param document the document to create.
137143
* @param collectionPath the path of the collection in which to create the document.
138-
* @param documentId the id of the document to create.
139-
* @param <T> the type of the document to create.
144+
* @param documentId the id of the document to create.
145+
* @param <T> the type of the document to create.
140146
*/
141147
public <T> void createDocument(T document, String collectionPath, String documentId) {
142148
List<ApiFuture<WriteResult>> futures = new ArrayList<>();
@@ -145,8 +151,9 @@ public <T> void createDocument(T document, String collectionPath, String documen
145151

146152
/**
147153
* Delete all documents in a specific Firestore collection.
154+
*
148155
* @param collectionType the type of collection to delete.
149-
* @param <T> the type of document
156+
* @param <T> the type of document
150157
* @throws DatabaseException exception
151158
*/
152159
public <T> void deleteAllDocuments(FirestoreCollections collectionType) throws DatabaseException {
@@ -169,14 +176,31 @@ public List<StatsContribution> getContributionsForAMemberOrderByYear(String memb
169176
ApiFuture<QuerySnapshot> querySnapshot = query.get();
170177
try {
171178
stats = querySnapshot.get().getDocuments().stream()
172-
.map(document -> document.toObject(StatsContribution.class))
173-
.sorted((s1, s2) -> s2.getYear().compareTo(s1.getYear()))
174-
.sorted((s1, s2) -> s2.getMonth().compareTo(s1.getMonth()))
175-
.collect(java.util.stream.Collectors.toList());
176-
} catch (InterruptedException | ExecutionException exception) {
177-
throw new DatabaseException(exception);
178-
}
179+
.map(document -> document.toObject(StatsContribution.class))
180+
.sorted((s1, s2) -> s2.getYear().compareTo(s1.getYear()))
181+
.sorted((s1, s2) -> s2.getMonth().compareTo(s1.getMonth()))
182+
.collect(java.util.stream.Collectors.toList());
183+
} catch (InterruptedException | ExecutionException exception) {
184+
throw new DatabaseException(exception);
185+
}
186+
187+
return stats;
188+
}
189+
190+
public List<StatsContribution> getContributionsForAYearAndMonthOrderByMonth(int year, String month) throws DatabaseException {
191+
List<StatsContribution> stats = null;
192+
CollectionReference zStats = firestore.collection(FirestoreCollections.STATS.value);
193+
Query query = zStats.whereEqualTo("year", String.valueOf(year)).whereEqualTo("month", month);
194+
ApiFuture<QuerySnapshot> querySnapshot = query.get();
195+
try {
196+
stats = querySnapshot.get().getDocuments().stream()
197+
.map(document -> document.toObject(StatsContribution.class))
198+
.sorted((s1, s2) -> s2.getMonth().compareTo(s1.getMonth()))
199+
.collect(java.util.stream.Collectors.toList());
200+
} catch (InterruptedException | ExecutionException exception) {
201+
throw new DatabaseException(exception);
202+
}
179203

180-
return stats;
204+
return stats;
181205
}
182206
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package zenika.oss.stats.mapper;
2+
3+
import org.junit.jupiter.api.Test;
4+
import zenika.oss.stats.beans.CustomStatsContributionsUserByMonth;
5+
import zenika.oss.stats.beans.gcp.StatsContribution;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class StatsMapperTest {
10+
11+
@Test
12+
void shouldMapGithubStatisticToStatsContribution() {
13+
// Given
14+
String githubMember = "test-dev";
15+
int year = 2023;
16+
CustomStatsContributionsUserByMonth monthlyStats = new CustomStatsContributionsUserByMonth(10, "October", 150);
17+
18+
// When
19+
StatsContribution result = StatsMapper.mapGithubStatisticToStatsContribution(githubMember, year, monthlyStats);
20+
21+
// Then
22+
assertNotNull(result);
23+
assertEquals(result.getYear(),"2023");
24+
assertEquals(result.getMonth(), "October");
25+
assertEquals(result.getNumberOfContributionsOnGitHub(),150);
26+
assertEquals(result.getGithubHandle(), "test-dev");
27+
assertNull(result.getGitlabHandle());
28+
assertNull(result.getIdZenikaMember());
29+
assertEquals(result.getNumberOfContributionsOnGitLab(), 0);
30+
}
31+
}

src/test/java/zenika/oss/stats/ressources/ContributionsRessourcesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void test_getContributionsByMember() {
2323
void test_getContributionsForAMonth() {
2424

2525
given().when()
26-
.get("/v1/contributions/contributions/month/1")
26+
.get("/v1/contributions?year=2025&month=1")
2727
.then()
2828
.statusCode(200);
2929

src/test/java/zenika/oss/stats/ressources/workflow/WorkflowRessourcesTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import org.junit.jupiter.api.Test;
77
import org.mockito.Mockito;
88
import zenika.oss.stats.beans.github.GitHubMember;
9+
import zenika.oss.stats.mapper.StatsMapper;
910
import zenika.oss.stats.services.FirestoreServices;
1011
import zenika.oss.stats.services.GitHubServices;
1112

12-
import java.util.Arrays;
1313
import java.util.List;
1414

1515
import static io.restassured.RestAssured.given;
@@ -20,14 +20,16 @@ public class WorkflowRessourcesTest {
2020
@InjectMock
2121
GitHubServices gitHubServices;
2222

23+
@InjectMock
24+
FirestoreServices firestoreServices;
25+
2326
@BeforeEach
2427
public void setup() {
2528
GitHubMember member = new GitHubMember();
2629
member.id = "test";
2730
member.login = "login-test";
2831

2932
List<GitHubMember> members = List.of(member);
30-
3133
Mockito.when(gitHubServices.getZenikaOpenSourceMembers()).thenReturn(members);
3234
}
3335

0 commit comments

Comments
 (0)