dnet-applications/apps/dhp-broker-application/src/main/java/eu/dnetlib/broker/controllers/EventsController.java

154 lines
5.3 KiB
Java
Raw Normal View History

2020-09-04 14:33:19 +02:00
package eu.dnetlib.broker.controllers;
2020-07-02 08:55:42 +02:00
2020-08-20 15:02:30 +02:00
import java.util.Date;
2020-07-02 08:55:42 +02:00
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
2020-08-20 15:02:30 +02:00
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
2020-07-02 08:55:42 +02:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.google.common.collect.Lists;
2021-01-27 12:17:38 +01:00
import eu.dnetlib.broker.LiteratureBrokerServiceApplication;
2020-09-04 14:33:19 +02:00
import eu.dnetlib.broker.common.elasticsearch.Event;
import eu.dnetlib.broker.common.elasticsearch.EventRepository;
import eu.dnetlib.broker.common.elasticsearch.EventStatsManager;
import eu.dnetlib.broker.common.elasticsearch.EventStatsManager.BrowseEntry;
import eu.dnetlib.broker.common.subscriptions.Subscription;
import eu.dnetlib.broker.common.subscriptions.SubscriptionRepository;
2021-01-27 12:17:38 +01:00
import eu.dnetlib.common.controller.AbstractDnetController;
2022-08-19 11:20:13 +02:00
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
2020-07-02 08:55:42 +02:00
@RestController
@RequestMapping("/api/events")
2022-08-19 11:20:13 +02:00
@Tag(name = LiteratureBrokerServiceApplication.TAG_EVENTS)
2021-01-27 12:17:38 +01:00
public class EventsController extends AbstractDnetController {
2020-07-02 08:55:42 +02:00
2021-01-27 12:17:38 +01:00
private static final Log log = LogFactory.getLog(AbstractDnetController.class);
2020-08-20 15:02:30 +02:00
2020-07-02 08:55:42 +02:00
@Autowired
private EventRepository eventRepository;
@Autowired
private SubscriptionRepository subscriptionRepo;
@Autowired
private EventStatsManager eventStatsManager;
2022-08-19 11:20:13 +02:00
@Operation(summary = "Return an event by ID")
2020-07-02 08:55:42 +02:00
@GetMapping("/{id}")
public Event getEvent(@PathVariable final String id) {
return eventRepository.findById(id).get();
}
2022-08-19 11:20:13 +02:00
@Operation(summary = "Delete an event by ID")
2020-07-02 08:55:42 +02:00
@DeleteMapping("/{id}")
public void deleteEvent(@PathVariable final String id) {
eventRepository.deleteById(id);
}
2022-08-19 11:20:13 +02:00
@Operation(summary = "Save an event by ID")
2020-07-02 08:55:42 +02:00
@PostMapping("/{id}")
public Event saveEvent(@RequestBody final Event event) {
return eventRepository.save(event);
}
2022-08-19 11:20:13 +02:00
@Operation(summary = "Return a page of events")
2020-07-02 08:55:42 +02:00
@GetMapping("/list/{page}/{pageSize}")
public List<Event> events(
@PathVariable final int page,
@PathVariable final int pageSize) {
2020-07-02 08:55:42 +02:00
return Lists.newArrayList(eventRepository.findAll(PageRequest.of(page, pageSize)));
}
2022-08-19 11:20:13 +02:00
@Operation(summary = "Return a page of events by topic")
2020-07-02 08:55:42 +02:00
@GetMapping("/byTopic/{page}/{pageSize}")
public List<Event> eventsByTopic(
@PathVariable final int page,
@PathVariable final int pageSize,
@RequestParam final String topic) {
2020-07-02 08:55:42 +02:00
return Lists.newArrayList(eventRepository.findByTopic(topic, PageRequest.of(page, pageSize)));
}
2022-08-19 11:20:13 +02:00
@Operation(summary = "Delete all the events")
2020-07-02 08:55:42 +02:00
@DeleteMapping("/all")
public Map<String, Object> clearEvents() {
eventRepository.deleteAll();
final Map<String, Object> res = new HashMap<>();
res.put("deleted", true);
return res;
}
2022-08-19 11:20:13 +02:00
@Operation(summary = "Delete the expired events")
2020-07-02 08:55:42 +02:00
@DeleteMapping("/expired")
2020-09-09 15:32:58 +02:00
public Map<String, Object> deleteExpiredEvents() {
2020-08-20 15:02:30 +02:00
return deleteEventsByExpiryDate(0, new Date().getTime());
}
2022-08-19 11:20:13 +02:00
@Operation(summary = "Delete the events with the creationDate in a range")
2020-08-20 15:02:30 +02:00
@DeleteMapping("/byCreationDate/{from}/{to}")
public Map<String, Long> deleteEventsByCreationDate(@PathVariable final long from, @PathVariable final long to) {
final Map<String, Long> res = new HashMap<>();
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;
}
2022-08-19 11:20:13 +02:00
@Operation(summary = "Delete the events with the expiryDate in a range")
2020-08-20 15:02:30 +02:00
@DeleteMapping("/byExpiryDate/{from}/{to}")
2020-09-09 15:32:58 +02:00
public Map<String, Object> deleteEventsByExpiryDate(@PathVariable final long from, @PathVariable final long to) {
new Thread(() -> {
final long n = eventRepository.deleteByExpiryDateBetween(from, to);
log.info(String.format("Deleted %s events in expiryDate range %s-%s", n, from, to));
}).start();
final Map<String, Object> res = new HashMap<>();
res.put("status", "deleting...");
2020-08-20 15:02:30 +02:00
res.put("from", from);
res.put("to", to);
2020-07-02 08:55:42 +02:00
return res;
}
2022-08-19 11:20:13 +02:00
@Operation(summary = "Return the topics of the indexed events (all)")
2020-07-02 08:55:42 +02:00
@GetMapping("/topics/all")
public List<BrowseEntry> browseTopics() {
return eventStatsManager.browseTopics();
}
2022-08-19 11:20:13 +02:00
@Operation(summary = "Return the topics of the indexed events (only with subscriptions)")
2020-07-02 08:55:42 +02:00
@GetMapping("/topics/withSubscriptions")
public List<BrowseEntry> browseTopicsWithSubscriptions() {
final Iterable<Subscription> iter = subscriptionRepo.findAll();
final Set<String> validTopics = StreamSupport.stream(iter.spliterator(), false)
.map(Subscription::getTopic)
.collect(Collectors.toSet());
2020-07-02 08:55:42 +02:00
return this.browseTopics()
.stream()
.filter(e -> validTopics.contains(e.getValue()))
.collect(Collectors.toList());
2020-07-02 08:55:42 +02:00
}
}