Search in sources :

Example 1 with ServiceException

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);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ServiceException(eu.openminted.registry.core.service.ServiceException) DateHistogramAggregationBuilder(org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder) DateHistogramInterval(org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with ServiceException

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;
}
Also used : ServiceException(eu.openminted.registry.core.service.ServiceException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException)

Example 3 with ServiceException

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;
}
Also used : Instant(java.time.Instant) Duration(java.time.Duration) ServiceException(eu.openminted.registry.core.service.ServiceException)

Example 4 with ServiceException

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);
}
Also used : Resource(eu.openminted.registry.core.domain.Resource) MapValues(eu.einfracentral.dto.MapValues) Cacheable(org.springframework.cache.annotation.Cacheable) InfraServiceManager(eu.einfracentral.registry.manager.InfraServiceManager) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) Autowired(org.springframework.beans.factory.annotation.Autowired) Service(eu.einfracentral.domain.Service) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) PgArray(org.postgresql.jdbc.PgArray) ServiceException(eu.openminted.registry.core.service.ServiceException) Logger(org.apache.log4j.Logger) SearchResponse(org.elasticsearch.action.search.SearchResponse) RequestOptions(org.elasticsearch.client.RequestOptions) DateHistogramInterval(org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval) SearchType(org.elasticsearch.action.search.SearchType) Paging(eu.openminted.registry.core.domain.Paging) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ProviderService(eu.einfracentral.registry.service.ProviderService) FacetFilter(eu.openminted.registry.core.domain.FacetFilter) PlaceCount(eu.einfracentral.dto.PlaceCount) Collectors(java.util.stream.Collectors) ParserService(eu.openminted.registry.core.service.ParserService) PipelineAggregatorBuilders(org.elasticsearch.search.aggregations.PipelineAggregatorBuilders) ProviderBundle(eu.einfracentral.domain.ProviderBundle) StatisticsService(eu.einfracentral.service.StatisticsService) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) CACHE_VISITS(eu.einfracentral.config.CacheConfig.CACHE_VISITS) Authentication(org.springframework.security.core.Authentication) java.util(java.util) SearchService(eu.openminted.registry.core.service.SearchService) SimpleValue(org.elasticsearch.search.aggregations.pipeline.SimpleValue) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) SearchRequest(org.elasticsearch.action.search.SearchRequest) SQLException(java.sql.SQLException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) DataSource(javax.sql.DataSource) Value(eu.einfracentral.dto.Value) EnableScheduling(org.springframework.scheduling.annotation.EnableScheduling) Event(eu.einfracentral.domain.Event) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) DateTime(org.joda.time.DateTime) ParsedDateHistogram(org.elasticsearch.search.aggregations.bucket.histogram.ParsedDateHistogram) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) IOException(java.io.IOException) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) DateHistogramAggregationBuilder(org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder) Component(org.springframework.stereotype.Component) InfraService(eu.einfracentral.domain.InfraService) AnalyticsService(eu.einfracentral.service.AnalyticsService) LogManager(org.apache.log4j.LogManager) VocabularyService(eu.einfracentral.registry.service.VocabularyService) SearchRequest(org.elasticsearch.action.search.SearchRequest) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) ParsedDateHistogram(org.elasticsearch.search.aggregations.bucket.histogram.ParsedDateHistogram) DateHistogramAggregationBuilder(org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder) ParsedDateHistogram(org.elasticsearch.search.aggregations.bucket.histogram.ParsedDateHistogram) DateHistogramInterval(org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) SimpleValue(org.elasticsearch.search.aggregations.pipeline.SimpleValue) ServiceException(eu.openminted.registry.core.service.ServiceException) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)

Example 5 with ServiceException

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);
}
Also used : InfraService(eu.einfracentral.domain.InfraService) ResponseEntity(org.springframework.http.ResponseEntity) ServiceException(eu.openminted.registry.core.service.ServiceException) ResourceException(eu.einfracentral.exception.ResourceException) ResourceNotFoundException(eu.openminted.registry.core.exception.ResourceNotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

ServiceException (eu.openminted.registry.core.service.ServiceException)5 InfraService (eu.einfracentral.domain.InfraService)2 IOException (java.io.IOException)2 SearchRequest (org.elasticsearch.action.search.SearchRequest)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 DateHistogramAggregationBuilder (org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder)2 DateHistogramInterval (org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval)2 CACHE_VISITS (eu.einfracentral.config.CacheConfig.CACHE_VISITS)1 Event (eu.einfracentral.domain.Event)1 ProviderBundle (eu.einfracentral.domain.ProviderBundle)1 Service (eu.einfracentral.domain.Service)1 MapValues (eu.einfracentral.dto.MapValues)1 PlaceCount (eu.einfracentral.dto.PlaceCount)1 Value (eu.einfracentral.dto.Value)1 ResourceException (eu.einfracentral.exception.ResourceException)1 InfraServiceManager (eu.einfracentral.registry.manager.InfraServiceManager)1 ProviderService (eu.einfracentral.registry.service.ProviderService)1 VocabularyService (eu.einfracentral.registry.service.VocabularyService)1 AnalyticsService (eu.einfracentral.service.AnalyticsService)1 StatisticsService (eu.einfracentral.service.StatisticsService)1