|
9 | 9 | ********************************************************************************/ |
10 | 10 | package org.eclipse.openvsx.admin; |
11 | 11 |
|
12 | | -import io.swagger.v3.oas.annotations.Operation; |
13 | | -import io.swagger.v3.oas.annotations.Parameter; |
14 | | -import io.swagger.v3.oas.annotations.media.Content; |
15 | | -import io.swagger.v3.oas.annotations.media.Schema; |
16 | | -import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 12 | +import java.net.URI; |
| 13 | +import java.time.Period; |
| 14 | +import java.time.format.DateTimeParseException; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | +import java.util.stream.Collectors; |
| 18 | + |
17 | 19 | import org.apache.commons.lang3.StringUtils; |
18 | 20 | import org.eclipse.openvsx.LocalRegistryService; |
19 | 21 | import org.eclipse.openvsx.entities.AdminStatistics; |
20 | 22 | import org.eclipse.openvsx.entities.NamespaceMembership; |
21 | 23 | import org.eclipse.openvsx.entities.PersistedLog; |
22 | | -import org.eclipse.openvsx.json.*; |
| 24 | +import org.eclipse.openvsx.json.AdminStatisticsJson; |
| 25 | +import org.eclipse.openvsx.json.ChangeNamespaceJson; |
| 26 | +import org.eclipse.openvsx.json.ExtensionJson; |
| 27 | +import org.eclipse.openvsx.json.NamespaceJson; |
| 28 | +import org.eclipse.openvsx.json.NamespaceMembershipListJson; |
| 29 | +import org.eclipse.openvsx.json.PersistedLogJson; |
| 30 | +import org.eclipse.openvsx.json.ResultJson; |
| 31 | +import org.eclipse.openvsx.json.StatsJson; |
| 32 | +import org.eclipse.openvsx.json.TargetPlatformVersionJson; |
| 33 | +import org.eclipse.openvsx.json.UserPublishInfoJson; |
23 | 34 | import org.eclipse.openvsx.repositories.RepositoryService; |
24 | 35 | import org.eclipse.openvsx.search.SearchUtilService; |
25 | 36 | import org.eclipse.openvsx.util.*; |
| 37 | +import org.springframework.data.domain.Page; |
| 38 | +import org.springframework.data.domain.Pageable; |
26 | 39 | import org.springframework.data.util.Streamable; |
27 | 40 | import org.springframework.http.HttpStatus; |
28 | 41 | import org.springframework.http.MediaType; |
29 | 42 | import org.springframework.http.ResponseEntity; |
30 | | -import org.springframework.web.bind.annotation.*; |
| 43 | +import org.springframework.web.bind.annotation.CrossOrigin; |
| 44 | +import org.springframework.web.bind.annotation.GetMapping; |
| 45 | +import org.springframework.web.bind.annotation.PathVariable; |
| 46 | +import org.springframework.web.bind.annotation.PostMapping; |
| 47 | +import org.springframework.web.bind.annotation.RequestBody; |
| 48 | +import org.springframework.web.bind.annotation.RequestParam; |
| 49 | +import org.springframework.web.bind.annotation.RestController; |
31 | 50 | import org.springframework.web.server.ResponseStatusException; |
32 | 51 |
|
33 | | -import java.net.URI; |
34 | | -import java.time.Period; |
35 | | -import java.time.format.DateTimeParseException; |
36 | | -import java.util.Collections; |
37 | | -import java.util.List; |
38 | | -import java.util.stream.Collectors; |
| 52 | +import io.swagger.v3.oas.annotations.Operation; |
| 53 | +import io.swagger.v3.oas.annotations.Parameter; |
| 54 | +import io.swagger.v3.oas.annotations.media.Content; |
| 55 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 56 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
39 | 57 |
|
40 | 58 | @RestController |
41 | 59 | @ApiResponse( |
@@ -168,6 +186,40 @@ public String getLog(@RequestParam(name = "period", required = false) String per |
168 | 186 | } |
169 | 187 | } |
170 | 188 |
|
| 189 | + @GetMapping( |
| 190 | + path = "/admin/logs", |
| 191 | + produces = MediaType.APPLICATION_JSON_VALUE |
| 192 | + ) |
| 193 | + public ResponseEntity<Page<PersistedLogJson>> getLog( |
| 194 | + Pageable pageable, |
| 195 | + @RequestParam(name = "period", required = false) String periodString |
| 196 | + ) { |
| 197 | + try { |
| 198 | + admins.checkAdminUser(); |
| 199 | + |
| 200 | + Page<PersistedLog> logsPage; |
| 201 | + if (StringUtils.isEmpty(periodString)) { |
| 202 | + logsPage = repositories.findPersistedLogsPaginated(pageable); |
| 203 | + } else { |
| 204 | + try { |
| 205 | + var period = Period.parse(periodString); |
| 206 | + var now = TimeUtil.getCurrentUTC(); |
| 207 | + logsPage = repositories.findPersistedLogsAfterPaginated(now.minus(period), pageable); |
| 208 | + } catch (DateTimeParseException _) { |
| 209 | + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid period"); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + return ResponseEntity.ok(logsPage.map(log -> { |
| 214 | + var timestamp = log.getTimestamp().minusNanos(log.getTimestamp().getNano()); |
| 215 | + return new PersistedLogJson(timestamp.toString(), log.getUser().getLoginName(), log.getMessage()); |
| 216 | + })); |
| 217 | + } catch (ErrorResultException exc) { |
| 218 | + var status = exc.getStatus() != null ? exc.getStatus() : HttpStatus.BAD_REQUEST; |
| 219 | + throw new ResponseStatusException(status); |
| 220 | + } |
| 221 | + } |
| 222 | + |
171 | 223 | private String toString(PersistedLog log) { |
172 | 224 | var timestamp = log.getTimestamp().minusNanos(log.getTimestamp().getNano()); |
173 | 225 | return timestamp + "\t" + log.getUser().getLoginName() + "\t" + log.getMessage(); |
|
0 commit comments