provide details for channel (work in progress, currently only shows channel id)

This commit is contained in:
Carsten Otto
2021-11-19 16:31:06 +01:00
parent 09d0fb1f17
commit 854786c1e2
10 changed files with 258 additions and 48 deletions

View File

@@ -0,0 +1,33 @@
package de.cotto.lndmanagej.controller;
import com.codahale.metrics.MetricRegistry;
import de.cotto.lndmanagej.metrics.Metrics;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.LocalChannel;
import de.cotto.lndmanagej.service.ChannelService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/channel/{channelId}")
public class ChannelDetailsController {
private final ChannelService channelService;
private final Metrics metrics;
public ChannelDetailsController(ChannelService channelService, Metrics metrics) {
this.channelService = channelService;
this.metrics = metrics;
}
@GetMapping("/details")
public ChannelDetailsDto getChannelDetails(@PathVariable ChannelId channelId) throws NotFoundException {
metrics.mark(MetricRegistry.name(getClass(), "getChannelDetails"));
LocalChannel localChannel = channelService.getLocalChannel(channelId).orElse(null);
if (localChannel == null) {
throw new NotFoundException();
}
return new ChannelDetailsDto(localChannel.getId());
}
}

View File

@@ -0,0 +1,8 @@
package de.cotto.lndmanagej.controller;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import de.cotto.lndmanagej.model.ChannelId;
public record ChannelDetailsDto(@JsonSerialize(using = ToStringSerializer.class) ChannelId channelId) {
}

View File

@@ -0,0 +1,7 @@
package de.cotto.lndmanagej.controller;
public class NotFoundException extends Exception {
public NotFoundException() {
super();
}
}

View File

@@ -0,0 +1,20 @@
package de.cotto.lndmanagej.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice
public class NotFoundExceptionHandler extends ResponseEntityExceptionHandler {
public NotFoundExceptionHandler() {
super();
}
@ExceptionHandler(NotFoundException.class)
public ResponseEntity<String> handleException(@SuppressWarnings("unused") NotFoundException exception) {
return ResponseEntity
.notFound()
.build();
}
}

View File

@@ -23,12 +23,14 @@ import java.util.stream.Stream;
public class ChannelService {
private static final int CACHE_EXPIRY_MINUTES = 1;
private final GrpcChannels grpcChannels;
private final LoadingCache<Object, Set<LocalOpenChannel>> channelsCache;
private final LoadingCache<Object, Set<ClosedChannel>> closedChannelsCache;
private final LoadingCache<Object, Set<ForceClosingChannel>> forceClosingChannelsCache;
private final LoadingCache<Object, Set<WaitingCloseChannel>> waitingCloseChannelsCache;
public ChannelService(GrpcChannels grpcChannels, GrpcClosedChannels grpcClosedChannels) {
this.grpcChannels = grpcChannels;
channelsCache = new CacheBuilder()
.withExpiryMinutes(CACHE_EXPIRY_MINUTES)
.build(grpcChannels::getChannels);
@@ -44,13 +46,23 @@ public class ChannelService {
}
public boolean isClosed(ChannelId channelId) {
return getClosedChannels().stream().anyMatch(c -> c.getId().equals(channelId));
return getClosedChannel(channelId).isPresent();
}
public Optional<LocalChannel> getLocalChannel(ChannelId channelId) {
return getAllLocalChannels()
.filter(c -> channelId.equals(c.getId()))
.findFirst();
}
public Set<LocalOpenChannel> getOpenChannels() {
return channelsCache.getUnchecked("");
}
public Optional<LocalOpenChannel> getOpenChannel(ChannelId channelId) {
return grpcChannels.getChannel(channelId);
}
public Set<ClosedChannel> getClosedChannels() {
return closedChannelsCache.getUnchecked("");
}
@@ -65,6 +77,12 @@ public class ChannelService {
return forceClosingChannelsCache.getUnchecked("");
}
public Optional<ForceClosingChannel> getForceClosingChannel(ChannelId channelId) {
return getForceClosingChannels().stream()
.filter(c -> channelId.equals(c.getId()))
.findFirst();
}
public Set<WaitingCloseChannel> getWaitingCloseChannels() {
return waitingCloseChannelsCache.getUnchecked("");
}
@@ -75,18 +93,12 @@ public class ChannelService {
.collect(Collectors.toSet());
}
public Set<LocalChannel> getAllChannelsWith(Pubkey pubkey) {
public Set<LocalChannel> getAllChannelsWith(Pubkey peer) {
return getAllLocalChannels()
.filter(c -> c.getRemotePubkey().equals(pubkey))
.filter(c -> peer.equals(c.getRemotePubkey()))
.collect(Collectors.toSet());
}
public Optional<LocalChannel> getLocalChannel(ChannelId channelId) {
return getAllLocalChannels()
.filter(c -> c.getId().equals(channelId))
.findFirst();
}
public Stream<LocalChannel> getAllLocalChannels() {
Set<LocalOpenChannel> openChannels = getOpenChannels();
Set<WaitingCloseChannel> waitingCloseChannels = getWaitingCloseChannels();