Search in sources :

Example 1 with Value

use of eu.einfracentral.dto.Value in project resource-catalogue by madgeek-arc.

the class StatisticsManager method mapServicesToVocabulary.

@Override
public List<MapValues> mapServicesToVocabulary(String providerId, Vocabulary vocabulary) {
    Map<String, Set<Value>> vocabularyServices = new HashMap<>();
    NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    MapSqlParameterSource in = new MapSqlParameterSource();
    in.addValue("resource_organisation", providerId);
    String query = "SELECT infra_service_id, name, " + vocabulary.getKey() + " FROM infra_service_view WHERE latest=true AND active=true ";
    if (providerId != null) {
        query += " AND :resource_organisation=resource_organisation";
    }
    List<Map<String, Object>> records = namedParameterJdbcTemplate.queryForList(query, in);
    try {
        for (Map<String, Object> entry : records) {
            Value value = new Value();
            value.setId(entry.get("infra_service_id").toString());
            value.setName(entry.get("name").toString());
            // TODO: refactor this code and Vocabulary enum
            String[] vocabularyValues;
            if (vocabulary != Vocabulary.ORDER_TYPE) {
                // because order type is not multivalued
                PgArray pgArray = ((PgArray) entry.get(vocabulary.getKey()));
                vocabularyValues = ((String[]) pgArray.getArray());
            } else {
                vocabularyValues = new String[] { ((String) entry.get(vocabulary.getKey())) };
            }
            for (String voc : vocabularyValues) {
                Set<Value> values;
                if (vocabularyServices.containsKey(voc)) {
                    values = vocabularyServices.get(voc);
                } else {
                    values = new HashSet<>();
                }
                values.add(value);
                vocabularyServices.put(voc, values);
            }
        }
    } catch (SQLException throwables) {
        logger.error(throwables);
    }
    return toListMapValues(vocabularyServices);
}
Also used : NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) SQLException(java.sql.SQLException) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) SimpleValue(org.elasticsearch.search.aggregations.pipeline.SimpleValue) Value(eu.einfracentral.dto.Value) PgArray(org.postgresql.jdbc.PgArray)

Example 2 with Value

use of eu.einfracentral.dto.Value in project resource-catalogue by madgeek-arc.

the class StatisticsManager method servicesByPlace.

@Override
public List<Value> servicesByPlace(String providerId, String place) {
    NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    MapSqlParameterSource in = new MapSqlParameterSource();
    in.addValue("resource_organisation", providerId);
    in.addValue("geographical_availabilities", place);
    String query = "SELECT infra_service_id, name FROM infra_service_view WHERE latest=true AND active=true ";
    if (providerId != null) {
        query += " AND :resource_organisation=resource_organisation";
    }
    if (place != null) {
        Set<String> geographical_availabilities = new HashSet<>(Arrays.asList(vocabularyService.getRegion("EU")));
        if (!place.equalsIgnoreCase("WW")) {
            query += " AND ( :geographical_availabilities=ANY(geographical_availabilities) ";
            // if Place belongs to EU then search for EU as well
            if (geographical_availabilities.contains(place) || place.equalsIgnoreCase("EU")) {
                query += " OR 'EU'=ANY(geographical_availabilities) ";
            }
            // always search for WW (because every Place belongs to WW)
            query += " OR 'WW'=ANY(geographical_availabilities) )";
        }
    }
    List<Map<String, Object>> records = namedParameterJdbcTemplate.queryForList(query, in);
    List<Value> placeServices;
    placeServices = records.stream().map(record -> new Value(record.get("infra_service_id").toString(), record.get("name").toString())).collect(Collectors.toList());
    return placeServices;
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) SimpleValue(org.elasticsearch.search.aggregations.pipeline.SimpleValue) Value(eu.einfracentral.dto.Value)

Example 3 with Value

use of eu.einfracentral.dto.Value in project resource-catalogue by madgeek-arc.

the class StatisticsManager method mapServicesToGeographicalAvailability.

@Override
public List<MapValues> mapServicesToGeographicalAvailability(String providerId) {
    Map<String, Set<Value>> placeServices = new HashMap<>();
    String[] world = vocabularyService.getRegion("WW");
    String[] eu = vocabularyService.getRegion("EU");
    for (String place : world) {
        placeServices.put(place, new HashSet<>());
    }
    placeServices.put("OT", new HashSet<>());
    placeServices.put("EL", new HashSet<>());
    placeServices.put("UK", new HashSet<>());
    NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    MapSqlParameterSource in = new MapSqlParameterSource();
    in.addValue("resource_organisation", providerId);
    String query = "SELECT infra_service_id, name, geographical_availabilities FROM infra_service_view WHERE latest=true AND active=true ";
    if (providerId != null) {
        query += " AND :resource_organisation=resource_organisation";
    }
    List<Map<String, Object>> records = namedParameterJdbcTemplate.queryForList(query, in);
    try {
        for (Map<String, Object> entry : records) {
            Value value = new Value();
            value.setId(entry.get("infra_service_id").toString());
            value.setName(entry.get("name").toString());
            PgArray pgArray = ((PgArray) entry.get("geographical_availabilities"));
            for (String place : ((String[]) pgArray.getArray())) {
                String[] expandedPlaces;
                if (place.equalsIgnoreCase("WW")) {
                    expandedPlaces = world;
                } else if (place.equalsIgnoreCase("EU")) {
                    expandedPlaces = eu;
                } else {
                    expandedPlaces = new String[] { place };
                }
                for (String p : expandedPlaces) {
                    if (placeServices.get(p) == null) {
                        continue;
                    }
                    Set<Value> values = placeServices.get(p);
                    values.add(value);
                    placeServices.put(p, values);
                }
            }
        }
    } catch (SQLException throwables) {
        logger.error(throwables);
    }
    return toListMapValues(placeServices);
}
Also used : NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) SQLException(java.sql.SQLException) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) SimpleValue(org.elasticsearch.search.aggregations.pipeline.SimpleValue) Value(eu.einfracentral.dto.Value) PgArray(org.postgresql.jdbc.PgArray)

Example 4 with Value

use of eu.einfracentral.dto.Value in project resource-catalogue by madgeek-arc.

the class StatisticsManager method mapServicesToProviderCountry.

@Override
public List<MapValues> mapServicesToProviderCountry() {
    Map<String, Set<Value>> mapValues = new HashMap<>();
    for (String place : vocabularyService.getRegion("WW")) {
        mapValues.put(place, new HashSet<>());
    }
    mapValues.put("OT", new HashSet<>());
    mapValues.put("EL", new HashSet<>());
    mapValues.put("UK", new HashSet<>());
    FacetFilter ff = new FacetFilter();
    ff.setQuantity(maxQuantity);
    Map<String, Set<String>> providerCountries = providerCountriesMap();
    List<InfraService> allServices = infraServiceManager.getAll(ff, null).getResults();
    for (InfraService infraService : allServices) {
        Value value = new Value(infraService.getId(), infraService.getService().getName());
        Set<String> countries = new HashSet<>(providerCountries.get(infraService.getService().getResourceOrganisation()));
        for (String country : countries) {
            if (mapValues.get(country) == null) {
                continue;
            }
            Set<Value> values = mapValues.get(country);
            values.add(value);
            mapValues.put(country, values);
        }
    }
    return toListMapValues(mapValues);
}
Also used : FacetFilter(eu.openminted.registry.core.domain.FacetFilter) InfraService(eu.einfracentral.domain.InfraService) SimpleValue(org.elasticsearch.search.aggregations.pipeline.SimpleValue) Value(eu.einfracentral.dto.Value)

Example 5 with Value

use of eu.einfracentral.dto.Value 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)

Aggregations

Value (eu.einfracentral.dto.Value)5 SimpleValue (org.elasticsearch.search.aggregations.pipeline.SimpleValue)5 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)4 NamedParameterJdbcTemplate (org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)4 SQLException (java.sql.SQLException)3 PgArray (org.postgresql.jdbc.PgArray)3 InfraService (eu.einfracentral.domain.InfraService)2 FacetFilter (eu.openminted.registry.core.domain.FacetFilter)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 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 Paging (eu.openminted.registry.core.domain.Paging)1