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