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);
}
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;
}
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);
}
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);
}
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);
}
Aggregations