Skip to content

Commit 63eae3e

Browse files
authored
Merge pull request #757 from amvanbaren/cleanup/issue-653
cleanup admin statistics endpoints
2 parents e155d2f + 23d7d94 commit 63eae3e

File tree

6 files changed

+12
-68
lines changed

6 files changed

+12
-68
lines changed

server/src/main/java/org/eclipse/openvsx/admin/AdminAPI.java

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,41 +59,6 @@ public class AdminAPI {
5959
@Autowired
6060
SearchUtilService search;
6161

62-
@GetMapping(
63-
path = "/admin/reports",
64-
produces = MediaType.APPLICATION_JSON_VALUE
65-
)
66-
public ResponseEntity<Map<String, List<String>>> getReports(
67-
@RequestParam("token") String tokenValue
68-
) {
69-
try {
70-
validateToken(tokenValue);
71-
return ResponseEntity.ok(admins.getReports());
72-
} catch (ErrorResultException exc) {
73-
return ResponseEntity.status(exc.getStatus()).build();
74-
}
75-
}
76-
77-
@PostMapping(
78-
path = "/admin/report/schedule",
79-
consumes = MediaType.APPLICATION_JSON_VALUE,
80-
produces = MediaType.APPLICATION_JSON_VALUE
81-
)
82-
public ResponseEntity<ResultJson> scheduleReport(
83-
@RequestParam String token,
84-
@RequestBody JsonNode json
85-
) {
86-
try {
87-
validateToken(token);
88-
var year = json.get("year").asInt();
89-
var month = json.get("month").asInt();
90-
admins.scheduleReport(year, month);
91-
return ResponseEntity.accepted().build();
92-
} catch (ErrorResultException exc) {
93-
return exc.toResponseEntity(ResultJson.class);
94-
}
95-
}
96-
9762
@GetMapping(
9863
path = "/admin/report",
9964
produces = MediaType.APPLICATION_JSON_VALUE
@@ -144,7 +109,7 @@ private AdminStatistics getReport(String tokenValue, int year, int month) {
144109
path = "/admin/stats",
145110
produces = MediaType.APPLICATION_JSON_VALUE
146111
)
147-
public ResponseEntity<StatsJson> getStats(@RequestParam("token") String tokenValue) {
112+
public ResponseEntity<StatsJson> getStats() {
148113
try {
149114
admins.checkAdminUser();
150115

server/src/main/java/org/eclipse/openvsx/admin/AdminService.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -363,17 +363,6 @@ public AdminStatistics getAdminStatistics(int year, int month) throws ErrorResul
363363
return statistics;
364364
}
365365

366-
public void scheduleReport(int year, int month) {
367-
validateYearAndMonth(year, month);
368-
if(repositories.findAdminStatisticsByYearAndMonth(year, month) != null) {
369-
throw new ErrorResultException("Report for " + year + "/" + month + " already exists");
370-
}
371-
372-
var jobIdText = "AdminStatistics::year=" + year + ",month=" + month;
373-
var jobId = UUID.nameUUIDFromBytes(jobIdText.getBytes(StandardCharsets.UTF_8));
374-
scheduler.enqueue(jobId, new AdminStatisticsJobRequest(year, month));
375-
}
376-
377366
private void validateYearAndMonth(int year, int month) {
378367
if(year < 0) {
379368
throw new ErrorResultException("Year can't be negative", HttpStatus.BAD_REQUEST);
@@ -387,15 +376,4 @@ private void validateYearAndMonth(int year, int month) {
387376
throw new ErrorResultException("Combination of year and month lies in the future", HttpStatus.BAD_REQUEST);
388377
}
389378
}
390-
391-
public Map<String, List<String>> getReports() {
392-
return repositories.findAllAdminStatistics().stream()
393-
.sorted(Comparator.comparingInt(AdminStatistics::getYear).thenComparing(AdminStatistics::getMonth))
394-
.map(stat -> {
395-
var yearText = String.valueOf(stat.getYear());
396-
var monthText = String.valueOf(stat.getMonth());
397-
return new AbstractMap.SimpleEntry<>(yearText, monthText);
398-
})
399-
.collect(Collectors.groupingBy(Map.Entry::getKey, () -> new LinkedHashMap<>(), Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
400-
}
401379
}

server/src/main/java/org/eclipse/openvsx/admin/MonthlyAdminStatisticsJobRequestHandler.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@
1616
import org.springframework.beans.factory.annotation.Autowired;
1717
import org.springframework.stereotype.Component;
1818

19+
import java.nio.charset.StandardCharsets;
20+
import java.util.UUID;
21+
1922
@Component
2023
public class MonthlyAdminStatisticsJobRequestHandler implements JobRequestHandler<HandlerJobRequest<?>> {
2124

2225
@Autowired
23-
AdminService admins;
26+
JobRequestScheduler scheduler;
2427

2528
@Override
2629
public void run(HandlerJobRequest<?> jobRequest) throws Exception {
2730
var lastMonth = TimeUtil.getCurrentUTC().minusMonths(1);
28-
admins.scheduleReport(lastMonth.getYear(), lastMonth.getMonthValue());
31+
var year = lastMonth.getYear();
32+
var month = lastMonth.getMonthValue();
33+
34+
var jobIdText = "AdminStatistics::year=" + year + ",month=" + month;
35+
var jobId = UUID.nameUUIDFromBytes(jobIdText.getBytes(StandardCharsets.UTF_8));
36+
scheduler.enqueue(jobId, new AdminStatisticsJobRequest(year, month));
2937
}
3038
}

server/src/main/java/org/eclipse/openvsx/repositories/AdminStatisticsRepository.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,5 @@
1515

1616
public interface AdminStatisticsRepository extends Repository<AdminStatistics, Long> {
1717

18-
Streamable<AdminStatistics> findAll();
19-
2018
AdminStatistics findByYearAndMonth(int year, int month);
2119
}

server/src/main/java/org/eclipse/openvsx/repositories/RepositoryService.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,4 @@ public void deleteAllKeyPairs() {
493493
public SignatureKeyPair findKeyPair(String publicId) {
494494
return signatureKeyPairRepo.findByPublicId(publicId);
495495
}
496-
497-
public Streamable<AdminStatistics> findAllAdminStatistics() {
498-
return adminStatisticsRepo.findAll();
499-
}
500496
}

server/src/test/java/org/eclipse/openvsx/repositories/RepositoryServiceSmokeTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ void testExecuteQueries() {
185185
() -> repositories.findVersionStringsSorted(extension, "targetPlatform", true),
186186
() -> repositories.findActiveVersions(queryRequest),
187187
() -> repositories.findActiveVersionStringsSorted(LONG_LIST,"targetPlatform"),
188-
() -> repositories.findActiveVersionReferencesSorted(List.of(extension)),
189-
() -> repositories.findAllAdminStatistics()
188+
() -> repositories.findActiveVersionReferencesSorted(List.of(extension))
190189
);
191190

192191
// check that we did not miss anything

0 commit comments

Comments
 (0)