Skip to content

Commit 2ccdb36

Browse files
authored
Merge pull request #10 from mcanoy/engagement-counts
Engagement counts
2 parents 32b41d5 + fdd0497 commit 2ccdb36

File tree

10 files changed

+68
-49
lines changed

10 files changed

+68
-49
lines changed

src/main/java/com/redhat/labs/lodestar/model/Artifact.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ public static List<ArtifactCount> countArtifactsForEachRegionAndType(List<String
120120
return mongoCollection().aggregate(bson, ArtifactCount.class).into(new ArrayList<>());
121121
}
122122

123+
public static List<ArtifactCount> countArtifactsForEachEngagement() {
124+
String count = "count";
125+
List<Bson> bson = new ArrayList<>();
126+
bson.add(group("$engagementUuid", sum(count, 1)));
127+
bson.add(project(fields(include(count), computed("type", "$_id"))));
128+
bson.add(sort(orderBy(descending(count), ascending("type"))));
129+
130+
return mongoCollection().aggregate(bson, ArtifactCount.class).into(new ArrayList<>());
131+
}
132+
123133
/**
124134
* Returns {@link List} of {@link Artifact}s sorted descending on modified
125135
* timestamp using the page specified.
@@ -163,8 +173,8 @@ public static List<Artifact> pagedArtifactsByRegionAndType(String type, List<Str
163173
* @param pageSize
164174
* @return
165175
*/
166-
public static List<Artifact> pagedArtifactsByEngagementUuid(String engagementUuid, int page, int pageSize) {
167-
return find("engagementUuid", Sort.descending(MODIFIED), engagementUuid).page(page, pageSize).list();
176+
public static List<Artifact> pagedArtifactsByEngagementUuid(String engagementUuid, int page, int pageSize, Sort sort) {
177+
return find("engagementUuid", sort, engagementUuid).page(page, pageSize).list();
168178
}
169179

170180
/**

src/main/java/com/redhat/labs/lodestar/model/GetListOptions.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,24 @@ public Sort getQuerySort(Sort defaultSort) {
4646
return defaultSort;
4747
}
4848
String[] sortAll = sort.split(",");
49-
Sort sort = null;
49+
Sort querySort = null;
5050
String direction;
5151

5252
for (String s : sortAll) {
5353
String[] sortFields = s.split("\\|");
5454
direction = sortFields.length == 2 ? sortFields[1] : "";
55-
if (sort == null) {
56-
sort = Sort.by(sortFields[0], getDirection(direction));
55+
if (querySort == null) {
56+
querySort = Sort.by(sortFields[0], getDirection(direction));
5757
} else {
58-
sort.and(sortFields[0], getDirection(direction));
58+
querySort.and(sortFields[0], getDirection(direction));
5959
}
6060
}
6161

62-
sort.and("uuid");
62+
if(querySort != null) {
63+
querySort.and("uuid");
64+
}
6365

64-
return sort;
66+
return querySort;
6567
}
6668

6769
private Sort.Direction getDirection(String dir) {

src/main/java/com/redhat/labs/lodestar/model/GetOptions.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public class GetOptions {
2727

2828
@Parameter(name = "region", required = false, description = "return only artifacts for the given region. Do not use with engagementUuid")
2929
@QueryParam("region")
30-
31-
private List<String> region = new ArrayList<String>();
30+
private List<String> region = new ArrayList<>();
3231

3332
public Optional<String> getEngagementUuid() {
3433
return Optional.ofNullable(engagementUuid);
@@ -39,11 +38,7 @@ public Optional<String> getType() {
3938
}
4039

4140
public List<String> getRegion() {
42-
if(region == null) {
43-
region = new ArrayList<String>();
44-
}
45-
46-
return region;
41+
return region == null ? new ArrayList<>() : region;
4742
}
4843

4944
}

src/main/java/com/redhat/labs/lodestar/resource/ArtifactResource.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.redhat.labs.lodestar.resource;
22

3-
import java.util.List;
4-
import java.util.Optional;
5-
import java.util.Set;
6-
import java.util.TreeSet;
3+
import java.util.*;
74

85
import javax.inject.Inject;
96
import javax.validation.Valid;
@@ -49,7 +46,7 @@ public Response processEngagementArtifacts(@Valid List<Artifact> artifacts, @Pat
4946
@QueryParam("authorName") Optional<String> authorName) {
5047

5148
service.updateArtifacts(engagementUuid, region, artifacts, authorEmail, authorName);
52-
return Response.ok().build();
49+
return Response.ok(service.getArtifactsByEngagement(engagementUuid)).build();
5350

5451
}
5552

@@ -83,6 +80,12 @@ public List<ArtifactCount> countArtifactsByType(@QueryParam("regions") List<Stri
8380
return service.getArtifactTypeSummary(regions);
8481
}
8582

83+
@GET
84+
@Path("engagements/count")
85+
public Map<String, Long> getEngagementCounts() {
86+
return service.getEngagementCounts();
87+
}
88+
8689
@GET
8790
@Path("/types")
8891
public Set<String> getAllTypes(@QueryParam("regions") List<String> regions) {

src/main/java/com/redhat/labs/lodestar/rest/client/EngagementApiRestClient.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
import java.util.List;
44

5-
import javax.ws.rs.GET;
6-
import javax.ws.rs.Path;
7-
import javax.ws.rs.PathParam;
8-
import javax.ws.rs.Produces;
9-
import javax.ws.rs.QueryParam;
5+
import javax.ws.rs.*;
106

117
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
128
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
@@ -26,4 +22,8 @@ public interface EngagementApiRestClient {
2622
@GET
2723
List<Engagement> getAllEngagements();
2824

25+
@PUT
26+
@Path("{uuid}/artifacts/{count}")
27+
void updateEngagement(@PathParam("uuid") String engagementUuid, @PathParam("count") int count);
28+
2929
}

src/main/java/com/redhat/labs/lodestar/service/ArtifactService.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import java.time.LocalDateTime;
44
import java.time.ZoneId;
5-
import java.util.Arrays;
6-
import java.util.List;
7-
import java.util.Optional;
8-
import java.util.UUID;
5+
import java.util.*;
96

107
import javax.enterprise.context.ApplicationScoped;
118
import javax.inject.Inject;
@@ -154,12 +151,13 @@ public void updateArtifacts(String engagementUuid, String region, List<Artifact>
154151

155152
commitMessage.append(cbo.toString());
156153
updateArtifactsFile(engagementUuid, authorEmail, authorName, Optional.ofNullable(commitMessage.toString()));
157-
154+
if(existing.size() != requestArtifacts.size()) {
155+
engagementRestClient.updateEngagement(engagementUuid, requestArtifacts.size());
156+
}
158157
});
159158
}
160159

161160
}
162-
163161

164162
/**
165163
* Returns a {@link List} of {@link Artifact}s matching the specified
@@ -171,7 +169,7 @@ public void updateArtifacts(String engagementUuid, String region, List<Artifact>
171169
public List<Artifact> getArtifacts(GetListOptions options) {
172170

173171
if(!options.getRegion().isEmpty() && options.getType().isPresent()) { //by region and type
174-
return Artifact.pagedArtifactsByRegionAndType(options.getType().get(), options.getRegion(), options.getPage(),
172+
return Artifact.pagedArtifactsByRegionAndType(options.getType().orElse(""), options.getRegion(), options.getPage(),
175173
options.getPageSize(), options.getQuerySort());
176174
}
177175

@@ -181,18 +179,22 @@ public List<Artifact> getArtifacts(GetListOptions options) {
181179

182180
if(options.getType().isPresent()) { //by type
183181
checkEngagementUuid(options.getEngagementUuid());
184-
return Artifact.pagedArtifactsByType(options.getType().get(), options.getPage(), options.getPageSize(), options.getQuerySort());
182+
return Artifact.pagedArtifactsByType(options.getType().orElse(""), options.getPage(), options.getPageSize(), options.getQuerySort());
185183
}
186184

187185
Optional<String> engagementUuid = options.getEngagementUuid();
188186

189187
return engagementUuid.isPresent()
190188
? Artifact.pagedArtifactsByEngagementUuid(engagementUuid.get(), options.getPage(),
191-
options.getPageSize()) //by uuid
189+
options.getPageSize(), options.getQuerySort(Sort.descending("modified"))) //by uuid
192190
: Artifact.pagedArtifacts(options.getPage(), options.getPageSize(), options.getQuerySort(Sort.descending("modified").and("engagementUuid"))); //all
193191

194192
}
195193

194+
public List<Artifact> getArtifactsByEngagement(String engagementUuid) {
195+
return Artifact.pagedArtifactsByEngagementUuid(engagementUuid, 0, 1000, Sort.descending("modified"));
196+
}
197+
196198
public List<ArtifactCount> getArtifactTypeSummary(List<String> regions) {
197199
return Artifact.countArtifactsForEachRegionAndType(regions);
198200
}
@@ -212,17 +214,18 @@ private void checkEngagementUuid(Optional<String> engagementUuid) {
212214
* @return
213215
*/
214216
public ArtifactCount countArtifacts(GetOptions options) {
217+
String type = options.getType().orElse("");
215218

216219
Optional<String> engagementUuid = options.getEngagementUuid();
217220

218221
ArtifactCount count;
219222

220223
if(!options.getRegion().isEmpty() && options.getType().isPresent()) {
221-
count = Artifact.countArtifactsByRegionAndType(options.getType().get(), options.getRegion());
224+
count = Artifact.countArtifactsByRegionAndType(type, options.getRegion());
222225
} else if(!options.getRegion().isEmpty()) {
223226
count = Artifact.countArtifactsByRegion(options.getRegion());
224227
} else if(options.getType().isPresent()) {
225-
count = Artifact.countArtifactsByType(options.getType().get());
228+
count = Artifact.countArtifactsByType(type);
226229
} else if(engagementUuid.isPresent()) {
227230
count = Artifact.countArtifactsByEngagementUuid(engagementUuid.get());
228231
} else {
@@ -233,6 +236,12 @@ public ArtifactCount countArtifacts(GetOptions options) {
233236

234237
}
235238

239+
public Map<String, Long> getEngagementCounts() {
240+
Map<String, Long> countMap = new HashMap<>();
241+
Artifact.countArtifactsForEachEngagement().forEach(e -> countMap.put(e.getType(), e.getCount()));
242+
return countMap;
243+
}
244+
236245
/**
237246
* Removes the {@link Artifact} from the database if the object has been
238247
* removed. Otherwise, creates or updates the {@link Artifact}.
@@ -341,7 +350,6 @@ public void updateArtifactsFile(String engagementUuid, Optional<String> authorEm
341350

342351
// update in git
343352
gitlabRestClient.updateFile(project.getProjectId(), artifactsFile, file);
344-
345353
}
346354

347355
/**

src/test/java/com/redhat/labs/mock/ExternalApiWireMock.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package com.redhat.labs.mock;
22

3-
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
4-
import static com.github.tomakehurst.wiremock.client.WireMock.get;
5-
import static com.github.tomakehurst.wiremock.client.WireMock.put;
6-
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
7-
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
8-
93
import java.util.HashMap;
104
import java.util.Map;
115

126
import com.github.tomakehurst.wiremock.WireMockServer;
137

148
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
159

10+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
11+
1612
public class ExternalApiWireMock implements QuarkusTestResourceLifecycleManager {
1713

1814
private WireMockServer wireMockServer;
@@ -65,6 +61,9 @@ public Map<String, String> start() {
6561
stubFor(put(urlEqualTo("/api/v4/projects/2/repository/files/engagement%2Fartifacts.json"))
6662
.willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(200)));
6763

64+
65+
stubFor(put(urlMatching("/api/v2/engagements/1111/artifacts/[0-9]")).willReturn(aResponse().withStatus(200)));
66+
6867
// set endpoint
6968

7069
Map<String, String> config = new HashMap<>();

src/test/java/com/redhat/labs/model/ArtifactTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void testPagedArtifacts() {
4848

4949
@Test
5050
void testPagedArtifactsByEngagementUuid() {
51-
assertEquals(1, Artifact.pagedArtifactsByEngagementUuid("1111", 0, 1).size());
51+
assertEquals(1, Artifact.pagedArtifactsByEngagementUuid("1111", 0, 1, Sort.by("uuid")).size());
5252
}
5353

5454
@Test

src/test/java/com/redhat/labs/resource/ArtifactResourceTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ void testGetArtifactsByEngagement() {
107107

108108
}
109109

110+
@Test
111+
void testGetEngagementCounts() {
112+
given().when().get("/api/artifacts/engagements/count").then().statusCode(200).body("1111", equalTo(2));
113+
}
114+
110115
@Test
111116
void testGetArtifactsByUnknownEngagement() {
112117

src/test/java/com/redhat/labs/service/ArtifactServiceTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void testModifyByEngagementIdUpdateInGit() {
8686
assertNotNull(updated);
8787
assertEquals(2, updated.size());
8888

89-
updated.stream().forEach(a -> {
89+
updated.forEach(a -> {
9090
if (a.getType().equals("Demo")) {
9191
assertEquals("Updated", a.getDescription());
9292
} else if (a.getType().equals("typeOne")) {
@@ -180,9 +180,7 @@ void testGetArtifactsByTypeException() {
180180
options.setType("Demo");
181181
options.setEngagementUuid("1111");
182182

183-
WebApplicationException ex = assertThrows(WebApplicationException.class, () -> {
184-
artifactService.getArtifacts(options);
185-
});
183+
WebApplicationException ex = assertThrows(WebApplicationException.class, () -> artifactService.getArtifacts(options));
186184

187185
assertEquals(400, ex.getResponse().getStatus());
188186

@@ -287,7 +285,6 @@ void testCountArtifactsByEngagement() {
287285
// when
288286
ArtifactCount count = artifactService.countArtifacts(options);
289287

290-
// then
291288
// then
292289
assertNotNull(count);
293290
assertEquals(2, count.getCount());

0 commit comments

Comments
 (0)