Skip to content
Snippets Groups Projects
Commit c49b8394 authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

[enh] Add conversion method

parent e5bbb622
No related branches found
No related tags found
No related merge requests found
Pipeline #4643 passed
...@@ -24,6 +24,7 @@ package org.duniter.core.client.model.local; ...@@ -24,6 +24,7 @@ package org.duniter.core.client.model.local;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import org.duniter.core.client.model.bma.*; import org.duniter.core.client.model.bma.*;
...@@ -84,11 +85,32 @@ public final class Peers { ...@@ -84,11 +85,32 @@ public final class Peers {
return peer.getStats() != null && peer.getStats().isReacheable(); return peer.getStats() != null && peer.getStats().isReacheable();
} }
public static NetworkPeers.Peer toBmaPeer(Peer endpointAsPeer) {
NetworkPeers.Peer result = new NetworkPeers.Peer();
try {
// Fill BMA peer, using the raw document
NetworkPeerings.parse(endpointAsPeer.getPeering().getRaw(), result);
// Override the status, last_try and first_down, using stats
Peer.PeerStatus status = getStatus(endpointAsPeer).orElse(Peer.PeerStatus.DOWN);
result.setStatus(status.name());
if (status == Peer.PeerStatus.UP) {
result.setLastTry(getLastUpTime(endpointAsPeer).get());
} else {
result.setFirstDown(getFirstDownTime(endpointAsPeer).get());
}
return result;
} catch (IOException e) {
log.error("Unable to parse peering raw document found in: " + e.getMessage());
// Continue to next endpoint
}
return null;
}
public static List<NetworkPeers.Peer> toBmaPeers(List<Peer> endpointAsPeers) { public static List<NetworkPeers.Peer> toBmaPeers(List<Peer> endpointAsPeers) {
if (CollectionUtils.isEmpty(endpointAsPeers)) return null; if (CollectionUtils.isEmpty(endpointAsPeers)) return null;
Joiner keyJoiner = Joiner.on(':');
// Group by peering document // Group by peering document
Multimap<String, Peer> groupByPeering = ArrayListMultimap.create(); Multimap<String, Peer> groupByPeering = ArrayListMultimap.create();
endpointAsPeers.stream() endpointAsPeers.stream()
...@@ -118,31 +140,9 @@ public final class Peers { ...@@ -118,31 +140,9 @@ public final class Peers {
processedPubkeys.add(pubkey); processedPubkeys.add(pubkey);
// Get the first endpoint found for this pubkey // Get the first endpoint found for this pubkey
for (Peer peer: groupByPeering.get(peeringKey)) { return groupByPeering.get(peeringKey).stream().map(Peers::toBmaPeer)
NetworkPeers.Peer result = new NetworkPeers.Peer(); .filter(Objects::nonNull)
.findFirst().orElse(null);
try {
// Fill BMA peer, using the raw document
NetworkPeerings.parse(peer.getPeering().getRaw(), result);
// Override the status, last_try and first_down, using stats
Peer.PeerStatus status = getStatus(peer).orElse(Peer.PeerStatus.DOWN);
result.setStatus(status.name());
if (status == Peer.PeerStatus.UP) {
result.setLastTry(getLastUpTime(peer).get());
} else {
result.setFirstDown(getFirstDownTime(peer).get());
}
return result;
} catch (IOException e) {
log.error("Unable to parse peering raw document found in: " + e.getMessage());
// Continue to next endpoint
}
}
return null;
}) })
// Remove skipped items // Remove skipped items
.filter(Objects::nonNull) .filter(Objects::nonNull)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment