new apis fro deletion of events

This commit is contained in:
Michele Artini 2020-08-20 15:02:30 +02:00
parent 64ef867437
commit 3e83dd4cef
3 changed files with 34 additions and 14 deletions

View File

@ -1,5 +1,6 @@
package eu.dnetlib.lbs.controllers;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -7,6 +8,8 @@ import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
@ -21,7 +24,6 @@ import org.springframework.web.bind.annotation.RestController;
import com.google.common.collect.Lists;
import eu.dnetlib.lbs.LiteratureBrokerServiceConfiguration;
import eu.dnetlib.lbs.cron.ScheduledActions;
import eu.dnetlib.lbs.elasticsearch.Event;
import eu.dnetlib.lbs.elasticsearch.EventRepository;
import eu.dnetlib.lbs.elasticsearch.EventStatsManager;
@ -36,6 +38,8 @@ import io.swagger.annotations.ApiOperation;
@Api(tags = LiteratureBrokerServiceConfiguration.TAG_EVENTS)
public class EventsController extends AbstractLbsController {
private static final Log log = LogFactory.getLog(EventsController.class);
@Autowired
private EventRepository eventRepository;
@ -45,9 +49,6 @@ public class EventsController extends AbstractLbsController {
@Autowired
private EventStatsManager eventStatsManager;
@Autowired(required = false)
private ScheduledActions scheduledActions;
@ApiOperation("Return an event by ID")
@GetMapping("/{id}")
public Event getEvent(@PathVariable final String id) {
@ -95,12 +96,32 @@ public class EventsController extends AbstractLbsController {
@ApiOperation("Delete the expired events")
@DeleteMapping("/expired")
public Map<String, Long> deleteExpiredEvents() {
return deleteEventsByExpiryDate(0, new Date().getTime());
}
@ApiOperation("Delete the events with the creationDate in a range")
@DeleteMapping("/byCreationDate/{from}/{to}")
public Map<String, Long> deleteEventsByCreationDate(@PathVariable final long from, @PathVariable final long to) {
final Map<String, Long> res = new HashMap<>();
if (scheduledActions != null) {
res.put("deleted", scheduledActions.deleteExpiredEvents());
} else {
res.put("error (deletion of expired events is disabled)", 0l);
}
res.put("deleted", eventRepository.deleteByCreationDateBetween(from, to));
res.put("from", from);
res.put("to", to);
log.info(String.format("Deleted %s events in creationDate range %s-%s", res.get("deleted"), from, to));
return res;
}
@ApiOperation("Delete the events with the expiryDate in a range")
@DeleteMapping("/byExpiryDate/{from}/{to}")
public Map<String, Long> deleteEventsByExpiryDate(@PathVariable final long from, @PathVariable final long to) {
final Map<String, Long> res = new HashMap<>();
res.put("deleted", eventRepository.deleteByExpiryDateBetween(from, to));
res.put("from", from);
res.put("to", to);
log.info(String.format("Deleted %s events in expiryDate range %s-%s", res.get("deleted"), from, to));
return res;
}

View File

@ -30,10 +30,7 @@ public class ScheduledActions {
@Scheduled(cron = "${lbs.task.deleteOldEvents.cron}")
public long deleteExpiredEvents() {
// TODO : the date used in the query should be the min among the dates of the subscriptions (considering only the subscriptions with
// creation_date > events.creationDate)
final long n = this.eventRepository.deleteByExpiryDateBefore(new Date().getTime());
final long n = this.eventRepository.deleteByExpiryDateBetween(0, new Date().getTime());
log.info("Number of deleted expired events: " + n);
return n;
}

View File

@ -14,6 +14,8 @@ public interface EventRepository extends ElasticsearchRepository<Event, String>
Page<Event> findByTopic(String topic, Pageable pageable);
long deleteByExpiryDateBefore(long l);
long deleteByExpiryDateBetween(long from, long to);
long deleteByCreationDateBetween(long from, long to);
}