use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.
the class AdminService method getAdminStatisticsCsv.
@Transactional
public String getAdminStatisticsCsv(int year, int month) throws ErrorResultException {
if (year < 0) {
throw new ErrorResultException("Year can't be negative", HttpStatus.BAD_REQUEST);
}
if (month < 1 || month > 12) {
throw new ErrorResultException("Month must be a value between 1 and 12", HttpStatus.BAD_REQUEST);
}
var now = LocalDateTime.now();
if (year > now.getYear() || (year == now.getYear() && month > now.getMonthValue())) {
throw new ErrorResultException("Combination of year and month lies in the future", HttpStatus.BAD_REQUEST);
}
var statistics = repositories.findAdminStatisticsByYearAndMonth(year, month);
if (statistics == null) {
LocalDateTime startInclusive;
try {
startInclusive = LocalDateTime.of(year, month, 1, 0, 0);
} catch (DateTimeException e) {
throw new ErrorResultException("Invalid month or year", HttpStatus.BAD_REQUEST);
}
var currentYearAndMonth = now.getYear() == year && now.getMonthValue() == month;
var endExclusive = currentYearAndMonth ? now.truncatedTo(ChronoUnit.MINUTES) : startInclusive.plusMonths(1);
var extensions = repositories.countActiveExtensions(endExclusive);
var downloads = repositories.downloadsBetween(startInclusive, endExclusive);
var downloadsTotal = repositories.downloadsUntil(endExclusive);
var publishers = repositories.countActiveExtensionPublishers(endExclusive);
var averageReviewsPerExtension = repositories.averageNumberOfActiveReviewsPerActiveExtension(endExclusive);
var namespaceOwners = repositories.countPublishersThatClaimedNamespaceOwnership(endExclusive);
var extensionsByRating = repositories.countActiveExtensionsGroupedByExtensionReviewRating(endExclusive);
var publishersByExtensionsPublished = repositories.countActiveExtensionPublishersGroupedByExtensionsPublished(endExclusive);
statistics = new AdminStatistics();
statistics.setYear(year);
statistics.setMonth(month);
statistics.setExtensions(extensions);
statistics.setDownloads(downloads);
statistics.setDownloadsTotal(downloadsTotal);
statistics.setPublishers(publishers);
statistics.setAverageReviewsPerExtension(averageReviewsPerExtension);
statistics.setNamespaceOwners(namespaceOwners);
statistics.setExtensionsByRating(extensionsByRating);
statistics.setPublishersByExtensionsPublished(publishersByExtensionsPublished);
if (!currentYearAndMonth) {
// archive statistics for quicker lookup next time
entityManager.persist(statistics);
}
}
return statistics.toCsv();
}
use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.
the class ExtensionProcessor method loadVsixManifest.
private void loadVsixManifest() {
if (vsixManifest != null) {
return;
}
readInputStream();
// Read extension.vsixmanifest
var bytes = ArchiveUtil.readEntry(zipFile, VSIX_MANIFEST);
if (bytes == null)
throw new ErrorResultException("Entry not found: " + VSIX_MANIFEST);
try {
var mapper = new XmlMapper();
vsixManifest = mapper.readTree(bytes);
} catch (JsonParseException exc) {
throw new ErrorResultException("Invalid JSON format in " + VSIX_MANIFEST + ": " + exc.getMessage());
} catch (IOException exc) {
throw new RuntimeException(exc);
}
}
use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.
the class ExtensionProcessor method readInputStream.
private void readInputStream() {
if (zipFile != null) {
return;
}
try {
if (inputStream != null)
content = ByteStreams.toByteArray(inputStream);
if (content.length > MAX_CONTENT_SIZE)
throw new ErrorResultException("The extension package exceeds the size limit of 512 MB.", HttpStatus.PAYLOAD_TOO_LARGE);
var tempFile = File.createTempFile("extension_", ".vsix");
Files.write(content, tempFile);
zipFile = new ZipFile(tempFile);
} catch (ZipException exc) {
throw new ErrorResultException("Could not read zip file: " + exc.getMessage());
} catch (EOFException exc) {
throw new ErrorResultException("Could not read from input stream: " + exc.getMessage());
} catch (IOException exc) {
throw new RuntimeException(exc);
}
}
use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.
the class ExtensionProcessor method loadPackageJson.
private void loadPackageJson() {
if (packageJson != null) {
return;
}
readInputStream();
// Read package.json
var bytes = ArchiveUtil.readEntry(zipFile, PACKAGE_JSON);
if (bytes == null)
throw new ErrorResultException("Entry not found: " + PACKAGE_JSON);
try {
var mapper = new ObjectMapper();
packageJson = mapper.readTree(bytes);
} catch (JsonParseException exc) {
throw new ErrorResultException("Invalid JSON format in " + PACKAGE_JSON + ": " + exc.getMessage());
} catch (IOException exc) {
throw new RuntimeException(exc);
}
}
use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.
the class AdminAPI method getExtension.
@GetMapping(path = "/admin/extension/{namespaceName}/{extensionName}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ExtensionJson> getExtension(@PathVariable String namespaceName, @PathVariable String extensionName) {
try {
admins.checkAdminUser();
var extension = repositories.findExtension(extensionName, namespaceName);
if (extension == null) {
var json = ExtensionJson.error("Extension not found: " + namespaceName + "." + extensionName);
return new ResponseEntity<>(json, HttpStatus.NOT_FOUND);
}
ExtensionJson json;
// Don't rely on the 'latest' relationship here because the extension might be inactive
var extVersion = getLatestVersion(extension);
if (extVersion == null) {
json = new ExtensionJson();
json.namespace = extension.getNamespace().getName();
json.name = extension.getName();
json.allVersions = Collections.emptyMap();
} else {
json = local.toExtensionVersionJson(extVersion, false);
}
json.active = extension.isActive();
return ResponseEntity.ok(json);
} catch (ErrorResultException exc) {
return exc.toResponseEntity(ExtensionJson.class);
}
}
Aggregations