use of eu.openminted.registry.core.service.ServiceException in project resource-catalogue by madgeek-arc.
the class StatisticsManager method histogram.
private ParsedDateHistogram histogram(String id, String eventType, Interval by) {
String dateFormat;
String aggregationName;
DateHistogramInterval dateHistogramInterval;
switch(StatisticsService.Interval.fromString(by.getKey())) {
case DAY:
dateFormat = "yyyy-MM-dd";
aggregationName = "day";
dateHistogramInterval = DateHistogramInterval.DAY;
break;
case WEEK:
dateFormat = "yyyy-MM-dd";
aggregationName = "week";
dateHistogramInterval = DateHistogramInterval.WEEK;
break;
case YEAR:
dateFormat = "yyyy";
aggregationName = "year";
dateHistogramInterval = DateHistogramInterval.YEAR;
break;
default:
dateFormat = "yyyy-MM";
aggregationName = "month";
dateHistogramInterval = DateHistogramInterval.MONTH;
}
DateHistogramAggregationBuilder dateHistogramAggregationBuilder = AggregationBuilders.dateHistogram(aggregationName).field("instant").calendarInterval(dateHistogramInterval).format(dateFormat).subAggregation(AggregationBuilders.terms("value").field("value"));
SearchRequest search = new SearchRequest("event");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
search.searchType(SearchType.DEFAULT);
searchSourceBuilder.query(getEventQueryBuilder(id, eventType));
searchSourceBuilder.aggregation(dateHistogramAggregationBuilder);
search.source(searchSourceBuilder);
SearchResponse response = null;
try {
response = client.search(search, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new ServiceException(e.getMessage());
}
return response.getAggregations().get(aggregationName);
}
use of eu.openminted.registry.core.service.ServiceException in project resource-catalogue by madgeek-arc.
the class ResourceManager method getAll.
@Override
public Browsing<T> getAll(FacetFilter ff, Authentication auth) {
ff.setBrowseBy(getBrowseBy());
Browsing<T> browsing;
try {
browsing = getResults(ff);
} catch (ElasticsearchStatusException e) {
// check elastic status
throw new ServiceException("Search error, check search parameters");
}
return browsing;
}
use of eu.openminted.registry.core.service.ServiceException in project resource-catalogue by madgeek-arc.
the class InfraServiceManager method migrateResourceHistory.
// TODO: First run with active/latest and no broke segment, second without active/latest and broke segment
public Map<String, List<LoggingInfo>> migrateResourceHistory(Authentication authentication) {
Map<String, List<LoggingInfo>> allMigratedLoggingInfos = new HashMap<>();
FacetFilter ff = new FacetFilter();
ff.setQuantity(10000);
// ff.addFilter("active", true);
// ff.addFilter("latest", true);
List<InfraService> allInfraServices = getAll(ff, securityService.getAdminAccess()).getResults();
List<Resource> allResources;
for (InfraService infraService : allInfraServices) {
// get all versions of a specific Service
allResources = getResourcesWithServiceId(infraService.getService().getId());
allResources.sort(Comparator.comparing((Resource::getCreationDate)));
boolean firstResource = true;
boolean broke = false;
for (Resource resource : allResources) {
List<LoggingInfo> resourceHistory = new ArrayList<>();
// get all updates of a specific Version of a specific Service
List<Version> versions = versionService.getVersionsByResource(resource.getId());
versions.sort(Comparator.comparing(Version::getCreationDate));
boolean firstVersion = true;
for (Version version : versions) {
// broke = false;
// Save Version as Resource so we can deserialize it and get userFullName
Resource tempResource = resource;
tempResource.setPayload(version.getPayload());
InfraService tempService = deserialize(tempResource);
if (tempService.getLoggingInfo() != null && !tempService.getLoggingInfo().isEmpty()) {
broke = true;
break;
}
LoggingInfo loggingInfo = new LoggingInfo();
if (firstResource && firstVersion) {
loggingInfo.setType(LoggingInfo.Types.ONBOARD.getKey());
loggingInfo.setActionType(LoggingInfo.ActionType.REGISTERED.getKey());
loggingInfo.setDate(String.valueOf(version.getCreationDate().getTime()));
if (tempService.getMetadata() != null && tempService.getMetadata().getRegisteredBy() != null) {
if (tempService.getMetadata().getRegisteredBy().equalsIgnoreCase("System") || tempService.getMetadata().getRegisteredBy().equalsIgnoreCase("einfracentral")) {
loggingInfo.setUserRole("system");
} else {
loggingInfo.setUserFullName(tempService.getMetadata().getRegisteredBy());
}
}
firstResource = false;
firstVersion = false;
} else if (!firstResource && firstVersion) {
loggingInfo.setType(LoggingInfo.Types.UPDATE.getKey());
loggingInfo.setActionType(LoggingInfo.ActionType.UPDATED_VERSION.getKey());
loggingInfo.setDate(String.valueOf(version.getCreationDate().getTime()));
if (tempService.getMetadata() != null && tempService.getMetadata().getModifiedBy() != null) {
if (tempService.getMetadata().getModifiedBy().equalsIgnoreCase("System") || tempService.getMetadata().getModifiedBy().equalsIgnoreCase("einfracentral")) {
loggingInfo.setUserRole("system");
} else {
loggingInfo.setUserFullName(tempService.getMetadata().getModifiedBy());
}
}
firstVersion = false;
} else {
loggingInfo.setType(LoggingInfo.Types.UPDATE.getKey());
loggingInfo.setActionType(LoggingInfo.ActionType.UPDATED.getKey());
loggingInfo.setDate(String.valueOf(version.getCreationDate().getTime()));
if (tempService.getMetadata() != null && tempService.getMetadata().getModifiedBy() != null) {
if (tempService.getMetadata().getModifiedBy().equalsIgnoreCase("System") || tempService.getMetadata().getModifiedBy().equalsIgnoreCase("einfracentral")) {
loggingInfo.setUserRole("system");
} else {
loggingInfo.setUserFullName(tempService.getMetadata().getModifiedBy());
}
}
}
resourceHistory.add(loggingInfo);
}
if (broke) {
continue;
}
resourceHistory.sort(Comparator.comparing(LoggingInfo::getDate));
InfraService service = deserialize(resource);
if (service.getLoggingInfo() != null) {
List<LoggingInfo> loggingInfoList = service.getLoggingInfo();
for (LoggingInfo loggingInfo : loggingInfoList) {
// update initialization type
if (loggingInfo.getType().equals("initialization")) {
loggingInfo.setType(LoggingInfo.Types.ONBOARD.getKey());
loggingInfo.setActionType(LoggingInfo.ActionType.REGISTERED.getKey());
// we may need to go further to core creationDate
loggingInfo.setDate(service.getMetadata().getRegisteredAt());
}
// migrate all the other states
if (loggingInfo.getType().equals("registered")) {
loggingInfo.setType(LoggingInfo.Types.ONBOARD.getKey());
loggingInfo.setActionType(LoggingInfo.ActionType.REGISTERED.getKey());
} else if (loggingInfo.getType().equals("updated")) {
loggingInfo.setType(LoggingInfo.Types.UPDATE.getKey());
loggingInfo.setActionType(LoggingInfo.ActionType.UPDATED.getKey());
} else if (loggingInfo.getType().equals("deleted")) {
loggingInfo.setType(LoggingInfo.Types.UPDATE.getKey());
loggingInfo.setActionType(LoggingInfo.ActionType.DELETED.getKey());
} else if (loggingInfo.getType().equals("activated")) {
loggingInfo.setType(LoggingInfo.Types.UPDATE.getKey());
loggingInfo.setActionType(LoggingInfo.ActionType.ACTIVATED.getKey());
} else if (loggingInfo.getType().equals("deactivated")) {
loggingInfo.setType(LoggingInfo.Types.UPDATE.getKey());
loggingInfo.setActionType(LoggingInfo.ActionType.DEACTIVATED.getKey());
} else if (loggingInfo.getType().equals("approved")) {
loggingInfo.setType(LoggingInfo.Types.ONBOARD.getKey());
loggingInfo.setActionType(LoggingInfo.ActionType.APPROVED.getKey());
} else if (loggingInfo.getType().equals("validated")) {
loggingInfo.setType(LoggingInfo.Types.ONBOARD.getKey());
loggingInfo.setActionType(LoggingInfo.ActionType.VALIDATED.getKey());
} else if (loggingInfo.getType().equals("rejected")) {
loggingInfo.setType(LoggingInfo.Types.ONBOARD.getKey());
loggingInfo.setActionType(LoggingInfo.ActionType.REJECTED.getKey());
} else if (loggingInfo.getType().equals("audited")) {
loggingInfo.setType(LoggingInfo.Types.AUDIT.getKey());
}
}
List<LoggingInfo> concatLoggingInfoList = new ArrayList<>();
if (loggingInfoList.get(0).getType().equals(LoggingInfo.Types.UPDATE.getKey()) || loggingInfoList.get(0).getType().equals(LoggingInfo.Types.AUDIT.getKey())) {
Instant loggingInstant = Instant.ofEpochSecond(Long.parseLong(loggingInfoList.get(0).getDate()));
Instant firstHistoryInstant = Instant.ofEpochSecond(Long.parseLong(resourceHistory.get(0).getDate()));
Duration dif = Duration.between(firstHistoryInstant, loggingInstant);
long sec = dif.getSeconds();
if (sec > 20) {
// if the difference < 20 secs, both lists contain the same items. If not (>20), concat them
for (LoggingInfo loggingFromHistory : resourceHistory) {
Instant historyInstant = Instant.ofEpochSecond(Long.parseLong(loggingFromHistory.getDate()));
Duration difference = Duration.between(historyInstant, loggingInstant);
long seconds = difference.getSeconds();
if (seconds > 20) {
concatLoggingInfoList.add(loggingFromHistory);
} else {
concatLoggingInfoList.addAll(loggingInfoList);
service.setLoggingInfo(concatLoggingInfoList);
break;
}
}
}
}
// else it's on the Onboard state, so we keep the existing LoggingInfo
} else {
service.setLoggingInfo(resourceHistory);
}
if (service.getService().getVersion() == null) {
allMigratedLoggingInfos.put(service.getService().getId() + " with null version", service.getLoggingInfo());
} else {
allMigratedLoggingInfos.put(service.getService().getId() + " with version " + service.getService().getVersion(), service.getLoggingInfo());
}
logger.info(String.format("Resource's [%s] new Logging Info %s", service.getService().getName(), service.getLoggingInfo()));
try {
// super.update(infraService, securityService.getAdminAccess());
} catch (ServiceException e) {
continue;
}
}
}
return allMigratedLoggingInfos;
}
use of eu.openminted.registry.core.service.ServiceException in project resource-catalogue by madgeek-arc.
the class StatisticsManager method ratings.
@Override
public Map<String, Float> ratings(String id, Interval by) {
String dateFormat;
String aggregationName;
DateHistogramInterval dateHistogramInterval;
switch(StatisticsService.Interval.fromString(by.getKey())) {
case DAY:
dateFormat = "yyyy-MM-dd";
aggregationName = "day";
dateHistogramInterval = DateHistogramInterval.DAY;
break;
case WEEK:
dateFormat = "yyyy-MM-dd";
aggregationName = "week";
dateHistogramInterval = DateHistogramInterval.WEEK;
break;
case YEAR:
dateFormat = "yyyy";
aggregationName = "year";
dateHistogramInterval = DateHistogramInterval.YEAR;
break;
default:
dateFormat = "yyyy-MM";
aggregationName = "month";
dateHistogramInterval = DateHistogramInterval.MONTH;
}
DateHistogramAggregationBuilder dateHistogramAggregationBuilder = AggregationBuilders.dateHistogram(aggregationName).field("instant").calendarInterval(dateHistogramInterval).format(dateFormat).subAggregation(AggregationBuilders.sum("rating").field("value")).subAggregation(AggregationBuilders.count("rating_count").field("value")).subAggregation(PipelineAggregatorBuilders.cumulativeSum("cum_sum", "rating")).subAggregation(PipelineAggregatorBuilders.cumulativeSum("ratings_num", "rating_count"));
SearchRequest search = new SearchRequest("event");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
search.searchType(SearchType.DFS_QUERY_THEN_FETCH);
searchSourceBuilder.query(getEventQueryBuilder(id, Event.UserActionType.RATING.getKey()));
searchSourceBuilder.aggregation(dateHistogramAggregationBuilder);
search.source(searchSourceBuilder);
SearchResponse response = null;
try {
response = client.search(search, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new ServiceException(e.getMessage());
}
List<? extends Histogram.Bucket> bucketsDay = ((ParsedDateHistogram) response.getAggregations().get(aggregationName)).getBuckets();
Map<String, Float> bucketMap = bucketsDay.stream().collect(Collectors.toMap(MultiBucketsAggregation.Bucket::getKeyAsString, e -> Float.parseFloat(((SimpleValue) e.getAggregations().get("cum_sum")).getValueAsString()) / Float.parseFloat(((SimpleValue) e.getAggregations().get("ratings_num")).getValueAsString())));
return new TreeMap<>(bucketMap);
}
use of eu.openminted.registry.core.service.ServiceException in project resource-catalogue by madgeek-arc.
the class PendingServiceController method pendingToInfra.
@PutMapping(path = "/transform/resource", produces = { MediaType.APPLICATION_JSON_VALUE })
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_EPOT') or @securityService.providerCanAddServices(#auth, #service)")
public ResponseEntity<Service> pendingToInfra(@RequestBody Service service, @ApiIgnore Authentication auth) {
if (service == null) {
throw new ServiceException("Cannot add a null Resource");
}
InfraService infraService = null;
try {
// check if service already exists
if (service.getId() == null || "".equals(service.getId())) {
// if service id is not given, create it
service.setId(idCreator.createServiceId(service));
}
infraService = this.pendingServiceManager.get(service.getId());
} catch (ResourceException | eu.einfracentral.exception.ResourceNotFoundException e) {
// continue with the creation of the service
}
if (infraService == null) {
// if existing Pending Service is null, create a new Active Service
infraService = infraServiceService.addService(new InfraService(service), auth);
logger.info("User '{}' added Resource:\n{}", auth.getName(), infraService);
} else {
// else update Pending Service and transform it to Active Service
// important to keep other fields of InfraService
infraService.setService(service);
infraService = pendingServiceManager.update(infraService, auth);
logger.info("User '{}' updated Pending Resource:\n{}", auth.getName(), infraService);
// transform to active
infraService = pendingServiceManager.transformToActive(infraService.getId(), auth);
}
return new ResponseEntity<>(infraService.getService(), HttpStatus.OK);
}
Aggregations