Search in sources :

Example 1 with Event

use of eu.einfracentral.domain.Event in project resource-catalogue by madgeek-arc.

the class StatisticsManager method events.

public Map<DateTime, Map<String, Long>> events(Event.UserActionType type, Date from, Date to, Interval by) {
    Map<DateTime, Map<String, Long>> results = new LinkedHashMap<>();
    Paging<Resource> resources = searchService.cqlQuery(String.format("type=\"%s\" AND creation_date > %s AND creation_date < %s", type, from.toInstant().toEpochMilli(), to.toInstant().toEpochMilli()), "event", maxQuantity, 0, "creation_date", "ASC");
    List<Event> events = resources.getResults().stream().map(resource -> parserService.deserialize(resource, Event.class)).collect(Collectors.toList());
    DateTime start = new DateTime(from);
    DateTime stop = new DateTime(to);
    Map<DateTime, List<Event>> eventsByDate = new LinkedHashMap<>();
    start.plusWeeks(1);
    while (start.getMillis() <= stop.getMillis()) {
        DateTime endDate = addInterval(start, by);
        List<Event> weekEvents = new LinkedList<>();
        events = events.stream().map(event -> {
            if (endDate.isAfter(event.getInstant())) {
                weekEvents.add(event);
                return null;
            } else
                return event;
        }).filter(Objects::nonNull).collect(Collectors.toList());
        // weekEvents.sort(Comparator.comparing(Event::getService));
        eventsByDate.put(start, weekEvents);
        start = endDate;
    }
    for (Map.Entry<DateTime, List<Event>> weekEntry : eventsByDate.entrySet()) {
        Map<String, Long> weekResults = weekEntry.getValue().stream().collect(Collectors.groupingBy(Event::getService, Collectors.counting()));
        weekResults = weekResults.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
        results.put(weekEntry.getKey(), weekResults);
    }
    return results;
}
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) Resource(eu.openminted.registry.core.domain.Resource) DateTime(org.joda.time.DateTime) Event(eu.einfracentral.domain.Event)

Example 2 with Event

use of eu.einfracentral.domain.Event in project resource-catalogue by madgeek-arc.

the class EventManager method add.

@Override
@CacheEvict(value = { CACHE_EVENTS, CACHE_SERVICE_EVENTS }, allEntries = true)
public Event add(Event event, Authentication auth) {
    event.setId(UUID.randomUUID().toString());
    event.setInstant(System.currentTimeMillis());
    if (auth != null) {
        event.setUser(AuthenticationInfo.getSub(auth));
    } else {
        event.setUser("-");
    }
    Event ret = super.add(event, auth);
    logger.debug("Adding Event: {}", event);
    return ret;
}
Also used : Event(eu.einfracentral.domain.Event) CacheEvict(org.springframework.cache.annotation.CacheEvict)

Example 3 with Event

use of eu.einfracentral.domain.Event in project resource-catalogue by madgeek-arc.

the class UserEventsController method favourites.

/**
 * Retrieve all the favourite Services of the authenticated user.
 *
 * @param auth
 * @return
 */
@GetMapping(path = "favourites", produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<List<RichService>> favourites(Authentication auth) {
    Map<String, Float> favouriteServices = new HashMap<>();
    List<Event> userEvents = eventService.getUserEvents(Event.UserActionType.FAVOURITE.getKey(), auth);
    List<RichService> services = new ArrayList<>();
    // Check if the serviceId exists and add it on the list, so to avoid errors
    FacetFilter ff = new FacetFilter();
    ff.setQuantity(10000);
    List<String> serviceIds = new ArrayList<>();
    for (InfraService infraService : infraServiceService.getAll(ff, auth).getResults()) {
        serviceIds.add(infraService.getService().getId());
    }
    for (Event userEvent : userEvents) {
        if (serviceIds.contains(userEvent.getService())) {
            favouriteServices.putIfAbsent(userEvent.getService(), userEvent.getValue());
        }
    }
    for (Map.Entry<String, Float> favouriteService : favouriteServices.entrySet()) {
        if (favouriteService.getValue() == 1) {
            // "1" is true
            services.add(infraServiceService.getRichService(favouriteService.getKey(), "latest", auth));
        }
    }
    return new ResponseEntity<>(services, HttpStatus.OK);
}
Also used : HashMap(java.util.HashMap) FacetFilter(eu.openminted.registry.core.domain.FacetFilter) ArrayList(java.util.ArrayList) InfraService(eu.einfracentral.domain.InfraService) ResponseEntity(org.springframework.http.ResponseEntity) RichService(eu.einfracentral.domain.RichService) Event(eu.einfracentral.domain.Event) HashMap(java.util.HashMap) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 4 with Event

use of eu.einfracentral.domain.Event in project resource-catalogue by madgeek-arc.

the class EventManager method addVisitsOnDay.

public void addVisitsOnDay(Date date, String serviceId, Float noOfVisits, Authentication authentication) {
    List<Event> serviceEvents = getServiceEvents(Event.UserActionType.VISIT.toString(), serviceId);
    for (Event event : serviceEvents) {
        // Compare the event.getInstant(long) to user's give date
        Date eventDate = new Date(event.getInstant());
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date);
        cal2.setTime(eventDate);
        boolean sameDay = cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
        if (event.getType().equals("AGGREGATED_VISITS") && sameDay) {
            Float oldVisits = event.getValue();
            Float newVisits = oldVisits + noOfVisits;
            event.setValue(newVisits);
            update(event, authentication);
        } else {
            logger.info("Event isn't of type {AGGREGATED_VISITS} and/or didn't happen on the given date.");
        }
    }
}
Also used : Event(eu.einfracentral.domain.Event)

Example 5 with Event

use of eu.einfracentral.domain.Event in project resource-catalogue by madgeek-arc.

the class EventManager method setScheduledRating.

@CacheEvict(value = { CACHE_EVENTS, CACHE_SERVICE_EVENTS }, allEntries = true)
public Event setScheduledRating(String serviceId, Float value) throws ResourceNotFoundException {
    if (!infraServiceService.exists(new SearchService.KeyValue("infra_service_id", serviceId))) {
        throw new ResourceNotFoundException("infra_service", serviceId);
    }
    Event event;
    event = new Event();
    event.setService(serviceId);
    event.setType(Event.UserActionType.RATING.getKey());
    event.setValue(value);
    // remove auth
    event = add(event, null);
    logger.debug("Adding a new RATING Event: {}", event);
    return event;
}
Also used : Event(eu.einfracentral.domain.Event) ResourceNotFoundException(eu.openminted.registry.core.exception.ResourceNotFoundException) CacheEvict(org.springframework.cache.annotation.CacheEvict)

Aggregations

Event (eu.einfracentral.domain.Event)20 CacheEvict (org.springframework.cache.annotation.CacheEvict)10 ResourceNotFoundException (eu.openminted.registry.core.exception.ResourceNotFoundException)8 FacetFilter (eu.openminted.registry.core.domain.FacetFilter)5 InfraService (eu.einfracentral.domain.InfraService)4 ValidationException (eu.einfracentral.exception.ValidationException)3 Paging (eu.openminted.registry.core.domain.Paging)3 Resource (eu.openminted.registry.core.domain.Resource)3 ParserService (eu.openminted.registry.core.service.ParserService)3 SearchService (eu.openminted.registry.core.service.SearchService)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 CACHE_VISITS (eu.einfracentral.config.CacheConfig.CACHE_VISITS)2 ProviderBundle (eu.einfracentral.domain.ProviderBundle)2 RichService (eu.einfracentral.domain.RichService)2 Service (eu.einfracentral.domain.Service)2 MapValues (eu.einfracentral.dto.MapValues)2 PlaceCount (eu.einfracentral.dto.PlaceCount)2 Value (eu.einfracentral.dto.Value)2 InfraServiceManager (eu.einfracentral.registry.manager.InfraServiceManager)2