Skip to content

Commit

Permalink
Merge pull request #757 from amvanbaren/cleanup/issue-653
Browse files Browse the repository at this point in the history
cleanup admin statistics endpoints
  • Loading branch information
amvanbaren authored Jun 15, 2023
2 parents e155d2f + 23d7d94 commit 63eae3e
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 68 deletions.
37 changes: 1 addition & 36 deletions server/src/main/java/org/eclipse/openvsx/admin/AdminAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,41 +59,6 @@ public class AdminAPI {
@Autowired
SearchUtilService search;

@GetMapping(
path = "/admin/reports",
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Map<String, List<String>>> getReports(
@RequestParam("token") String tokenValue
) {
try {
validateToken(tokenValue);
return ResponseEntity.ok(admins.getReports());
} catch (ErrorResultException exc) {
return ResponseEntity.status(exc.getStatus()).build();
}
}

@PostMapping(
path = "/admin/report/schedule",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<ResultJson> scheduleReport(
@RequestParam String token,
@RequestBody JsonNode json
) {
try {
validateToken(token);
var year = json.get("year").asInt();
var month = json.get("month").asInt();
admins.scheduleReport(year, month);
return ResponseEntity.accepted().build();
} catch (ErrorResultException exc) {
return exc.toResponseEntity(ResultJson.class);
}
}

@GetMapping(
path = "/admin/report",
produces = MediaType.APPLICATION_JSON_VALUE
Expand Down Expand Up @@ -144,7 +109,7 @@ private AdminStatistics getReport(String tokenValue, int year, int month) {
path = "/admin/stats",
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<StatsJson> getStats(@RequestParam("token") String tokenValue) {
public ResponseEntity<StatsJson> getStats() {
try {
admins.checkAdminUser();

Expand Down
22 changes: 0 additions & 22 deletions server/src/main/java/org/eclipse/openvsx/admin/AdminService.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,17 +363,6 @@ public AdminStatistics getAdminStatistics(int year, int month) throws ErrorResul
return statistics;
}

public void scheduleReport(int year, int month) {
validateYearAndMonth(year, month);
if(repositories.findAdminStatisticsByYearAndMonth(year, month) != null) {
throw new ErrorResultException("Report for " + year + "/" + month + " already exists");
}

var jobIdText = "AdminStatistics::year=" + year + ",month=" + month;
var jobId = UUID.nameUUIDFromBytes(jobIdText.getBytes(StandardCharsets.UTF_8));
scheduler.enqueue(jobId, new AdminStatisticsJobRequest(year, month));
}

private void validateYearAndMonth(int year, int month) {
if(year < 0) {
throw new ErrorResultException("Year can't be negative", HttpStatus.BAD_REQUEST);
Expand All @@ -387,15 +376,4 @@ private void validateYearAndMonth(int year, int month) {
throw new ErrorResultException("Combination of year and month lies in the future", HttpStatus.BAD_REQUEST);
}
}

public Map<String, List<String>> getReports() {
return repositories.findAllAdminStatistics().stream()
.sorted(Comparator.comparingInt(AdminStatistics::getYear).thenComparing(AdminStatistics::getMonth))
.map(stat -> {
var yearText = String.valueOf(stat.getYear());
var monthText = String.valueOf(stat.getMonth());
return new AbstractMap.SimpleEntry<>(yearText, monthText);
})
.collect(Collectors.groupingBy(Map.Entry::getKey, () -> new LinkedHashMap<>(), Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,23 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

@Component
public class MonthlyAdminStatisticsJobRequestHandler implements JobRequestHandler<HandlerJobRequest<?>> {

@Autowired
AdminService admins;
JobRequestScheduler scheduler;

@Override
public void run(HandlerJobRequest<?> jobRequest) throws Exception {
var lastMonth = TimeUtil.getCurrentUTC().minusMonths(1);
admins.scheduleReport(lastMonth.getYear(), lastMonth.getMonthValue());
var year = lastMonth.getYear();
var month = lastMonth.getMonthValue();

var jobIdText = "AdminStatistics::year=" + year + ",month=" + month;
var jobId = UUID.nameUUIDFromBytes(jobIdText.getBytes(StandardCharsets.UTF_8));
scheduler.enqueue(jobId, new AdminStatisticsJobRequest(year, month));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,5 @@

public interface AdminStatisticsRepository extends Repository<AdminStatistics, Long> {

Streamable<AdminStatistics> findAll();

AdminStatistics findByYearAndMonth(int year, int month);
}
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,4 @@ public void deleteAllKeyPairs() {
public SignatureKeyPair findKeyPair(String publicId) {
return signatureKeyPairRepo.findByPublicId(publicId);
}

public Streamable<AdminStatistics> findAllAdminStatistics() {
return adminStatisticsRepo.findAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ void testExecuteQueries() {
() -> repositories.findVersionStringsSorted(extension, "targetPlatform", true),
() -> repositories.findActiveVersions(queryRequest),
() -> repositories.findActiveVersionStringsSorted(LONG_LIST,"targetPlatform"),
() -> repositories.findActiveVersionReferencesSorted(List.of(extension)),
() -> repositories.findAllAdminStatistics()
() -> repositories.findActiveVersionReferencesSorted(List.of(extension))
);

// check that we did not miss anything
Expand Down

0 comments on commit 63eae3e

Please sign in to comment.